Skip to content

feat(oauth2): add PKCE support to the authorization code flow - #67563

Open
zyx1121 wants to merge 2 commits into
nextcloud:masterfrom
zyx1121:feat/oauth2-pkce
Open

zyx1121 wants to merge 2 commits into
nextcloud:masterfrom
zyx1121:feat/oauth2-pkce

Conversation

@zyx1121

@zyx1121 zyx1121 commented Dec 3, 2026 •

Copy link
Copy Markdown

Summary

This adds PKCE (RFC 7636) to the OAuth2 authorization code flow. It continues #59930 by @julien-nc on current master.

@pringelmann, you offered in September to take #59930 over. If you already have your own version, please let me know and I will close this one.

Why: OAuth 2.1 and the MCP authorization spec require PKCE. Clients such as the Claude and ChatGPT connectors always send a code challenge, and Nextcloud ignores it today.

What changes for clients:

  • authorize accepts code_challenge with code_challenge_method=S256. The plain method, or a challenge without a method, gets an invalid_request error redirect.
  • The token endpoint requires the matching code_verifier for a code that was requested with a challenge.
  • Clients that send neither parameter work as before.

Commit 1 is the port of #59930. master moved the OAuth2 entities to ORM attributes, so I resolved the conflicts by hand and squashed the original commits. The migration is now Version030000Date20261202224022, so it sorts after Version011901..., and the app version goes to 3.0.0-dev.1.

Commit 2 is my addition and not part of #59930. Drop it or ask me to split it if you prefer the port alone:

  • The token endpoint rejects a code_verifier for a code that has no challenge (RFC 9700 section 2.1.1).
  • A new authorization request invalidates a consent page that is still open in the same session. Without this, a second request without PKCE replaced the challenge in the session, and approving the first page issued a code with no challenge.
  • The verifier is checked after the client is authenticated, so a caller without the client secret learns nothing about the challenge.
  • A challenge must be exactly 43 base64url characters, the patterns no longer match a trailing newline, and error redirects use a literal &.

Behaviour you should know about:

  • From Add support for PKCE in oauth2 #59930: for a registered redirect URI with a fragment, state and code now go into the query, before the fragment. Before, they were appended after the fragment. This also applies to clients that do not use PKCE.
  • A PKCE client that got its code before the upgrade and redeems it after the upgrade is rejected once, because the code has no stored challenge. The window is the 10 minute lifetime of a code.
  • A wrong verifier does not consume the code. The request is throttled like the other token errors.
  • Known limit: the challenge is stored in the session, next to oauth.state. A second authorization request that arrives between the authorize response and the redirect to the login flow page can still replace it. Closing this needs a per-request value that travels through the login flow pages. I can do that here or in a follow-up, whichever you prefer.
  • Overlap: fix(oauth2): clarify client configuration and validate deletion #67400 also edits LoginRedirectorController.php (one message) and info.xml (the description text). Whichever lands second needs a small rebase. I will do it if it is this one.

How I tested:

  • Unit tests: apps/oauth2/tests (70 tests) and tests/Core/Controller/ClientFlowLoginControllerTest.php (19 tests) pass.
  • Static checks: lint, cs-fixer, rector and rector:strict report nothing on the changed files. psalm strict, ocp, ncu and the taint analysis report no errors. The default psalm run reports 19 errors on my machine, none in the changed files. All 19 are about the imagick and ftp extensions, which my image lacks.
  • Live on a dev instance (PHP 8.4, sqlite) with a real browser login: S256 with the right verifier returns tokens. A wrong verifier returns invalid_grant, and the same code still works afterwards with the right verifier. A missing verifier returns invalid_request. plain, a missing method and a challenge of 44 characters are rejected at authorize. A verifier without a challenge returns invalid_grant. A flow without PKCE still works. A wrong client secret returns invalid_client before any PKCE answer.
  • Live session test: with consent page A open, a second authorize request without PKCE in the same session makes page A fail with "State token does not match".
  • Upgrade: a token issued on master still works and refreshes after occ upgrade to this branch.
  • Not tested: MySQL, PostgreSQL and Oracle. The migration ran on sqlite only.

