fix(G404): flag missing math/rand weak-random functions#1694
Merged
ccojocar merged 1 commit intoJun 15, 2026
Conversation
ccojocar
approved these changes
Jun 15, 2026
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #1694 +/- ##
=======================================
Coverage 80.46% 80.46%
=======================================
Files 110 110
Lines 10255 10255
=======================================
Hits 8252 8252
Misses 1516 1516
Partials 487 487 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
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.
the problem
G404's call lists for
math/randandmath/rand/v2are missing several package-level functions that emit weak randomness, so calls to them aren't flagged. that's a false negative in a security rule:rand.Perm,rand.Shuffle, andrand.ExpFloat64all return or apply weak randomness but pass G404 clean today, and in v2rand.Uint(added in Go 1.23) was never backfilled into the list either.rand.Uint64()is flagged; siblingrand.Uint()is not.NormFloat64is in the list butExpFloat64right next to it isn't. these are gaps, not deliberate exclusions.the fix
add the missing functions to both
AddAlllists inrules/rand.go:math/rand:Perm,Shuffle,ExpFloat64math/rand/v2:Uint,Perm,Shuffle,ExpFloat64same class of gap as issue #486 (
rand.Intnwas missing), which was fixed by extending this exact list.scope
after this, the v2 list is a complete superset of the package's weak-random functions. the one
math/randpackage-level function still excluded isSeed, on purpose: it consumes a seed and emits no number, so flagging it would be a false positive. seed-source constructors (NewSource,NewPCG,NewChaCha8) stay out for the same reason;Newitself is already listed and still flagged.tests
added two positive samples to
testutils/g404_samples.go(amath/randcase expecting 3 issues, amath/rand/v2case expecting 4). both report 0 on the current rule and the expected counts only after the fix lands. the existingrand.New(rand.NewSource(...))/rand.New(rand.NewPCG(...))samples still report exactly 1, so the seed-constructor boundary is unchanged.go test ./rules/green,golangci-lint run ./rules/... ./testutils/...clean, and gosec's own G404 self-scan stays at 0 issues (gosec doesn't usemath/randin its source).