Skip to content

Commit 4a0deee

Browse files
committed
make the zap parser not error out when receiving an empty file
1 parent c1dd4f1 commit 4a0deee

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-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: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ package transformer_test
33
import (
44
"context"
55
_ "embed"
6+
"os"
7+
"path/filepath"
68
"testing"
79
"time"
810

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

0 commit comments

Comments
 (0)