TODO

Checklist

AI (if applicable)

  • The content of this PR was partly or fully generated using AI

The port, the tests and this description were written with Claude Code (claude-fable-5-2). The agent also ran the checks and the live test matrix listed above. I read the diff, walked one PKCE login in my own browser against the dev instance (a wrong verifier was refused, the right one got a working token) and signed off the commits myself.

Assisted by ClaudeCode:claude-fable-5-2, reviewed by me before sending.

🤖 Generated with Claude Code

@zyx1121
zyx1121 requested review from Altahrim and removed request for a team December 3, 2026 06:49
zyx1121 and others added 2 commits December 4, 2026 01:45
OAuth 2.1 and the MCP authorization spec require clients to send a PKCE
code challenge and expect the server to verify it. Nextcloud ignored
these parameters, so an intercepted authorization code could still be
exchanged without the code verifier, and clients that check the server
for PKCE support refused to connect.

The authorization endpoint now accepts a code challenge with the S256
method and the token endpoint verifies the code verifier against it.
The plain method is rejected because it gives no protection against an
attacker who can read the authorization request. Clients that send no
challenge keep working as before.

Error redirects now keep the query string and fragment of the redirect
URI and honour the legacy localhost redirect of ownCloud clients, so
the new PKCE errors reach the client on the URL it registered.

This continues nextcloud#59930 by Julien Veyssier on top of current master,
where the OAuth2 entities moved to ORM attributes.

Co-authored-by: Julien Veyssier <julien-nc@posteo.net>
Assisted-by: ClaudeCode:claude-fable-5-2
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Signed-off-by: Yong Xiang Zhan <yongxiang.zhan@outlook.com>
PKCE only helps if a code can never lose the challenge its client sent.
Three gaps remained after the port:

- The token endpoint accepted a code verifier for a code that has no
  challenge. RFC 9700 section 2.1.1 forbids that, because it hides a
  challenge that was stripped from the authorization request.
- The challenge lives in the session, so a second authorization request
  in the same session replaced it while the consent page of the first
  request could still be approved. A new request now invalidates the
  consent page that is still open.
- The verifier was checked before the client was authenticated, so
  anybody holding a code could find out whether it is bound to a
  challenge and test verifiers without the client secret.

A challenge must now be exactly 43 base64url characters, the only shape
an S256 challenge can have, and the patterns no longer match a trailing
newline. Error redirects use a literal "&" so the PHP setting
arg_separator.output cannot change them.

Assisted-by: ClaudeCode:claude-fable-5-2
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Signed-off-by: Yong Xiang Zhan <yongxiang.zhan@outlook.com>
@zyx1121

zyx1121 commented Dec 4, 2026

Copy link
Copy Markdown
Author

I rebased this onto current master (576765a). Two commits on master touched the same code:

  • 46f5d81 stores oauth.state as an array that is bound to its client. authorize now writes that array next to the two PKCE values, and the tests expect the new shape.
  • f0a549d removed the throttler from the token endpoint. The PKCE test for a matching verifier no longer expects resetDelay().

Nothing else changed, and git range-diff shows only these adaptations. I reran the unit tests, the static checks, the live matrix and the session test on the new head, and updated the description: the test count, and the follow-up for #67152, which is now #67665. I did not repeat the upgrade test, because the migration file is unchanged.

I did not extend the client binding from 46f5d81 to the PKCE challenge. That is new behaviour and does not belong in a rebase. The "Known limit" paragraph in the description still applies.

I read the range-diff, repeated the PKCE login in my own browser on the new head and signed off both commits again.

Assisted by ClaudeCode:claude-fable-5-2, reviewed by me before sending.

This branch has not been deployed

No deployments
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.

Implement OAUTH2 Authorization code with PKCE

1 participant