Repository navigation
Conversation
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]>
|
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. |
There was a problem hiding this comment.
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.
| // 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 |
There was a problem hiding this comment.
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)
}## 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]>
Summary
In non-MCP mode the Google auth service validates ID tokens in
GetClaimsFromHeaderwith:When
clientIdis left empty,idtoken.Validateis called with an empty audience. The library only enforces the audience claim when the audience argument is non-empty:So a deployment that configures a
googleauth service withoutclientId(withmcpEnabled: 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 (audienceorclientIdrequired whenmcpEnabledis true, added in #3450), andValidateMCPAuthrefuses to validate when no audience is resolvable. The non-MCP branch had no equivalent guard.The reference docs already document
clientIdas "Required for validating ID tokens in non-MCP web apps (GetClaimsFromHeader)", but the code never enforced it — a code-vs-docs divergence.Change
Initialize(): whenmcpEnabledis false, return an error ifclientIdis empty (fail-closed config validation, mirroring the existingmcpEnabledguard).validateIDToken = idtoken.Validateindirection 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: assertsInitialize()fails on emptyclientIdin non-MCP mode (the previously-passing "neither clientId nor audience, mcpEnabled false" case now expects an error).TestGetClaimsFromHeader_AudienceBinding: with a configuredclientId, a token whoseaudmatches is accepted and a token whoseauddiffers is rejected.TestGetClaimsFromHeader_NoToken: a missing token header is a no-op (returnsnil, nil).go test ./internal/auth/google/...passes;gofmt -landgo vetare clean.Compatibility
This is a hardening / fail-closed change, not a report of active exploitation. It is a behavioural change: a
googleauth service configured withmcpEnabled: falseand noclientIdwas 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.