Fix gosec G118 findings on main - #1064
Open
kaidaguerre wants to merge 2 commits into
Open
Conversation
The golangci-lint 'version: latest' resolved by the lint CI action on main now bundles a gosec ruleset that flags 3 pre-existing G118 context-propagation findings. These already have equivalent fixes on v1.5.x that were never back-ported to main; this commit applies the same resolution. - internal/cmdconfig/cmd_hooks.go:101 G118: context cancellation function returned by WithCancel/WithTimeout/ WithDeadline is not called. The cancel ownership is intentionally transferred to the package-level tasksCancelFn. postRunHook now defers tasksCancelFn() so the context is cancelled on every exit path (previously only the timeout branch cancelled it, leaking the context on the channel-closed path). gosec cannot trace cancel ownership through a package global, so a specific, justified //nolint:gosec is added. - internal/dashboardserver/api.go:23 G118: Goroutine uses context.Background/TODO while request-scoped context is available. The graceful-shutdown context now derives from the request-scoped ctx via context.WithoutCancel(ctx) (ctx is already Done at that point) instead of context.Background(), propagating context lineage. - internal/db_client/db_client_execute.go:159 G118: context cancellation function returned by WithCancel/WithTimeout/ WithDeadline is not called. The cancel is deliberately discarded (existing //nolint:govet documents that pgx prematurely closes the PG connection if this cancel fires in defer); the deadline timer still bounds the context. Extended the existing directive to //nolint:govet,gosec with the G118 justification. Fixes #1063
…e now-dead //nolint:gosec directive
MichaelBurgess
approved these changes
May 19, 2026
|
This PR is stale because it has been open 60 days with no activity. Remove stale label or comment or this will be closed in 30 days. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
The lint CI on
mainusesgolangci/golangci-lint-actionwithversion: latest. The currently-resolved gosec ruleset flags 3 pre-existing G118 (context-propagation) findings in code that predates this change. These break the lint CI gate onmain.These findings already have equivalent fixes on
v1.5.x(which pinsversion: v2.11.4and is lint-clean) — they were simply never back-ported tomain. This PR applies the same resolution tomain.Verified with golangci-lint v2.11.4 rebuilt from source with
GOTOOLCHAIN=go1.26.1(the locally-installed binary is built with an older Go and refuses go1.26 modules), run exactly as CI does:golangci-lint run --timeout=10m ./....Exact gosec G118 messages (before fix)
internal/cmdconfig/cmd_hooks.go:101:47internal/dashboardserver/api.go:23:2internal/db_client/db_client_execute.go:159:35Per-site fix
internal/cmdconfig/cmd_hooks.go:101— The cancel func is intentionally stored in the package-leveltasksCancelFn, whose ownership belongs topostRunHook.postRunHookpreviously only called it on the timeoutselectbranch, leaking the context on the channel-closed branch. ChangedpostRunHooktodefer tasksCancelFn()so the context is cancelled on every exit path (a real correctness improvement). gosec's lost-cancel detector cannot trace cancel ownership through a package-level global (it only handles struct fields and direct returns), so a specific, justified//nolint:gosecis added — this is the documented judgement call for this site.internal/dashboardserver/api.go:23— The graceful-shutdown context wascontext.WithTimeout(context.Background(), 5*time.Second)inside a goroutine that has a request-scopedctxavailable. Real code fix: derive fromcontext.WithTimeout(context.WithoutCancel(ctx), 5*time.Second).ctxis alreadyDone()at that point (we only reach this line after<-ctx.Done()), soWithoutCancelpropagates the request context lineage without inheriting the already-fired cancellation that would otherwise make the shutdown context immediately expired. (Identical to the existing v1.5.x resolution.)internal/db_client/db_client_execute.go:159— The cancel func is deliberately discarded; the existing//nolint:govetcomment (since 2024) documents that pgx prematurely closes the PG connection if this cancel fires indefer. The deadline timer still bounds the context lifetime, so there is no unbounded leak. Extended the existing directive to//nolint:govet,gosecwith the G118 justification — judgement call: genuine deliberate-design false positive (matches the existing v1.5.x resolution).Judgement calls (nolint)
Two sites use a narrowly-scoped, explained
//nolint:gosec(config requiresrequire-explanation+require-specific):cmd_hooks.go:101— gosec limitation: cannot follow cancel ownership through a package global. Thedefer tasksCancelFn()change makes cancellation guaranteed on all paths regardless.db_client_execute.go:159— deliberate, long-documented design (pgx connection-cancellation behaviour); deadline timer still bounds the context.api.gois a real code fix, no nolint.Verification
GOTOOLCHAIN=go1.26.1),run --timeout=10m ./...: 3 issues → 0 issues. Zero new issues introduced.GOTOOLCHAIN=auto go build ./...: clean (exit 0).GOTOOLCHAIN=auto go test ./...: 6 test packages pass, 0 fail, exit 0.v1.5.xverified with the same binary: 0 issues (not affected — already fixed there). This is a main-only PR.Why this matters
This unblocks the lint CI gate on
main, which is currently failing on these 3 pre-existing findings independently of any other change — including the pgx v5.9.2 backport PR #1062, whose lint CI is blocked by exactly these findings.Fixes #1063