Skip to content

Commit

Permalink
Resolve Service Worker redirects based on the response
Browse files Browse the repository at this point in the history
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
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2665871
Reviewed-by: Makoto Shimazu <shimazu@chromium.org>
Reviewed-by: Kinuko Yasuda <kinuko@chromium.org>
Reviewed-by: Ben Kelly <wanderview@chromium.org>
Commit-Queue: David Benjamin <davidben@chromium.org>
Cr-Commit-Position: refs/heads/master@{#850874}
  • Loading branch information
davidben authored and chromium-wpt-export-bot committed Feb 5, 2021
1 parent 9376be0 commit 8da6e39
Show file tree
Hide file tree
Showing 5 changed files with 168 additions and 1 deletion.
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>
98 changes: 97 additions & 1 deletion service-workers/service-worker/redirected-response.https.html
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@
.then(() => {
const url = host_info['HTTPS_ORIGIN'] + base_path() +
'sample?url=' + encodeURIComponent(TARGET_URL) +
'&original-redirect-mode=follow&sw=gen';
'&original-redirect-mode=manual&sw=gen';
return redirected_test({url: url,
fetch_option: {redirect: 'manual'},
fetch_method: frame.contentWindow.fetch,
Expand All @@ -307,6 +307,102 @@
}),
'mode: "manual", generated redirect response');

// =======================================================
// Tests for requests that are in-scope of the service worker. The service
// worker returns a generated redirect response manually with the Response
// constructor.
// =======================================================
promise_test(t => setup_and_clean()
.then(() => {
const url = host_info['HTTPS_ORIGIN'] + base_path() +
'sample?url=' + encodeURIComponent(TARGET_URL) +
'&original-redirect-mode=follow&sw=gen-manual';
return redirected_test({url: url,
fetch_option: {redirect: 'follow'},
fetch_method: frame.contentWindow.fetch,
expected_type: 'basic',
expected_redirected: true,
expected_intercepted_urls: [url, TARGET_URL]})
}),
'mode: "follow", manually-generated redirect response');

promise_test(t => setup_and_clean()
.then(() => {
const url = host_info['HTTPS_ORIGIN'] + base_path() +
'sample?url=' + encodeURIComponent(TARGET_URL) +
'&original-redirect-mode=error&sw=gen-manual';
return promise_rejects_js(
t, frame.contentWindow.TypeError,
frame.contentWindow.fetch(url, {redirect: 'error'}),
'The generated redirect response from the service worker should ' +
'be treated as an error when the redirect flag of request was' +
' \'error\'.')
.then(() => check_intercepted_urls([url]));
}),
'mode: "error", manually-generated redirect response');

promise_test(t => setup_and_clean()
.then(() => {
const url = host_info['HTTPS_ORIGIN'] + base_path() +
'sample?url=' + encodeURIComponent(TARGET_URL) +
'&original-redirect-mode=manual&sw=gen-manual';
return redirected_test({url: url,
fetch_option: {redirect: 'manual'},
fetch_method: frame.contentWindow.fetch,
expected_type: 'opaqueredirect',
expected_redirected: false,
expected_intercepted_urls: [url]})
}),
'mode: "manual", manually-generated redirect response');

// =======================================================
// Tests for requests that are in-scope of the service worker. The service
// worker returns a generated redirect response with a relative location header.
// Generated responses do not have URLs, so this should fail to resolve.
// =======================================================
promise_test(t => setup_and_clean()
.then(() => {
const url = host_info['HTTPS_ORIGIN'] + base_path() +
'sample?url=blank.html' +
'&original-redirect-mode=follow&sw=gen-manual';
return promise_rejects_js(
t, frame.contentWindow.TypeError,
frame.contentWindow.fetch(url, {redirect: 'follow'}),
'Following the generated redirect response from the service worker '+
'should result fail.')
.then(() => check_intercepted_urls([url]));
}),
'mode: "follow", generated relative redirect response');

promise_test(t => setup_and_clean()
.then(() => {
const url = host_info['HTTPS_ORIGIN'] + base_path() +
'sample?url=blank.html' +
'&original-redirect-mode=error&sw=gen-manual';
return promise_rejects_js(
t, frame.contentWindow.TypeError,
frame.contentWindow.fetch(url, {redirect: 'error'}),
'The generated redirect response from the service worker should ' +
'be treated as an error when the redirect flag of request was' +
' \'error\'.')
.then(() => check_intercepted_urls([url]));
}),
'mode: "error", generated relative redirect response');

promise_test(t => setup_and_clean()
.then(() => {
const url = host_info['HTTPS_ORIGIN'] + base_path() +
'sample?url=blank.html' +
'&original-redirect-mode=manual&sw=gen-manual';
return redirected_test({url: url,
fetch_option: {redirect: 'manual'},
fetch_method: frame.contentWindow.fetch,
expected_type: 'opaqueredirect',
expected_redirected: false,
expected_intercepted_urls: [url]})
}),
'mode: "manual", generated relative redirect response');

// =======================================================
// Tests for requests that are in-scope of the service worker. The service
// worker returns a generated redirect response. And the fetch follows the
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,10 @@ self.addEventListener('fetch', function(event) {
}
}

if (params['clone']) {
response = response.clone();
}

// |cache| means to bounce responses through Cache Storage and back.
if (params['cache']) {
var cacheName = "cached-fetches-" + performance.now() + "-" +
Expand Down
7 changes: 7 additions & 0 deletions service-workers/service-worker/resources/redirect-worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,13 @@ self.addEventListener('fetch', function(event) {
event.respondWith(waitUntilPromise.then(async () => {
if (params['sw'] == 'gen') {
return Response.redirect(params['url']);
} else if (params['sw'] == 'gen-manual') {
// Note this differs from Response.redirect() in that relative URLs are
// preserved.
return new Response("", {
status: 301,
headers: {location: params['url']},
});
} else if (params['sw'] == 'fetch') {
return fetch(event.request);
} else if (params['sw'] == 'fetch-url') {
Expand Down
2 changes: 2 additions & 0 deletions service-workers/service-worker/resources/subdir/blank.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<!DOCTYPE html>
<title>Empty doc</title>

0 comments on commit 8da6e39

Please sign in to comment.