Skip to content

fix(auth/google): require clientId when mcpEnabled is false - #3480

Closed
CorsenAI wants to merge 1 commit into
googleapis:mainfrom
CorsenAI:fix-auth-google-require-clientid-non-mcp
Closed

CorsenAI wants to merge 1 commit into
googleapis:mainfrom
CorsenAI:fix-auth-google-require-clientid-non-mcp

Conversation

@CorsenAI

Copy link
Copy Markdown
Contributor

Summary

In non-MCP mode the Google auth service validates ID tokens in GetClaimsFromHeader with:

payload, err := idtoken.Validate(ctx, token, a.ClientID)

When clientId is left empty, idtoken.Validate is called with an empty audience. The library only enforces the audience claim when the audience argument is non-empty:

// google.golang.org/api/[email protected] validate.go
// "If audience is not empty the audience claim of the Token is validated."
if audience != "" && payload.Audience != audience {
    return nil, fmt.Errorf("idtoken: audience provided does not match aud claim in the JWT")
}

So a deployment that configures a google auth service without clientId (with mcpEnabled: false) silently accepts any Google-signed ID token — for any OAuth client / any audience — for Authenticated Parameters, instead of only tokens minted for the configured client.

Config.Initialize() already fails closed for the MCP path (audience or clientId required when mcpEnabled is true, added in #3450), and ValidateMCPAuth refuses to validate when no audience is resolvable. The non-MCP branch had no equivalent guard.

The reference docs already document clientId as "Required for validating ID tokens in non-MCP web apps (GetClaimsFromHeader)", but the code never enforced it — a code-vs-docs divergence.

Change

  • Initialize(): when mcpEnabled is false, return an error if clientId is empty (fail-closed config validation, mirroring the existing mcpEnabled guard).
  • Introduce a small package-level validateIDToken = idtoken.Validate indirection so the audience-binding behaviour can be unit-tested hermetically, without live calls to Google's certificate endpoint. Both ID-token validation paths now go through it.

Tests

  • TestInitialize_Validation: asserts Initialize() fails on empty clientId in non-MCP mode (the previously-passing "neither clientId nor audience, mcpEnabled false" case now expects an error).
  • TestGetClaimsFromHeader_AudienceBinding: with a configured clientId, a token whose aud matches is accepted and a token whose aud differs is rejected.
  • TestGetClaimsFromHeader_NoToken: a missing token header is a no-op (returns nil, nil).

go test ./internal/auth/google/... passes; gofmt -l and go vet are clean.

Compatibility

This is a hardening / fail-closed change, not a report of active exploitation. It is a behavioural change: a google auth service configured with mcpEnabled: false and no clientId was previously accepted at startup and will now fail to initialize with a clear error. That configuration provided no audience binding (insecure by default), so failing fast surfaces the misconfiguration rather than silently accepting unintended tokens. Happy to adjust the message or soften to a startup warning if maintainers prefer.

In non-MCP mode the Google auth service validates ID tokens in
GetClaimsFromHeader via idtoken.Validate(ctx, token, clientId). When
clientId is empty, idtoken.Validate is invoked with an empty audience and
skips the audience check entirely (the library only enforces it when
`audience != ""`). A deployment that omits clientId therefore silently
accepts any Google-signed ID token, regardless of which OAuth client it
was issued for.

The reference docs already mark clientId as "Required for validating ID
tokens in non-MCP web apps (GetClaimsFromHeader)", but Initialize() never
enforced it. Make config validation fail closed: Initialize() now returns
an error when mcpEnabled is false and clientId is empty, mirroring the
existing audience-or-clientId guard used when mcpEnabled is true.

Tests:
- Initialize() rejects empty clientId in non-MCP mode.
- GetClaimsFromHeader binds the configured clientId as the expected token
  audience (accepts matching aud, rejects mismatched aud).

Co-Authored-By: Claude Opus 4.8 <[email protected]>
@CorsenAI
CorsenAI requested a review from a team as a code owner June 18, 2026 23:33
@google-cla

google-cla Bot commented Jun 18, 2026

Copy link
Copy Markdown

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

@gemini-code-assist gemini-code-assist Bot left a comment

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.

Code Review

This pull request enhances Google ID token validation by requiring clientId when mcpEnabled is false, preventing audience checks from being silently skipped. It also introduces a package-level validateIDToken variable to facilitate mocking in tests, and adds corresponding unit tests. The reviewer recommends refactoring AuthService to store the validator function as a struct field rather than using a package-level global variable, which is a Go anti-pattern that can cause flaky tests and hinder parallel execution.

Comment on lines +33 to +37
// validateIDToken validates a Google-issued ID token against the expected
// audience. It is a package-level indirection over idtoken.Validate so tests
// can substitute a fake validator and exercise the audience-binding logic
// without performing live network calls to Google's certificate endpoint.
var validateIDToken = idtoken.Validate

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.

medium

Using a package-level global variable (validateIDToken) for mocking/stubbing in tests is a known anti-pattern in Go. It introduces global state that can lead to flaky tests, data races, and prevents running tests in parallel (e.g., using t.Parallel()).

To make this cleaner and concurrency-safe, consider refactoring AuthService to hold a validator function field, or use idtoken.NewValidator with a custom http.Client (which is already present in AuthService). Since the struct definition and Initialize function are partially outside the current diff, you can refactor this in a future change or update the struct to:

type AuthService struct {
	Config
	client    *http.Client
	validator func(context.Context, string, string) (*idtoken.Payload, error)
}

And define a helper method:

func (a AuthService) validateIDToken(ctx context.Context, token, audience string) (*idtoken.Payload, error) {
	if a.validator != nil {
		return a.validator(ctx, token, audience)
	}
	return idtoken.Validate(ctx, token, audience)
}

@Yuan325 Yuan325 assigned duwenxin99 and unassigned Yuan325 Jul 7, 2026
@Yuan325 Yuan325 added the priority: p1 Important issue which blocks shipping the next release. Will be fixed prior to next release. label Jul 7, 2026
@CorsenAI CorsenAI closed this by deleting the head repository Jul 12, 2026
duwenxin99 pushed a commit that referenced this pull request Nov 20, 2026
## Summary

In non-MCP mode, `GetClaimsFromHeader` validates ID tokens via
`idtoken.Validate(ctx, token, a.ClientID)`. When `clientId` is empty,
`idtoken.Validate` receives an empty audience and **skips the audience
check entirely**, so the service silently accepts **any Google-signed ID
token**, regardless of which OAuth client it was issued for.
`Initialize()` guards this for the MCP path (`audience` or `clientId`
required when `mcpEnabled: true`) but the non-MCP branch has no
equivalent guard.

The reference docs already mark `clientId` as "Required for validating
ID tokens in non-MCP web apps (GetClaimsFromHeader)", but `Initialize()`
never enforced it. Still reproduces on `main`.

## Change

`Initialize()` now fails closed when `mcpEnabled: false` and `clientId`
is empty, mirroring the existing guard used when `mcpEnabled: true`. The
validation call sites keep the original `idtoken.Validate` usage.

## Tests

- `Initialize()` rejects empty `clientId` in non-MCP mode.
- `GetClaimsFromHeader` with no token returns no claims.

`go test ./internal/auth/google/` and `go vet` pass.

## Note

Re-opens the fix from #3480 (labelled `priority: p1`, assigned to
@duwenxin99), which auto-closed when the original fork was deleted; the
CLA is now signed (check green). Happy to align with any in-flight
internal fix.

Co-authored-by: Juexin Wang <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

priority: p1 Important issue which blocks shipping the next release. Will be fixed prior to next release.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants