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

Add storage test for setting a cookie via the cookieStore API in a serviceworker #166

Merged
merged 2 commits into from
Oct 12, 2023
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
20 changes: 19 additions & 1 deletion privacy-protections/storage-blocking/main.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* globals commonTests,THIRD_PARTY_ORIGIN,THIRD_PARTY_TRACKER_ORIGIN,THIRD_PARTY_AD_ORIGIN */
/* globals commonTests,THIRD_PARTY_ORIGIN,THIRD_PARTY_TRACKER_ORIGIN,THIRD_PARTY_AD_ORIGIN,cookieStore */

const storeButton = document.querySelector('#store');
const retriveButton = document.querySelector('#retrive');
Expand Down Expand Up @@ -146,6 +146,24 @@ const tests = [
throw new Error(`Invalid response (${r.status})`);
});
}
},
{
id: 'service worker cookieStore',
store: async (data) => {
await navigator.serviceWorker.register(`./service-worker.js?data=${data}`, { scope: './' });
const resp = await fetch(`./service-worker-set-cookie?name=swcookiestoredata&data=${data}`);
return await resp.text();
},
retrive: async () => {
return (await cookieStore.get('swcookiestoredata')).value;
},
extra: () => {
if (window.cookieStore) {
return cookieStore.get('swcookiestoredata').then(cookie => {
return 'expires in ' + ((cookie.expires - Date.now()) / (1000 * 60 * 60 * 24)).toFixed(2) + ' days';
});
}
}
}
];

Expand Down
12 changes: 12 additions & 0 deletions privacy-protections/storage-blocking/service-worker.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/* eslint-env serviceworker */
/* global cookieStore */

self.addEventListener('install', (evt) => {
self.skipWaiting();
Expand All @@ -12,4 +13,15 @@ self.addEventListener('fetch', (event) => {
if (event.request.url.includes('/service-worker-data')) {
event.respondWith(new Response(location.search.replace('?data=', '')));
}
if (event.request.url.includes('/service-worker-set-cookie')) {
const url = new URL(event.request.url);
if (globalThis.cookieStore) {
cookieStore.set({
name: url.searchParams.get('name'),
value: url.searchParams.get('data'),
expires: new Date('Wed, 21 Aug 2030 20:00:00 UTC').getTime()
});
}
event.respondWith(new Response('OK'));
}
});
Loading