Skip to content

Commit 4e387cd

Browse files
authored
ci(govulncheck): drop duplicate SARIF stacks before upload (#463)
Closes #409. The `govulncheck-sarif` job has passed since the go1.26.9 bump (f2ca046) removed the finding whose duplicate call stacks the upload rejected. The cause is still there, so: - `.github/sarif/dedup.jq` removes repeated entries from every array the SARIF 2.1.0 schema marks `uniqueItems` (not only `stacks`). It keeps the first of each in the original order, and leaves `frames` and `codeFlows` alone, since a recursive stack repeats frames. A file with no `runs` passes through. - A fixture (`.github/sarif/testdata`) is checked before the filter runs, so a broken filter fails the step rather than going unnoticed until a real finding. - The upload step is `continue-on-error`, with a warning annotation when it fails. It only feeds the Security tab; the govulncheck step in `check` is what gates merges. Checked locally: the fixture passes, and the real govulncheck v1.8.0 SARIF for this repo is unchanged by the filter. `actionlint` is clean.
1 parent aae0828 commit 4e387cd

4 files changed

Lines changed: 65 additions & 1 deletion

File tree

‎.github/sarif/dedup.jq‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Drops repeated entries from the SARIF 2.1.0 arrays whose schema says
2+
# "uniqueItems": true, keeping the first of each and the order govulncheck
3+
# gave. upload-sarif rejects a whole file over one repeat (issue #409).
4+
#
5+
# Arrays are matched by key name. Where a name is unique in one object and
6+
# not in another (results[].locations is not; a notification's locations
7+
# is), the other loses only exact copies, which carry nothing extra. frames
8+
# and codeFlows are left alone: a recursive call stack repeats frames.
9+
def unique_keys: [
10+
"addresses", "analysisToolLogFiles", "annotations", "artifactChanges",
11+
"artifacts", "attachments", "children", "contents", "conversionSources",
12+
"deprecatedGuids", "deprecatedIds", "deprecatedNames", "edges",
13+
"extensions", "fixes", "graphTraversals", "graphs", "invocations", "kinds",
14+
"locations", "logicalLocations", "newlineSequences", "nodes",
15+
"notificationConfigurationOverrides", "notifications", "policies",
16+
"rectangles", "redactionTokens", "regions", "relatedLocations",
17+
"relationships", "responseFiles", "roles", "ruleConfigurationOverrides",
18+
"rules", "runAggregates", "stacks", "suppressions", "supportedTaxonomies",
19+
"tags", "taxa", "taxonomies", "threadFlowLocations", "translations",
20+
"versionControlProvenance", "webRequests", "webResponses", "workItemUris"
21+
];
22+
23+
def first_of_each: reduce .[] as $x ([]; if any(.[]; . == $x) then . else . + [$x] end);
24+
25+
unique_keys as $keys
26+
| walk(
27+
if type == "object" then
28+
with_entries(
29+
if (.key | IN($keys[])) and (.value | type) == "array"
30+
then .value |= first_of_each
31+
else . end)
32+
else . end)

‎.github/sarif/testdata/dup.sarif‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{"version":"2.1.0","runs":[{"tool":{"driver":{"name":"govulncheck","rules":[{"id":"GO-1"},{"id":"GO-1"}]}},"results":[
2+
{"ruleId":"GO-1","stacks":[{"frames":[{"module":"b"},{"module":"b"}]},{"frames":[{"module":"a"}]},{"frames":[{"module":"b"},{"module":"b"}]}],
3+
"codeFlows":[{"message":{"text":"x"}},{"message":{"text":"x"}}],
4+
"relatedLocations":[{"id":1},{"id":1}]},
5+
{"ruleId":"GO-2"}]},
6+
{"tool":{"driver":{"name":"empty"}}}]}

‎.github/sarif/testdata/want.sarif‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{"version":"2.1.0","runs":[{"tool":{"driver":{"name":"govulncheck","rules":[{"id":"GO-1"}]}},"results":[
2+
{"ruleId":"GO-1","stacks":[{"frames":[{"module":"b"},{"module":"b"}]},{"frames":[{"module":"a"}]}],
3+
"codeFlows":[{"message":{"text":"x"}},{"message":{"text":"x"}}],
4+
"relatedLocations":[{"id":1}]},
5+
{"ruleId":"GO-2"}]},
6+
{"tool":{"driver":{"name":"empty"}}}]}

‎.github/workflows/ci.yml‎

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,11 +70,31 @@ jobs:
7070
- name: govulncheck (SARIF)
7171
run: go run "golang.org/x/vuln/cmd/govulncheck@${GOVULNCHECK_VERSION}" -format sarif ./... > govulncheck.sarif
7272

73-
- uses: github/codeql-action/upload-sarif@5595ccaf912efad79be6eef63a5619ff05969be3 # v4.37.6
73+
# govulncheck can repeat an entry in an array the SARIF schema says is
74+
# unique, such as one result's stacks, and the upload rejects the whole
75+
# file over it (issue #409). dedup.jq keeps the first of each, in order;
76+
# its fixture is checked first so a broken filter fails here, not on
77+
# the day a real finding needs uploading.
78+
- name: De-duplicate SARIF arrays
79+
run: |
80+
diff <(jq -S -f .github/sarif/dedup.jq .github/sarif/testdata/dup.sarif) <(jq -S . .github/sarif/testdata/want.sarif)
81+
jq -f .github/sarif/dedup.jq govulncheck.sarif > govulncheck.dedup.sarif
82+
mv govulncheck.dedup.sarif govulncheck.sarif
83+
84+
# The upload only feeds the Security tab; the govulncheck step in
85+
# `check` is what blocks a merge. A failed upload is a warning, not a
86+
# red PR.
87+
- id: upload
88+
uses: github/codeql-action/upload-sarif@5595ccaf912efad79be6eef63a5619ff05969be3 # v4.37.6
89+
continue-on-error: true
7490
with:
7591
sarif_file: govulncheck.sarif
7692
category: govulncheck
7793

94+
- name: Report a failed upload
95+
if: steps.upload.outcome == 'failure'
96+
run: echo "::warning title=govulncheck SARIF upload failed::The Security tab was not updated; see the upload step's log."
97+
7898
dependency-review:
7999
if: github.event_name == 'pull_request'
80100
runs-on: ubuntu-latest

0 commit comments

Comments
 (0)