[Bug]: PKCE code exchange uses non-constant-time comparison for the code_challenge hash
What's the bug?
What's the bug?
OAuthPKCEService.exchangeCode() validates the PKCE code_verifier against the
stored code_challenge using a plain string inequality check:
backend/src/services/auth/oauth-pkce.service.ts:
const computedChallenge = crypto.createHash('sha256').update(codeVerifier).digest('base64url'); if (computedChallenge !== data.codeChallenge) { logger.warn('PKCE validation failed', { provider: data.provider }); throw new AppError('PKCE verification failed', 400, ERROR_CODES.AUTH_UNAUTHORIZED); }
!== on strings short-circuits at the first mismatched character, so comparison
time correlates with how many leading characters match — the textbook definition
of a timing side-channel on a security-sensitive comparison.
Why it's inconsistent, not just theoretical
Every other signature/hash comparison in this codebase already does this correctly:
- backend/src/api/routes/webhooks/vercel.routes.ts — verifies the x-vercel-signature header with crypto.timingSafeEqual() after an explicit length check.
- backend/src/providers/payments/razorpay.provider.ts (verifyWebhookSignature) — same pattern: length check + crypto.timingSafeEqual().
- Stripe webhook verification delegates to the Stripe SDK's own constant-time comparison.
The PKCE exchange is the one place in the codebase handling a security-sensitive hash comparison that doesn't follow this established convention.
Proof
$ grep -n "codeChallenge" backend/src/services/auth/oauth-pkce.service.ts ... if (computedChallenge !== data.codeChallenge) { ...
Confirmed against current master. No existing issue or PR addresses this (searched for "PKCE", "timingSafeEqual", "constant-time" across issues/PRs).
Severity note
Flagging this honestly as a defensive-hardening / consistency issue rather than a critical vulnerability. Practical exploitability is low: the compared value is a fixed-length SHA-256 digest (not a variable-length secret), and PKCE codes are single-use and expire after 5 minutes, both of which shrink the real-world attack window significantly. Still worth fixing since the codebase clearly already knows the correct pattern and applies it everywhere else.
Desired Behavior
Use crypto.timingSafeEqual() for this comparison, matching the pattern already used in vercel.routes.ts and razorpay.provider.ts:
const computedBuf = Buffer.from(computedChallenge); const storedBuf = Buffer.from(data.codeChallenge); const isValid = computedBuf.length === storedBuf.length && crypto.timingSafeEqual(computedBuf, storedBuf);
if (!isValid) { logger.warn('PKCE validation failed', { provider: data.provider }); throw new AppError('PKCE verification failed', 400, ERROR_CODES.AUTH_UNAUTHORIZED); }
Acceptance Criteria
- computedChallenge is compared against data.codeChallenge using crypto.timingSafeEqual(), not string inequality.
- Length mismatch is checked explicitly before calling timingSafeEqual() (it throws on unequal-length buffers rather than returning false).
- No behavior change for valid/invalid PKCE exchanges — same accept/reject outcomes, only the comparison method changes.
- Existing PKCE exchange tests continue to pass; a new test case confirms a near-miss codeChallenge (same length, differs by one character) is still correctly rejected.
I'd like to work on this and will follow up with a PR.
How to reproduce
No response
Environment (optional)
No response
Source: InsForge/InsForge