Skip to content

Fix: Send the PKCE code verifier instead of the challenge in the OAuth2 token request - #167

Open
ready4god2513 wants to merge 3 commits into
mainfrom
fix/oauth2-pkce-verifier
Open

ready4god2513 wants to merge 3 commits into
mainfrom
fix/oauth2-pkce-verifier

Conversation

@ready4god2513

Copy link
Copy Markdown
Contributor

What

session.OAuth2 sent the same value as both the code_challenge in the authorization request and the code_verifier in the token request. It hashed random bytes into an S256 challenge, used that challenge for both, and discarded the random input. The secret verifier that PKCE (RFC 7636) depends on was never sent. Anyone who saw the authorization request had everything needed to redeem the code, and a server that checks PKCE correctly rejects the token request.

Change

  • generatePKCE returns a verifier and its challenge:
    • the verifier is 32 random bytes, base64url-encoded without padding (43 characters, within the 43–128 range in RFC 7636 §4.1)
    • the challenge is BASE64URL(SHA256(verifier)), per §4.2
  • The challenge goes in the authorization request (with code_challenge_method=S256, as before), and the verifier in the token request.
  • No public API changes.

Tests

New session/oauth2_test.go:

  • TestGeneratePKCE: the verifier matches the RFC 7636 character set and length; the challenge is the S256 transform of the verifier; the verifier differs from the challenge and between calls.
  • TestRetrieveTokenSendsVerifier: the token request posts the verifier as code_verifier, checked against a local test server.

go test ./..., go vet ./... and golangci-lint run ./... pass.

🤖 Generated with Claude Code

…h2 token request

The OAuth2 session sent the S256 code challenge as both the
code_challenge in the authorization request and the code_verifier in
the token request, so the secret verifier RFC 7636 relies on was never
used. Generate a random verifier, derive the challenge from it as
BASE64URL(SHA256(verifier)), and send the verifier with the token
request.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@ready4god2513
ready4god2513 requested a review from a team as a code owner November 3, 2026 21:18

@rafaeljusto rafaeljusto left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tested this change with our MCP and the DCR flow looks good 👍

Image

@bakaat bakaat left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

rollins wanna run claude through it ? lgtm anyway

@chrisrollins65

Copy link
Copy Markdown
Contributor

rollins wanna run claude through it ? lgtm anyway

Yeah I always run claude on PRs before I approve them now, so already done.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants