Skip to content

Commit

Permalink
fix: check root metadata verification before snapshotting (#293)
Browse files Browse the repository at this point in the history
Signed-off-by: Asra Ali <asraa@google.com>
  • Loading branch information
asraa authored May 25, 2022
1 parent e2594e6 commit 9a41055
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
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)
}

0 comments on commit 9a41055

Please sign in to comment.