Skip to content

feat(authorization-server): negative checks for PKCE downgrade, error redirect state and redirect URI matching - #610

Draft
anurag1504t wants to merge 1 commit into
modelcontextprotocol:mainfrom
anurag1504t:feat/authorization-server-negative-checks-578
Draft

anurag1504t wants to merge 1 commit into
modelcontextprotocol:mainfrom
anurag1504t:feat/authorization-server-negative-checks-578

Conversation

@anurag1504t

Copy link
Copy Markdown

Part of #578. This is a first small step: three checks that need no extra login, added to the existing authorization-code-grant scenario as that issue and AGENTS.md ask. Opening it as a draft so the shape can change if the questions in #578 are answered differently.

Checks

Each probe is an otherwise valid authorization request that changes one thing. A control request, valid in every way, is sent first.

Check Changes Passes when Spec
pkce-downgrade no code_challenge an error redirect with invalid_request; or, if a code is issued, redeeming it with a code_verifier is refused RFC 7636 4.4.1, RFC 9700 2.1.1
error-redirect-state the above, and an unsupported response_type every error redirect to the redirect URI carries the request's state exactly once RFC 6749 4.1.2.1
redirect-uri-exact the unsupported-response_type request with the registered redirect URI plus /extra, and plus x neither is redirected to, while the same request with the registered URI gets an error redirect RFC 9700 2.1, RFC 6749 4.1.2.1

Severity follows the keyword (MUST, so FAILURE). Each check uses one id for success and failure, and carries specReferences.

