Skip to content

Commit b4989d5

Browse files
authored
Make protections testable (#14)
* Make cookie scripts testable * ability to pass custom window object to fingerprinting scripts * make temp storage script testable * Last minute fixes found in review * Drop old console.log * 1p cookies script - clear state on load * use globalThis in tracking-cookies-3p * Update 1p expiery script to use globalThis * update all files to use globalThis * revert some global passing that I missed * Include build scripts
1 parent 4426f9d commit b4989d5

11 files changed

+105
-53
lines changed

build/apple/contentScope.js

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1074,7 +1074,9 @@ var contentScopeFeatures = (function (exports) {
10741074
* as well as prevent any script from listening to events.
10751075
*/
10761076
function init$b (args) {
1077-
if (navigator.getBattery) {
1077+
if (globalThis.navigator.getBattery) {
1078+
const BatteryManager = globalThis.BatteryManager;
1079+
10781080
const spoofedValues = {
10791081
charging: true,
10801082
chargingTime: 0,
@@ -2293,6 +2295,9 @@ var contentScopeFeatures = (function (exports) {
22932295
});
22942296

22952297
function init$9 (args) {
2298+
const Navigator = globalThis.Navigator;
2299+
const navigator = globalThis.navigator;
2300+
22962301
overrideProperty('keyboard', {
22972302
object: Navigator.prototype,
22982303
origValue: navigator.keyboard,
@@ -2334,7 +2339,7 @@ var contentScopeFeatures = (function (exports) {
23342339
function setWindowPropertyValue (property, value) {
23352340
// Here we don't update the prototype getter because the values are updated dynamically
23362341
try {
2337-
defineProperty(window, property, {
2342+
defineProperty(globalThis, property, {
23382343
get: () => value,
23392344
set: () => {},
23402345
configurable: true
@@ -2352,6 +2357,9 @@ var contentScopeFeatures = (function (exports) {
23522357
*/
23532358
function setWindowDimensions () {
23542359
try {
2360+
const window = globalThis;
2361+
const top = globalThis.top;
2362+
23552363
const normalizedY = normalizeWindowDimension(window.screenY, window.screen.height);
23562364
const normalizedX = normalizeWindowDimension(window.screenX, window.screen.width);
23572365
if (normalizedY <= origPropertyValues.availTop) {
@@ -2395,6 +2403,9 @@ var contentScopeFeatures = (function (exports) {
23952403
}
23962404

23972405
function init$8 (args) {
2406+
const Screen = globalThis.Screen;
2407+
const screen = globalThis.screen;
2408+
23982409
origPropertyValues.availTop = overrideProperty('availTop', {
23992410
object: Screen.prototype,
24002411
origValue: screen.availTop,
@@ -2438,6 +2449,9 @@ var contentScopeFeatures = (function (exports) {
24382449
});
24392450

24402451
function init$7 () {
2452+
const navigator = globalThis.navigator;
2453+
const Navigator = globalThis.Navigator;
2454+
24412455
/**
24422456
* Temporary storage can be used to determine hard disk usage and size.
24432457
* This will limit the max storage to 4GB without completely disabling the
@@ -2642,11 +2656,6 @@ var contentScopeFeatures = (function (exports) {
26422656
}
26432657
}
26442658

2645-
// support node-requires (for test import)
2646-
if (typeof module !== 'undefined' && module.exports) {
2647-
module.exports = Cookie;
2648-
}
2649-
26502659
let loadedPolicyResolve;
26512660
// Listen for a message from the content script which will configure the policy for this context
26522661
const trackerHosts = new Set();
@@ -2655,8 +2664,10 @@ var contentScopeFeatures = (function (exports) {
26552664
* Apply an expiry policy to cookies set via document.cookie.
26562665
*/
26572666
function applyCookieExpiryPolicy () {
2658-
const cookieSetter = Object.getOwnPropertyDescriptor(Document.prototype, 'cookie').set;
2659-
const cookieGetter = Object.getOwnPropertyDescriptor(Document.prototype, 'cookie').get;
2667+
const document = globalThis.document;
2668+
const Error = globalThis.Error;
2669+
const cookieSetter = Object.getOwnPropertyDescriptor(globalThis.Document.prototype, 'cookie').set;
2670+
const cookieGetter = Object.getOwnPropertyDescriptor(globalThis.Document.prototype, 'cookie').get;
26602671
const lineTest = /(\()?(http[^)]+):[0-9]+:[0-9]+(\))?/;
26612672

26622673
const loadPolicy = new Promise((resolve) => {
@@ -2762,6 +2773,8 @@ var contentScopeFeatures = (function (exports) {
27622773

27632774
// Set up 1st party cookie blocker
27642775
function load (args) {
2776+
trackerHosts.clear();
2777+
27652778
// The cookie expiry policy is injected into every frame immediately so that no cookie will
27662779
// be missed.
27672780
applyCookieExpiryPolicy();
@@ -2787,14 +2800,14 @@ var contentScopeFeatures = (function (exports) {
27872800

27882801
function blockCookies (debug) {
27892802
// disable setting cookies
2790-
defineProperty(document, 'cookie', {
2803+
defineProperty(globalThis.document, 'cookie', {
27912804
configurable: false,
27922805
set: function (value) {
27932806
if (debug) {
27942807
postDebugMessage('jscookie', {
27952808
action: 'block',
27962809
reason: 'tracker frame',
2797-
documentUrl: document.location.href,
2810+
documentUrl: globalThis.document.location.href,
27982811
scriptOrigins: [],
27992812
value: value
28002813
});
@@ -2805,7 +2818,7 @@ var contentScopeFeatures = (function (exports) {
28052818
postDebugMessage('jscookie', {
28062819
action: 'block',
28072820
reason: 'tracker frame',
2808-
documentUrl: document.location.href,
2821+
documentUrl: globalThis.document.location.href,
28092822
scriptOrigins: [],
28102823
value: 'getter'
28112824
});
@@ -2817,7 +2830,7 @@ var contentScopeFeatures = (function (exports) {
28172830

28182831
function init (args) {
28192832
args.cookie.debug = args.debug;
2820-
if (window.top !== window && args.cookie.isTrackerFrame && args.cookie.shouldBlock && args.cookie.isThirdParty) {
2833+
if (globalThis.top !== globalThis && args.cookie.isTrackerFrame && args.cookie.shouldBlock && args.cookie.isThirdParty) {
28212834
// overrides expiry policy with blocking - only in subframes
28222835
blockCookies(args.debug);
28232836
}

build/chrome/inject.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

build/firefox/inject.js

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1090,7 +1090,9 @@ var contentScopeFeatures = (function (exports) {
10901090
* as well as prevent any script from listening to events.
10911091
*/
10921092
function init$b (args) {
1093-
if (navigator.getBattery) {
1093+
if (globalThis.navigator.getBattery) {
1094+
const BatteryManager = globalThis.BatteryManager;
1095+
10941096
const spoofedValues = {
10951097
charging: true,
10961098
chargingTime: 0,
@@ -2309,6 +2311,9 @@ var contentScopeFeatures = (function (exports) {
23092311
});
23102312

23112313
function init$9 (args) {
2314+
const Navigator = globalThis.Navigator;
2315+
const navigator = globalThis.navigator;
2316+
23122317
overrideProperty('keyboard', {
23132318
object: Navigator.prototype,
23142319
origValue: navigator.keyboard,
@@ -2350,7 +2355,7 @@ var contentScopeFeatures = (function (exports) {
23502355
function setWindowPropertyValue (property, value) {
23512356
// Here we don't update the prototype getter because the values are updated dynamically
23522357
try {
2353-
defineProperty(window, property, {
2358+
defineProperty(globalThis, property, {
23542359
get: () => value,
23552360
set: () => {},
23562361
configurable: true
@@ -2368,6 +2373,9 @@ var contentScopeFeatures = (function (exports) {
23682373
*/
23692374
function setWindowDimensions () {
23702375
try {
2376+
const window = globalThis;
2377+
const top = globalThis.top;
2378+
23712379
const normalizedY = normalizeWindowDimension(window.screenY, window.screen.height);
23722380
const normalizedX = normalizeWindowDimension(window.screenX, window.screen.width);
23732381
if (normalizedY <= origPropertyValues.availTop) {
@@ -2411,6 +2419,9 @@ var contentScopeFeatures = (function (exports) {
24112419
}
24122420

24132421
function init$8 (args) {
2422+
const Screen = globalThis.Screen;
2423+
const screen = globalThis.screen;
2424+
24142425
origPropertyValues.availTop = overrideProperty('availTop', {
24152426
object: Screen.prototype,
24162427
origValue: screen.availTop,
@@ -2454,6 +2465,9 @@ var contentScopeFeatures = (function (exports) {
24542465
});
24552466

24562467
function init$7 () {
2468+
const navigator = globalThis.navigator;
2469+
const Navigator = globalThis.Navigator;
2470+
24572471
/**
24582472
* Temporary storage can be used to determine hard disk usage and size.
24592473
* This will limit the max storage to 4GB without completely disabling the
@@ -2658,11 +2672,6 @@ var contentScopeFeatures = (function (exports) {
26582672
}
26592673
}
26602674

2661-
// support node-requires (for test import)
2662-
if (typeof module !== 'undefined' && module.exports) {
2663-
module.exports = Cookie;
2664-
}
2665-
26662675
let loadedPolicyResolve;
26672676
// Listen for a message from the content script which will configure the policy for this context
26682677
const trackerHosts = new Set();
@@ -2671,8 +2680,10 @@ var contentScopeFeatures = (function (exports) {
26712680
* Apply an expiry policy to cookies set via document.cookie.
26722681
*/
26732682
function applyCookieExpiryPolicy () {
2674-
const cookieSetter = Object.getOwnPropertyDescriptor(Document.prototype, 'cookie').set;
2675-
const cookieGetter = Object.getOwnPropertyDescriptor(Document.prototype, 'cookie').get;
2683+
const document = globalThis.document;
2684+
const Error = globalThis.Error;
2685+
const cookieSetter = Object.getOwnPropertyDescriptor(globalThis.Document.prototype, 'cookie').set;
2686+
const cookieGetter = Object.getOwnPropertyDescriptor(globalThis.Document.prototype, 'cookie').get;
26762687
const lineTest = /(\()?(http[^)]+):[0-9]+:[0-9]+(\))?/;
26772688

26782689
const loadPolicy = new Promise((resolve) => {
@@ -2778,6 +2789,8 @@ var contentScopeFeatures = (function (exports) {
27782789

27792790
// Set up 1st party cookie blocker
27802791
function load (args) {
2792+
trackerHosts.clear();
2793+
27812794
// The cookie expiry policy is injected into every frame immediately so that no cookie will
27822795
// be missed.
27832796
applyCookieExpiryPolicy();
@@ -2803,14 +2816,14 @@ var contentScopeFeatures = (function (exports) {
28032816

28042817
function blockCookies (debug) {
28052818
// disable setting cookies
2806-
defineProperty(document, 'cookie', {
2819+
defineProperty(globalThis.document, 'cookie', {
28072820
configurable: false,
28082821
set: function (value) {
28092822
if (debug) {
28102823
postDebugMessage('jscookie', {
28112824
action: 'block',
28122825
reason: 'tracker frame',
2813-
documentUrl: document.location.href,
2826+
documentUrl: globalThis.document.location.href,
28142827
scriptOrigins: [],
28152828
value: value
28162829
});
@@ -2821,7 +2834,7 @@ var contentScopeFeatures = (function (exports) {
28212834
postDebugMessage('jscookie', {
28222835
action: 'block',
28232836
reason: 'tracker frame',
2824-
documentUrl: document.location.href,
2837+
documentUrl: globalThis.document.location.href,
28252838
scriptOrigins: [],
28262839
value: 'getter'
28272840
});
@@ -2833,7 +2846,7 @@ var contentScopeFeatures = (function (exports) {
28332846

28342847
function init (args) {
28352848
args.cookie.debug = args.debug;
2836-
if (window.top !== window && args.cookie.isTrackerFrame && args.cookie.shouldBlock && args.cookie.isThirdParty) {
2849+
if (globalThis.top !== globalThis && args.cookie.isTrackerFrame && args.cookie.shouldBlock && args.cookie.isThirdParty) {
28372850
// overrides expiry policy with blocking - only in subframes
28382851
blockCookies(args.debug);
28392852
}

build/integration/contentScope.js

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1074,7 +1074,9 @@ var contentScopeFeatures = (function (exports) {
10741074
* as well as prevent any script from listening to events.
10751075
*/
10761076
function init$b (args) {
1077-
if (navigator.getBattery) {
1077+
if (globalThis.navigator.getBattery) {
1078+
const BatteryManager = globalThis.BatteryManager;
1079+
10781080
const spoofedValues = {
10791081
charging: true,
10801082
chargingTime: 0,
@@ -2293,6 +2295,9 @@ var contentScopeFeatures = (function (exports) {
22932295
});
22942296

22952297
function init$9 (args) {
2298+
const Navigator = globalThis.Navigator;
2299+
const navigator = globalThis.navigator;
2300+
22962301
overrideProperty('keyboard', {
22972302
object: Navigator.prototype,
22982303
origValue: navigator.keyboard,
@@ -2334,7 +2339,7 @@ var contentScopeFeatures = (function (exports) {
23342339
function setWindowPropertyValue (property, value) {
23352340
// Here we don't update the prototype getter because the values are updated dynamically
23362341
try {
2337-
defineProperty(window, property, {
2342+
defineProperty(globalThis, property, {
23382343
get: () => value,
23392344
set: () => {},
23402345
configurable: true
@@ -2352,6 +2357,9 @@ var contentScopeFeatures = (function (exports) {
23522357
*/
23532358
function setWindowDimensions () {
23542359
try {
2360+
const window = globalThis;
2361+
const top = globalThis.top;
2362+
23552363
const normalizedY = normalizeWindowDimension(window.screenY, window.screen.height);
23562364
const normalizedX = normalizeWindowDimension(window.screenX, window.screen.width);
23572365
if (normalizedY <= origPropertyValues.availTop) {
@@ -2395,6 +2403,9 @@ var contentScopeFeatures = (function (exports) {
23952403
}
23962404

23972405
function init$8 (args) {
2406+
const Screen = globalThis.Screen;
2407+
const screen = globalThis.screen;
2408+
23982409
origPropertyValues.availTop = overrideProperty('availTop', {
23992410
object: Screen.prototype,
24002411
origValue: screen.availTop,
@@ -2438,6 +2449,9 @@ var contentScopeFeatures = (function (exports) {
24382449
});
24392450

24402451
function init$7 () {
2452+
const navigator = globalThis.navigator;
2453+
const Navigator = globalThis.Navigator;
2454+
24412455
/**
24422456
* Temporary storage can be used to determine hard disk usage and size.
24432457
* This will limit the max storage to 4GB without completely disabling the
@@ -2642,11 +2656,6 @@ var contentScopeFeatures = (function (exports) {
26422656
}
26432657
}
26442658

2645-
// support node-requires (for test import)
2646-
if (typeof module !== 'undefined' && module.exports) {
2647-
module.exports = Cookie;
2648-
}
2649-
26502659
let loadedPolicyResolve;
26512660
// Listen for a message from the content script which will configure the policy for this context
26522661
const trackerHosts = new Set();
@@ -2655,8 +2664,10 @@ var contentScopeFeatures = (function (exports) {
26552664
* Apply an expiry policy to cookies set via document.cookie.
26562665
*/
26572666
function applyCookieExpiryPolicy () {
2658-
const cookieSetter = Object.getOwnPropertyDescriptor(Document.prototype, 'cookie').set;
2659-
const cookieGetter = Object.getOwnPropertyDescriptor(Document.prototype, 'cookie').get;
2667+
const document = globalThis.document;
2668+
const Error = globalThis.Error;
2669+
const cookieSetter = Object.getOwnPropertyDescriptor(globalThis.Document.prototype, 'cookie').set;
2670+
const cookieGetter = Object.getOwnPropertyDescriptor(globalThis.Document.prototype, 'cookie').get;
26602671
const lineTest = /(\()?(http[^)]+):[0-9]+:[0-9]+(\))?/;
26612672

26622673
const loadPolicy = new Promise((resolve) => {
@@ -2762,6 +2773,8 @@ var contentScopeFeatures = (function (exports) {
27622773

27632774
// Set up 1st party cookie blocker
27642775
function load (args) {
2776+
trackerHosts.clear();
2777+
27652778
// The cookie expiry policy is injected into every frame immediately so that no cookie will
27662779
// be missed.
27672780
applyCookieExpiryPolicy();
@@ -2787,14 +2800,14 @@ var contentScopeFeatures = (function (exports) {
27872800

27882801
function blockCookies (debug) {
27892802
// disable setting cookies
2790-
defineProperty(document, 'cookie', {
2803+
defineProperty(globalThis.document, 'cookie', {
27912804
configurable: false,
27922805
set: function (value) {
27932806
if (debug) {
27942807
postDebugMessage('jscookie', {
27952808
action: 'block',
27962809
reason: 'tracker frame',
2797-
documentUrl: document.location.href,
2810+
documentUrl: globalThis.document.location.href,
27982811
scriptOrigins: [],
27992812
value: value
28002813
});
@@ -2805,7 +2818,7 @@ var contentScopeFeatures = (function (exports) {
28052818
postDebugMessage('jscookie', {
28062819
action: 'block',
28072820
reason: 'tracker frame',
2808-
documentUrl: document.location.href,
2821+
documentUrl: globalThis.document.location.href,
28092822
scriptOrigins: [],
28102823
value: 'getter'
28112824
});
@@ -2817,7 +2830,7 @@ var contentScopeFeatures = (function (exports) {
28172830

28182831
function init (args) {
28192832
args.cookie.debug = args.debug;
2820-
if (window.top !== window && args.cookie.isTrackerFrame && args.cookie.shouldBlock && args.cookie.isThirdParty) {
2833+
if (globalThis.top !== globalThis && args.cookie.isTrackerFrame && args.cookie.shouldBlock && args.cookie.isThirdParty) {
28212834
// overrides expiry policy with blocking - only in subframes
28222835
blockCookies(args.debug);
28232836
}

src/cookie.js

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,3 @@ export class Cookie {
4848
return this.parts.join(';')
4949
}
5050
}
51-
52-
// support node-requires (for test import)
53-
if (typeof module !== 'undefined' && module.exports) {
54-
module.exports = Cookie
55-
}

0 commit comments

Comments
 (0)