Files
openclaw/src/security/secret-equal.ts
David Rudduck f1e1ad73ad 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.
2026-02-19 03:16:35 -08:00

13 lines
407 B
TypeScript

import { createHash, timingSafeEqual } from "node:crypto";
export function safeEqualSecret(
provided: string | undefined | null,
expected: string | undefined | null,
): boolean {
if (typeof provided !== "string" || typeof expected !== "string") {
return false;
}
const hash = (s: string) => createHash("sha256").update(s).digest();
return timingSafeEqual(hash(provided), hash(expected));
}