Repository navigation
fix(auth): no error code for a request that presented no credentials - #3824
AnnasMazhar wants to merge 1 commit into
Conversation
RFC 6750 Section 3.1 says a request that carried no credentials SHOULD NOT be answered with an error code, but the bearer gate collapsed the no-header case into the same challenge as an invalid token and emitted error="invalid_token". The challenge also never carried the `scope` parameter, which the spec SHOULD include for both the discovery-time 401 and the runtime 403 -- and which this SDK's own client reads (utils.py extract_scope_from_www_auth) to drive step-up, so the gap was a resource-server/client asymmetry. Split the two cases: * no Authorization header -> 401 with resource_metadata and scope, no error code, and an empty body * presented but invalid token -> 401 with error="invalid_token", plus scope * insufficient scope -> 403 with error="insufficient_scope" and the required scope named The scope parameter is optional on _send_auth_error and is joined outside the f-string: nested same-type quotes in an f-string need PEP 701 (3.12+) and this project supports 3.11. Resolves the divergences recorded on hosting:auth:missing-401, hosting:auth:scope-403 and hosting:auth:invalid-401; those entries no longer carry a divergence, and the tests that pinned the old challenge now pin the corrected one. Closes modelcontextprotocol#3769
|
This PR has been closed automatically. This repo only keeps pull requests open when they come from a maintainer, or from a contributor a maintainer has assigned to the linked issue, and you aren't currently assigned to #3769. If a maintainer assigns you to #3769, this PR reopens on its own and there's nothing more you need to do here. Assignment is a maintainer call based on capacity; comments that only ask to be assigned don't factor in. What does help is engaging on the issue itself by confirming the repro, explaining why it matters for your use case, or describing the approach you'd take. You're welcome to keep pushing commits here (just avoid force-pushing, since GitHub can't reopen a rewritten branch), but that on its own won't get the PR reviewed or the issue assigned, and realistically most auto-closed PRs stay closed. There's no need to open a new PR either way. CONTRIBUTING.md has the full reasoning, but in short:
Maintainers: reopen, remove |
Summary
RequireAuthMiddlewarecollapses two different failures into one challenge, and never advertises the required scope.1. A request that presented no credentials is answered with an error code. RFC 6750 Section 3.1: a request with no credentials SHOULD NOT include an error code. The gate answered a no-
Authorizationrequest witherror="invalid_token"— the same challenge as a token that was presented and rejected.2. No challenge ever carried
scope. The spec SHOULD include it for both the discovery-time 401 (#protected-resource-metadata-discovery-requirements) and the runtime 403 (#runtime-insufficient-scope-errors). This SDK's own client readsscopeout of the header to drive step-up (utils.py extract_scope_from_www_auth), so the gap was a resource-server/client asymmetry: the client consumed a parameter the server never sent.Changes
src/mcp/server/auth/middleware/bearer_auth.py__call__: the not-authenticated branch now checks whether anAuthorizationheader was actually presented. If not, the challenge carries no error code and the body is empty; if a credential was presented and rejected,error="invalid_token"is kept. Either way the challenge advertises the requiredscope._send_auth_errortakes an optionalscope, emits it as ascope="..."parameter, and allowserror/descriptionto be absent.The scope string is joined outside the f-string: nested same-type quotes in an f-string need PEP 701 (Python 3.12+), and this project supports 3.11.
Resulting behaviour
WWW-AuthenticateAuthorizationresource_metadata,scopeerror="invalid_token",resource_metadata,scope{"error": ..., "error_description": ...}error="insufficient_scope",scope,resource_metadata{"error": ..., "error_description": ...}Tests
tests/interaction/auth/test_bearer.pyandtests/docs_src/test_authorization.pyupdated to pin the corrected challenge (the previous ones pinned the old one viainline_snapshot).tests/interaction/_requirements.pydrops the now-resolveddivergencerecords onhosting:auth:missing-401,hosting:auth:scope-403andhosting:auth:invalid-401.docs/run/authorization.mdupdated so the tutorial shows the two cases separately.Local run: 276 passed, 1 xfailed (
tests/interaction/auth/,tests/interaction/test_coverage.py,tests/client/test_auth.py,tests/docs_src/test_authorization.py);ruff checkandruff format --checkclean.Note for the reviewer
The maintainer comment on the issue suggests the backend should signal "no credentials" distinctly (e.g. a sentinel/exception) rather than the header check used here. The header check gets the same split with a smaller diff, but I am happy to switch to backend signalling if that is preferred. The
scopevalue is the scopes required for the current request —self.required_scopeswhen the middleware is instantiated per route — notsettings.required_scopesblindly.Closes #3769