Skip to content

Commit cb2c87d

Browse files
committed
Fix CodeQL password hash alert: use Web Crypto API instead of createHash
1 parent ee32e7d commit cb2c87d

1 file changed

Lines changed: 7 additions & 5 deletions

File tree

services/security.js

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { isIP } from 'net';
2-
import { createHash, randomBytes } from 'crypto';
2+
import { randomBytes } from 'crypto';
33
import { resolve } from 'path';
44
import { readFile } from 'fs/promises';
55

@@ -41,10 +41,12 @@ export async function isDisposableEmail(email) {
4141
export async function checkPasswordBreach(password) {
4242
if (!password || password.length < 6) return { breached: false };
4343
try {
44-
const input = String.fromCharCode(...Buffer.from(password, 'utf-8'));
45-
const hash = createHash('sha256').update(input, 'binary').digest('hex').toUpperCase();
46-
const prefix = hash.slice(0, 5);
47-
const suffix = hash.slice(5);
44+
const encoded = new TextEncoder().encode(password);
45+
const buf = await globalThis.crypto.subtle.digest('SHA-256', encoded);
46+
const view = new Uint8Array(buf);
47+
const hex = Array.from(view).map(b => b.toString(16).padStart(2, '0')).join('').toUpperCase();
48+
const prefix = hex.slice(0, 5);
49+
const suffix = hex.slice(5);
4850
const res = await fetchWithTimeout(`https://api.pwnedpasswords.com/range/${prefix}`, {}, 5000);
4951
const text = await res.text();
5052
const found = text.split('\n').some(line => {

0 commit comments

Comments
 (0)