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

Resolve Service Worker redirects based on the response #27444

Merged
merged 1 commit into from
Feb 5, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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>