github: stop cross-host redirects to prevent bearer token leakage#4189
Closed
evilgensec wants to merge 1 commit intogoogle:masterfrom
Closed
github: stop cross-host redirects to prevent bearer token leakage#4189evilgensec wants to merge 1 commit intogoogle:masterfrom
evilgensec wants to merge 1 commit intogoogle:masterfrom
Conversation
WithAuthToken wraps the Transport with a RoundTripper that unconditionally injects Authorization: Bearer TOKEN on every RoundTrip call, including intermediate calls that http.Client makes when following redirects. Go's http.Client strips the Authorization header before building a cross-host redirect request, but the wrapped Transport immediately re-adds it, forwarding the token to every host in the redirect chain. The GitHub API redirects certain endpoints to different hosts. Repository archive downloads are redirected to codeload.github.com and release asset downloads are redirected to objects.githubusercontent.com. Any application that calls an endpoint that redirects cross-host, or that accepts user-provided URLs, will leak its GitHub token to the redirect destination. Fix: add a CheckRedirect hook in WithAuthToken that returns http.ErrUseLastResponse when the redirect target host differs from the initial request host. This surfaces the 3xx to the caller instead of following it, preventing token injection into cross-host requests. The hook composes correctly with any CheckRedirect already set on the underlying http.Client. Same-host redirects continue to work as before. New tests: - cross-host redirect does not leak token: verifies 302 is returned and the redirect destination does not receive the Authorization header. - same-host redirect is followed: verifies backward compatibility for same-host redirects. Fixes google#3386
Collaborator
|
@evilgensec - I believe this issue was already resolved by #4171. |
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master #4189 +/- ##
==========================================
- Coverage 93.71% 93.70% -0.02%
==========================================
Files 209 209
Lines 19770 19778 +8
==========================================
+ Hits 18527 18532 +5
- Misses 1046 1048 +2
- Partials 197 198 +1 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
Author
|
Confirmed duplicate of #4171 — closing. Thanks for the pointer! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
WithAuthToken wraps the Transport with a RoundTripper that unconditionally
injects Authorization: Bearer TOKEN on every RoundTrip call, including
intermediate calls that http.Client makes when following redirects.
Go's http.Client strips the Authorization header before building a
cross-host redirect request, but the wrapped Transport immediately
re-adds it, forwarding the token to every host in the redirect chain.
The GitHub API redirects certain endpoints to different hosts. Repository
archive downloads are redirected to codeload.github.com and release
asset downloads are redirected to objects.githubusercontent.com. Any
application that calls an endpoint that redirects cross-host, or that
accepts user-provided URLs, will leak its GitHub token to the redirect
destination.
Fix: add a CheckRedirect hook in WithAuthToken that returns
http.ErrUseLastResponse when the redirect target host differs from the
initial request host. This surfaces the 3xx to the caller instead of
following it, preventing token injection into cross-host requests. The
hook composes correctly with any CheckRedirect already set on the
underlying http.Client.
Same-host redirects continue to work as before.
New tests:
the redirect destination does not receive the Authorization header.
same-host redirects.
Fixes #4190