Repository navigation
transport: do not re-attach bearer token after cross-host redirect - #2349
Conversation
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 Report✅ All modified and coverable lines are covered by tests. 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. 🚀 New features to boost your workflow:
|
|
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. |
|
@evilgensec Thanks for the contribution. |
|
@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. |
|
@evilgensec I see, so how do you suggest opening the issue again in a way that will be understandable and not trigger a vulnerability? I would assume that there is no risk in attached the host B token when calling host B. |
|
What do you think? |
|
@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:
|
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
|
@evilgensec thank you very much. 🙏 |
|
@evilgensec following up on that, this PR reverted the other issue's fix. |
|
@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:
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." |
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
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
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
Summary
bearerTransport.RoundTripre-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 unconditionalin.Header.Set("Authorization", ...)+bt.inner.RoundTrip(in), intentionally bypassing thematchesHostguard. That guard exists to keep theAuthorizationheader from being forwarded across anhttp.Client-level redirect to a different host.With the guard bypassed, the credential leaks across a cross-host redirect:
302to 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).RoundTripre-runs for the redirected host. The firstsendRequest()correctly omitsAuthorization(host mismatch). The redirected host replies401with a Bearer challenge.RoundTriprefreshes the token from the trusted realm and, after fix(transport): apply refreshed bearer token after cross-host redirect #2337, setsAuthorization: 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 tosendRequest()(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
pkg/v1/remote/transport/bearer.go— guard the post-refresh re-attach withmatchesHost.pkg/v1/remote/transport/bearer_test.go—TestBearerTokenReappliedOnSameHostChallenge(the ggcr: Bearer token from redirected registry challenge is fetched but not applied to redirected registry host #2333 same-host retry still works) andTestBearerTokenNotLeakedOnCrossHostChallenge(token is not sent to a cross-host challenger).Test plan
go test ./pkg/v1/remote/...passes (existing tests + both new tests).TestBearerTokenNotLeakedOnCrossHostChallengefail (the credential is forwarded), confirming the test catches the regression.