-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Resolve Service Worker redirects based on the response
We currently resolve Service-Worker-forwarded location headers using the request. While this matches Firefox, this does not match the spec or Safari's behavior. Instead, the spec says to resolve the location header based on the response's URL. This comes up if the FetchEvent was for /, but the Service Worker responded with ev.respondWith(fetch("/foo/", {redirect: "manual"})). In that case, a Location: bar.html header would result in /bar.html by our version and /foo/bar.html by the spec's version. Align with the spec. This makes the redirect go where it would have gone under {redirect: "follow"}. This has two platform-visible behavior changes: - First, cases like the above will result in a different URL. - Second, script-constructed Response objects do not have a URL list. If the URLs are absolute, this works fine. If they are relative, those fetches will now result in a network error. Note Response.redirect() internally constructs absolute URLs, so those continue to work. This only affects ev.respondWith(new Response(... location: "bar.html"}})). Both of these changes match Safari. Note that, as of writing, the Fetch spec describes this behavior in terms of a location URL property on the response object. This would require computing the location URL earlier and preserving it across many layers, including persisting in CacheStorage. See https://chromium-review.googlesource.com/c/chromium/src/+/2648648. Instead, this CL uses the equivalent formulation in whatwg/fetch#1149. See also discussion in whatwg/fetch#1146. Bug: 1170379 Change-Id: Ibb6b12566244fd259029e67787dd7f08edeece9d
- Loading branch information
1 parent
28de622
commit 13ee3f3
Showing
5 changed files
with
168 additions
and
1 deletion.
There are no files selected for viewing
58 changes: 58 additions & 0 deletions
58
service-workers/service-worker/navigation-redirect-resolution.https.html
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
<!DOCTYPE html> | ||
<title>Service Worker: Navigation Redirect Resolution</title> | ||
<script src="/resources/testharness.js"></script> | ||
<script src="/resources/testharnessreport.js"></script> | ||
<script src="resources/test-helpers.sub.js"></script> | ||
<body> | ||
<script> | ||
|
||
function make_absolute(url) { | ||
return new URL(url, location).toString(); | ||
} | ||
|
||
const script = 'resources/fetch-rewrite-worker.js'; | ||
|
||
function redirect_result_test(scope, expected_url, description) { | ||
promise_test(async t => { | ||
const registration = await service_worker_unregister_and_register( | ||
t, script, scope); | ||
t.add_cleanup(() => { | ||
return service_worker_unregister(t, scope); | ||
}) | ||
await wait_for_state(t, registration.installing, 'activated'); | ||
|
||
// The navigation to |scope| will be resolved by a fetch to |redirect_url| | ||
// which returns a relative Location header. If it is resolved relative to | ||
// |scope|, the result will be navigate-redirect-resolution/blank.html. If | ||
// relative to |redirect_url|, it will be resources/blank.html. The latter | ||
// is correct. | ||
const iframe = await with_iframe(scope); | ||
t.add_cleanup(() => { iframe.remove(); }); | ||
assert_equals(iframe.contentWindow.location.href, | ||
make_absolute(expected_url)); | ||
}, description); | ||
} | ||
|
||
// |redirect_url| serves a relative redirect to resources/blank.html. | ||
const redirect_url = 'resources/redirect.py?Redirect=blank.html'; | ||
|
||
// |scope_base| does not exist but will be replaced with a fetch of | ||
// |redirect_url| by fetch-rewrite-worker.js. | ||
const scope_base = 'resources/subdir/navigation-redirect-resolution?' + | ||
'redirect-mode=manual&url=' + | ||
encodeURIComponent(make_absolute(redirect_url)); | ||
|
||
// When the Service Worker forwards the result of |redirect_url| as an | ||
// opaqueredirect response, the redirect uses the response's URL list as the | ||
// base URL, not the request. | ||
redirect_result_test(scope_base, 'resources/blank.html', | ||
'test relative opaqueredirect'); | ||
|
||
// The response's base URL should be preserved across CacheStorage and clone. | ||
redirect_result_test(scope_base + '&cache=1', 'resources/blank.html', | ||
'test relative opaqueredirect with CacheStorage'); | ||
redirect_result_test(scope_base + '&clone=1', 'resources/blank.html', | ||
'test relative opaqueredirect with clone'); | ||
|
||
</script> | ||
</body> |
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
<!DOCTYPE html> | ||
<title>Empty doc</title> |