Skip to content

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

Closed

Description

@scttfrdmn

Two gosec runs exist in this repo and they read different suppression directives:

  • golangci-lint's gosec (.golangci.yml) honors //nolint:gosec. This is the lint job, and it is a required check.
  • The standalone gosec (.github/workflows/security.yml:58, gosec -fmt sarif -no-fail ./...) honors only #nosec. Its SARIF becomes GitHub code scanning, which surfaces as the gosec check on every PR.

So a finding suppressed for one and not the other passes lint and stays open in the security tab. Found while fixing PR #263, where two such sites failed the gosec check while lint reported 0 issues.

The six sites

Each carries a //nolint:gosec with a reason, and each is an open code-scanning alert on main today:

site rule alert
internal/difftest/fs.go:70 G304 #52
internal/awsname/awsname.go:127 G703 #48
internal/network/congestion_linux.go:67 G304 #51
internal/network/congestion_linux.go:84 G304 #50
internal/storage/s3/config.go:202 G115 #41
internal/testaws/testaws.go:33 G101 #47

Two more were fixed in PR #263 (internal/awsrates/offerfile/cmd/genrates/main.go G306, internal/fuse/unmount_path.go G204) by adding #nosec alongside the existing //nolint.

Why this is worth fixing rather than living with

The gosec check reports new alerts in code changed by this pull request, so it only fires when a PR touches one of these lines or adds a new one. That makes it a check that passes for months and then fails on an unrelated change, in a file whose comment already explains the finding is not real. The reviewer's reasonable conclusion is that the security check is noise, which is the state a gate should never reach.

It also cuts the other way: because these alerts are permanently open, a genuine finding in the same rule family arrives into a security tab that already has six entries nobody acts on.

What to do

Not a blanket #nosec sweep. Each needs its own judgment, and two of the six look like they may be more than a suppression:

  • s3/config.go:202 (G115, uint32(cfg.FailureThreshold)) is suppressed as "guarded positive above". Worth confirming the guard actually bounds it below 2^32 rather than only above zero — the comment claims less than the conversion needs.
  • testaws.go:33 (G101, hardcoded credential) is a test fixture, and the comment says so. But the suppression is at package scope in a file that a non-test package imports, which is a different question from whether the constant matters.

The other four read as genuinely-not-findings (procfs constants, t.TempDir() paths, an operator-supplied AWS_CONFIG_FILE) and want #nosec alongside the existing comment.

Consider also whether the two runs should be reconciled rather than double-annotated — for example, having the security workflow consume golangci-lint's gosec output, or documenting the dual-directive convention in .golangci.yml so the next person hits it in the file rather than in CI. The dual annotation is the smaller change; a single run is the one that stops this recurring.

Acceptance criteria

  • Each of the six sites is either suppressed for both tools with its reason, or fixed
  • ~/go/bin/gosec -no-fail ./... and golangci-lint run agree on which findings remain
  • The convention is written down where someone adding a suppression will see it
  • The corresponding code-scanning alerts are closed or dismissed with a reason
  • s3/config.go:202's guard is confirmed to bound the conversion, not just its sign

