Skip to content

fix: suppressions that suppress, and the defect one of them hid - #356

Merged
scttfrdmn merged 3 commits into
mainfrom
fix/gosec-suppression-directives-264
Aug 5, 2026
Merged

scttfrdmn merged 3 commits into
mainfrom
fix/gosec-suppression-directives-264

Conversation

@scttfrdmn

Copy link
Copy Markdown
Owner

Closes #264.

The issue is right that six gosec findings are annotated with //nolint:gosec, which golangci-lint honours and gosec's own run does not — so each passes lint and stays open as a code-scanning alert. Two of its premises turn out not to hold, and one of the six sites was hiding a real defect.

The defect

internal/storage/s3/config.go's readyToTrip converts the configured int failure threshold to the uint32 that circuit.Counts.TotalFailures is, guarded by if cfg.FailureThreshold <= 0 and annotated "guarded positive above". That is true and insufficient: the guard bounds the sign, not the width.

circuit_breaker:
  failure_threshold: 4294967296     # narrows to 0

uint32(4294967296) is 0, so the predicate becomes TotalFailures >= 0 — true on the zeroth failure. The breaker opens before the first request and rejects every S3 operation for the life of the mount, which is precisely the outcome the function's own zero case exists to prevent, arriving by the one path that bypassed it. 4294967297 is the quieter half: a threshold of 1, so the breaker trips on a single failure while the configuration says four billion. Measured in a standalone probe.

Now clamped to math.MaxUint32, above which no count of failures can reach the threshold anyway — unreachable is the honest reading of an unreachable number, and clamping keeps this a total function rather than adding a second validation site next to internal/config's.

Why neither run reported it. .golangci.yml excludes G115 repo-wide, so golangci-lint never had the finding; the //nolint form meant the standalone run that did have it was not being asked to skip it — but that run gates nothing. A finding excluded from one runner and suppressed for the other is invisible in both directions.

Two premises corrected, both by measurement

  • #nosec alone satisfies both runs, and writing both directives fails lint. golangci-lint runs gosec's own analyser, which strips #nosec sites before golangci-lint sees them. So the dual annotation the issue prescribes leaves the //nolint:gosec with nothing to suppress and nolintlint reports it unused — turning a suppression into a lint failure.
  • G703 is not in the gosec golangci-lint bundles. Probed on a two-line os.Getenv → os.ReadFile program: standalone gosec reports G703 and G304; golangci-lint reports G304 alone. The //nolint:gosec at internal/awsname could not have worked even written correctly.

Twelve directives that were not directives

// nolint:gosec with a leading space is an ordinary comment; only //nolint: is a directive. Twelve sites across nine files carried the spaced form, each with a paragraph of justification attached to nothing. Eleven more had no explanation at all, which nolintlint's require-explanation reports. Five of those were staticcheck findings that were simply fixable — two QF1008 embedded selectors, two QF1003 if-chains wanting a tagged switch, one QF1012 WriteString(Sprintf(...)) — so they are fixed rather than annotated. nolintlint: 23 → 0.

The hardcoded credential is now provably test-only

testaws.SecretAccessKey's G101 argument was that it is AWS's own documentation example key, accepted only by the substrate emulator. True, and not the whole risk: the constant is at package scope in testaws.go, not a _test.go, so the filename keeps neither it nor the embedded emulator out of a shipped binary. What does is that no non-test package imports testaws — a property of the import graph that drifts silently.

TestNoNonTestPackageImportsThisOne asserts it with go list -deps, whose Imports field is the non-test import set. Verifying it took two attempts and both failure modes are recorded in the test: go test runs each binary in its own package directory, so the first ./... saw only internal/testaws while a deliberate violation sat in internal/awsrates/offerfile; and the mutation needs -count=1, since a violation in another package leaves this binary byte-identical and the cached pass is served.

