Skip to content

ci(security): fail Security Scan on a gosec finding that is not baselined - #435

Merged
scttfrdmn merged 1 commit into
mainfrom
fix/gosec-baseline-gate-415
Aug 14, 2026
Merged

scttfrdmn merged 1 commit into
mainfrom
fix/gosec-baseline-gate-415

Conversation

@scttfrdmn

Copy link
Copy Markdown
Owner

Closes #415.

The defect

A gosec finding did not block a merge, and every part of the mechanism looked like it did.

Security Scan runs gosec -no-fail (security.yml:58), so the job passes on any number of findings. The check that did go red was gosec — the code-scanning check derived from the SARIF upload, which is a different check with a different name. And that one reports on pull requests and not on push: main, measured across four commits, so it could not simply be added to the required contexts either: a required check that does not always report blocks every PR forever, which is the failure mode #413 was written to avoid.

The result: #414 added a uint64 → int64 conversion, gosec reported "1 new alert including 1 error", and the PR stayed mergeable. It was caught by someone reading the checks by hand.

The fix

-no-fail stays. The upload needs the complete report, including the three sdks/c G115 findings that the "Fix Gosec SARIF format" step back-fills a path onto — and which golangci-lint discards for the same cgo reason, so they reach no other tool. Dropping the flag would end the job before either step ran: it reads as a tightening and is a loosening.

The gate is instead a final step running .github/scripts/gosec-gate.sh, which diffs the report against .github/gosec-baseline.txt. Security Scan is a job name, so it reports on every trigger the workflow has — which is what lets it be required. It runs last, with if: always(), so one new conversion does not also cost the run its govulncheck result, Trivy upload and license check.

Why a baseline and not a severity threshold

Measured locally: 12 findings, all G115, all error level.

G115 internal/archive/index.go 1
G115 internal/cache/predictive.go 1
G115 internal/difftest/program.go 1
G115 internal/health/remediation.go 2
G115 internal/storage/s3/backend.go 1
G115 internal/storage/s3/multipart_upload.go 2
G115 pkg/recovery/connection.go 1
G115 sdks/c/main.go 3

There is no level that separates the backlog from a new finding, because gosec reports every G115 identically. So the issue's severity-threshold option gates nothing, and the baseline is what the acceptance criterion actually needs.

Keyed by (rule, file, count) — not by line number, which moves whenever anything above it moves, so a line-keyed baseline goes stale on unrelated edits and the failure it reports is always about the wrong thing. Keyed this way, an unrelated refactor inside one of these files is silent and a second conversion added to it is not.

The ratchet is exact in both directions: fixing one of these fails too, until its line goes with it. A slot kept after its finding was fixed is permanent headroom for the next conversion in that file — the same hole as a coverage floor that only ever moves down.

Verification

The script's five failure paths were run, not reasoned about, against mutated SARIF:

mutant result
second finding in an already-baselined file (index.go 1→2) fails, diff names the count
finding in a file not in the baseline fails, diff names the file
a baselined finding removed fails as a stale baseline
a different rule (G401) in a baselined file fails, diff names the rule
zero results (a scan that analysed nothing) fails with its own message, not a bogus 8-fixed-findings diff
the real report passes, exit 0

That caught a bug in the script. diff exits 1 on a difference, pipefail promotes it to the pipeline, and set -e ended the script at diff | tail — so the first mutant printed the diff and then none of the three paragraphs explaining what to do about it. Which is precisely the failure #433 had just fixed in the install-script job, occurring directly beneath a comment describing it. Fixed with || true and re-verified; all four diff-path mutants now print their full guidance.

internal/config/gosec_gate_test.go gates the two silent failure modes, and all six of its branches were mutation-verified (each fails with its own message; suite returns to green):

  • a baselined path that no longer exists — a renamed file leaves a standing allowance pointing at nothing, and the script would report it as a fixed finding, which is true said misleadingly
  • an unparsable line, and a zero count
  • the workflow step deleted — the change that restores the original defect while every check stays green
  • -no-fail dropped
  • the script not executable — the job would fail with a permission error that says nothing about security findings

