fix: use opaque state parameter in OAuth authorization flow - #41
Merged
Conversation
mattzcarey
force-pushed
the
fix/oauth-state-parameter
branch
from
June 9, 2026 16:29
1b8b85f to
627140b
Compare
Replace base64-encoded AuthRequest in the OAuth state parameter with an opaque UUID token. The full AuthRequest is already stored in KV via createOAuthState() — embedding it in the URL was redundant and caused authorization URLs to exceed Cloudflare's size limits when combined with CIMD client IDs or many scopes. Security is unchanged: state is still validated via KV lookup + SHA-256 session cookie binding + single-use deletion.
Update OAuth tests for the opaque-state-token contract: - cloudflare-auth.test.ts: getAuthorizationURL now takes stateToken and passes it through verbatim (no base64 AuthRequest). - oauth-routes.test.ts full-flow: assert the state forwarded to Cloudflare is opaque, not a base64-encoded AuthRequest. - oauth-routes.test.ts reject case: use a plain opaque token now that state is the KV lookup key directly.
mattzcarey
force-pushed
the
fix/oauth-state-parameter
branch
from
June 16, 2026 19:55
627140b to
2484de5
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
AuthRequestin the upstream Cloudflare OAuthstateparameter with an opaque UUID token.Why
The complete downstream authorization request is already stored by
createOAuthState()under a random UUID with a 10-minute TTL. Sending a second copy through the upstream Cloudflare authorization URL is redundant. The callback currently decodes that copy only to recover the UUID and load the authoritative KV record.Using the UUID directly keeps the upstream URL bounded regardless of downstream client metadata or client
state, avoids unnecessarily forwarding downstream client details to the upstream authorization server, and follows OAuth's definition ofstateas an opaque value.The original PR was motivated by a 5,120-byte authorization endpoint limit observed at the time. That exact threshold is no longer reproducible, but the structural issue remains: client-controlled fields are duplicated and base64-amplified in the URL. Current examples with 78 scopes are roughly:
The previous Latin-1-only consent state encoding also threw on valid Unicode client state. The internal consent transport now encodes JSON as UTF-8 before base64 encoding.
Security properties
The change preserves and tests the existing protections:
crypto.randomUUID().AuthRequestand PKCE verifier remain in KV.HttpOnly; Secure; SameSite=Laxcookie and verified on callback.invalid_requestbefore KV access.AuthRequestand is returned bycompleteAuthorization().Test coverage
AuthRequest.Maintenance
Rebased onto current
mainbefore updating the implementation and tests.