From 628a5270e8e5774511221372989468e76e2e934b Mon Sep 17 00:00:00 2001 From: TheRealToxicDev Date: Tue, 27 Oct 2026 02:30:25 -0600 Subject: [PATCH] fix(codeql): use of password hash with insufficient computational effort --- apps/dashboard/lib/files/password-guard.ts | 27 ++++++++++++++++++---- 1 file changed, 22 insertions(+), 5 deletions(-) diff --git a/apps/dashboard/lib/files/password-guard.ts b/apps/dashboard/lib/files/password-guard.ts index b8460cf..313e29d 100644 --- a/apps/dashboard/lib/files/password-guard.ts +++ b/apps/dashboard/lib/files/password-guard.ts @@ -1,5 +1,13 @@ import { compare } from 'bcryptjs' -import { createHash } from 'crypto' +import { randomBytes, scrypt } from 'crypto' +import { promisify } from 'util' + +const scryptAsync = promisify(scrypt) as ( + password: string, + salt: Buffer, + keylen: number, + options: { N: number; r: number; p: number } +) => Promise const WINDOW_MS = 15 * 60 * 1000 const PER_CLIENT_LIMIT = 10 @@ -18,8 +26,17 @@ interface Attempts { const attempts = new Map() const accepted = new Map() -const digest = (...parts: string[]) => - createHash('sha256').update(parts.join('\0')).digest('hex') +// Remembered passwords are keyed by a cheap scrypt with a per-process secret, never a fast hash +const SECRET = randomBytes(32) +async function rememberKeyFor( + storedHash: string, + client: string, + password: string +) { + const salt = Buffer.concat([SECRET, Buffer.from(`${storedHash}\0${client}`)]) + const key = await scryptAsync(password, salt, 32, { N: 1024, r: 8, p: 1 }) + return key.toString('hex') +} function sweep(now: number) { if (attempts.size + accepted.size < 10_000) return @@ -76,10 +93,10 @@ export async function checkFilePassword( const now = Date.now() sweep(now) - const rememberKey = digest(storedHash, client, password) + const rememberKey = await rememberKeyFor(storedHash, client, password) if ((accepted.get(rememberKey) ?? 0) > now) return { ok: true } - const file = digest(storedHash) + const file = storedHash const perClient = bucket(`${file}:${client}`, now) const perFile = bucket(file, now)