auth: bind GitHub OIDC token exchange to a per-deployment audience (#1229)
## What this does The publisher CLI now derives the OIDC audience to request from the `--registry` URL instead of using a fixed constant, and the server validates the token's `aud` claim against a new per-deployment env var (`MCP_REGISTRY_GITHUB_OIDC_AUDIENCE`). Production sets this env var to `https://registry.modelcontextprotocol.io` (and per-environment URL elsewhere), so the deployment only accepts tokens that were requested for it. ## Compatibility This is a breaking change for older `mcp-publisher` binaries that still request the legacy hardcoded audience. After this rolls to a deployment, users on old `mcp-publisher` will see a clear `invalid audience: expected …, got […]` error and need to upgrade. Users following the documented `releases/latest` install pattern get the new binary automatically. ## Test plan - [ ] CI green - [ ] Run a synthetic publish from a real GitHub Actions workflow against staging after deploy - [ ] Confirm an older `mcp-publisher` against the same staging deploy returns the expected `invalid audience` error 🤖 Generated with [Claude Code](https://claude.com/claude-code) --------- Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
parent
88bbb851ad
commit
3f89fc2b1f
2 changed files with 166 additions and 8 deletions
|
|
@ -252,8 +252,10 @@ func RegisterGitHubOIDCEndpoint(api huma.API, pathPrefix string, cfg *config.Con
|
|||
|
||||
// ExchangeToken exchanges a GitHub OIDC token for a Registry JWT token
|
||||
func (h *GitHubOIDCHandler) ExchangeToken(ctx context.Context, oidcToken string) (*auth.TokenResponse, error) {
|
||||
// Validate OIDC token with audience "mcp-registry"
|
||||
claims, err := h.validator.ValidateToken(ctx, oidcToken, "mcp-registry")
|
||||
if h.config.GitHubOIDCAudience == "" {
|
||||
return nil, fmt.Errorf("GitHub OIDC exchange disabled: this deployment has no audience configured")
|
||||
}
|
||||
claims, err := h.validator.ValidateToken(ctx, oidcToken, h.config.GitHubOIDCAudience)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to validate OIDC token: %w", err)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -26,7 +26,8 @@ func (m *MockOIDCValidator) ValidateToken(ctx context.Context, token string, aud
|
|||
|
||||
func TestGitHubOIDCHandler_ExchangeToken(t *testing.T) {
|
||||
cfg := &config.Config{
|
||||
JWTPrivateKey: "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef", // 32 bytes hex
|
||||
JWTPrivateKey: "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef", // 32 bytes hex
|
||||
GitHubOIDCAudience: "https://registry.test",
|
||||
}
|
||||
|
||||
handler := auth.NewGitHubOIDCHandler(cfg)
|
||||
|
|
@ -118,9 +119,82 @@ func TestGitHubOIDCHandler_ExchangeToken(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
func TestGitHubOIDCHandler_AudienceBinding(t *testing.T) {
|
||||
t.Run("empty audience config fails closed", func(t *testing.T) {
|
||||
cfg := &config.Config{
|
||||
JWTPrivateKey: "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef",
|
||||
GitHubOIDCAudience: "",
|
||||
}
|
||||
handler := auth.NewGitHubOIDCHandler(cfg)
|
||||
// Validator should never be called — the audience precondition rejects first.
|
||||
handler.SetValidator(&MockOIDCValidator{
|
||||
validateFunc: func(_ context.Context, _ string, _ string) (*auth.GitHubOIDCClaims, error) {
|
||||
t.Fatal("validator must not be called when audience is unconfigured")
|
||||
return nil, fmt.Errorf("unreachable")
|
||||
},
|
||||
})
|
||||
|
||||
_, err := handler.ExchangeToken(context.Background(), "any-token")
|
||||
require.Error(t, err)
|
||||
assert.Contains(t, err.Error(), "no audience configured")
|
||||
})
|
||||
|
||||
t.Run("configured audience is what the validator sees", func(t *testing.T) {
|
||||
cfg := &config.Config{
|
||||
JWTPrivateKey: "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef",
|
||||
GitHubOIDCAudience: "https://registry.example.com",
|
||||
}
|
||||
handler := auth.NewGitHubOIDCHandler(cfg)
|
||||
|
||||
var seenAudience string
|
||||
handler.SetValidator(&MockOIDCValidator{
|
||||
validateFunc: func(_ context.Context, _ string, audience string) (*auth.GitHubOIDCClaims, error) {
|
||||
seenAudience = audience
|
||||
return &auth.GitHubOIDCClaims{
|
||||
RegisteredClaims: jwt.RegisteredClaims{
|
||||
Subject: "repo:octo-org/octo-repo:environment:prod",
|
||||
ExpiresAt: jwt.NewNumericDate(time.Now().Add(1 * time.Hour)),
|
||||
Audience: jwt.ClaimStrings{audience},
|
||||
},
|
||||
RepositoryOwner: "octo-org",
|
||||
}, nil
|
||||
},
|
||||
})
|
||||
|
||||
_, err := handler.ExchangeToken(context.Background(), "any-token")
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, "https://registry.example.com", seenAudience)
|
||||
})
|
||||
|
||||
t.Run("token issued for another deployment is rejected", func(t *testing.T) {
|
||||
cfg := &config.Config{
|
||||
JWTPrivateKey: "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef",
|
||||
GitHubOIDCAudience: "https://registry.modelcontextprotocol.io",
|
||||
}
|
||||
handler := auth.NewGitHubOIDCHandler(cfg)
|
||||
|
||||
// The mock validator does what the real one does: reject if the
|
||||
// expected audience is not present in the token's aud claim.
|
||||
handler.SetValidator(&MockOIDCValidator{
|
||||
validateFunc: func(_ context.Context, _ string, expected string) (*auth.GitHubOIDCClaims, error) {
|
||||
tokenAudience := "https://attacker-registry.example" //nolint:gosec // G101 false positive: URL string, not a credential
|
||||
if expected != tokenAudience {
|
||||
return nil, fmt.Errorf("invalid audience: expected %s, got [%s]", expected, tokenAudience)
|
||||
}
|
||||
return &auth.GitHubOIDCClaims{RepositoryOwner: "octo-org"}, nil
|
||||
},
|
||||
})
|
||||
|
||||
_, err := handler.ExchangeToken(context.Background(), "captured-token")
|
||||
require.Error(t, err)
|
||||
assert.Contains(t, err.Error(), "invalid audience")
|
||||
})
|
||||
}
|
||||
|
||||
func TestBuildPermissionsFromOIDC(t *testing.T) {
|
||||
cfg := &config.Config{
|
||||
JWTPrivateKey: "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef",
|
||||
JWTPrivateKey: "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef",
|
||||
GitHubOIDCAudience: "https://registry.test",
|
||||
}
|
||||
handler := auth.NewGitHubOIDCHandler(cfg)
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue