Skip to content

Return the OAuth state parameter to the client unchanged - #88

Merged
joehoyle merged 1 commit into
WP-API:mainfrom
humanmade:roborourke/preserve-oauth-state
Sep 23, 2026
Merged

joehoyle merged 1 commit into
WP-API:mainfrom
humanmade:roborourke/preserve-oauth-state

Conversation

@roborourke

Copy link
Copy Markdown
Collaborator

The authorize flow now sends state back to the client exactly as it arrived, as RFC 6749 section 4.1.2 requires.

Before this change, state went through sanitize_text_field(). That strips percent-encoded octets, tags and extra spaces, so a state like %41 came back empty. The client then sees a mismatch and rejects the response. state is 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 send state back. That dropped a valid state of "0", so they now use isset().

The implicit grant did not encode its URL fragment, so a state with & 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

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>

Copilot AI 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.

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.

@roborourke
roborourke requested a review from joehoyle September 23, 2026 12:25

@joehoyle joehoyle left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks!

@joehoyle
joehoyle merged commit 7c42c39 into WP-API:main Sep 23, 2026
44 checks passed
@joehoyle
joehoyle deleted the roborourke/preserve-oauth-state branch September 23, 2026 12:52
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.

3 participants