GitGuardian / ggshield

Find and fix 360+ types of hardcoded secrets and 70+ types of infrastructure-as-code misconfigurations.
https://gitguardian.com
MIT License
1.57k stars 138 forks source link

Add support for SARIF output for secret scanning #920

Closed agateau-gg closed 1 week ago

agateau-gg commented 2 weeks ago

Context

This PR adds support for generating reports in SARIF format when scanning for secrets.

See #869.

What has been done

The PR first introduces a --format (text|json) option, and make existing commands use it. --format json is a more verbose version of --json, but it makes it possible to add new formats without adding a new flag for each format.

Then it introduces a sarif value for --format. This value is only used by secret scan commands for now. Adding it to other verticals will be done separately.

Representing multi-match secrets in SARIF output

There is a bit of an impedance mismatch between ggshield secret output and SARIF output. A SARIF result is supposed to have one location, but a secret often contains multiple matches, so each match needs to be a separate location. To reconcile this, the result location starts at the start of the first match and ends at the end of the last match. Then each match is added as a "related" location.

This can be seen in this screenshot of a GitHub import of a ggshield SARIF document:

image(1)

I am open to suggestions regarding better ways to represent multi-match secrets.

Validation

The code can be tested in different ways.

The first thing is to generate some SARIF report. This can be done with a command like this:

ggshield secret scan path -ry --use-gitignore . --format sarif -o myreport.sarif

Then you can try to use the generated report in different places:

PR check list

agateau-gg commented 1 week ago

For the multi match secret it does not seem ideal but it is probably the best we can do. Do we know how Github Advance security & others displays those with SARIF format ?

I have no idea :/

I tested with the validator and pycharm extension it looks good to me !

Oh, I could not find a Pycharm extension! Need to look again.