Skip to content

auth: add ErrForbidden for valid tokens the resource must refuse - #1544

Merged
guglielmo-san merged 2 commits into
modelcontextprotocol:mainfrom
xianshujun:fix-1543-forbidden
Nov 24, 2026
Merged

guglielmo-san merged 2 commits into
modelcontextprotocol:mainfrom
xianshujun:fix-1543-forbidden

Conversation

@xianshujun

@xianshujun xianshujun commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #1543

TokenVerifier can 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, RequireBearerToken answers 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_metadata and scope from 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 ErrForbidden through verify and through the middleware (with and without options), plus a plain error that still yields 500. The scope check keeps error="insufficient_scope", so step-up still applies to missing scopes. go test ./auth/... ./mcp/..., go build ./..., gofmt -l auth/ and go vet ./auth/... pass.

Naming (ErrForbidden vs ErrInsufficientScope) 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.

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.
Comment thread auth/auth.go
Comment on lines +161 to +163
if errors.Is(err, ErrForbidden) {
return nil, err.Error(), http.StatusForbidden, "insufficient_scope"
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

emitting "insufficient_scope" will trigger a new authentication flow in authentication_code.go. In this case we should not emit any challengeErr

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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".
@guglielmo-san

Copy link
Copy Markdown
Contributor

@xianshujun thank you for the contribution!

@guglielmo-san
guglielmo-san merged commit a5f29d0 into modelcontextprotocol:main Nov 24, 2026
10 checks passed
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.

auth: RequireBearerToken answers any verifier rejection other than ErrInvalidToken with 500; no way to return 403

2 participants