Skip to content

transport: do not re-attach bearer token after cross-host redirect - #2349

Merged
Subserial merged 1 commit into
google:mainfrom
evilgensec:fix-cross-host-bearer-token-leak
Jun 17, 2026
Merged

Subserial merged 1 commit into
google:mainfrom
evilgensec:fix-cross-host-bearer-token-leak

Conversation

@evilgensec

Copy link
Copy Markdown
Contributor

Summary

bearerTransport.RoundTrip re-attaches the registry bearer token on its post-401 retry without checking that the request is still talking to the registry we authenticated against.

PR #2337 replaced the post-bt.refresh() return sendRequest() with an unconditional in.Header.Set("Authorization", ...) + bt.inner.RoundTrip(in), intentionally bypassing the matchesHost guard. That guard exists to keep the Authorization header from being forwarded across an http.Client-level redirect to a different host.

With the guard bypassed, the credential leaks across a cross-host redirect:

  1. A pull is issued against a registry; the transport seeds a bearer token from the trusted realm.
  2. The registry answers the manifest/blob GET with a 302 to a different host. checkRedirectSSRF (pkg/v1/remote/fetcher.go) allows it for any public host or DNS name (it only blocks private/link-local IP literals).
  3. RoundTrip re-runs for the redirected host. The first sendRequest() correctly omits Authorization (host mismatch). The redirected host replies 401 with a Bearer challenge.
  4. RoundTrip refreshes the token from the trusted realm and, after fix(transport): apply refreshed bearer token after cross-host redirect #2337, sets Authorization: Bearer <token> unconditionally and sends it to the redirected host.

A registry the client did not authenticate against (a malicious/compromised peer, or a MITM on insecure transport) thus receives the operator's registry token.

Fix

Gate the re-attach on matchesHost: only apply the refreshed token when the request is still on the registry host we logged in to; otherwise fall back to sendRequest() (which omits the credential). This preserves the #2333 behavior for same-host 401 retries (e.g. token expiry mid-session) and closes the cross-host credential leak.

Changes

Test plan

  • go test ./pkg/v1/remote/... passes (existing tests + both new tests).
  • Reverting the guard makes TestBearerTokenNotLeakedOnCrossHostChallenge fail (the credential is forwarded), confirming the test catches the regression.

PR google#2337 made bearerTransport.RoundTrip set the Authorization header
unconditionally on the post-401 retry, bypassing the matchesHost guard.
That guard exists to stop the credential being forwarded across an
http.Client-level redirect to a different host. With it bypassed, a
malicious or compromised registry can 302 a manifest/blob fetch to an
attacker-controlled host, answer the follow-up request with a Bearer
challenge, and harvest the operator's registry token: the client
refreshes the token from the trusted realm and then sends it to the
redirected host.

Gate the re-attach on matchesHost so the refreshed token is only applied
when the request is still talking to the registry we authenticated
against; for a cross-host request fall back to sendRequest(), which omits
the credential. This preserves the google#2333 fix for same-host 401 retries
(e.g. mid-session token expiry) while closing the credential leak.

Add a regression test for the same-host retry and a test asserting the
token is not leaked to a cross-host challenger.
@codecov-commenter

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 57.05%. Comparing base (09fe1e5) to head (37a9ca7).

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #2349   +/-   ##
=======================================
  Coverage   57.04%   57.05%           
=======================================
  Files         166      166           
  Lines       11445    11447    +2     
=======================================
+ Hits         6529     6531    +2     
  Misses       4140     4140           
  Partials      776      776           

☔ 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.

@Subserial
Subserial merged commit da61d86 into google:main Jun 17, 2026
21 checks passed
@evilgensec

Copy link
Copy Markdown
Contributor Author

Hi @Subserial , thanks again for merging this.

A quick clarification for my records: I reported this cross-host bearer-token regression through the Google OSS VRP, and the response stated it had been "identified and fixed internally by the maintainers via PR #2349" before my report. Since #2349 is the PR I opened (and you merged it about six minutes later), I wanted to check with you directly: was this regression already being tracked or fixed internally before I opened #2349, or was this PR the fix that resolved it? I just want to represent the timeline accurately. Thanks either way, and thanks for the fast review.

@amitz-jfrog

Copy link
Copy Markdown

@evilgensec Thanks for the contribution.
Just to make sure, does this PR reverts the resolution of issue #2333?
What registry does it match to? My issue was involving cross-host redirect. But a specific case in which the same host generated the token and later did not receive it.
Does it make sense?

@evilgensec

Copy link
Copy Markdown
Contributor Author