Activity

  1. added
    type: technical-debt
    priority: low
    area: ci-cd
    area: build
    effort: small
    on Aug 3, 2026
  2. scttfrdmn commented on Aug 3, 2026

    @scttfrdmn
    OwnerAuthor

    This bit for real on #270, which is worth recording since the issue was filed from reading the two
    configurations rather than from an incident.

    I added //nolint:gosec to two deliberate findings in internal/distributed/auth.go and its test,
    and verified locally with golangci-lint run --new-from-merge-base=origin/main: 0 issues. The
    pull request was then blocked by a gosec check-run reporting 1 new alert including 1 error, which
    is GitHub code scanning gating on the SARIF from the standalone gosec in security.yml — a
    different tool reading a different directive.

    Two details worth having written down, because neither is obvious from either tool's documentation:

    1. //nolint:gosec and #nosec are not interchangeable, which the issue already says.
    2. Putting both on the same line does not work either. //nolint:gosec // #nosec G304 -- reason
      satisfies golangci-lint and is still reported by standalone gosec. It wants #nosec in the
      trailing comment on its own, with //nolint on a separate line above — which is what
      internal/fuse/unmount_path.go:89-90 and
      internal/awsrates/offerfile/cmd/genrates/main.go:111-117 already do. Both of those files carry
      comments explaining the trap, so this has been learned before and is easy to re-learn the slow way.

    Neither finding was fixable by changing the code: one reads a file at an operator-supplied path,
    which is the function; the other writes a 0644 secret file because that is the insecure file the
    loader has to refuse, so satisfying G306 would delete the test.

    Whatever this issue settles on, the useful outcome is that a suppression cannot be half-applied. A
    lint job reporting 0 issues while code scanning blocks the merge is the specific failure mode.

  3. scttfrdmn commented on Aug 5, 2026

    @scttfrdmn
    OwnerAuthor

    Two measurements from #200 that change this issue's premise. Both were made with the versions this repo uses today — golangci-lint 2.12.2, gosec installed from @latest as security.yml does — and both were probed rather than reasoned about.

    #nosec alone satisfies both runs, so the dual annotation is more than is needed

    The issue says golangci-lint's gosec "honors //nolint:gosec" and the standalone run "honors only #nosec", and prescribes writing both. Only half of that holds.

    Probed with four sites in one package — //nolint:gosec alone, #nosec alone, both, and neither:

    directive standalone gosec golangci-lint's gosec
    //nolint:gosec only flagged suppressed
    #nosec only suppressed suppressed
    neither flagged flagged

    golangci-lint honors #nosec as well, because it runs gosec's own analyser and gosec strips #nosec sites before reporting. So #nosec alone is sufficient for both runs, and the dual annotation this issue asks for adds a directive that is never the one doing the work.

    That doesn't change the acceptance criteria much — the six sites still need something they don't have today — but it changes what to write at each one. A single #nosec with its reason, rather than two directives whose relationship the next reader has to reconstruct.

    Worth noting the asymmetry is fully explained by which annotation each tool owns: //nolint is golangci-lint's own mechanism and gosec has never heard of it, while #nosec is gosec's and golangci-lint inherits it by delegation.

    Neither directive works at all inside a cgo package

    This is the part with no workaround, and it is why #200 could not be closed by suppression.

    sdks/c/main.go is the module's only cgo package. Probed there with four placements — the directive on the line above the call, first of a comment block, last of a block, and trailing the call itself. In a pure-Go package all four suppress. In the cgo package none do, and gosec reports nosec: 0, meaning it recognised no directive at all.

    The cause is in what gosec is given to analyse. cgo rewrites every C.f(...) call into an inline closure carrying /*line :N:C*/ directives:

    // #nosec G115 -- directive first of three
    // second line
    func() _cgo_unsafe.Pointer{ _cgo0 := /*line :16:11*/dst; ... _Ctype_size_t /*line :16:29*/(n); ... }()

    The call and any comment near it end up on the same synthetic position, so the association a suppression depends on is gone.

    The two runs then diverge in what they do with findings neither can suppress, and the divergence is not the one this issue describes:

    • golangci-lint discards them, and says so in its own log: runner/invalid_issue: issue related to file <go-build cache path> is skipped. The analysed file is a build-cache artifact rather than a repository path. This is not the .golangci.yml G115 exclusion doing it — lifting that exclusion still yields 0 issues for the package.
    • The standalone run keeps them, with "artifactLocation": {}. GitHub's SARIF ingester rejects a result with no location and fails the whole upload, so security.yml was filtering them out by hand.

    So for cgo the failure mode is worse than "passes lint, stays open as an alert" — the findings were reachable by neither run. Three integer conversions across a C ABI went unreviewed, and one of them was a real defect: objectfs_put narrowed a size_t length to a C.int, so a 4 GiB PUT returned OBJECTFS_OK and stored an empty object (fixed in #355).

    #355 also fixes the reporting half — security.yml now back-fills sdks/c/main.go rather than dropping those results, since cgo's //line directives mean the reported line numbers are already correct and sdks/c is the only cgo package in the module. Verified by inserting eight blank lines after the import block, which moved all three findings by exactly eight.

    Consequence for this issue's last section

    The issue asks whether the two runs should be reconciled rather than double-annotated, and calls the dual annotation "the smaller change". Given the above, the dual annotation is not just larger than necessary — it is a no-op at one of the two directives everywhere, and a no-op at both inside sdks/c. Reconciling to a single run looks better than it did when this was filed.

    sdks/c should be carved out either way, and stated as such wherever the convention is documented: no suppression directive works there, so a finding in that package has to be fixed or the reason recorded in a plain comment. main.go's header comment now says this, including the negative result that CGO_ENABLED=0 gosec ./sdks/c/... analyses zero files (the package cannot build without cgo) and so is a vacuous pass rather than an escape hatch.

    Also, on the two sites this issue flags as possibly more than suppressions — s3/config.go:202 (whether the G115 guard bounds below 2^32, not only above zero) and testaws.go:33 (G101 at package scope in a file a non-test package imports) — neither was examined as part of #200. They stay open here.

  4. scttfrdmn commented on Aug 5, 2026

    @scttfrdmn
    OwnerAuthor

    Fixed in #356. Three notes for anyone reading this issue rather than the PR, since two of them contradict what the issue asks for.

    The prescribed fix fails lint. This issue says to write both #nosec Gxxx and //nolint:gosec so that each run sees a directive it understands. golangci-lint runs gosec's own analyser, which strips #nosec sites before golangci-lint sees them — so #nosec alone already satisfies both runs, and adding the //nolint:gosec leaves it with nothing to suppress, at which point nolintlint reports it unused. Writing both converts a suppression into a lint failure. #nosec alone is the convention, now written down at .golangci.yml's gosec settings.

    G703 is not in the gosec golangci-lint bundles. Probed on a two-line program that reads os.Getenv and passes the result to os.ReadFile: standalone gosec reports G703 and G304; golangci-lint reports G304 alone. So the //nolint:gosec at internal/awsname/awsname.go was not merely suppressing for the wrong run — the run it targeted never had that finding to suppress.

    One of the six was hiding a real defect, which is the half of this issue I had explicitly left open above. internal/storage/s3/config.go's //nolint justification read "guarded positive above", referring to if cfg.FailureThreshold <= 0. That guard bounds the sign and not the width. failure_threshold: 4294967296 passes it, narrows to 0 through the uint32 conversion, and yields the predicate TotalFailures >= 0 — true on the zeroth failure, so the circuit breaker opens before the first request and rejects every S3 operation for the life of the mount. 4294967297 narrows to 1, which is quieter and worse: a breaker tripping on one failure while the config says four billion. Now clamped, with four mutations of the clamp detected by test.

    It was invisible to both runners at once, in opposite ways: .golangci.yml excludes G115 repo-wide so golangci-lint never had the finding, and the standalone run that did have it is not gating anything. That combination — excluded from one runner, suppressed for the other — is the general hazard behind this issue, and it is worth more than the six sites named here.

    Also fixed along the way: twelve // nolint directives across nine files had a leading space, which makes them ordinary comments rather than directives. Every one was inert, each with a full paragraph of justification attached to nothing. Eleven further //nolints had no explanation; five of those were staticcheck findings that were simply fixable, so they are fixed rather than annotated. nolintlint is at 0, down from 23.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area: buildarea: ci-cdeffort: smallpriority: lowtype: technical-debt

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions