Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: check root metadata verification before snapshotting #293

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -1280,6 +1280,11 @@ func (r *Repo) SnapshotWithExpires(expires time.Time) error {
return err
}

// Verify root metadata before verifying signatures on role metadata.
if err := r.verifySignatures("root.json"); err != nil {
return err
}

for _, metaName := range r.snapshotMetadata() {
if err := r.verifySignatures(metaName); err != nil {
return err
Expand Down
36 changes: 36 additions & 0 deletions repo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2595,3 +2595,39 @@ func (rs *RepoSuite) TestOfflineFlow(c *C) {
c.Assert(err, IsNil)
}
}

// Regression test: Snapshotting an invalid root should fail.
func (rs *RepoSuite) TestSnapshotWithInvalidRoot(c *C) {
files := map[string][]byte{"foo.txt": []byte("foo")}
local := MemoryStore(make(map[string]json.RawMessage), files)
r, err := NewRepo(local)
c.Assert(err, IsNil)

// Init should create targets.json, but not signed yet
r.Init(false)

genKey(c, r, "root")
genKey(c, r, "targets")
genKey(c, r, "snapshot")
genKey(c, r, "timestamp")
c.Assert(r.AddTarget("foo.txt", nil), IsNil)

// Clear the root signature so that signature verification fails.
s, err := r.SignedMeta("root.json")
c.Assert(err, IsNil)
c.Assert(s.Signatures, HasLen, 1)
s.Signatures[0].Signature = data.HexBytes{}
b, err := r.jsonMarshal(s)
c.Assert(err, IsNil)
r.meta["root.json"] = b
local.SetMeta("root.json", b)

// Snapshotting should fail.
c.Assert(r.Snapshot(), Equals, ErrInsufficientSignatures{"root.json", verify.ErrInvalid})

// Correctly sign root
c.Assert(r.Sign("root.json"), IsNil)
c.Assert(r.Snapshot(), IsNil)
c.Assert(r.Timestamp(), IsNil)
c.Assert(r.Commit(), IsNil)
}