PKCE S256 Enforcement
Mandatory Proof Key for Code Exchange using SHA-256 challenge verification and constant-time comparison on every OAuth 2.1 code exchange.
Enforcement Policy
In accordance with OAuth 2.1 and RFC 7636, Proof Key for Code Exchange (PKCE) with S256 is mandatory for every authorization code request. Requests that omit code_challenge, omit code_challenge_method, or specify code_challenge_method=plain are rejected immediately with invalid_request.
- Required Method
- S256 only (plain is prohibited)
- Verifier Length
- 43 to 128 characters
- Allowed Charset
- RFC 3986 unreserved: A-Z, a-z, 0-9, -, ., _, ~
- Challenge Formula
- BASE64URL-NO-PAD(SHA256(ASCII(code_verifier)))
- Comparison Primitive
- Node crypto.timingSafeEqual on equal-length UTF-8 buffers
Generating a Verifier and S256 Challenge
Generate at least 32 bytes (256 bits) of cryptographically secure randomness per authorization transaction, encode as base64url without padding, and derive the S256 challenge before opening /oauth/authorize.
import crypto from 'node:crypto';
export function createPkcePair(): { codeVerifier: string; codeChallenge: string } {
const codeVerifier = crypto.randomBytes(32).toString('base64url');
const codeChallenge = crypto
.createHash('sha256')
.update(codeVerifier, 'ascii')
.digest('base64url');
return { codeVerifier, codeChallenge };
}
const { codeVerifier, codeChallenge } = createPkcePair();
console.log({ codeVerifier, codeChallenge });CODE_VERIFIER=$(openssl rand -base64 32 | tr '+/' '-_' | tr -d '=')
CODE_CHALLENGE=$(printf '%s' "$CODE_VERIFIER" | openssl dgst -sha256 -binary | openssl base64 -A | tr '+/' '-_' | tr -d '=')
echo "verifier=$CODE_VERIFIER"
echo "challenge=$CODE_CHALLENGE"Server-Side Verification & Failure Handling
When POST /oauth/token receives grant_type=authorization_code, Taicket validates code_verifier syntax, recomputes BASE64URL(SHA256(code_verifier)), and compares it against the codeChallenge stored on the McpOAuthCode document using crypto.timingSafeEqual.
Because authorization codes are consumed atomically on lookup, a failed PKCE verification invalidates the code immediately. An attacker who intercepts a redirect URL cannot retry brute-force verifiers against the same code.
{
"error": "invalid_grant",
"error_description": "PKCE code_verifier verification failed."
}