mirror of
https://github.com/moltbot/moltbot.git
synced 2026-03-21 16:41:56 +00:00
fix(security): SHA-256 hash before timingSafeEqual to prevent length leak (#20856)
The previous implementation returned early when buffer lengths differed, leaking the expected secret's length via timing side-channel. Hashing both inputs with SHA-256 before comparison ensures fixed-length buffers and constant-time comparison regardless of input lengths.
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
import { timingSafeEqual } from "node:crypto";
|
||||
import { createHash, timingSafeEqual } from "node:crypto";
|
||||
|
||||
export function safeEqualSecret(
|
||||
provided: string | undefined | null,
|
||||
@@ -7,10 +7,6 @@ export function safeEqualSecret(
|
||||
if (typeof provided !== "string" || typeof expected !== "string") {
|
||||
return false;
|
||||
}
|
||||
const providedBuffer = Buffer.from(provided);
|
||||
const expectedBuffer = Buffer.from(expected);
|
||||
if (providedBuffer.length !== expectedBuffer.length) {
|
||||
return false;
|
||||
}
|
||||
return timingSafeEqual(providedBuffer, expectedBuffer);
|
||||
const hash = (s: string) => createHash("sha256").update(s).digest();
|
||||
return timingSafeEqual(hash(provided), hash(expected));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user