Skip to content

Commit caf8857

Browse files
authored
fix(G404): flag missing math/rand weak-random functions (#1694)
1 parent cbef395 commit caf8857

2 files changed

Lines changed: 40 additions & 4 deletions

File tree

rules/rand.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,10 @@ func NewWeakRandCheck(id string, _ gosec.Config) (gosec.Rule, []ast.Node) {
3030
rule := &weakRand{newCallListRule(id,
3131
"Use of weak random number generator (math/rand or math/rand/v2 instead of crypto/rand)",
3232
issue.High, issue.Medium)}
33-
rule.AddAll("math/rand", "New", "Read", "Float32", "Float64", "Int", "Int31", "Int31n",
34-
"Int63", "Int63n", "Intn", "NormFloat64", "Uint32", "Uint64")
35-
rule.AddAll("math/rand/v2", "New", "Float32", "Float64", "Int", "Int32", "Int32N",
36-
"Int64", "Int64N", "IntN", "N", "NormFloat64", "Uint32", "Uint32N", "Uint64", "Uint64N", "UintN")
33+
rule.AddAll("math/rand", "New", "Read", "ExpFloat64", "Float32", "Float64", "Int", "Int31", "Int31n",
34+
"Int63", "Int63n", "Intn", "NormFloat64", "Perm", "Shuffle", "Uint32", "Uint64")
35+
rule.AddAll("math/rand/v2", "New", "ExpFloat64", "Float32", "Float64", "Int", "Int32", "Int32N",
36+
"Int64", "Int64N", "IntN", "N", "NormFloat64", "Perm", "Shuffle", "Uint", "Uint32", "Uint32N", "Uint64", "Uint64N", "UintN")
3737

3838
return rule, []ast.Node{(*ast.CallExpr)(nil)}
3939
}

testutils/g404_samples.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,4 +184,40 @@ func main() {
184184
_ = rand3.IntN(2) // bad
185185
}
186186
`}, 3, gosec.NewConfig()},
187+
{[]string{`
188+
package main
189+
190+
import "math/rand"
191+
192+
func main() {
193+
p := rand.Perm(10) // bad
194+
println(len(p))
195+
f := rand.ExpFloat64() // bad
196+
println(f)
197+
nums := []int{1, 2, 3}
198+
rand.Shuffle(len(nums), func(i, j int) { // bad
199+
nums[i], nums[j] = nums[j], nums[i]
200+
})
201+
println(nums[0])
202+
}
203+
`}, 3, gosec.NewConfig()},
204+
{[]string{`
205+
package main
206+
207+
import "math/rand/v2"
208+
209+
func main() {
210+
u := rand.Uint() // bad
211+
println(u)
212+
p := rand.Perm(10) // bad
213+
println(len(p))
214+
f := rand.ExpFloat64() // bad
215+
println(f)
216+
nums := []int{1, 2, 3}
217+
rand.Shuffle(len(nums), func(i, j int) { // bad
218+
nums[i], nums[j] = nums[j], nums[i]
219+
})
220+
println(nums[0])
221+
}
222+
`}, 4, gosec.NewConfig()},
187223
}

0 commit comments

Comments
 (0)