Skip to content

Commit 334c7c8

Browse files
fix(security): suppress the CodeQL password-hash alert on sha256Hex (#5982)
CodeQL fails #5963 (the v0.7.46 release PR) with 2 high-severity js/insufficient-password-hash alerts on packages/security/src/hash.ts. Both are false positives that surfaced now because d362e68 added sha256Base64Url to this file — touching it made every pre-existing taint path ending here count as 'new code changed by this pull request'. The flagged lines and all their callers are unchanged. Neither taint source is a human password: - hashApiKey passes an API key built from generateSecureToken(24) — 192 bits of randomBytes. A fast digest is the right construction for indexed lookup; a slow KDF would run on every authenticated request and buy nothing against an unsearchable keyspace. - passwordSlot passes an already-encrypted value, hashed to an 8-char discriminator that invalidates deployment auth tokens when the password changes — not credential storage. User credentials never reach this helper; Better Auth owns them and applies its own KDF. Suppress with the inline lgtm comments this repo already uses for the same class of judgement (see uuidV5 in ee/workspace-forking/lib/remap/block-identity.ts), and document the invariant on sha256Hex so a future caller knows a genuine password belongs in Better Auth, not here. Only the two flagged lines are suppressed — sha256Base64Url is left unannotated so a real finding there would still surface.
1 parent 9c10943 commit 334c7c8

1 file changed

Lines changed: 17 additions & 2 deletions

File tree

packages/security/src/hash.ts

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,28 @@ import { createHash } from 'node:crypto'
55
* content passes as a Uint8Array/Buffer. Use for indexed lookup of sensitive
66
* values (e.g. API key hash columns) and content-integrity receipts where the
77
* caller only needs to verify equality without ever reversing the hash.
8+
*
9+
* NOT for human-chosen passwords — those are low-entropy and need a slow KDF.
10+
* User credentials never reach this helper: Better Auth owns them and applies
11+
* its own KDF. What does reach it is high-entropy material where a fast digest
12+
* is the correct construction:
13+
*
14+
* - API keys (`sim_`/`sk-sim-` + `generateSecureToken(24)`, 192 random bits).
15+
* A slow KDF here would buy nothing against a keyspace that cannot be
16+
* searched, while forcing every authenticated request through it.
17+
* - Already-encrypted values, content digests, and cache/idempotency keys.
18+
*
19+
* CodeQL's `js/insufficient-password-hash` flags the sink because callers pass
20+
* arguments it name-matches as passwords (`apiKey`, `encryptedPassword`). The
21+
* suppressions below record that judgement; keep the invariant above true, and
22+
* route any genuine password through Better Auth rather than this function.
823
*/
924
export function sha256Hex(input: string | Uint8Array): string {
1025
const hash = createHash('sha256')
1126
if (typeof input === 'string') {
12-
hash.update(input, 'utf8')
27+
hash.update(input, 'utf8') // lgtm[js/insufficient-password-hash]
1328
} else {
14-
hash.update(input)
29+
hash.update(input) // lgtm[js/insufficient-password-hash]
1530
}
1631
return hash.digest('hex')
1732
}

0 commit comments

Comments
 (0)