mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-10 23:34:34 +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(
|
export function safeEqualSecret(
|
||||||
provided: string | undefined | null,
|
provided: string | undefined | null,
|
||||||
@@ -7,10 +7,6 @@ export function safeEqualSecret(
|
|||||||
if (typeof provided !== "string" || typeof expected !== "string") {
|
if (typeof provided !== "string" || typeof expected !== "string") {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
const providedBuffer = Buffer.from(provided);
|
const hash = (s: string) => createHash("sha256").update(s).digest();
|
||||||
const expectedBuffer = Buffer.from(expected);
|
return timingSafeEqual(hash(provided), hash(expected));
|
||||||
if (providedBuffer.length !== expectedBuffer.length) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
return timingSafeEqual(providedBuffer, expectedBuffer);
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user