Skip to content

fix(G404): flag missing math/rand weak-random functions#1694

Merged
ccojocar merged 1 commit into
securego:masterfrom
StressTestor:fix/g404-missing-weak-rand-funcs
Jun 15, 2026
Merged

fix(G404): flag missing math/rand weak-random functions#1694
ccojocar merged 1 commit into
securego:masterfrom
StressTestor:fix/g404-missing-weak-rand-funcs

Conversation

@StressTestor

Copy link
Copy Markdown
Contributor

the problem

G404's call lists for math/rand and math/rand/v2 are 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, and rand.ExpFloat64 all return or apply weak randomness but pass G404 clean today, and in v2 rand.Uint (added in Go 1.23) was never backfilled into the list either.

rand.Uint64() is flagged; sibling rand.Uint() is not. NormFloat64 is in the list but ExpFloat64 right next to it isn't. these are gaps, not deliberate exclusions.

the fix

add the missing functions to both AddAll lists in rules/rand.go:

  • math/rand: Perm, Shuffle, ExpFloat64
  • math/rand/v2: Uint, Perm, Shuffle, ExpFloat64

same class of gap as issue #486 (rand.Intn was 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/rand package-level function still excluded is Seed, 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; New itself is already listed and still flagged.

tests

added two positive samples to testutils/g404_samples.go (a math/rand case expecting 3 issues, a math/rand/v2 case expecting 4). both report 0 on the current rule and the expected counts only after the fix lands. the existing rand.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 use math/rand in its source).

@codecov

codecov Bot commented Jun 15, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 80.46%. Comparing base (f1c81de) to head (e49928c).
⚠️ Report is 1 commits behind head on master.

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.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@ccojocar ccojocar merged commit caf8857 into securego:master Jun 15, 2026
14 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants