if (!token || typeof token !== 'string') returnfalse;
6
+
if (token.length !== expected.length) returnfalse;
L6 · new
warningexpected may be undefined
If process.env.SESSION_SECRET isn't set at boot, line 6 throws TypeError: Cannot read properties of undefined before the length check fires. Validate on startup or guard here with expected != null.
6
−
return token === expected;
L6 · removed
criticalFixes a timing attack — flag this for security review
The removed line compared secrets with ===, which short-circuits on the first byte and leaks token length and prefix via response timing. The new implementation uses crypto.timingSafeEqual on equal-length buffers. Confirm the length-guard above runs first so timingSafeEqual never throws on mismatched lengths.
7
+
return crypto.timingSafeEqual(
8
+
Buffer.from(token),
9
+
Buffer.from(expected)
10
+
);
L8–9 · new
nitMake the Buffer encoding explicit
Buffer.from(token) defaults to UTF-8; for opaque session tokens that's fine, but if these are ever hex- or base64-encoded the silent default will hash-mismatch. Spell it: Buffer.from(token, 'utf8').