Repository navigation
auth: add ErrForbidden for valid tokens the resource must refuse - #1544
Conversation
A TokenVerifier can only signal an invalid token (401), an OAuth protocol error (400), or some other error that the middleware answers with 500. A valid token that the resource server must refuse, for example one issued to a client the server does not serve, has no way to reach the 403 response RFC 6750 §3.1 and the MCP authorization spec call for. Add ErrForbidden: when a verifier error unwraps to it, RequireBearerToken answers 403 with a WWW-Authenticate challenge carrying error="insufficient_scope", plus resource_metadata and scope as before. Unknown verifier errors still answer 500, so a broken verifier stays distinguishable from a refused token.
| if errors.Is(err, ErrForbidden) { | ||
| return nil, err.Error(), http.StatusForbidden, "insufficient_scope" | ||
| } |
There was a problem hiding this comment.
emitting "insufficient_scope" will trigger a new authentication flow in authentication_code.go. In this case we should not emit any challengeErr
There was a problem hiding this comment.
you're right, this would trigger step-up authorization: Authorize in authorization_code.go only skips the flow for a 403 whose challenge lacks error="insufficient_scope". A token that is valid but refused for a non-scope reason would be refused again after that flow, so ErrForbidden now answers 403 with no OAuth error code in the challenge (commit ae9831f). The scope check still emits insufficient_scope, so step-up works as before for missing scopes. I updated the doc and the table tests, including the middleware case where resource_metadata/scope are still emitted but without error.
A 403 whose WWW-Authenticate challenge carries error="insufficient_scope" makes the authorization-code client start step-up authorization (authorization_code.go Authorize). A token that is valid but refused for a non-scope reason would be refused again after that flow, so ErrForbidden now answers 403 with a challenge that has no OAuth error code. The scope check keeps error="insufficient_scope".
|
@xianshujun thank you for the contribution! |
Brings the fork up to upstream main at 723d70e, which is v1.9.0 plus fourteen later commits. Upstream changes: modelcontextprotocol#1238, modelcontextprotocol#1511, modelcontextprotocol#1515, modelcontextprotocol#1517, modelcontextprotocol#1521, modelcontextprotocol#1523, modelcontextprotocol#1524, modelcontextprotocol#1527, modelcontextprotocol#1533, modelcontextprotocol#1537, modelcontextprotocol#1539, modelcontextprotocol#1540, modelcontextprotocol#1544, modelcontextprotocol#1547, modelcontextprotocol#1549, modelcontextprotocol#1550, modelcontextprotocol#1553, modelcontextprotocol#1554, modelcontextprotocol#1558, modelcontextprotocol#1560, modelcontextprotocol#1561, modelcontextprotocol#1562. Fork-side adaptations: - internal/jsonrpc2: the -32600 reply to a duplicate in-flight ID (modelcontextprotocol#1550) is written off the read loop and holds its admission slot until written, like every other reply under MaxIncomingRequests. - mcp: ClientSession.initialize takes the session state lock that upstream added, and still projects the negotiated version to the connection. - mcp: the fork's copies of hintomitempty, nowrapinvalidparams and nomethodnotfoundcodeinerror go away with upstream's removal (modelcontextprotocol#1533). - extensions/skills: reads the protocol version from the fork's json.RawMessage Meta. - Tests: the duplicate-ID admission test drains the new reply, and the client cancellation test runs on 2025-11-25 only, because 2026-07-28 clients reject every standalone server request (modelcontextprotocol#1537, modelcontextprotocol#1540, modelcontextprotocol#1554).
Fixes #1543
TokenVerifiercan currently signal an invalid token (401), an OAuth protocol error (400), or a verifier failure (500). A valid token that the resource server must refuse — for example one issued to a client the server does not serve — has no way to reach the 403 that the MCP authorization spec asks for.This adds
auth.ErrForbidden: when a verifier error unwraps to it,RequireBearerTokenanswers 403 Forbidden. The challenge deliberately carries no OAuth error code, so the authorization-code client does not start step-up authorization for a token that would be refused the same way;resource_metadataandscopefrom the options are still emitted. Unknown verifier errors still return 500, so a broken verifier stays distinguishable from a refused token.Tests: table cases for a bare and a wrapped
ErrForbiddenthroughverifyand through the middleware (with and without options), plus a plain error that still yields 500. The scope check keepserror="insufficient_scope", so step-up still applies to missing scopes.go test ./auth/... ./mcp/...,go build ./...,gofmt -l auth/andgo vet ./auth/...pass.Naming (
ErrForbiddenvsErrInsufficientScope) is easy to change if you prefer.AI-assisted: implementation, tests and this description were drafted with an agent under my direction; I reproduced the issue and reviewed and tested the change.