Verification

  • 4/4 clamp mutations detected — removing it, > → >=, bounding at MaxInt32, restoring the always-true predicate. Each fails with the message naming its own rationale.
  • The jumbo test cases sit behind if math.MaxInt > math.MaxUint32 and take their values from int64 variables. The guard alone is not enough: it stops the block executing, not compiling, and a constant conversion is checked at compile time — the literal form failed go vet under GOARCH=386 with "overflows int". CI type-checks tests on every cross-build cell, which is where that surfaced.
  • go test -race -timeout 25m ./... — exit 0, 38 packages.
  • Whole-repo golangci-lint 615, unchanged from this branch's merge base; --new-from-merge-base=origin/main 0 issues.
  • Standalone gosec nosec 4 → 10, all six sites gone from its output.
  • go vet and gofmt clean; native, linux/386 and linux/arm/7 all build and vet.
  • markdownlint on CHANGELOG.md at the 361 baseline — the existing [#264] link definition was reused rather than duplicated.

All four of #264's acceptance criteria are addressed: each site suppressed for the run that reports it, both runners agreeing, the convention written down at .golangci.yml's gosec settings where someone adding a suppression will be, and config.go's guard confirmed not to bound the conversion — and fixed.

Issue #264 lists six gosec findings annotated with //nolint:gosec, which
golangci-lint honours and gosec's own run does not, so each passes lint
and stays open as a code-scanning alert. Both halves of that are true.
Two of the issue's premises are not, and one of the six sites was hiding
a real defect.

The defect is at internal/storage/s3/config.go. readyToTrip converts the
configured int failure threshold to the uint32 that
circuit.Counts.TotalFailures is, guarded by `if cfg.FailureThreshold <= 0`
and annotated "guarded positive above". That is true and insufficient:
the guard bounds the sign, not the width. On a 64-bit platform
failure_threshold: 4294967296 passes it, narrows to 0, and produces the
predicate `TotalFailures >= 0` — true on the zeroth failure. The breaker
opens before the first request and rejects every S3 operation for the
life of the mount, which is exactly the outcome the function's own zero
case exists to prevent, arriving by the one path that bypassed it.
4294967297 is the quieter half: a threshold of 1, so the breaker trips on
a single failure while the configuration says four billion. Measured in a
standalone probe, not reasoned about.

It is now clamped to math.MaxUint32, above which no count of failures can
reach the threshold anyway — unreachable is the honest reading of an
unreachable number, and clamping keeps this a total function rather than
adding a second validation site next to internal/config's. Four mutations
are detected by TestCircuitBreakerConfigReachesTheBreaker: removing the
clamp, > for >=, bounding at MaxInt32, and restoring the always-true
predicate.

Why neither run reported it. .golangci.yml excludes G115 repo-wide, so
golangci-lint never had the finding; the //nolint form meant the
standalone run that did have it was not being asked to skip it, but that
run was not gating anything either. A finding excluded from one runner and
suppressed for the other is invisible in both directions.

The two premises that do not hold, both probed:

  #nosec alone satisfies both runs, and writing both directives fails
  lint. golangci-lint runs gosec's own analyser, which strips #nosec
  sites before golangci-lint sees them, so the dual annotation the issue
  prescribes leaves the //nolint:gosec with nothing to suppress and
  nolintlint reports it unused. The prescription converts a suppression
  into a lint failure.

  G703 is not in the gosec golangci-lint bundles. On a two-line os.Getenv
  program, standalone gosec reports G703 and G304; golangci-lint reports
  G304 alone. The //nolint:gosec at internal/awsname could not have
  worked even written correctly.

Twelve //nolint directives across nine files had a leading space, which
makes them ordinary comments — only //nolint: is a directive — each with
a paragraph of justification attached to nothing. Eleven more had no
explanation, which nolintlint's require-explanation reports. Five of those
were staticcheck findings that were simply fixable (two QF1008 embedded
selectors, two QF1003 if-chains wanting a tagged switch, one QF1012
WriteString(Sprintf)), so they are fixed rather than annotated. The two
switch rewrites needed default arms, which .golangci.yml's
default-signifies-exhaustive accepts and which say why the remaining
statuses and op types belong together. nolintlint: 23 findings to 0.

testaws.SecretAccessKey's G101 argument was that it is AWS's own
documentation example key, accepted only by the emulator. True and not
the whole risk: the constant is at package scope in testaws.go, not a
_test.go, so the filename keeps neither it nor the embedded emulator out
of a shipped binary. What does is that no non-test package imports
testaws, a property of the import graph that drifts silently.
TestNoNonTestPackageImportsThisOne asserts it with `go list -deps`, whose
Imports field is the non-test import set. Verifying that test took two
attempts and both failure modes are recorded in it: `go test` runs each
binary in its own package directory, so the first ./... saw only
internal/testaws while a deliberate violation sat in
internal/awsrates/offerfile; and the mutation needs -count=1, because a
violation in another package leaves this binary byte-identical and the
cached pass is served.

The convention is written down at .golangci.yml's gosec settings, since
that is where someone adding a suppression will be. Recorded there too:
no directive of either form works in a cgo package, and
congestion_linux.go is linux-only so a //nolint there is unverifiable
from a macOS machine.

The jumbo test cases are guarded by `if math.MaxInt > math.MaxUint32` and
take their values from int64 variables. The guard alone is not enough:
it stops the block executing, not compiling, and a constant conversion is
checked at compile time — so the literal form failed `go vet` under
GOARCH=386 with "overflows int". CI type-checks tests on every cross-build
cell, which is where that surfaced.

Whole-repo golangci-lint is unchanged at 615, standalone gosec nosec 4 to
10 with all six sites gone from its output, go test -race clean across 38
packages, and native, linux/386 and linux/arm/7 all build and vet.

Closes #264
@scttfrdmn scttfrdmn added type: technical-debt Code refactoring, cleanup, or architecture improvements priority: low Nice to have, low urgency labels Aug 5, 2026
@scttfrdmn scttfrdmn added area: build Build system, compilation, packaging area: ci-cd CI workflows, gates, and release automation effort: small Small effort - 2-4 hours labels Aug 5, 2026
@scttfrdmn
scttfrdmn merged commit da490b6 into main Aug 5, 2026
33 checks passed
@scttfrdmn
scttfrdmn deleted the fix/gosec-suppression-directives-264 branch August 5, 2026 22:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area: build Build system, compilation, packaging area: ci-cd CI workflows, gates, and release automation effort: small Small effort - 2-4 hours priority: low Nice to have, low urgency type: technical-debt Code refactoring, cleanup, or architecture improvements

Projects

None yet

Development

Successfully merging this pull request may close these issues.

gosec: six sites suppress for golangci-lint only, so they pass lint and stay open as code-scanning alerts

1 participant