fix(fetch): keep the cookie header passed by the caller on redirects - #42841
Open
abhijeet sharma (thegoodengineer) wants to merge 1 commit into
Open
abhijeet sharma (thegoodengineer) wants to merge 1 commit into
abhijeet sharma (thegoodengineer) wants to merge 1 commit into
Conversation
An explicit cookie header from `headers` or `extraHTTPHeaders` wins over the context cookies on the first request, but was dropped before every redirect because the context cookies were merged into the same headers object and the two could not be told apart. The redirected request was sent with only the cookies the context had for the new URL, which is nothing for a standalone request context. Context cookies are now added to a per-request copy of the headers, so an explicit cookie header is carried over to same-origin redirects like every other header and dropped on cross-origin redirects together with authorization. Cookies set by a redirect response are applied to the explicit header for the next hop, so that route.fetch() through a login redirect still sends the new session cookie. Fixes microsoft#42840
Author
|
@microsoft-github-policy-service agree |
This branch has not been deployed
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.
Fixes #42840
APIRequestContexthonors an explicitcookieheader on the first request (headersandextraHTTPHeaderswin over the context cookie store), but dropped it unconditionally before following a redirect. Context cookies were merged into the same headers object, so the redirect code could not tell the caller's header apart from the ones added from the store and removed both. The redirected request went out with only whatever the store had for the new URL, which is nothing for a standalone request context. The documentation for theheadersoption promises that headers "will apply to the fetched request as well as any redirects initiated by it".Changes
options.headersnow only ever holds what the caller passed, so redirects carry an explicitcookieheader over like every other header.cookieheader is dropped together withauthorization, and the cookie store is consulted for the new origin, following fix(fetch): drop authorization and recompute client cert on cross-ori… #40547.cookieheader for the next hop (added when they match the next URL, replaced when the name is already present, removed when expired), the same way they are stored in the context. This keepsroute.fetch()correct: it forwards thecookieheader captured from the browser, and the session cookie set by a login redirect has to reach the redirect target.Requests without an explicit
cookieheader are unaffected: the store is still consulted for every hop.Tests
browsercontext-fetch.spec.ts: an explicit header is kept across two same-origin redirects while the store has a cookie for the host; cookies set by redirect responses are added, skipped on path mismatch, updated and expired on the following hops; an explicit header is dropped on a cross-origin redirect and the store cookie for that origin is used instead.global-fetch.spec.ts: acookiefromextraHTTPHeadersis kept on a same-origin redirect and dropped on a cross-origin one, mirroring the existing authorization test.page-request-intercept.spec.ts:route.fetch()through a redirect that sets a session cookie sends both the browser cookie and the new one to the redirect target.The three tests for explicit headers on same-origin redirects fail on
mainand pass with this change. The cross-origin test and theroute.fetch()test pass on both and pin down the behavior that must not change.Verified locally in Chromium, Firefox and WebKit with
browsercontext-fetch,global-fetch,page-request-interceptandpage-route;npm run tscand eslint pass.