🤖 The AI Prompt That Sniffs Out an Auth Timing Side-Channel
A string comparison like storedHash == providedHash or a naive loop-based token check returns as soon as it finds the first mismatched character, which means a wrong guess that happens to share a correct first few characters takes measurably longer to reject than one that’s wrong from character one — and an attacker measuring response latency over enough requests can use that timing difference to recover a secret one character at a time, without ever seeing the actual value being compared. This pattern hides easily inside password checks, API key validation, HMAC signature verification, and session token comparisons, because the code reads as completely correct — it’s the timing of a correct-looking comparison that leaks the secret, not its logic.
🔎 The Problem
Paste this prompt along with the auth-related file(s) you want checked: "Scan this code for any place a secret value (password hash, API key, HMAC signature, session token, CSRF token) is compared against untrusted input using a standard equality operator, string comparison function, or character-by-character loop that can return early on a mismatch. For each one found: show the exact comparison, explain in one sentence why its runtime depends on how many leading characters match, and give the constant-time replacement for this specific language/framework (for example hmac.compare_digest in Python, crypto.timingSafeEqual in Node, CryptographicOperations.FixedTimeEquals in C#). Do not flag comparisons of genuinely public, non-secret values."
✅ Fix: Replace Every Flagged Comparison With a Constant-Time One
- Swap each flagged secret comparison for your language’s built-in constant-time equality function rather than hand-rolling one — these are deceptively easy to get subtly wrong (a short-circuiting length check before the loop starts can reintroduce the exact same leak).
- Ask the follow-up prompt “Which of these comparisons sits directly on a request path reachable without authentication?” first, since a timing side-channel on an already-authenticated, rate-limited internal endpoint is a far lower priority fix than one sitting on a public login or API-key check.
- Re-run the same scan after the fix as a regression check — it’s common to fix the main comparison but miss a second one nearby (an “early return if API key is missing” check that still does a non-constant-time length comparison first).
🚨 Why This Is Dangerous
- A timing side-channel produces zero error logs, zero failed-auth alerts, and zero visible symptoms right up until someone has quietly extracted a valid credential — by the time it’s noticed, there’s no incident to point to, just a comparison that’s been leaking information to every request it ever answered.
A secret comparison that’s logically correct can still be a side-channel — the bug isn’t in what the code decides, it’s in how long it takes to decide it.




