Skip to content

auth: advertise RFC 6750 error code in WWW-Authenticate challenge - #1135

Closed
PratikDhanave wants to merge 2 commits into
modelcontextprotocol:mainfrom
PratikDhanave:fix/bearer-www-authenticate-error-code
Closed

PratikDhanave wants to merge 2 commits into
modelcontextprotocol:mainfrom
PratikDhanave:fix/bearer-www-authenticate-error-code

Conversation

@PratikDhanave

Copy link
Copy Markdown
Contributor

Summary

RequireBearerToken never added an error= auth-param to the WWW-Authenticate header it emits. On insufficient scope it returned 403 with resource_metadata/scope but no error="insufficient_scope".

The SDK's own client gates step-up re-authorization on exactly that value (AuthorizationCodeHandler.Authorize):

if resp.StatusCode == http.StatusForbidden && errorFromChallenges(wwwChallenges) != "insufficient_scope" {
    return nil // skip step-up
}

errorFromChallenges reads Params["error"]; since the server never set it, it returned "", the condition was true, and step-up never fired. A client hitting an SDK-based server that needs additional scopes just kept failing with 403 instead of upgrading scopes. It's also an RFC 6750 §3.1 deviation.

Fixes #1134

Change

Thread the RFC 6750 error code out of verify and add the error= auth-param to the challenge:

Failure code error=
insufficient scope 403 insufficient_scope
invalid / expired / missing-exp token 401 invalid_token
ErrOAuth 400 invalid_request
no token (no credentials) 401 (none — per RFC 6750)
5xx 500 (none)

The change only adds a param to the header — it is non-breaking.

Testing

  • Extended TestVerify to assert the error code returned for each failure path.
  • Added TestRequireBearerTokenAdvertisesInsufficientScope, which drives the middleware end-to-end and parses the emitted header with the same oauthex.ParseWWWAuthenticate the client uses, asserting error="insufficient_scope" is visible.
  • Updated TestRequireBearerToken's expected WWW-Authenticate headers to include the new code.
  • Verified the interop test fails without the fix (error param = "", want "insufficient_scope") and passes with it. go vet ./auth/ and the full go test ./auth/ suite pass.

RequireBearerToken never added an error= auth-param to the
WWW-Authenticate header, so on insufficient scope it returned 403 with
resource_metadata/scope but no error="insufficient_scope". The SDK's own
client gates step-up re-authorization on exactly that value
(AuthorizationCodeHandler.Authorize), so errorFromChallenges returned ""
and step-up never fired — a client hitting an SDK server that needs more
scopes just kept failing with 403 instead of upgrading scopes.

Thread the RFC 6750 error code out of verify and add the error= param:
insufficient_scope on the 403 scope check, invalid_token on the
token-invalid 401 paths, invalid_request on the 400. A missing token (no
credentials presented) and 5xx failures advertise no code. The change is
additive to the header, non-breaking.

Fixes modelcontextprotocol#1134
Comment thread auth/auth.go
}
if errors.Is(err, ErrOAuth) {
return nil, err.Error(), http.StatusBadRequest
return nil, err.Error(), http.StatusBadRequest, "invalid_request"

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.

this will not trigger the authorization flow as the code in @authorization_code is just checking for http.StatuForbidden case.
I am not sure tho if we should extend the case for the flow re-trigger

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.

Thanks @guglielmo-san — good catch to check, but the step-up path does fire with this change. AuthorizationCodeHandler.Authorize gates re-auth on two conditions together:

if resp.StatusCode == http.StatusForbidden && errorFromChallenges(wwwChallenges) != "insufficient_scope" {
    return nil // skip step-up
}

It skips step-up only when the status is 403 and the challenge's error param is not insufficient_scope. Before this PR the server never set error= at all, so errorFromChallenges returned "", the condition was true, and step-up was skipped — that's the bug. By advertising error="insufficient_scope" on the 403 challenge (RFC 6750 §3.1), the condition becomes false and re-authorization proceeds.

TestRequireBearerTokenAdvertisesInsufficientScope drives this end-to-end and parses the emitted header with the same oauthex.ParseWWWAuthenticate the client uses, asserting the code is visible. So the 403-checking logic doesn't need to change — the fix is exactly to populate the error param it already reads.

guglielmo-san pushed a commit that referenced this pull request Oct 7, 2026
While reading `RequireBearerToken` I noticed that `verify` checks
required scopes before expiry. A token that's expired and also missing a
scope gets `403 insufficient scope` instead of `401 token expired`, and
a token with no expiration gets the same 403 when
`AllowMissingExpiration` is false. Repro is in #1349.

An expired token is `invalid_token` (RFC 6750 §3.1), so the client
should be told to get a new token, not to ask for more scopes. I moved
the expiry check, including the missing-expiration case, above the scope
loop. No API change.

This touches the same lines in `verify` and `TestVerify` as #1135. If
that one lands first I'll rebase on it.

Tested with go1.27.1 on darwin/arm64:

- The two new `TestVerify` cases ("expired and missing scope", "no
expiration and missing scope") fail on main with `got ("insufficient
scope", 403)` and pass with the fix.
- `gofmt -l auth` and `go vet ./...` are clean, and `go test -count=1
./...` passes.
- The repro from the issue now prints `401 token expired`.

Fixes #1349

_AI assistance: I used Claude Code to help write this fix and the
tests._

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-authored-by: Claude Opus 5.5 <[email protected]>
@guglielmo-san

Copy link
Copy Markdown
Contributor

Closing as duplicate of #1134

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.

RequireBearerToken omits RFC 6750 error code, so the SDK's own step-up flow never triggers

2 participants