@amitz-jfrog you're right, it does. #2349 re-gates the retry on matchesHost, so the cross-host case in #2333 no longer re-applies the token and regresses.

I couldn't keep #2337's unconditional re-attach: it re-applies bt.realm's token (the original registry's credential) to whatever host the redirect lands on, so a malicious or compromised registry can 302 to an attacker host, return a Bearer challenge, and harvest the token. That's what #2349 closed.

The safe fix for your gateway case is to treat the redirected host's 401 as a fresh challenge: fetch a token from that host's own realm and apply it there, instead of forwarding the original registry's token cross-host.

@amitz-jfrog

Copy link
Copy Markdown

@evilgensec I see, so how do you suggest opening the issue again in a way that will be understandable and not trigger a vulnerability?
My use-case is a constant redirect between host A to B.
So the flow is, as I understand it:

Docker calls A ->(redirect) B. Gets 401 from B, to B token endpoint
Docker calls B directly and gets the token.
Docker calls host A for the image since it now thinks it got the token. ->(redirect) B, does not attach the token received from B. 
Fails.

I would assume that there is no risk in attached the host B token when calling host B.
Also, this use-case is working with other docker clients (when using docker-ce for example)

@amitz-jfrog

Copy link
Copy Markdown

What do you think?

@evilgensec

Copy link
Copy Markdown
Contributor Author

@amitz-jfrog You're right, and that's the key distinction: attaching host B's token to host B is safe. The leak #2349 closed was #2337 attaching host A's token to B.

Why a simple "re-attach on same host" won't fix your flow: bearerTransport is built once from the original registry's challenge, so realm/service/creds stay pinned to A. On B's 401, refresh() re-mints a token from A's realm with A's creds (B's challenge realm/service are ignored, only scope is read). So the token in hand is always A's. Re-attaching it to B is the cross-host forward we removed.

The fix for #2333 that doesn't reintroduce the leak is a per-host token exchange: when the 401 comes from a host different from the one we authenticated to, treat it as a fresh auth context. Parse that host's realm/service/scope, resolve creds for that host from the keychain, exchange a token against its own realm, and attach it only to that host. A's credential never reaches B; B's token never leaves B. That's the per-host model docker-ce appears to use.

For reopening, frame it as a feature request, not a revert:

amitzig added a commit to amitzig/go-containerregistry that referenced this pull request Jun 30, 2026
When a registry request is redirected to a different host that issues
its own WWW-Authenticate Bearer challenge, perform a fresh token
exchange from that host's own realm rather than falling back silently
to no credentials.

The original registry's token is never forwarded cross-host (the
security invariant from google#2349 is preserved). Anonymous auth is used
for the redirected host's token exchange; its token is applied only
to the retry against that host.

Fixes google#2359
@amitz-jfrog

amitz-jfrog commented Jun 30, 2026 •

Copy link
Copy Markdown

@evilgensec thank you very much. 🙏
Is this what you had in mind?

Issue #2359
PR #2360

@amitz-jfrog

Copy link
Copy Markdown

@evilgensec following up on that, this PR reverted the other issue's fix.
I have created a more specific fix, but I am not sure how to get it reviewed.
Can you please assist?

@evilgensec

Copy link
Copy Markdown
Contributor Author

@amitz-jfrog Reviewed it. Correct approach, and it holds the #2349 invariant: handleCrossHostChallenge exchanges with authn.Anonymous and fromChallenge still SSRF-validates the redirected host's realm, so the original registry's token never reaches host B. Restores #2333 without reopening the leak.

Two tightenings:

  • Move bt.refresh() into the same-host branch. On the cross-host path it mints an original-registry token that gets discarded, and pollutes shared bt.scopes with B's scope.

  • Note in the description that the exchange is anonymous-only (public token endpoints); auth-gated endpoints 401 and fall through to no creds, keychain support is a follow-up. Pre-empts the obvious review question.

CI is green and the diff is small. Request review from @Subserial , who merged #2349 and owns this path. Framing: "restores #2333 while preserving the #2349 no-cross-host-forward invariant."

amitzig added a commit to amitzig/go-containerregistry that referenced this pull request Jul 12, 2026
When a registry request is redirected to a different host that issues
its own WWW-Authenticate Bearer challenge, perform a fresh token
exchange from that host's own realm rather than falling back silently
to no credentials.

The original registry's token is never forwarded cross-host (the
security invariant from google#2349 is preserved). Anonymous auth is used
for the redirected host's token exchange; its token is applied only
to the retry against that host.

