mirror of
https://github.com/Shik3i/KoalaSync.git
synced 2026-08-06 01:17:42 +00:00
fix(security): prevent admin token length leak via timing side-channel
isAdminMetricsAuthorized() returned early when buffer lengths differed, allowing an attacker to discover the token length by measuring response time. Now always calls crypto.timingSafeEqual (using a zeroed dummy buffer on length mismatch) so every auth attempt takes constant time regardless of whether the length guess was correct. Reported-by: Kaia-Alenia
This commit is contained in:
+7
-2
@@ -33,8 +33,13 @@ export function isAdminMetricsAuthorized(authHeader, adminToken) {
|
||||
const expectedBuffer = Buffer.from(adminToken);
|
||||
const providedBuffer = Buffer.from(provided);
|
||||
|
||||
if (expectedBuffer.length !== providedBuffer.length) return false;
|
||||
return crypto.timingSafeEqual(expectedBuffer, providedBuffer);
|
||||
// Always run timingSafeEqual to prevent length-based timing leaks.
|
||||
// timingSafeEqual throws on different-length buffers, so when lengths
|
||||
// differ we compare against a zeroed buffer of the provided length
|
||||
// (guaranteed mismatch, constant time).
|
||||
const sameLength = expectedBuffer.length === providedBuffer.length;
|
||||
const compareBuf = sameLength ? expectedBuffer : Buffer.alloc(providedBuffer.length);
|
||||
return sameLength && crypto.timingSafeEqual(compareBuf, providedBuffer);
|
||||
}
|
||||
|
||||
export function isAdminMetricsTokenStrong(adminToken, minLength = 32) {
|
||||
|
||||
Reference in New Issue
Block a user