Skip to content

Commit 51b62cb

Browse files
authored
fix(cli): handle empty array in csaf (#1392)
Signed-off-by: Miguel Martinez <[email protected]>
1 parent a9520b0 commit 51b62cb

File tree

4 files changed

+40
-9
lines changed

4 files changed

+40
-9
lines changed

pkg/attestation/crafter/materials/csaf.go

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -102,14 +102,22 @@ func (i *CSAFCrafter) Craft(ctx context.Context, filepath string) (*api.Attestat
102102
}
103103

104104
// Validate the CSAF file against the specified category.
105-
document, docExists := v.(map[string]interface{})["document"]
106-
if docExists {
107-
documentMap, docMapOk := document.(map[string]interface{})
108-
category, categoryExists := documentMap["category"].(string)
109-
110-
if docMapOk && categoryExists && category != "" && category != i.category {
111-
return nil, fmt.Errorf("invalid CSAF category field in file, expected: %v", i.category)
112-
}
105+
// First check that the content is a map with a `document` root
106+
doc, ok := v.(map[string]interface{})
107+
if !ok {
108+
return nil, fmt.Errorf("invalid CSAF file: %w", ErrInvalidMaterialType)
109+
}
110+
111+
// Map its content
112+
documentMap, ok := doc["document"].(map[string]interface{})
113+
if !ok {
114+
return nil, fmt.Errorf("invalid CSAF file: %w", ErrInvalidMaterialType)
115+
}
116+
117+
// extract category: not mandatory but if it comes we check it
118+
category, categoryExists := documentMap["category"].(string)
119+
if categoryExists && category != "" && category != i.category {
120+
return nil, fmt.Errorf("invalid CSAF category field in file, expected: %v", i.category)
113121
}
114122

115123
// The validator will try in cascade the different schemas since CSAF specification

pkg/attestation/crafter/materials/csaf_test.go

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
//
2-
// Copyright 2023 The Chainloop Authors.
2+
// Copyright 2024 The Chainloop Authors.
33
//
44
// Licensed under the Apache License, Version 2.0 (the "License");
55
// you may not use this file except in compliance with the License.
@@ -132,6 +132,24 @@ func TestCSAFCraft(t *testing.T) {
132132
Type: contractAPI.CraftingSchema_Material_CSAF_VEX,
133133
},
134134
},
135+
{
136+
name: "empty json array",
137+
filePath: "./testdata/empty_array.json",
138+
wantErr: "unexpected material type",
139+
schema: &contractAPI.CraftingSchema_Material{
140+
Name: "test",
141+
Type: contractAPI.CraftingSchema_Material_CSAF_VEX,
142+
},
143+
},
144+
{
145+
name: "empty file",
146+
filePath: "./testdata/empty.txt",
147+
wantErr: "unexpected material type",
148+
schema: &contractAPI.CraftingSchema_Material{
149+
Name: "test",
150+
Type: contractAPI.CraftingSchema_Material_CSAF_VEX,
151+
},
152+
},
135153
{
136154
name: "valid json file invalid artifact",
137155
filePath: "./testdata/random.json",

pkg/attestation/crafter/materials/materials.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,10 @@ func uploadAndCraft(ctx context.Context, input *schemaapi.CraftingSchema_Materia
5757
}
5858
defer result.r.Close()
5959

60+
if result.size == 0 {
61+
return nil, fmt.Errorf("%w: %w", ErrBaseUploadAndCraft, errors.New("file is empty"))
62+
}
63+
6064
l.Debug().Str("filename", result.filename).Str("digest", result.digest).Str("path", artifactPath).
6165
Str("size", bytefmt.ByteSize(uint64(result.size))).
6266
Str("max_size", bytefmt.ByteSize(uint64(backend.MaxSize))).
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
[]

0 commit comments

Comments
 (0)