// challenge_JW
The Security Hole in String Comparison
This auth check leaks one byte at a time. Enough requests, your signing key is gone.
hardSecurityNode.js⏱ 10-15 min
Attempts
654
Solved
18%
Avg time
7min
Published
2024-02-15
// the code
Here's what passed code review.
A standard pattern. Nothing in this code is obviously wrong. It works perfectly in dev. It works perfectly in staging. It even works in production — for a while.
auth.jsjavascript
const crypto = require('crypto');
function verifySignature(token, secret) {
const [header, payload, signature] = token.split('.');
// Recreate the signature
const data = `${header}.${payload}`;
const expectedSignature = crypto
.createHmac('sha256', secret)
.update(data)
.digest('base64url');
// Compare signatures
if (signature === expectedSignature) {
return true;
}
return false;
}
// This JWT verification has a security vulnerability.
// An attacker can forge valid tokens, given time and patience.
// What's the issue?// stuck? progressive hints
Reveal hints one at a time.
Each hint nudges you closer without giving it away. Use as many as you need.
hint_01Start here →+
The vulnerability is in how the comparison is performed.
hint_02Need another nudge?+
Think about timing differences.
hint_03Need another nudge?+
String comparison in JavaScript is not constant-time.
// submit your guess
Want more code challenges? Subscribe.
New challenge every two weeks. Production bugs that passed review. ~5 min reads.