Files
Tom Moor 3fafd2a7ea fix: Keep existing CSRF token across concurrent requests (#14411)
* fix: Keep existing CSRF token across concurrent requests

* fix: Compare byte length in safeEqual so malformed input cannot throw
2026-10-29 18:47:54 -04:00

33 lines
732 B
TypeScript

import crypto from "node:crypto";
/**
* Compare two strings in constant time to prevent timing attacks.
*
* @param a The first string to compare
* @param b The second string to compare
* @returns Whether the strings are equal
*/
export function safeEqual(a?: string | null, b?: string | null) {
if (!a || !b) {
return false;
}
const bufA = Buffer.from(a);
const bufB = Buffer.from(b);
if (bufA.length !== bufB.length) {
return false;
}
return crypto.timingSafeEqual(bufA, bufB);
}
/**
* Hash a string using SHA-256.
*
* @param input The input string to hash
* @returns The hashed input
*/
export function hash(input: string) {
return crypto.createHash("sha256").update(input).digest("hex");
}