Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use request's current url as base URL in redirects #633

Closed
wants to merge 1 commit into from

Conversation

annevk
Copy link
Member

@annevk annevk commented Nov 20, 2017

As the response's url list is not set at this point using that doesn not work. We could potentially set it earlier, but that brings along other complications that this setup avoid.

This also returns opaque redirect responses earlier, to avoid having them processed twice, which was simply wrong (and would become extra wrong here).

Fixes #631.


Preview | Diff

@wanderview
Copy link
Member

LGTM. Is this covered by existing WPT tests? I think it probably is.

@annevk
Copy link
Member Author

annevk commented Nov 20, 2017

I'm not sure about the various scenarios involving service workers.

@harrishancock
Copy link
Contributor

FWIW, looks like a good fix to me.

@jakearchibald jakearchibald self-requested a review November 21, 2017 11:15
Copy link
Collaborator

@jakearchibald jakearchibald left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@annevk
Copy link
Member Author

annevk commented Nov 21, 2017

@jakearchibald do you happen to know if we have opaqueredirect tets?

@wanderview
Copy link
Member

There is also a fetch API test here:

https://searchfox.org/mozilla-central/source/testing/web-platform/tests/service-workers/service-worker/fetch-event-redirect.https.html

@annevk
Copy link
Member Author

annevk commented Nov 23, 2017

To test this I think what we need is this:

  1. Attempt to navigate to /.
  2. The server returns a redirect with Location: x.
  3. That opaque redirect is stored in the Cache API.
  4. You then attempt to navigate to /y/.
  5. The service worker returns the opaque redirect.
  6. You end up at /x.

Does that make sense? If someone could do this for me that'd be great, if not I'll have to sort out how to set up HTTPS testing and how to write service worker tests.

@annevk annevk added the needs tests Moving the issue forward requires someone to write tests label Apr 12, 2018
annevk added a commit to web-platform-tests/wpt that referenced this pull request Apr 12, 2018
As per whatwg/fetch#633 (comment) except using global state instead of the cache API. (Improvements welcome!)
@annevk
Copy link
Member Author

annevk commented Apr 15, 2018

So I think with this change we end up doing what I think is the correct thing for opaque redirects, but it doesn't quite work for synthetic redirects not created through Response.redirect() (those are protected because the URL is already parsed and headers are immutable):

const res = new Response(null, { status: 302 });
res.headers.append("Location", "something-relative");

In order to make this work we'd have to parse the Location header if status is a redirect status when it's first stored/used somewhere. Otherwise those responses would always end up being relative to the current request.

But maybe we should live with that inconsistency? Not entirely sure how to fix it.

@annevk
Copy link
Member Author

annevk commented Apr 16, 2018

So there's basically four types of redirect responses:

  1. A Response.redirect() response.
  2. A new Response() response (with status and Location set appropriately).
  3. A fresh network response.
  4. A reused network response.

1 is different from 2 because 1 always has an absolute URL and therefore always goes to the same URL.

This PR as it stands ensures 3 and 4 always go to the same URL. This does not match Chrome and Firefox, but does match Safari. Given https://fetch.spec.whatwg.org/#atomic-http-redirect-handling it seems good to me that the client side cannot fiddle with the intent of the server, even if it was weakly stated (with a relative URL) and therefore I'd argue we align with Safari.

One approach we could take here is that 2 becomes a network error or "not a redirect" (just a response that happens to look like one). This would be easy to accomplish using the https://fetch.spec.whatwg.org/#concept-response-location-url field and setting it for 1.

I quite like that idea, but I'd like to solicit feedback on that from you all first. (An alternative is that 2 remains an oddball redirect response whose location varies on who requests it, but that seems a little less clean.)

cc @youennf @yutakahirano @mattto @hober

@wanderview
Copy link
Member

One approach we could take here is that 2 becomes a network error or "not a redirect" (just a response that happens to look like one). This would be easy to accomplish using the https://fetch.spec.whatwg.org/#concept-response-location-url field and setting it for 1.

Could we allow (2), but make it throw if the Location URL is relative?

@annevk
Copy link
Member Author

annevk commented Apr 16, 2018

@wanderview the response remains mutable, so that won't work. We could maybe do something like that when first handling such a response, but it'll be rather special.

@annevk annevk force-pushed the redirect-url-parsing branch from b5d9ad4 to 7922b6c Compare April 23, 2018 10:04
@annevk
Copy link
Member Author

