Repository navigation
Conversation
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>
3ef3154 to
ab1180a
Compare
|
I rebased this onto current master (576765a). Two commits on master touched the same code:
Nothing else changed, and 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. |
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:
authorizeacceptscode_challengewithcode_challenge_method=S256. Theplainmethod, or a challenge without a method, gets aninvalid_requesterror redirect.code_verifierfor a code that was requested with a challenge.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 afterVersion011901..., and the app version goes to3.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:
code_verifierfor a code that has no challenge (RFC 9700 section 2.1.1).&.Behaviour you should know about:
stateandcodenow go into the query, before the fragment. Before, they were appended after the fragment. This also applies to clients that do not use PKCE.oauth.state. A second authorization request that arrives between theauthorizeresponse 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.LoginRedirectorController.php(one message) andinfo.xml(the description text). Whichever lands second needs a small rebase. I will do it if it is this one.How I tested:
apps/oauth2/tests(70 tests) andtests/Core/Controller/ClientFlowLoginControllerTest.php(19 tests) pass.invalid_grant, and the same code still works afterwards with the right verifier. A missing verifier returnsinvalid_request.plain, a missing method and a challenge of 44 characters are rejected atauthorize. A verifier without a challenge returnsinvalid_grant. A flow without PKCE still works. A wrong client secret returnsinvalid_clientbefore any PKCE answer.authorizerequest without PKCE in the same session makes page A fail with "State token does not match".occ upgradeto this branch.TODO
/.well-known/oauth-authorization-server#64852)Checklist
3. to review, feature component)stable32)AI (if applicable)
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