Scope-challenge middleware fails open when the token-scope fetch returns an error

Aberta Para iniciantes
#3,929 0 comentários 0 reações 0 responsáveis Ver no GitHub

Mantenedores costumam responder em até 3 dias

@Shanmukh-l33t já está trabalhando nisso.

Desde 23/11/2026.

  • #3932 de @Shanmukh-l33t — aberto

Avaliação

Dificuldade
2/5
Tempo estimado
1-3 horas
Facilidade para iniciantes
78/100
Tipo de issue
Bug
Clareza
Claramente especificada
Status de atividade
Ativa
Stack de tecnologia
go

Direção de pesquisa

Comece pelas duas ramificações de erro nomeadas: pkg/http/middleware/scope_challenge.go por volta das linhas 87-91 e pkg/http/handler.go por volta das linhas 488-491. Leia os testes de middleware existentes e a interface do scope-fetcher para que um fetcher falso que retorna um erro possa conduzir a cadeia, como o esboço de reprodução mostra. Pronto significa um teste que prova que um erro de fetch não chama mais next.ServeHTTP (challenge ou 503 em vez disso), e que o caminho de inventário de PAT retém ferramentas condicionadas por scope quando os scopes não podem ser buscados.

Escrita pelo modelo de indexação a partir do texto da issue.

Descrição

Scope-challenge middleware fails open when the token-scope fetch returns an error

Target repository: github/github-mcp-server
Affected revision: e3809fe91c75 (the same branches are present on main as of 2026-11-01).

Summary

Two error branches treat a failed token-scope lookup as "allow":

  1. OAuth tools/call middleware — pkg/http/middleware/scope_challenge.go:87-91:
fetchedScopes, err := scopeFetcher.FetchTokenScopes(ctx, tokenInfo.Token)
if err != nil {
    next.ServeHTTP(w, r)
    return
}

When the scope endpoint is unavailable, the request is forwarded without the 403 + WWW-Authenticate: ... insufficient_scope challenge that the middleware exists to produce.

  1. PAT inventory filtering — pkg/http/handler.go:488-491:
scopes, err := f.fetchScopes(ctx, token)
if err != nil {
    return b
}

A failed scope fetch returns the unfiltered tool inventory instead of withholding tools whose scopes cannot be verified.

Reproduction

The real middleware chain can be driven with a fake scope fetcher:

fetcher := &fakeFetcher{err: errors.New("scope endpoint unavailable (simulated HTTP 503)")}
h := WithMCPParse()(WithScopeChallenge(&oauth.Config{}, fetcher)(recordingNext))
// tool scope map: delete_repository requires delete_repo + repo
// request: tools/call delete_repository, OAuth token, no scopes in context

Observed:

Case Result
fetch error next=true, status=200, WWW-Authenticate="", no scopes installed in context
fetched scopes [repo] (control) 403, WWW-Authenticate: ... scope="delete_repo repo", next not called
scopes [repo] already in context + erroring fetcher 403 (fetcher not called)

For the PAT path, with a real github.LabelWrite tool and a failing fetch, the production NewDefaultInventoryFactory exposed 134 tools including label_write, versus 46 tools when scopes were [gist] — 88 scope-gated tools exposed.

Expected behavior

If the token's scopes cannot be verified, the request should fail closed:

  • the OAuth tools/call middleware should return the insufficient-scope challenge (or a 503/authorization error), not call the next handler;
  • the PAT inventory path should withhold scope-gated tools, or fail the listing, rather than returning the unfiltered inventory.

A scope-endpoint outage is an authorization-verification failure, not evidence that the token is authorized.

Actual behavior

Both paths call the success path on error:

  • scope_challenge.go:88-91 calls next.ServeHTTP(w, r); the request reaches the tool handler with status=200, no WWW-Authenticate, and no scopes in context.
  • handler.go:488-491 returns the builder unchanged; with a failing fetch the production inventory exposed 134 tools (including label_write) versus 46 tools when the token's scopes were [gist].

Impact

  • Protocol/UX fail-open: the client is never told to re-authenticate and the call proceeds to GitHub. GitHub enforces the token's real scopes at the API, and the tool handlers use the caller's own token, so this does not widen privileges; an out-of-scope call still fails at the API.
  • Inventory disclosure: a failing scope fetch reveals the names/descriptions of tools the token should not see. Those descriptions are public in this open-source repository, so the confidentiality impact is low.
  • Severity: informational/low. The bug is that an availability failure silently disables a security control instead of failing closed or using a cached scope set.

Suggested fix

Fail closed on a scope-fetch error:

if err != nil {
    http.Error(w, "scope verification unavailable", http.StatusServiceUnavailable)
    return
}

or fall back to the last successfully fetched scopes for that token, never to the unrestricted path. For the inventory path, withhold scope-gated tools when scopes cannot be fetched.

Evidence

  • PoC tests: zz_verify173_test.go, zz_verify174_test.go (inside the target tree during the audit)
  • Upstream status checked on 2026-11-01: both error branches are still present.

Reproduced with the real go test runs (exit 0).

Linguagem predominante
Go
Estrelas
34.8k
Forks
5.4k
Merge médio
3d 22h
PRs com merge (30d)
31

Preparar o ambiente

Primeiros passos

  1. Leia a issue inteira e depois o guia de contribuição do projeto.
  2. Comente na issue dizendo que vai assumir — evita que duas pessoas façam o mesmo trabalho.
  3. Faça um fork do repositório e trabalhe em uma branch.
  4. Abra um pull request que referencie o número da issue.

Mais de github/github-mcp-server

Todas as issues de github/github-mcp-server

Issues semelhantes

Mais issues de Go

Receba novas issues na sua caixa de entrada

Um resumo curto de issues do GitHub para quem está começando.