Skip to content

Commit 94c9fc4

Browse files
committed
make the zap parser not error out when receiving an empty file
1 parent 461bac7 commit 94c9fc4

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

components/scanners/zaproxy/internal/transformer/transformer.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,10 @@ func (g *zapTransformer) Transform(ctx context.Context) ([]*ocsf.VulnerabilityFi
124124
}
125125
return nil, errors.Errorf("failed to read raw output file '%s': %w", g.rawOutFilePath, err)
126126
}
127-
127+
if len(b) == 0 {
128+
logger.Info("input file is empty, exiting without findings")
129+
return []*ocsf.VulnerabilityFinding{}, nil
130+
}
128131
var report sarifschemav210.SchemaJson
129132
if err := report.UnmarshalJSON(b); err != nil {
130133
return nil, errors.Errorf("failed to parse raw zap output: %w", err)

components/scanners/zaproxy/internal/transformer/transformer_test.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package transformer_test
33
import (
44
"context"
55
_ "embed"
6+
"os"
67
"testing"
78
"time"
89

@@ -230,4 +231,28 @@ func TestZapTransformer_Transform(t *testing.T) {
230231
assert.Equal(t, expectedMetadataUIDs[idx], *finding.Metadata.Uid, "Unexpected metadata uid for finding %d", idx)
231232
}
232233
})
234+
t.Run("it should return an empty finding array when the input file is empty", func(t *testing.T) {
235+
emptyFilePath := "./testdata/empty.sarif.json"
236+
require.NoError(t, os.WriteFile(emptyFilePath, []byte{}, 0644))
237+
defer func() {
238+
require.NoError(t, os.Remove(emptyFilePath))
239+
}()
240+
241+
ocsfTransformer, err := transformer.New(
242+
transformer.ZapRawOutFilePath(emptyFilePath),
243+
transformer.ZapTransformerWithTarget(transformer.TargetTypeWebsite),
244+
transformer.ZapTransformerWithClock(clock),
245+
)
246+
require.NoError(t, err)
247+
248+
ctx := context.WithValue(ctx, component.SCANNER_TARGET_METADATA_CTX_KEY, &ocsffindinginfo.DataSource{
249+
TargetType: ocsffindinginfo.DataSource_TARGET_TYPE_WEBSITE,
250+
})
251+
ctx, cancel := context.WithCancel(ctx)
252+
defer cancel()
253+
254+
findings, err := ocsfTransformer.Transform(ctx)
255+
require.NoError(t, err)
256+
assert.Empty(t, findings, "Expected no findings for an empty input file")
257+
})
233258
}

0 commit comments

Comments
 (0)