Skip to content

Commit 1370ba8

Browse files
committed
fixing redundant maxRandFloat call
1 parent a201458 commit 1370ba8

File tree

1 file changed

+5
-4
lines changed

1 file changed

+5
-4
lines changed

dependency/vault_common.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ var (
2121
onceVaultDefaultLeaseDuration sync.Once
2222
VaultLeaseRenewalThreshold float64
2323
onceVaultLeaseRenewalThreshold sync.Once
24+
// maxRandFloat is the maximum value for cryptographically secure random float64 generation
25+
maxRandFloat = big.NewInt(1 << 53)
2426
)
2527

2628
// Secret is the structure returned for every secret within Vault.
@@ -78,19 +80,18 @@ type renewer interface {
7880
secrets() (*Secret, *api.Secret)
7981
}
8082

81-
// cryptoRandFloat64 generates a cryptographically secure random float64 in [0.0, 1.0)
83+
// cryptoRandFloat64 generates a cryptographically secure random float64 in [0.0, 1.0) (CWE-338 fix)
8284
func cryptoRandFloat64() float64 {
8385
// Generate a random 53-bit integer (mantissa precision of float64)
84-
max := big.NewInt(1 << 53)
85-
n, err := rand.Int(rand.Reader, max)
86+
n, err := rand.Int(rand.Reader, maxRandFloat)
8687
if err != nil {
8788
// Fallback to a reasonable default if crypto/rand fails
8889
// This should never happen in practice
8990
log.Printf("[WARN] crypto/rand failed, using 0.5 as fallback: %v", err)
9091
return 0.5
9192
}
9293
// Convert to float64 in range [0.0, 1.0)
93-
return float64(n.Int64()) / float64(max.Int64())
94+
return float64(n.Int64()) / float64(maxRandFloat.Int64())
9495
}
9596

9697
func renewSecret(clients *ClientSet, d renewer) error {

0 commit comments

Comments
 (0)