From e6ce2423c1e91c162cbca6e5d255ac02ca279ed8 Mon Sep 17 00:00:00 2001 From: Sam Macbeth Date: Fri, 29 Sep 2023 12:26:12 +0200 Subject: [PATCH 1/2] Add storage test for setting a cookie via the cookieStore API in a serviceworker --- .../storage-blocking/helpers/commonTests.js | 2 +- privacy-protections/storage-blocking/main.js | 20 ++++++++++++++++++- .../storage-blocking/service-worker.js | 12 +++++++++++ 3 files changed, 32 insertions(+), 2 deletions(-) diff --git a/privacy-protections/storage-blocking/helpers/commonTests.js b/privacy-protections/storage-blocking/helpers/commonTests.js index 471780f..db6c2c6 100644 --- a/privacy-protections/storage-blocking/helpers/commonTests.js +++ b/privacy-protections/storage-blocking/helpers/commonTests.js @@ -158,7 +158,7 @@ const commonTests = [ }, extra: () => { if (window.cookieStore) { - return cookieStore.get('cookiestoredata').then(cookie => { + return cookieStore.get('swcookiestoredata').then(cookie => { return 'expires in ' + ((cookie.expires - Date.now()) / (1000 * 60 * 60 * 24)).toFixed(2) + ' days'; }); } diff --git a/privacy-protections/storage-blocking/main.js b/privacy-protections/storage-blocking/main.js index 17ba89e..7e28467 100644 --- a/privacy-protections/storage-blocking/main.js +++ b/privacy-protections/storage-blocking/main.js @@ -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'); @@ -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('cookiestoredata').then(cookie => { + return 'expires in ' + ((cookie.expires - Date.now()) / (1000 * 60 * 60 * 24)).toFixed(2) + ' days'; + }); + } + } } ]; diff --git a/privacy-protections/storage-blocking/service-worker.js b/privacy-protections/storage-blocking/service-worker.js index b80e1f7..5adc206 100644 --- a/privacy-protections/storage-blocking/service-worker.js +++ b/privacy-protections/storage-blocking/service-worker.js @@ -1,4 +1,5 @@ /* eslint-env serviceworker */ +/* global cookieStore */ self.addEventListener('install', (evt) => { self.skipWaiting(); @@ -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')); + } }); From 99418d453b9c6d4b9f5ae151d20ffca50a72af1e Mon Sep 17 00:00:00 2001 From: Sam Macbeth Date: Fri, 29 Sep 2023 12:43:32 +0200 Subject: [PATCH 2/2] Fix cookie names --- privacy-protections/storage-blocking/helpers/commonTests.js | 2 +- privacy-protections/storage-blocking/main.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/privacy-protections/storage-blocking/helpers/commonTests.js b/privacy-protections/storage-blocking/helpers/commonTests.js index db6c2c6..471780f 100644 --- a/privacy-protections/storage-blocking/helpers/commonTests.js +++ b/privacy-protections/storage-blocking/helpers/commonTests.js @@ -158,7 +158,7 @@ const commonTests = [ }, extra: () => { if (window.cookieStore) { - return cookieStore.get('swcookiestoredata').then(cookie => { + return cookieStore.get('cookiestoredata').then(cookie => { return 'expires in ' + ((cookie.expires - Date.now()) / (1000 * 60 * 60 * 24)).toFixed(2) + ' days'; }); } diff --git a/privacy-protections/storage-blocking/main.js b/privacy-protections/storage-blocking/main.js index 7e28467..c033c0a 100644 --- a/privacy-protections/storage-blocking/main.js +++ b/privacy-protections/storage-blocking/main.js @@ -159,7 +159,7 @@ const tests = [ }, extra: () => { if (window.cookieStore) { - return cookieStore.get('cookiestoredata').then(cookie => { + return cookieStore.get('swcookiestoredata').then(cookie => { return 'expires in ' + ((cookie.expires - Date.now()) / (1000 * 60 * 60 * 24)).toFixed(2) + ' days'; }); }