Skip to content

Commit

Permalink
Add WPT for cookies/service worker partitioning in importScripts
Browse files Browse the repository at this point in the history
Bug: 1427879
Change-Id: I20d5a74daee8f59d537472cb7836c7e576b24b5e
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/4543752
Commit-Queue: Dylan Cutler <dylancutler@google.com>
Reviewed-by: Steven Bingler <bingler@chromium.org>
Cr-Commit-Position: refs/heads/main@{#1146188}
  • Loading branch information
DCtheTall authored and chromium-wpt-export-bot committed May 18, 2023
1 parent fa1adf0 commit 9146569
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 8 deletions.
12 changes: 12 additions & 0 deletions cookies/resources/list-cookies-for-script.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import json
from cookies.resources import helpers

from wptserve.utils import isomorphic_decode

def main(request, response):
headers = helpers.setNoCacheAndCORSHeaders(request, response)
headers[0] = (b"Content-Type", b"text/javascript")
cookies = helpers.readCookies(request)
decoded_cookies = {isomorphic_decode(key): isomorphic_decode(val) for key, val in cookies.items()}
return headers, 'self._cookies = [{}];\n'.format(
', '.join(['"{}"'.format(name) for name in decoded_cookies.keys()]))
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,14 @@
const scope = './resources/partitioned-cookies-'
const absolute_scope = new URL(scope, window.location).href;

// Set a Partitioned cookie.
document.cookie = '__Host-partitioned=123; Secure; Path=/; SameSite=None; Partitioned;';
assert_true(document.cookie.includes('__Host-partitioned=123'));

// Set an unpartitioned cookie.
document.cookie = 'unpartitioned=456; Secure; Path=/; SameSite=None;';
assert_true(document.cookie.includes('unpartitioned=456'));

const reg = await service_worker_unregister_and_register(t, script, scope);
await wait_for_state(t, reg.installing, 'activated');
t.add_cleanup(() => reg.unregister());
Expand Down Expand Up @@ -55,14 +63,6 @@
await wait_promise;
assert_true(got.ok, 'Message passing');

// Set a Partitioned cookie.
document.cookie = '__Host-partitioned=123; Secure; Path=/; SameSite=None; Partitioned;';
assert_true(document.cookie.includes('__Host-partitioned=123'));

// Set an unpartitioned cookie.
document.cookie = 'unpartitioned=456; Secure; Path=/; SameSite=None;';
assert_true(document.cookie.includes('unpartitioned=456'));

// Test that the partitioned cookie is available to this worker via HTTP.
wait_promise = new Promise(resolve => {
resolve_wait_promise = resolve;
Expand All @@ -89,6 +89,22 @@
await wait_promise;
assert_true(got.ok, 'Get cookies');
assert_true(got.cookies.includes('__Host-partitioned'), 'Can access partitioned cookie via JS');
assert_true(got.cookies.includes('unpartitioned'), 'Can access unpartitioned cookie via JS');

// Test that the partitioned cookie is not available to this worker in HTTP
// requests from importScripts.
wait_promise = new Promise(resolve => {
resolve_wait_promise = resolve;
});
on_message = ev => {
got = ev.data;
resolve_wait_promise();
};
filtered_registrations[0].active.postMessage({type: 'echo_cookies_import'});
await wait_promise;
assert_true(got.ok, 'Get cookies');
assert_true(got.cookies.includes('__Host-partitioned'), 'Can access partitioned cookie via importScripts');
assert_true(got.cookies.includes('unpartitioned'), 'Can access unpartitioned cookie via importScripts');

const popup = window.open(
new URL(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,25 @@
assert_false(
got.cookies.includes('unpartitioned'),
'Credentialless frame worker cannot access unpartitioned cookie via HTTP');

// Test that the partitioned cookie is not available to this worker in HTTP
// requests from importScripts.
wait_promise = new Promise(resolve => {
resolve_wait_promise = resolve;
});
on_message = ev => {
got = ev.data;
resolve_wait_promise();
};
filtered_registrations[0].active.postMessage({ type: 'echo_cookies_import' });
await wait_promise;
assert_true(got.ok, 'Get cookies');
assert_true(
got.cookies.includes('__Host-partitioned'),
'Credentialless frame worker can access partitioned cookie via importScripts');
assert_false(
got.cookies.includes('unpartitioned'),
'Credentialless frame worker cannot access unpartitioned cookie via importScripts');
});

</script>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,25 @@
assert_true(
got.cookies.includes('unpartitioned'),
'Worker can access unpartitioned cookie via JS');

// Test that the partitioned cookie is not available to this worker in HTTP
// requests from importScripts.
wait_promise = new Promise(resolve => {
resolve_wait_promise = resolve;
});
on_message = ev => {
got = ev.data;
resolve_wait_promise();
};
filtered_registrations[0].active.postMessage({type: 'echo_cookies_import'});
await wait_promise;
assert_true(got.ok, 'Get cookies');
assert_false(
got.cookies.includes('__Host-partitioned'),
'Worker cannot access partitioned cookie via importScripts');
assert_true(
got.cookies.includes('unpartitioned'),
'Worker can access unpartitioned cookie via importScripts');
});

</script>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ async function onMessage(event) {
return onEchoCookiesHttp(event);
case 'echo_cookies_js':
return onEchoCookiesJs(event);
case 'echo_cookies_import':
return onEchoCookiesImport(event);
default:
return;
}
Expand Down Expand Up @@ -41,3 +43,11 @@ async function onEchoCookiesJs(event) {
event.source.postMessage({ok: false});
}
}

// Sets `self._cookies` variable, array of the names of cookies available to
// the request.
importScripts(`${self.origin}/cookies/resources/list-cookies-for-script.py`);

function onEchoCookiesImport(event) {
event.source.postMessage({ok: true, cookies: self._cookies});
}
10 changes: 10 additions & 0 deletions service-workers/service-worker/resources/partitioned-cookies-sw.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ async function onMessage(event) {
return onEchoCookiesHttp(event);
case 'echo_cookies_js':
return onEchoCookiesJs(event);
case 'echo_cookies_import':
return onEchoCookiesImport(event);
default:
return;
}
Expand Down Expand Up @@ -41,3 +43,11 @@ async function onEchoCookiesJs(event) {
event.source.postMessage({ok: false});
}
}

// Sets `self._cookies` variable, array of the names of cookies available to
// the request.
importScripts(`${self.origin}/cookies/resources/list-cookies-for-script.py`);

function onEchoCookiesImport(event) {
event.source.postMessage({ok: true, cookies: self._cookies});
}

0 comments on commit 9146569

Please sign in to comment.