Repository navigation
Keep token-file HTTP clients on same-host redirects - #7252
Conversation
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #7252 +/- ##
==========================================
+ Coverage 81.65% 81.69% +0.04%
==========================================
Files 904 904
Lines 91838 91840 +2
==========================================
+ Hits 74989 75030 +41
+ Misses 16844 16805 -39
Partials 5 5 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
HttpClientBuilder attaches a file-sourced bearer token through oauth2.Transport, which re-adds it on every redirect hop, so the stdlib's cross-domain Authorization stripping never applies. A discovery, JWKS, or introspection endpoint that redirected to another host would receive the token. Install SameHostRedirectPolicy on any client built with a token file. This covers the token validator's OIDC discovery and JWKS client and the RFC 7662 introspection provider, the only two callers. Co-Authored-By: Claude Opus 5.5 <[email protected]>
b3995e4 to
ca26ec3
Compare
jhrozek
left a comment
There was a problem hiding this comment.
LGTM, nice catch that oauth2.Transport re-adds the header on every hop, so the stdlib's cross-host stripping never kicks in. Putting it in the builder seems like the right spot. A few small things, none blocking:
thv run --jwks-auth-token-filegets this behaviour too, but only the operator arch doc mentions it. Maybe add a few words to the flag help and runtask docs?- The new sentence in 09-operator-architecture.md says redirects to another host fail. A different port on the same host is refused as well, so "another host or port" would be more accurate.
- The comment on
TestAuthTokenFileClientsRefuseCrossHostRedirectssays it covers JWKS, but only discovery and introspection run. JWKS is protected because the cache reuses the same client. Either add a JWKS case or drop "and JWKS" from the comment. - That test also reads
TOOLHIVE_SKIP_OIDC_DISCOVERYfrom the real environment, so it fails if someone has it exported. PassingWithEnvReaderwith a stub like the other tests in the file would avoid that.
Unrelated to this PR, but I noticed it while reading: when the RFC 7662 provider has both client credentials and a token file, oauth2.Transport overwrites the Basic auth header with the bearer token, so the introspection endpoint never sees the client ID and secret. Probably worth a separate fix.
Summary
--jwks-auth-token-file,jwksAuthTokenPath, or the service-account token used forkubernetesServiceAccountMCPOIDCConfigs),HttpClientBuilderattaches that token throughoauth2.Transport. The transport re-adds theAuthorizationheader on every round trip, including each redirect hop, so Go's built-in stripping ofAuthorizationon cross-domain redirects never applies. The client had noCheckRedirect, so an OIDC discovery, JWKS, or RFC 7662 introspection endpoint that answered with a redirect to another host would receive the token. Send KSA service account token only to the in-cluster API server #7248 limits that token to the in-cluster API server by checking the configuredjwksUrlor issuer, but that check does not cover redirects. This is hardening, not a vulnerability: the redirect has to come from the API server or another endpoint the operator configured.HttpClientBuilder.Buildinstallsnetworking.SameHostRedirectPolicy()whenever a token file is set. That covers both callers: the token validator's discovery and JWKS client (NewTokenValidator) and the RFC 7662 provider (NewRFC7662ProviderWithAuth). Clients built without a token file are unchanged.Type of change
Test plan
Ran
go test ./pkg/auth/ ./pkg/networking/and golangci-lint v2.15.0 on both packages (task lint-fixfails locally on Go 1.27 export-data skew). The new tests fail with theCheckRedirectline removed:TestBuild_TokenFileRestrictsRedirects: a cross-host redirect fails withErrRedirectRefusedand the target receives no request. A same-host redirect still arrives with the bearer token.TestAuthTokenFileClientsRefuseCrossHostRedirects: OIDC discovery and RFC 7662 introspection withAuthTokenFileset both fail withErrRedirectRefusedwhen the endpoint answers 307 to another host, and the target receives nothing. A 307 would also replay the introspection POST body, which contains the token being introspected.Does this introduce a user-facing change?
Yes, only for deployments that set an auth token file for OIDC/JWKS/introspection requests.
redirect refused. Token validation then fails as it would for an unreachable endpoint.A deployment whose discovery, JWKS, or introspection endpoint redirects to a different host must now be configured with the final URL. Documented in
docs/arch/09-operator-architecture.md.Special notes for reviewers
pkg/auth, so any future caller ofWithTokenFromFilegets it too.NewTokenValidatorandNewRFC7662ProviderWithAuthare the only current callers.host:port, matching every otherSameHostRedirectPolicyuser. A redirect to another port on the same host is refused.🤖 Generated with Claude Code