Skip to content

fix(transport): apply refreshed bearer token after cross-host redirect - #2337

Merged
Subserial merged 2 commits into
google:mainfrom
64johnlee:fix/bearer-token-redirect-2333
Jun 17, 2026
Merged

Subserial merged 2 commits into
google:mainfrom
64johnlee:fix/bearer-token-redirect-2333

Conversation

@64johnlee

Copy link
Copy Markdown
Contributor

Summary

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 for cross-host requests. The previous code called sendRequest() again after bt.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 from bt.bearer directly and set the Authorization header unconditionally before retrying, bypassing the matchesHost guard (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 — replace return sendRequest() after bt.refresh() with a direct in.Header.Set("Authorization", ...) + bt.inner.RoundTrip(in)
  • pkg/v1/remote/transport/bearer_test.go — add TestBearerTokenAppliedAfterCrossHostRedirect regression test: sets up a registry server whose host differs from bt.registry, verifies that after a 401 challenge the retry succeeds with the fresh token applied

Test plan

  • go test ./pkg/v1/remote/transport/... passes (all existing tests + new regression test)
  • New test TestBearerTokenAppliedAfterCrossHostRedirect reproduces the bug on the old code path and passes on the fix

@google-cla

google-cla Bot commented Jun 7, 2026

Copy link
Copy Markdown

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.

@amitz-jfrog

Copy link
Copy Markdown

@64johnlee just saw this PR, and wanted to ask what is the status?

Thank you for contributing.

@64johnlee

Copy link
Copy Markdown
Contributor Author

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.

@amitz-jfrog

Copy link
Copy Markdown

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

Co-Authored-By: Claude Sonnet 4.6 noreply@anthropic.com

image

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
@64johnlee
64johnlee force-pushed the fix/bearer-token-redirect-2333 branch from 878a5dd to 4c946cb Compare June 10, 2026 14:06
@64johnlee

Copy link
Copy Markdown
Contributor Author

Thanks for catching that @amitz-jfrog! The Co-Authored-By: noreply@anthropic.com line has been removed from the commit message — the CLA check should now pass. The fix itself is unchanged.

@amitz-jfrog

Copy link
Copy Markdown

@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-commenter

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 57.05%. Comparing base (02649ea) to head (973019f).

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

@64johnlee

Copy link
Copy Markdown
Contributor Author

Hi — following up on this PR. The CLA co-author issue raised by @amitz-jfrog has been resolved (no noreply@anthropic.com trailer in the current commits). The branch is up to date with main and has been approved by @Subserial. Would appreciate a maintainer review/merge when you get a chance. Thanks!

@Subserial
Subserial merged commit 97a8a17 into google:main Jun 17, 2026
21 checks passed
Subserial pushed a commit that referenced this pull request Jun 17, 2026
…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.
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.

ggcr: Bearer token from redirected registry challenge is fetched but not applied to redirected registry host

4 participants