Return the OAuth state parameter to the client unchanged - #88
Merged
Merged
Conversation
RFC 6749 section 4.1.2 requires the authorization response to carry the exact state value the client sent, so the client can match the response to its request and reject a forged one. Three things broke that: - state went through sanitize_text_field(), which strips percent-encoded octets, tags and surrounding whitespace. A state of "%41" came back empty, so the client saw a CSRF mismatch. State is opaque and is only ever written into the redirect URL, where it is encoded, so it is now kept as the raw unslashed string. A non-string (state[]=x) is treated as absent. - Both grants used empty() to decide whether to echo state, which drops a valid state of "0". They now use isset(). - The implicit grant built its fragment with build_query(), which does not encode. A state containing & or # corrupted the fragment. Values are now encoded first, as the authorization code grant already does through add_query_arg(). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Contributor
There was a problem hiding this comment.
Copilot review overview
🟢 Approval recommended
All reviewed changes are covered by tests and no unresolved blocking issues were identified.
Review effort: Lite
Findings: None
What changed in this PR
Updates OAuth authorization responses to preserve state values unchanged and safely encode implicit-grant fragments.
Changes:
- Preserve string state values, including
"0", while ignoring arrays. - Encode implicit-grant response parameters.
- Add round-trip tests for both grant types.
| File | Description |
|---|---|
tests/test-state.php |
Tests state preservation and edge cases. |
inc/types/class-implicit.php |
Preserves and encodes implicit response parameters. |
inc/types/class-base.php |
Reads raw state values without sanitization. |
inc/types/class-authorization-code.php |
Preserves present state values in redirects. |
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
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.
The authorize flow now sends
stateback to the client exactly as it arrived, as RFC 6749 section 4.1.2 requires.Before this change,
statewent throughsanitize_text_field(). That strips percent-encoded octets, tags and extra spaces, so a state like%41came back empty. The client then sees a mismatch and rejects the response.stateis only ever written into the redirect URL, where it is encoded, so it is now kept as the raw string. An array value is treated as missing.Both grants also used
empty()to decide whether to sendstateback. That dropped a valid state of"0", so they now useisset().The implicit grant did not encode its URL fragment, so a
statewith&or#broke the response. It now encodes each value first.Copilot flagged this on #85. It predates that PR, so the fix is here.
🤖 Generated with Claude Code