Skip to content

Commit 744339f

Browse files
authored
Fix indexing bug for intoto attestations (#870)
Signed-off-by: Priya Wadhwa <priya@chainguard.dev>
1 parent 71db899 commit 744339f

File tree

2 files changed

+43
-5
lines changed

2 files changed

+43
-5
lines changed

pkg/types/intoto/v0.0.1/entry.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -79,12 +79,10 @@ func (v V001Entry) IndexKeys() ([]string, error) {
7979

8080
switch v.env.PayloadType {
8181
case in_toto.PayloadType:
82-
if v.IntotoObj.Content == nil || v.IntotoObj.Content.Hash == nil {
83-
log.Logger.Info("IntotoObj content or hash is nil")
84-
return result, nil
82+
if v.IntotoObj.Content != nil && v.IntotoObj.Content.Hash != nil {
83+
hashkey := strings.ToLower(fmt.Sprintf("%s:%s", swag.StringValue(v.IntotoObj.Content.Hash.Algorithm), swag.StringValue(v.IntotoObj.Content.Hash.Value)))
84+
result = append(result, hashkey)
8585
}
86-
hashkey := strings.ToLower(fmt.Sprintf("%s:%s", *v.IntotoObj.Content.Hash.Algorithm, *v.IntotoObj.Content.Hash.Value))
87-
result = append(result, hashkey)
8886

8987
statement, err := parseStatement(v.env.Payload)
9088
if err != nil {

pkg/types/intoto/v0.0.1/entry_test.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -329,3 +329,43 @@ func TestV001Entry_IndexKeys(t *testing.T) {
329329
})
330330
}
331331
}
332+
333+
func TestIndexKeysNoContentHash(t *testing.T) {
334+
statement := in_toto.Statement{
335+
Predicate: "hello",
336+
StatementHeader: in_toto.StatementHeader{
337+
Subject: []in_toto.Subject{
338+
{
339+
Name: "myimage",
340+
Digest: slsa.DigestSet{
341+
"sha256": "mysha256digest",
342+
},
343+
},
344+
},
345+
},
346+
}
347+
b, err := json.Marshal(statement)
348+
if err != nil {
349+
t.Fatal(err)
350+
}
351+
payload := base64.StdEncoding.EncodeToString(b)
352+
v := V001Entry{
353+
env: dsse.Envelope{
354+
Payload: payload,
355+
PayloadType: in_toto.PayloadType,
356+
},
357+
}
358+
sha := sha256.Sum256([]byte(payload))
359+
// Always start with the hash
360+
want := []string{"sha256:" + hex.EncodeToString(sha[:])}
361+
want = append(want, "sha256:mysha256digest")
362+
got, err := v.IndexKeys()
363+
if err != nil {
364+
t.Fatal(err)
365+
}
366+
sort.Strings(got)
367+
sort.Strings(want)
368+
if !cmp.Equal(got, want) {
369+
t.Errorf("V001Entry.IndexKeys() = %v, want %v", got, want)
370+
}
371+
}

0 commit comments

Comments
 (0)