Repository navigation
Stop the redirect check before the cross-origin destination - #213
Conversation
f7f5403 to
fcd94e8
Compare
|
🤖 Review pass on this branch, run by an agent on Rosa's account. Two commits pushed on top of yours; everything else below is left for you to decide. Base branch
Retargeting to Credentials on a redirect, every hopWith
Hostile Pushed
Worth deciding before merge1. Multi-hop is the regression to weigh. 2. 3. The origin comparison widens behaviour as well as tightening it. 4. Nits. An empty 5. The description and commit message describe the pre-#211 code ("attached the WebView's cookies as a static Cookie header and issued the request with a redirect-following client"). On this base that isn't what the fetch does any more. I left your commit message alone. 6. #215 overlaps. It adds One nice side effect nobody claimed: |
|
Thanks, looks good but I'll re-build the changes on #215 when it's merged. |
7791f59 to
d2e3ff8
Compare
0a7efdb to
037e768
Compare
There was a problem hiding this comment.
🟢 Approval recommended
No active review issues remain for the redirect-verification change.
No open issues 🎉
What changed in this PR
This PR prevents the native redirect-verification fetch from forwarding credentials to redirect destinations by inspecting the first redirect response without following it.
Changes:
- Disables redirect following, resolves
Locationheaders, compares full origins, and closes verification responses. - Updates
Sessionto handle cross-origin redirects without requiring a successful final response. - Adds tests for credential containment, origin detection, relative locations, non-redirect responses, and connection reuse.
| File | Description |
|---|---|
core/src/test/kotlin/dev/hotwire/core/turbo/http/HttpRepositoryTest.kt |
Adds redirect-verification and response-cleanup tests. |
core/src/test/kotlin/dev/hotwire/core/turbo/BaseRepositoryTest.kt |
Configures the test client with WebView cookies. |
core/src/main/kotlin/dev/hotwire/core/turbo/session/Session.kt |
Consumes redirect metadata directly when handling failed visits. |
core/src/main/kotlin/dev/hotwire/core/turbo/http/HttpRepository.kt |
Inspects redirects without following them and returns full-origin comparison results. |
Review effort: Balanced
The native redirect-verification fetch attached the WebView's cookies as a static Cookie header and issued the request with a redirect-following client. On a cross-origin redirect, OkHttp forwards that caller-set Cookie header to the destination (it drops Authorization on a host change, but not a caller-set Cookie), delivering first-party cookies to a different origin. The fetch only needs to detect a cross-origin redirect, not follow it with credentials. Disable redirect following on the verification client and resolve the destination from the Location header, comparing the full origin (scheme, host, and port) so a scheme downgrade or port change counts as cross-origin. No credential-bearing request is ever sent to a redirect destination. Session.visitRequestFailedWithNonHttpStatusCode no longer gates on response.isSuccessful (which only held when redirects were followed to a 2xx); detecting the cross-origin redirect response is sufficient to propose the cross-origin redirect visit. Adds HttpRepositoryTest (MockWebServer) covering the no-leak invariant, cross/same-origin detection, and relative Location resolution.
Keep the reason the fetch doesn't follow redirects and drop the rest: the same-origin comparison reads off its own body, and the guarantee it supports is stated once where the client is built. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01DyPGJuffiyfWW9113PNxHG
Both MockWebServer instances bind the same loopback host, so the existing cross-origin case differs from the first-party origin only by port and exercises just the port term of the origin comparison. Add a case with an absolute off-host Location, which needs no second server precisely because the redirect isn't followed. Also trim the test's comments, keeping the two that say why a request count is asserted. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01DyPGJuffiyfWW9113PNxHG
Use the shared origin comparison from the trust policy instead of a private HttpUrl extension. Both compare scheme, host, and effective port, and an HttpUrl is always http(s), so the result is unchanged.
The response was never closed, so a redirect with a body kept its connection out of the pool. Session no longer reads the response now that the fetch doesn't follow redirects, so return the redirect and close the response with use.
The timeouts can't change, and a lazily built client would also see the cache. What an eager field would miss is the caching client Session installs after constructing this repository.
037e768 to
b1bfaa5
Compare
The verification fetch only read the first Location, so a chain like /account -> /login -> https://idp.example/auth looked same-origin and the visit failed with an error. That's the chain Turbo hands to native: the browser's fetch follows the same-origin hops itself and only stops at the cross-origin one. The fetch now follows same-origin hops by hand, up to 20 like OkHttp, and returns the first cross-origin Location without requesting it. An empty or non-http Location ends the check. HttpRepository returns that location directly, since Session never read anything else. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
What
The redirect check in
HttpRepositoryno longer requests the cross-origin destination. It follows same-origin hops by hand and returns the first cross-originLocationwithout requesting it. ThenSessionproposes that location as a cross-origin redirect visit.Why
When Turbo's
fetchhits a cross-origin redirect, the browser blocks it, and Turbo callsvisitRequestFailedWithNonHttpStatusCode. Native then fetches the location again to find out where the redirect goes.Since #211, that fetch only sends each host its own cookies, so cookies don't leak to the destination anymore. But the fetch still followed the whole chain, so the destination got a request from native before the WebView visited it. That's a problem when the destination URL carries a single-use token, like an OAuth or SAML handoff. Native's request uses up the token, and then the WebView's visit fails.
The fetch can't stop at the first hop, though. The usual chain is
/account→/login→https://idp.example/auth: the browser follows the same-origin hops itself and only stops at the cross-origin one. A check that read just the firstLocationwould see/loginas same-origin and show an error screen instead of the IdP.How
HttpRepository.fetchCrossOriginRedirectfollows them with redirects turned off on the client, up to 20 hops (OkHttp's own limit). Each hop gets its own host's cookies fromWebViewCookieJar.Locationwith a different scheme, host or port, without requesting it.Locationor a non-httpLocation(javascript:,intent:) returns null, and the visit fails with the usual error.Sessionreads the returned location directly. It no longer requires a 2xx at the end of the chain, because the fetch never requests the destination.Behavior changes
https://app→http://appandhttps://app→https://app:8443now count as cross-origin redirects.navigator.route()checks those locations against the origin trust policy, so an untrusted origin opens in a browser tab, not in the app.Testing
HttpRepositoryTest(MockWebServer) covers:LocationLocationand ajavascript:locationSessionTestcovers the multi-hop case end to end: a same-origin hop, then a cross-origin redirect, proposes the cross-origin visit.With the hop limit set to 1, which is first-hop-only behavior, the 5 multi-hop tests fail.
:core:testDebugUnitTestpasses (208 tests).🤖 Generated with Claude Code