Fixes google#2359
Subserial pushed a commit that referenced this pull request Jul 29, 2026
When a registry request is redirected to a different host that issues
its own WWW-Authenticate Bearer challenge, perform a fresh token
exchange from that host's own realm rather than falling back silently
to no credentials.

The original registry's token is never forwarded cross-host (the
security invariant from #2349 is preserved). Anonymous auth is used
for the redirected host's token exchange; its token is applied only
to the retry against that host.

Fixes #2359
social4hyq pushed a commit to social4hyq/homebrew-core that referenced this pull request Sep 20, 2026
crane 0.21.7

Created-by: HarmonybrewBot
Commit-by: HarmonybrewBot
Merged-by: HarmonybrewBot
Description: Created by `brew bump`

---

Created with `brew bump-formula-pr`.<details>
  <summary>release notes</summary>
  <pre>## What's Changed
* tarball: return error instead of panicking on missing rootfs.diff_ids by @iahsanGill in google/go-containerregistry#2304
* gcrane: honor --platform flag in copy by @iahsanGill in google/go-containerregistry#2307
* mutate: verify layer digests in Extract and Time by @momenashrafff in google/go-containerregistry#2303
* tarball: close layer readers during Write by @nandbhat in google/go-containerregistry#2308
* build(deps): bump the actions group across 1 directory with 2 updates by @dependabot[bot] in google/go-containerregistry#2311
* build(deps): bump github.com/docker/cli from 29.4.3+incompatible to 29.5.2+incompatible in the go-deps group across 1 directory by @dependabot[bot] in google/go-containerregistry#2312
* BUGFIX: Fail with error when read exceeds maximum by @inteon in google/go-containerregistry#2328
* build(deps): bump the actions group across 1 directory with 2 updates by @dependabot[bot] in google/go-containerregistry#2327
* fix(name): anchor loopback registry detection by @rohan-patnaik in google/go-containerregistry#2314
* Reject symlinks in OCI layout blobs by @mosskappa in google/go-containerregistry#2306
* fix(crane): avoid creating export tar on pull failure by @Haihan-Jiang in google/go-containerregistry#2318
* feat(kubernetes): allow ignoring pull secrets by @rohan-patnaik in google/go-containerregistry#2315
* fix(name): preserve localhost registry references by @rohan-patnaik in google/go-containerregistry#2316
* pkg/registry: export ErrNotFound by @malt3 in google/go-containerregistry#2176
* pkg/registry: export RedirectError by @malt3 in google/go-containerregistry#2177
* build(deps): bump the go-deps group across 1 directory with 2 updates by @dependabot[bot] in google/go-containerregistry#2343
* build(deps): bump the actions group across 1 directory with 2 updates by @dependabot[bot] in google/go-containerregistry#2344
* fix: prevent SSRF in google.List() pagination by @tufstraka in google/go-containerregistry#2332
* internal/gzip: fix goroutine leak in ReadCloserLevel by @amarkdotdev in google/go-containerregistry#2347
* fix(transport): apply refreshed bearer token after cross-host redirect by @64johnlee in google/go-containerregistry#2337
* build(deps): bump the go-deps group across 3 directories with 4 updates by @dependabot[bot] in google/go-containerregistry#2348
* fix(tarball): normalize paths when matching files by @bstoll in google/go-containerregistry#2334
* transport: do not re-attach bearer token after cross-host redirect by @evilgensec in google/go-containerregistry#2349
* Bump CI go version to 1.26.4 by @Subserial in google/go-containerregistry#2350

## New Contributors
* @momenashrafff made their first contribution in google/go-containerregistry#2303
* @nandbhat made their first contribution in google/go-containerregistry#2308
* @inteon made their first contribution in google/go-containerregistry#2328
* @rohan-patnaik made their first contribution in google/go-containerregistry#2314
* @mosskappa made their first contribution in google/go-containerregistry#2306
* @Haihan-Jiang made their first contribution in google/go-containerregistry#2318
* @tufstraka made their first contribution in google/go-containerregistry#2332
* @amarkdotdev made their first contribution in google/go-containerregistry#2347
* @64johnlee made their first contribution in google/go-containerregistry#2337
* @bstoll made their first contribution in google/go-containerregistry#2334

**Full Changelog**: https://github.com/google/go-containerregistry/compare/v0.21.6...v0.21.7</pre>
  <p>View the full release notes at <a href="https://github.com/google/go-containerregistry/releases/tag/v0.21.7">https://github.com/google/go-containerregistry/releases/tag/v0.21.7</a>.</p>
</details>
<hr>

See merge request: Harmonybrew/homebrew-core!11861
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants