fix(auth/httptransport): prevent bearer token leakage on cross-host redirects#14547
Open
evilgensec wants to merge 2 commits into
Open
fix(auth/httptransport): prevent bearer token leakage on cross-host redirects#14547evilgensec wants to merge 2 commits into
evilgensec wants to merge 2 commits into
Conversation
Contributor
There was a problem hiding this comment.
Code Review
This pull request introduces security enhancements to prevent bearer token leakage during cross-host redirects in the authTransport and adds comprehensive tests for these scenarios. Additionally, it limits the size of error bodies read in the storage client to 64 KiB to prevent potential out-of-memory issues. Feedback was provided to use case-insensitive host comparison and to remove an unnecessary request clone in the redirect handling logic.
…edirects authTransport.RoundTrip unconditionally injects the Authorization header on every call, including intermediate calls http.Client makes when following redirects. Go's http.Client strips Authorization before building a cross-host redirect request, but authTransport immediately re-adds the token, forwarding it to every host in the redirect chain. Any code using NewClient or AddAuthorizationMiddleware that follows a cross-host redirect (e.g. a webhook caller, an API proxy, or code that accepts user-supplied URLs) leaks its OAuth bearer token to the redirect destination. An attacker who controls a redirect via an open redirect on a Google API endpoint or a user-controlled URL can steal the token. Fix: check req.Response in RoundTrip. When req.Response is non-nil, this call is a redirect. If the current URL host differs from the previous hop's host, skip auth injection and delegate directly to the base transport. Adds two tests: one verifying tokens are not forwarded to a different host, and one verifying same-host redirects continue to be authorized.
16e548c to
0ee17f0
Compare
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
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.
Problem
authTransport.RoundTripunconditionally injects theAuthorizationheader on every call, including the intermediate callshttp.Clientmakes when following redirects.Go's
http.Clientstrips theAuthorizationheader before building a cross-host redirect request (per the standard library'sshouldCopyHeaderOnRedirectlogic), butauthTransportimmediately re-adds the bearer token — forwarding it to every host in the redirect chain.Reproduction sketch
Any application using
NewClientorAddAuthorizationMiddlewarethat follows a cross-host redirect (a webhook caller, an API proxy, or code that accepts user-provided URLs) will forward its OAuth bearer token to the redirect destination. An attacker who controls a redirect via an open redirect on a Google API endpoint or a user-controlled URL can steal the token.Note: the same class of bug is being fixed upstream in
golang.org/x/oauth2(PR #804); this is an independent fix for thecloud.google.com/go/authtransport layer.Fix
Check
req.ResponseinRoundTrip. Whenreq.Responseis non-nil, the call is a redirect. If the current URL's host differs from the previous hop's host, skip auth injection and delegate directly to the base transport.Same-host redirects continue to carry the bearer token as before.
Tests
TestAuthTransport_CrossHostRedirectDoesNotLeakToken: verifies the token is NOT forwarded to a different-host redirect destination.TestAuthTransport_SameHostRedirectIsAuthorized: verifies same-host redirects remain fully authorized.