Repository navigation
fix(transport): apply refreshed bearer token after cross-host redirect - #2337
Conversation
|
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. |
|
@64johnlee just saw this PR, and wanted to ask what is the status? Thank you for contributing. |
|
Hi @amitz-jfrog — thanks for checking in! The CLA is now signed (completed June 7), so the PR should be unblocked for review. Quick summary of the fix: when a bearer-token-authenticated request gets redirected to a different host, the transport was reusing the stale token from the original host rather than refreshing credentials for the redirect target. This PR makes the transport fetch a fresh bearer token after a cross-host redirect, so auth stays valid across the redirect chain. Happy to rebase or address any review feedback whenever the maintainers have a chance to look at it. |
|
@64johnlee Looks like noreply@anthropic.com is marked as a co-author for this PR, and therefore Google's CLA bot will not advance this.
|
When an http.Client-level redirect sends a request to a host that differs from bt.registry, the sendRequest() closure skips setting the Authorization header (its matchesHost guard returns false). The previous code called sendRequest() again after bt.refresh(), so the freshly fetched token was never applied to the retried request and the client received a second 401 instead of succeeding. Fix by reading the token directly from bt.bearer after a successful refresh and setting the Authorization header unconditionally before retrying, bypassing the matchesHost guard that is only needed to prevent header forwarding on client-visible redirects. Closes google#2333
878a5dd to
4c946cb
Compare
|
Thanks for catching that @amitz-jfrog! The |
|
@tufstraka @Subserial - Hey, saw that you recently contributed to the main branch, how can we get a review on this PR? Thanks in advance |
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #2337 +/- ##
==========================================
+ Coverage 57.03% 57.05% +0.01%
==========================================
Files 166 166
Lines 11442 11446 +4
==========================================
+ Hits 6526 6530 +4
Misses 4140 4140
Partials 776 776 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
|
Hi — following up on this PR. The CLA co-author issue raised by @amitz-jfrog has been resolved (no |
…2349) PR #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 #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.
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
When an
http.Client-level redirect sends a request to a host that differs frombt.registry, thesendRequest()closure skips setting theAuthorizationheader — itsmatchesHostguard returns false for cross-host requests. The previous code calledsendRequest()again afterbt.refresh(), so the freshly fetched token was never applied to the retry and the client received a second 401.Fix: after a successful
bt.refresh(), read the token frombt.bearerdirectly and set theAuthorizationheader unconditionally before retrying, bypassing thematchesHostguard (which exists to prevent header forwarding on client-visible redirects, not to gate the post-challenge retry).Closes #2333
Changes
pkg/v1/remote/transport/bearer.go— replacereturn sendRequest()afterbt.refresh()with a directin.Header.Set("Authorization", ...)+bt.inner.RoundTrip(in)pkg/v1/remote/transport/bearer_test.go— addTestBearerTokenAppliedAfterCrossHostRedirectregression test: sets up a registry server whose host differs frombt.registry, verifies that after a 401 challenge the retry succeeds with the fresh token appliedTest plan
go test ./pkg/v1/remote/transport/...passes (all existing tests + new regression test)TestBearerTokenAppliedAfterCrossHostRedirectreproduces the bug on the old code path and passes on the fix