Local: go build ./..., gofmt -l ., go vet ./..., golangci-lint run, go test -race ./internal/config/, shellcheck .github/scripts/gosec-gate.sh — all clean.

Follow-up, deliberately not in this PR

Adding Security Scan to main's 19 required contexts. It should be required only once this PR is on main and the job has been observed green on a push: main run with the new step in it — the acceptance criterion says verified by looking at check runs on a merge commit, not assumed, and requiring a context whose new step has never run there is the same mistake in the other direction.

…ined (#415)

A gosec finding did not block a merge, and every part of the mechanism looked
like it did. `Security Scan` runs `gosec -no-fail`, so the job passed on any
number of findings. The check that went red was `gosec` — the code-scanning
check derived from the SARIF upload, a different check with a different name —
and that one reports on pull requests and not on `push: main`, measured across
four commits. So it could not be added to the required contexts either: a
required check that does not always report blocks every PR forever (#413).

The result was that #414 added a `uint64 -> int64` conversion, gosec reported
"1 new alert including 1 error", and the PR stayed mergeable. It was caught by
someone reading the checks by hand.

`-no-fail` stays. The upload needs the complete report, including the three
`sdks/c` G115 findings that the "Fix Gosec SARIF format" step back-fills a path
onto — and which golangci-lint discards for the same cgo reason, so they reach
no other tool. Dropping the flag would end the job before either step ran,
which reads as a tightening and is a loosening.

The gate is instead a final step running .github/scripts/gosec-gate.sh, which
diffs the report against .github/gosec-baseline.txt. `Security Scan` is a job
name, so it reports on every trigger the workflow has, which is what lets it be
required. Last in the job and `if: always()`, so a new conversion does not also
cost the run its govulncheck result, Trivy upload and license check.

The baseline is the twelve pre-existing findings across eight files, keyed by
(rule, file, count). Not by line number, which moves whenever anything above it
moves, so a line-keyed baseline goes stale on unrelated edits and reports on the
wrong thing. A severity threshold was not available: all twelve are G115 at
`error` level, identical to what a new finding reports, so there is nothing to
threshold on. The ratchet is exact in both directions — fixing one of these
fails too, until its line goes with it, because a slot kept after its finding
was fixed is permanent headroom for the next conversion in that file.

Two things are gated in Go, in internal/config/gosec_gate_test.go, because both
failure modes are silent: every path in the baseline must exist (a renamed file
leaves a standing allowance pointing at nothing, and the script would report it
as a fixed finding, which is true said misleadingly), and the workflow must
still invoke the script, still pass `-no-fail`, and the script must still be
executable. All six branches were mutation-verified: each fails with its own
message and the suite returns to green.

The script's own failure path was verified by running it, not by reading it —
and that caught a bug in it. `diff` exits 1 on a difference, `pipefail` promotes
that to the pipeline, and `set -e` ended the script at `diff | tail`, so the
first mutant printed the diff and then none of the three paragraphs explaining
what to do about it. Which is the exact failure #433 had just fixed elsewhere,
sitting directly below a comment describing it.

Refs #415
@scttfrdmn scttfrdmn added type: security Security vulnerability or hardening type: ci-cd Continuous integration or deployment improvements priority: medium Important but not urgent area: ci-cd CI workflows, gates, and release automation labels Aug 14, 2026
@scttfrdmn
scttfrdmn merged commit bb914cf into main Aug 14, 2026
46 checks passed
@scttfrdmn
scttfrdmn deleted the fix/gosec-baseline-gate-415 branch August 14, 2026 04:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area: ci-cd CI workflows, gates, and release automation priority: medium Important but not urgent type: ci-cd Continuous integration or deployment improvements type: security Security vulnerability or hardening

Projects

None yet

Development

Successfully merging this pull request may close these issues.

gosec findings do not block a merge, and the job that runs gosec cannot fail

1 participant