Repository navigation
Fail closed when the token scope fetch errors - #3932
Shanmukh-l33t wants to merge 2 commits into
Conversation
The OAuth scope challenge middleware forwarded tools/call requests to the next handler when FetchTokenScopes failed, and PATScopeFilter returned the unfiltered inventory. Both now treat an unverifiable token as unauthorized: the middleware answers 503 and the inventory withholds scope-gated tools. Fixes github#3929
There was a problem hiding this comment.
🟡 Changes recommended
The middleware misclassifies expired or revoked tokens as 503 errors and unnecessarily refetches known empty scopes, allowing outages to suppress the appropriate authentication or insufficient-scope challenges.
2 open issues
What changed in this PR
This PR makes HTTP token-scope verification fail closed in the GitHub MCP Server, addressing #3929 without adding a cached-scope fallback.
Changes:
- Return 503 instead of forwarding OAuth tool calls when scope lookup fails.
- Withhold scope-gated classic PAT tools on lookup errors, while preserving unscoped and public-read tools.
- Add regression tests for both failure paths.
| File | Description |
|---|---|
pkg/http/middleware/scope_challenge.go |
Stop OAuth tool calls when scope verification fails. |
pkg/http/middleware/scope_challenge_test.go |
Verify the 503 response and that the next handler is not called. |
pkg/http/handler.go |
Apply an empty scope filter after PAT lookup errors. |
pkg/http/handler_test.go |
Verify lookup failures hide scoped tools but retain unscoped tools. |
Review effort: Max (adaptive)
| @@ -86,7 +86,8 @@ func WithScopeChallenge(oauthCfg *oauth.Config, scopeFetcher scopes.FetcherInter | |||
| if !ok || (len(activeScopes) == 0 && tokenInfo.Token != "") { | |||
There was a problem hiding this comment.
Fixed in e4aebb0. The middleware now falls back to the GitHub lookup only when the context has no scope value or a nil one. A non-nil empty slice is used as is, so the request gets the insufficient scope challenge instead of a lookup that can turn an outage into a 503.
Test: TestWithScopeChallengeTreatsExplicitEmptyScopesAsKnown. On the previous commit the same request returned 503 because the fetcher was called and failed; now it returns 403 with insufficient_scope and the fetcher is never called.
| if err != nil { | ||
| next.ServeHTTP(w, r) | ||
| // Fail closed: an unverifiable token must not reach the tool handler. | ||
| http.Error(w, "scope verification unavailable", http.StatusServiceUnavailable) |
There was a problem hiding this comment.
Fixed in e4aebb0. FetchTokenScopes now returns the sentinel scopes.ErrInvalidToken for a 401, and WithScopeChallenge maps it to the sendAuthChallenge 401 response with the WWW-Authenticate resource metadata header. Other fetch errors still return 503.
Test: TestWithScopeChallengeReturns401ForInvalidToken. On the previous commit it failed with 503 and no challenge header; it passes now. Full go test ./... and script/lint are green.
FetchTokenScopes now returns ErrInvalidToken for a 401, and the scope challenge middleware answers it with a 401 Bearer challenge instead of a 503, so an OAuth client learns to obtain a new token rather than retrying an unverifiable one. A non-nil empty scope slice in the request context is treated as known, so a token with no scopes gets the insufficient scope challenge without a redundant GitHub lookup. Fixes github#3929
|
This is all intentional and for good reason. Fail closed is not more secure and fail open is intentional in this case. When listing tools, if we fail to get scopes then you just spend more tokens on tools you cannot use, but the reason the filter exists is to save on wasted tokens - not a security boundary. Scope challenges also are about up-scoping, not restriction. They are primarily a mechanism for asking the user to get more scopes if they do not have one. Scope challenges only fire if you are logged in with oauth and the client is able to ask the auth server for additional scopes. So I don't think we'd intend to weaken this. It's a token saving, and avoids broken tool calls but failing to start up a session because of a glitch with getting scopes breaks the whole server. We do not want that. I can't say enough, this is a tooken optimization and not a security feature. |

Fixes #3929
Problem
When the token scope fetch returns an error, two code paths fail open:
WithScopeChallengeforwarded OAuthtools/callrequests to the next handler without the403 insufficient_scopechallenge.PATScopeFilterreturned the unfiltered tool inventory, exposing scope-gated tools to a classic PAT whose scopes could not be verified.Root cause
Both error branches called the success path (
next.ServeHTTPandreturn b), so a scope endpoint outage silently disabled the control.Fix
pkg/http/middleware/scope_challenge.go: on a fetch error, respond503 scope verification unavailableand do not call next.pkg/http/handler.go: on a fetch error, filter with an empty scope set, so tools that declare a scope requirement are withheld and tools with no scope requirement (or public read access) stay listed.pkg/scopes/fetcher.go: a 401 from the scope endpoint now returnsErrInvalidToken, andWithScopeChallengeanswers it with a 401 Bearer challenge instead of a 503, so an expired or revoked token gets the response that tells the client to obtain a new one.pkg/http/middleware/scope_challenge.go: a non-nil empty scope slice in the request context is treated as known ("this token has no scopes") instead of triggering a lookup, so the empty set still produces the insufficient scope challenge during an outage.The first two points came from the review on this PR. I did not add a cached-scope fallback to keep the change limited to the error branches.
WithPATScopesstill logs and continues on error as before, andPATScopeFilternow fails closed on the same error.Tests
Four regression tests, each failing on the code before its fix and passing with it:
TestWithScopeChallengeFailsClosedWhenScopeFetchErrors(pkg/http/middleware): before the fix next was called and the status was 204, after the fix next is not called and the status is 503.TestPATScopeFilterWithholdsScopeGatedToolsWhenScopeFetchErrors(pkg/http): before the fix both tools were listed, after the fix only the unscoped tool is listed.TestWithScopeChallengeReturns401ForInvalidToken: withErrInvalidTokenfrom the fetcher the response is 401 with aWWW-Authenticatechallenge; on the previous commit it was 503 with no challenge.TestWithScopeChallengeTreatsExplicitEmptyScopesAsKnown: an empty non-nil scope set yields the 403 insufficient scope challenge without calling the fetcher; on the previous commit the fetcher error produced a 503.Run with Go 1.27.4:
go test -p 2 ./pkg/scopes/ ./pkg/http/middleware/: okgo test -p 2 ./...: all packages okscript/lint(golangci-lint v2.14.0): 0 issuesNo tool snapshots changed.
AI assistance was used to prepare this change, and the result was reviewed and tested by me.