|
21 | 21 | onceVaultDefaultLeaseDuration sync.Once |
22 | 22 | VaultLeaseRenewalThreshold float64 |
23 | 23 | onceVaultLeaseRenewalThreshold sync.Once |
| 24 | + // maxRandFloat is the maximum value for cryptographically secure random float64 generation |
| 25 | + maxRandFloat = big.NewInt(1 << 53) |
24 | 26 | ) |
25 | 27 |
|
26 | 28 | // Secret is the structure returned for every secret within Vault. |
@@ -78,19 +80,18 @@ type renewer interface { |
78 | 80 | secrets() (*Secret, *api.Secret) |
79 | 81 | } |
80 | 82 |
|
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) |
82 | 84 | func cryptoRandFloat64() float64 { |
83 | 85 | // 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) |
86 | 87 | if err != nil { |
87 | 88 | // Fallback to a reasonable default if crypto/rand fails |
88 | 89 | // This should never happen in practice |
89 | 90 | log.Printf("[WARN] crypto/rand failed, using 0.5 as fallback: %v", err) |
90 | 91 | return 0.5 |
91 | 92 | } |
92 | 93 | // 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()) |
94 | 95 | } |
95 | 96 |
|
96 | 97 | func renewSecret(clients *ClientSet, d renewer) error { |
|
0 commit comments