Design points

  • The control request goes first. If the server refuses it (the client or the redirect URI is not registered, or something blocks the probes), a refusal of a probe would say nothing about the requirement, so all three checks report "not testable" and nothing else is sent.
  • Each check asserts the reason, not only a refusal (see Prevent false greens when a negative check rejects for the wrong reason #467). pkce-downgrade passes only on an error redirect with invalid_request: RFC 7636 4.4.1 requires the authorization error response, which RFC 6749 4.1.2.1 delivers by redirecting. A direct 400 does refuse the request but is not that response, so it fails with that message; another error code fails too. A 400 with an HTML page or no error code, or any 401, 403, 404 or 429, is not testable, whatever the body says. For a redeemed code, only a 400 with invalid_grant or invalid_request passes, and a token in the response body, whatever the status, means the code was redeemed and fails.
  • redirect-uri-exact is a comparison. A refusal of a near-match says something about the redirect URI only if the same request with the registered URI got an error redirect; otherwise the server may refuse for another reason. A 200 page (a login page, or an "invalid redirect URI" page) proves nothing either way, so it is not read as acceptance; a redirect to a near-match fails.
  • Policy for all three: a check passes only when the requirement is seen to hold and fails only when it is seen to be violated. Anything else (a login page, a refusal whose reason is not visible, a server error, a timeout, an unexpected token endpoint answer) is "not testable".
  • The authorization endpoint's own query parameters are kept (RFC 6749 3.1): /authorize?tenant=abc used to become tenant=abc?response_type=.... The same bug in the interactive request URL is fixed in this PR too, since the probes would otherwise work where the login could not.
  • A check that cannot be exercised fails as "Not testable: ..." through untestableCheck() (AGENTS.md, Standardize how scenarios report setup/execution failures #248), not as SKIPPED. A server that asks for a login before it looks at an anonymous request therefore fails all three with that reason. That follows the repo's rule, but it leaves such a server red until a way to log in exists: question 4 in Authorization server: negative checks for the authorization code grant (PKCE, code reuse, client binding, redirect URI, state) #578 asks whether the tester should be given another URL to open, which would fix it.
  • The new logic is in authorization-endpoint-probes.ts; authorization-code-grant.ts changes by a call and a few lines in the description, and the existing test file only mocks the probes off. The probes run before the interactive login, so they are reported even if it fails. Every request has a 10 second timeout.

Not in this PR

Near-match redirect URIs other than path variants (query, host and port). A native app's loopback redirect may use any port (RFC 8252 7.3), so a changed port is not automatically invalid.

code-replay (@ryjen's branch), the iss check (#581), code-other-client (needs a second client, question 3 in #578), pkce-wrong-verifier and pkce-missing-verifier (each needs another login), and the points @Tetsurohhori raised (#601, a wrong client_secret, the OpenID discovery document).

Where this differs from AGENTS.md

  • The broken servers for the HTTP tests are inline in the test file, not in examples/servers/typescript/. The existing authorization-server tests mock undici and have no example server, and question 5 in Authorization server: negative checks for the authorization code grant (PKCE, code reuse, client binding, redirect URI, state) #578 asks if there is a fixture to extend. I can move them.
  • redeemWithVerifier repeats the client authentication code of requestToken (about 25 lines), to keep this change apart from the open code-replay branch, which edits that method. It can be shared once that lands.
  • The change is large: about 1,600 lines, over half of them tests and the rest the new module. The three checks are independent, so I can split it per check if that is easier to review.

Tests

69 new tests: 58 unit tests with undici mocked, and 11 over real HTTP against a small authorization server that behaves, one that issues a code but refuses a verifier for it, one that accepts the verifier, one that drops state, one that matches redirect URIs by prefix, one that asks for a login first, one that shows its login page inline with a 200, one that answers every invalid request with an HTML 400, one that refuses directly with a 400 instead of an error redirect, an endpoint that has its own query parameter, and a request for a client that is not registered. Each broken server turns its own check to FAILURE and leaves the others alone. npm run build, npm run check and npm test (710 tests) pass.

Run against real servers

Server pkce-downgrade error-redirect-state redirect-uri-exact
TypeScript SDK 1.32.2, authorizationHandler in Express pass fail: state missing on both error redirects pass
FastMCP OAuth proxy (local) pass pass pass
Django OAuth Toolkit 3.4.1 (local) not testable (asks for a login first; the login itself works, see below) not testable not testable

The TypeScript SDK result is the bug reported in modelcontextprotocol/typescript-sdk#2773 (fix PRs #2774 and #2842 are open), so the check finds a real gap.

With the real CLI (node dist/index.js authorization --url <issuer> --client-id <id> --port 8765) against Django OAuth Toolkit 3.4.1, with the login done by hand: the metadata scenario passed, authorization-code-grant passed (the code was issued and exchanged for a Bearer token and a refresh token), and the three new checks reported "Not testable", because DOT redirects every anonymous request to its login page. The CLI exit code was 1 for that reason alone. Any server that asks for a login before it looks at an anonymous request will behave the same until the tester can supply a login (question 4 in #578).

Disclosure: I used AI assistance (Claude) for this change. I ran the test suite myself.

…rror redirect state and redirect URI matching

Adds three checks to the authorization-code-grant scenario, as proposed in
issue 578. Each sends an authorization request that is valid except for one
thing and reads the answer without following redirects, so none needs another
login. A control request, valid in every way, goes first:

- pkce-downgrade: a request without a code_challenge gets an error redirect
  with invalid_request (RFC 7636 4.4.1; RFC 6749 4.1.2.1 delivers the error by
  redirecting). A direct 400 refuses the request but is not that response, so
  it fails. If a code is issued, redeeming it with a code_verifier must be
  refused with invalid_grant or invalid_request (RFC 9700 2.1.1), and a token
  in the response body, whatever the status, means the code was redeemed.
- error-redirect-state: an error redirect to the redirect URI carries the
  request's state exactly once (RFC 6749 4.1.2.1).
- redirect-uri-exact: an unsupported-response_type request with a redirect
  URI that only starts with the registered one is not redirected to, while
  the same request with the registered URI gets an error redirect
  (RFC 9700 2.1). A 200 page proves nothing and is not read as acceptance.

A check passes only when the requirement is seen to hold and fails only when
it is seen to be violated. Anything else, such as a refused control request, a
login page, a refusal whose reason is not visible, a server error or a
timeout, fails as "Not testable" through untestableCheck() (issue 248), not as
SKIPPED.

The authorization endpoint's own query parameters are kept when the request
URL is built (RFC 6749 3.1), in the probes and in the interactive request.

Each check has a unit test and a test over real HTTP against a server that
behaves and servers broken in the way the check looks for.
@Tetsurohhori

Copy link
Copy Markdown

@anurag1504t As promised in #578, I transcribed the three checks at 25c0430 to Python and ran them against my server with one rule removed at a time. I used the same 22 removals plus 9 extra ones. I froze the extra ones after reading the PR description but before reading its code. I didn't run the TypeScript or read your tests. All three checks pass on the unmodified server, for a public and a confidential client.

Removals Current checks change A new check reports a violation Either
the earlier 22 7 2 9
the 9 extra 0 8 8
  • error-redirect-state catches all 5 of its removals: state missing from both errors, missing from only one of them (two removals), changed, or sent twice. Probing both errors matters: each single-error removal shows up in only one of them.
  • redirect-uri-exact catches all 4 removals that let a longer path through. The ninth extra removal, where the query isn't compared, passes. Your description already says query variants aren't covered.
  • pkce-downgrade catches a server that accepts any verifier when no challenge was sent (public client).
  • Each violation is in the check its removal targets. No other new check changes.

Two points on pkce-downgrade

  1. A public client that gets a code without a code_challenge can still pass. As your description says, citing RFC 9700 §2.1.1, refusing the verifier later is enough. (I haven't read RFC 9700 itself.) But MCP requires authorization servers to implement OAuth 2.1, and draft-ietf-oauth-v2-1-13 §4.1.2.1 says: "An AS MUST reject requests without a code_challenge from public clients". For other clients, the same sentence allows an exception. I added a server with optional PKCE after reading your code. It issues a code without a challenge, refuses a verifier for that code, and issues a token if no verifier is sent. For a public client, the check is SUCCESS. Should an issued code be a FAILURE for a public client, with the redemption test kept for confidential clients? Authorization server: negative checks for the authorization code grant (PKCE, code reuse, client binding, redirect URI, state) #578 allows the current result too, and I didn't raise this in my first comment there. A missing clientSecret may not be enough to tell that a client is public, so the maintainers may need to decide that part.
  2. With a confidential client, Auth scenarios use token_endpoint_auth_method "none" even when a client secret is provided #601 can make the redemption "Not testable". If the metadata also lists none, no secret is sent and the server answers invalid_client. Without none in the metadata, the check gives a result: FAILURE when any verifier is accepted, SUCCESS when it is refused. fix(auth): prefer client secret auth over none when a secret is configured #602 changes that choice, but I haven't run it.

My server consents without a login, so I didn't exercise the login-page or timeout paths. The record has the code, the removals and the results: https://github.com/aos-standard/catalog/tree/b2f5ef2/docs/runs/mcp-pr610-authorization-probes

Disclosure: assisted by AI (Claude). I ran the checks myself.

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.

2 participants