annevk commented Apr 23, 2018

Arguably even nicer would be throwing a RangeError when constructing a Response with a 3xx status. Is that unrealistic? If it is, how realistic is returning a network error for some of them?

@annevk
Copy link
Member Author

annevk commented Jun 6, 2018

@wanderview @jakearchibald @yutakahirano @ricea @youennf thoughts on the above?

annevk added a commit to web-platform-tests/wpt that referenced this pull request Jun 6, 2018
As per whatwg/fetch#633 (comment) except using global state instead of the cache API. (Improvements welcome!)
@wanderview
Copy link
Member

Arguably even nicer would be throwing a RangeError when constructing a Response with a 3xx status.

Haven't we promoted Response.redirect() as a convenience, but not required to create a redirect? We would probably need data to know if people are manually constructing Response objects with 3xx status codes.

@annevk
Copy link
Member Author

annevk commented Jun 6, 2018

Yeah, that is correct.

@jakearchibald
Copy link
Collaborator

I don't think we should prevent users creating redirects with new Response().

In the new Response() case, the developer is setting the header themselves, so it seems fine to revolve that URL later.

@yutakahirano
Copy link
Member

If there were no backward-compatibility issues, would we want to disallow a user-created redirect response with a relative location at 3-3 in https://fetch.spec.whatwg.org/#http-fetch?

@annevk
Copy link
Member Author

annevk commented Jun 15, 2018

@yutakahirano I think that would be the most conservative solution that solves the stated problem. I think the ideal solution is not allowing the creation of such responses in the first place, but that's far less likely to be compatible.

@annevk
Copy link
Member Author

annevk commented Jun 15, 2018

@jakearchibald I guess, but it does create a rather weird special case that we'll likely have to take into consideration forever and ever when it comes to redirects. I'm worried that it'll trip us up somehow.

As the response's url list is not set at this point using that doesn not work. We could potentially set it earlier, but that brings along other complications that this setup avoid.

This also returns opaque redirect responses earlier, to avoid having them processed twice, which was simply wrong (and would become extra wrong here).

Fixes #631.
@annevk annevk force-pushed the redirect-url-parsing branch from 7922b6c to 0ae5dca Compare August 23, 2018 12:04
@annevk
Copy link
Member Author

annevk commented Aug 23, 2018

Anyone any further thoughts here? It'd be nice to resolve this.

annevk added a commit to web-platform-tests/wpt that referenced this pull request Apr 3, 2020
As per whatwg/fetch#633 (comment) except using global state instead of the cache API. (Improvements welcome!)
Base automatically changed from master to main January 15, 2021 07:38
annevk added a commit that referenced this pull request Jan 29, 2021
It does not need to be stored on a response and therefore resulted in confusion.

Also clarify that synthetic responses need to have an absolute URL in the Location header field value (Response.redirect() does this automatically).

Corresponding HTML PR: TODO.

Tests: TODO.

Closes #631, closes #633, closes #958, and closes #1146. (Some of these can be closed due to #1030 making response's URL no longer null for network responses.)
annevk added a commit that referenced this pull request Feb 1, 2021
It does not need to be stored on a response and therefore resulted in confusion.

Also clarify that synthetic responses need to have an absolute URL in the Location header field value (Response.redirect() does this automatically).

Corresponding HTML PR: TODO.

Tests: TODO.

Closes #631, closes #633, closes #958, and closes #1146. (Some of these can be closed due to #1030 making response's URL no longer null for network responses.)
@annevk annevk closed this in #1149 Feb 2, 2021
annevk added a commit that referenced this pull request Feb 2, 2021
It does not need to be stored on a response and therefore resulted in confusion.

Also clarify that synthetic responses need to have an absolute URL in the Location header field value (Response.redirect() does this automatically).

Corresponding HTML PR: whatwg/html#6340.

Tests: https://chromium-review.googlesource.com/c/chromium/src/+/2665871.

Closes #631, closes #633, closes #958, closes #1146, and closes web-platform-tests/wpt#10449. (Some of these can be closed due to #1030 making response's URL no longer null for network responses.)
@annevk annevk deleted the redirect-url-parsing branch February 2, 2021 15:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs tests Moving the issue forward requires someone to write tests topic: redirects
Development

Successfully merging this pull request may close these issues.

Clarify response's url list state when parsing a 3xx response's Location header value
5 participants