Skip to content

fix: generatePKCEPair - validate crypto.subtle before use and fix code_challenge_method on fallback #206

@coderabbitai

Description

@coderabbitai

Summary

The generatePKCEPair function in lib/utils/generateAuthUrl.ts has two related issues identified during the code review of PR #192:

1. Insufficient crypto.subtle guard

The current production guard only checks if (!crypto), but the else branch directly calls crypto.subtle.digest() without verifying that crypto.subtle itself exists. If crypto is defined but crypto.subtle is undefined (e.g. in certain non-browser environments or older runtimes), this will throw a runtime error.

Fix: Update the guard to fully validate Web Crypto availability before use:

typeof crypto !== 'undefined' && crypto && crypto.subtle && typeof crypto.subtle.digest === 'function'

2. PKCE protocol violation on fallback path

When the no-crypto fallback executes, codeChallenge is produced via base64UrlEncode(codeVerifier) (a plain encoding), but the auth flow continues to send code_challenge_method=S256. This is a violation of the PKCE specification (RFC 7636) — S256 should only be sent when the challenge is the SHA-256 hash of the verifier.

Fix: Return the appropriate code_challenge_method value from generatePKCEPair ("S256" for the hash path, "plain" for the fallback), and ensure the auth URL construction uses the returned value.

References

Requested by

@pesickaa

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions