Skip to content

Keep token-file HTTP clients on same-host redirects - #7252

Merged
rdimitrov merged 1 commit into
mainfrom
rdimitrov/token-file-same-host-redirects
Nov 23, 2026
Merged

rdimitrov merged 1 commit into
mainfrom
rdimitrov/token-file-same-host-redirects

Conversation

@rdimitrov

@rdimitrov rdimitrov commented Nov 23, 2026 •

Copy link
Copy Markdown
Member

Summary

  • Why: when a token validator is configured with an auth token file (--jwks-auth-token-file, jwksAuthTokenPath, or the service-account token used for kubernetesServiceAccount MCPOIDCConfigs), HttpClientBuilder attaches that token through oauth2.Transport. The transport re-adds the Authorization header on every round trip, including each redirect hop, so Go's built-in stripping of Authorization on cross-domain redirects never applies. The client had no CheckRedirect, 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 configured jwksUrl or 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.
  • What: HttpClientBuilder.Build installs networking.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

  • Bug fix

Test plan

  • Manual testing (describe below)

Ran go test ./pkg/auth/ ./pkg/networking/ and golangci-lint v2.15.0 on both packages (task lint-fix fails locally on Go 1.27 export-data skew). The new tests fail with the CheckRedirect line removed:

  • TestBuild_TokenFileRestrictsRedirects: a cross-host redirect fails with ErrRedirectRefused and the target receives no request. A same-host redirect still arrives with the bearer token.
  • TestAuthTokenFileClientsRefuseCrossHostRedirects: OIDC discovery and RFC 7662 introspection with AuthTokenFile set both fail with ErrRedirectRefused when 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.

  • Before: discovery, JWKS, and introspection requests followed any redirect, up to Go's default of 10 hops, and sent the bearer token to every host in the chain.
  • After: they follow only redirects to the same host and port, at most 10 hops, and never from HTTPS to HTTP. Any other redirect fails the request with 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

  • The policy is applied in the builder, not in pkg/auth, so any future caller of WithTokenFromFile gets it too. NewTokenValidator and NewRFC7662ProviderWithAuth are the only current callers.
  • Same-host comparison uses host:port, matching every other SameHostRedirectPolicy user. A redirect to another port on the same host is refused.

🤖 Generated with Claude Code

@github-actions github-actions Bot added the size/S Small PR: 100-299 lines changed label Nov 23, 2026
@codecov

codecov Bot commented Nov 23, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 81.69%. Comparing base (48a829b) to head (ca26ec3).

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.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

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]>
@rdimitrov
rdimitrov force-pushed the rdimitrov/token-file-same-host-redirects branch from b3995e4 to ca26ec3 Compare November 23, 2026 16:32
@github-actions github-actions Bot added size/S Small PR: 100-299 lines changed and removed size/S Small PR: 100-299 lines changed labels Nov 23, 2026

@jhrozek jhrozek 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.

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-file gets this behaviour too, but only the operator arch doc mentions it. Maybe add a few words to the flag help and run task 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 TestAuthTokenFileClientsRefuseCrossHostRedirects says 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_DISCOVERY from the real environment, so it fails if someone has it exported. Passing WithEnvReader with 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.

@rdimitrov
rdimitrov merged commit 4475f45 into main Nov 23, 2026
47 checks passed
@rdimitrov
rdimitrov deleted the rdimitrov/token-file-same-host-redirects branch November 23, 2026 16:53
@github-actions github-actions Bot mentioned this pull request Nov 24, 2026
2 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size/S Small PR: 100-299 lines changed

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants