Skip to content

Commit 032fc8d

Browse files
authored
Merge pull request projectdiscovery#7215 from sandiyochristan/fix/use-crypto-rand-in-js-globals
fix: use crypto/rand instead of math/rand in JS global functions
2 parents 3ec23a9 + 44ef9ce commit 032fc8d

File tree

1 file changed

+9
-4
lines changed

1 file changed

+9
-4
lines changed

pkg/js/global/scripts.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@ package global
33
import (
44
"bytes"
55
"context"
6+
"crypto/rand"
67
"embed"
7-
"math/rand"
8+
"math/big"
89
"net"
910
"reflect"
1011
"time"
@@ -49,8 +50,8 @@ func initBuiltInFunc(runtime *goja.Runtime) {
4950
Description: "Rand returns a random byte slice of length n",
5051
FuncDecl: func(n int) []byte {
5152
b := make([]byte, n)
52-
for i := range b {
53-
b[i] = byte(rand.Intn(255))
53+
if _, err := rand.Read(b); err != nil {
54+
return nil
5455
}
5556
return b
5657
},
@@ -61,7 +62,11 @@ func initBuiltInFunc(runtime *goja.Runtime) {
6162
Signatures: []string{"RandInt() int"},
6263
Description: "RandInt returns a random int",
6364
FuncDecl: func() int64 {
64-
return rand.Int63()
65+
n, err := rand.Int(rand.Reader, new(big.Int).SetInt64(1<<63-1))
66+
if err != nil {
67+
return 0
68+
}
69+
return n.Int64()
6570
},
6671
})
6772

0 commit comments

Comments
 (0)