mirror of
https://github.com/Shik3i/KoalaSync.git
synced 2026-08-31 21:21:41 +00:00
fix(security): actually prevent timing leak — eagerly evaluate timingSafeEqual
The previous fix had a subtle bug: JavaScript && short-circuits, so crypto.timingSafeEqual() was skipped when buffer lengths differed (sameLength was false). The dummy buffer was allocated but never compared, leaving the original length-based timing leak intact. Now timingSafeEqual is eagerly assigned to a const before the && guard, guaranteeing it runs in constant time on every auth attempt regardless of whether the length guess was correct.
This commit is contained in:
+5
-1
@@ -37,9 +37,13 @@ export function isAdminMetricsAuthorized(authHeader, adminToken) {
|
|||||||
// timingSafeEqual throws on different-length buffers, so when lengths
|
// timingSafeEqual throws on different-length buffers, so when lengths
|
||||||
// differ we compare against a zeroed buffer of the provided length
|
// differ we compare against a zeroed buffer of the provided length
|
||||||
// (guaranteed mismatch, constant time).
|
// (guaranteed mismatch, constant time).
|
||||||
|
// NOTE: timingSafeEqual must be evaluated eagerly (assigned to const)
|
||||||
|
// before the && short-circuit, otherwise it's skipped on length mismatch
|
||||||
|
// and the timing leak remains.
|
||||||
const sameLength = expectedBuffer.length === providedBuffer.length;
|
const sameLength = expectedBuffer.length === providedBuffer.length;
|
||||||
const compareBuf = sameLength ? expectedBuffer : Buffer.alloc(providedBuffer.length);
|
const compareBuf = sameLength ? expectedBuffer : Buffer.alloc(providedBuffer.length);
|
||||||
return sameLength && crypto.timingSafeEqual(compareBuf, providedBuffer);
|
const equal = crypto.timingSafeEqual(compareBuf, providedBuffer);
|
||||||
|
return sameLength && equal;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function isAdminMetricsTokenStrong(adminToken, minLength = 32) {
|
export function isAdminMetricsTokenStrong(adminToken, minLength = 32) {
|
||||||
|
|||||||
Reference in New Issue
Block a user