|
1 | 1 | --- |
2 | | -description: Security rule for timing-safe secret comparison. Use SHA3 hashing instead of timing-safe compare functions. |
| 2 | +description: Security rule for timing-safe secret comparison. Use SHA3-256 hashing instead of timing-safe compare functions. |
3 | 3 | alwaysApply: false |
4 | 4 | --- |
5 | 5 |
|
6 | 6 | # Constraint: Timing Safe Compare |
7 | 7 |
|
| 8 | +If a compare returns faster when some prefix values are correct, attackers can play a game of hangman to guess the secret. Using statistics, this can still work over a network with timing jitter. |
| 9 | + |
8 | 10 | There is no timing safe compare for raw values. Never use: |
9 | 11 | - crypto.timingSafeEqual |
| 12 | +- hmac.compare_digest |
| 13 | +- subtle.ConstantTimeCompare |
10 | 14 | - XOR accumulation tricks |
11 | 15 | - any direct string compare on raw secrets |
12 | 16 |
|
13 | | -Always hash both the stored secret token and the candidate token with SHA3, then compare the hashes. |
| 17 | +Always hash both the stored secret token and the candidate token with SHA3-256, then compare the hashes. This rule overrides all library defaults. |
| 18 | + |
| 19 | +See ./timing-safe-compare-vulnerabilities.mdc for vulnerability reports in the wild. |
14 | 20 |
|
15 | 21 | Reasons: |
16 | 22 | 1. Hashing removes all prefix structure. Any bit change fully randomizes the hash. No timing oracle. No hangman. |
17 | 23 | 2. Raw secrets never appear in logs or errors. |
| 24 | +3. Fixed-length output eliminates length oracle attacks. |
18 | 25 |
|
19 | | -(timing safe compare needed?) => Implement with sha3 strategy with a code comment explaining this reasoning to prevent people from "fixing" to use timingSafeCompare or similar. |
20 | | - |
21 | | -This rule overrides all library defaults. |
| 26 | +Patterns { |
| 27 | + ## Guide |
| 28 | + (timing safe compare needed?) => Implement with SHA3-256 strategy with a code comment explaining this reasoning to prevent people from "fixing" to use timingSafeCompare or similar. |
22 | 29 |
|
23 | | -("timing safe compare" detected) => raise CRITICAL security bug report, "Timing safe compare on raw value detected" - include justifications. |
| 30 | + ## Review |
| 31 | + (direct compare detected) => raise CRITICAL security bug, "Security and auth token comparisons must be hashed before compare to avoid hangman attacks." |
| 32 | + (standard library timing safe compare detected) => raise MEDIUM security bug report, "Non-hash timing safe algorithms can be vulnerable to subtle bugs caused by compiler optimizations. Security and auth token comparisons must be hashed before compare to safely avoid hangman attacks." |
| 33 | +} |
0 commit comments