Interview frequency: …
Short answer
These attacks measure the server's response time in nanoseconds to infer how many characters matched before failure; timingSafeEqual prevents this by comparing bytes in constant time.
Explanation
Ordinary comparisons such as === compare strings character by character and stop at the first difference (Short-circuiting). This lets an attacker make many attempts and accurately guess encryption keys and Webhook signatures. crypto.timingSafeEqual compares two Buffer objects in constant time regardless of where the first match or difference occurs.
Code example
import crypto from "node:crypto";
const isMatch = crypto.timingSafeEqual(
Buffer.from(signatureA, "utf8"),
Buffer.from(signatureB, "utf8")
);Common mistakes
- Comparing Webhook signatures (such as Stripe or GitHub) with the ordinary === equality operator.