mirror of
https://github.com/restic/restic.git
synced 2026-12-04 15:44:46 +00:00
repository: validate id in repo config (#22214)
This commit is contained in:
1 parent
00b2f19c3d
commit
d7b184f4ce
6 files changed
+17
-11
No files matched your search
@@ -87,7 +87,8 @@ func runInit(ctx context.Context, opts InitOptions, gopts global.Options, args [
|
||||
}
|
||||
|
||||
if !gopts.JSON {
|
||||
printer.P("created restic repository %v at %s", s.Config().ID[:10], location.StripPassword(gopts.Backends, gopts.Repo))
|
||||
id := s.Config().ID
|
||||
printer.P("created restic repository %v at %s", id.Str(), location.StripPassword(gopts.Backends, gopts.Repo))
|
||||
if opts.CopyChunkerParameters && chunkerPolynomial != nil {
|
||||
printer.P(" with chunker parameters copied from secondary repository")
|
||||
}
|
||||
@@ -99,7 +100,7 @@ func runInit(ctx context.Context, opts InitOptions, gopts global.Options, args [
|
||||
} else {
|
||||
status := initSuccess{
|
||||
MessageType: "initialized",
|
||||
ID: s.Config().ID,
|
||||
ID: s.Config().ID.String(),
|
||||
Repository: location.StripPassword(gopts.Backends, gopts.Repo),
|
||||
}
|
||||
return json.NewEncoder(gopts.Term.OutputWriter()).Encode(status)
|
||||
|
||||
@@ -154,7 +154,7 @@ func runMount(ctx context.Context, opts MountOptions, gopts global.Options, args
|
||||
return err
|
||||
}
|
||||
|
||||
fuseMountName := fmt.Sprintf("restic:%s", repo.Config().ID[:10])
|
||||
fuseMountName := fmt.Sprintf("restic:%s", repo.Config().ID.String()[:10])
|
||||
|
||||
mountOptions := []systemFuse.MountOption{
|
||||
systemFuse.ReadOnly(),
|
||||
|
||||
@@ -183,7 +183,7 @@ func runPrune(ctx context.Context, opts PruneOptions, gopts global.Options, term
|
||||
defer unlock()
|
||||
|
||||
if opts.UnsafeNoSpaceRecovery != "" {
|
||||
repoID := repo.Config().ID
|
||||
repoID := repo.Config().ID.String()
|
||||
if opts.UnsafeNoSpaceRecovery != repoID {
|
||||
return errors.Fatalf("must pass id '%s' to --unsafe-recover-no-free-space", repoID)
|
||||
}
|
||||
|
||||
@@ -410,19 +410,16 @@ func decryptRepository(ctx context.Context, s *repository.Repository, gopts *Opt
|
||||
// printRepositoryInfo displays the repository ID, version and compression level.
|
||||
func printRepositoryInfo(s *repository.Repository, gopts Options, printer restic.Printer) {
|
||||
id := s.Config().ID
|
||||
if len(id) > 8 {
|
||||
id = id[:8]
|
||||
}
|
||||
extra := ""
|
||||
if s.Config().Version >= 2 {
|
||||
extra = ", compression level " + gopts.Compression.String()
|
||||
}
|
||||
printer.PT("repository %v opened (version %v%s)", id, s.Config().Version, extra)
|
||||
printer.PT("repository %v opened (version %v%s)", id.Str(), s.Config().Version, extra)
|
||||
}
|
||||
|
||||
// setupCache creates a new cache and removes old cache directories if instructed to do so.
|
||||
func setupCache(s *repository.Repository, gopts Options, printer restic.Printer) error {
|
||||
c, err := cache.New(s.Config().ID, gopts.CacheDir)
|
||||
c, err := cache.New(s.Config().ID.String(), gopts.CacheDir)
|
||||
if err != nil {
|
||||
printer.E("unable to open cache: %v", err)
|
||||
return err
|
||||
|
||||
@@ -15,7 +15,7 @@ import (
|
||||
// Config contains the configuration for a repository.
|
||||
type Config struct {
|
||||
Version uint `json:"version"`
|
||||
ID string `json:"id"`
|
||||
ID ID `json:"id"`
|
||||
ChunkerPolynomial chunker.Pol `json:"chunker_polynomial"`
|
||||
}
|
||||
|
||||
@@ -43,7 +43,7 @@ func CreateConfig(version uint, pol *chunker.Pol) (Config, error) {
|
||||
cfg.ChunkerPolynomial = *pol
|
||||
}
|
||||
|
||||
cfg.ID = NewRandomID().String()
|
||||
cfg.ID = NewRandomID()
|
||||
cfg.Version = version
|
||||
|
||||
debug.Log("New config: %#v", cfg)
|
||||
|
||||
@@ -63,3 +63,11 @@ func TestConfig(t *testing.T) {
|
||||
rtest.Assert(t, cfg1 == cfg2,
|
||||
"configs aren't equal: %v != %v", cfg1, cfg2)
|
||||
}
|
||||
|
||||
func TestLoadConfigRejectsInvalidID(t *testing.T) {
|
||||
load := func(tpe restic.FileType, id restic.ID) ([]byte, error) {
|
||||
return []byte(`{"version":1,"id":"not-an-id","chunker_polynomial":{"a":1,"b":2}}`), nil
|
||||
}
|
||||
_, err := restic.LoadConfig(context.TODO(), loader{load})
|
||||
rtest.Assert(t, err != nil, "invalid repository id was accepted")
|
||||
}
|
||||
Reference in new issue
Block a user