Skip to content

Commit 93bacbd

Browse files
Allow debugging of globalObj for testing purposes (#79)
1 parent cb9ca46 commit 93bacbd

File tree

6 files changed

+62
-29
lines changed

6 files changed

+62
-29
lines changed

build/apple/contentScope.js

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -723,8 +723,8 @@
723723

724724
// Only use globalThis for testing this breaks window.wrappedJSObject code in Firefox
725725
// eslint-disable-next-line no-global-assign
726-
const globalObj = typeof window === 'undefined' ? globalThis : window;
727-
const Error$1 = globalObj.Error;
726+
let globalObj = typeof window === 'undefined' ? globalThis : window;
727+
let Error$1 = globalObj.Error;
728728

729729
function getDataKeySync (sessionKey, domainKey, inputData) {
730730
// eslint-disable-next-line new-cap
@@ -1264,15 +1264,21 @@
12641264
return framingOrigin
12651265
}
12661266

1267+
/**
1268+
* Returns true if hostname is a subset of exceptionDomain or an exact match.
1269+
* @param {string} hostname
1270+
* @param {string} exceptionDomain
1271+
* @returns {boolean}
1272+
*/
1273+
function matchHostname (hostname, exceptionDomain) {
1274+
return hostname === exceptionDomain || hostname.endsWith(`.${exceptionDomain}`)
1275+
}
1276+
12671277
let protectionExempted = true;
12681278
const tabOrigin = getTabOrigin();
12691279
try {
12701280
const tabUrl = new URL(tabOrigin);
12711281

1272-
function matchHostname (hostname, exceptionDomain) {
1273-
return hostname === exceptionDomain || hostname.endsWith(`.${exceptionDomain}`)
1274-
}
1275-
12761282
const tabExempted = exceptions.some((exception) => {
12771283
return matchHostname(tabUrl.hostname, exception.domain)
12781284
});
@@ -1398,7 +1404,7 @@
13981404
debugHelper('ignore', 'disabled', setCookieContext);
13991405
return
14001406
}
1401-
const sameSiteScript = [...scriptOrigins].every((host) => host === tabRegisteredDomain || host.endsWith(`.${tabRegisteredDomain}`));
1407+
const sameSiteScript = [...scriptOrigins].every((host) => matchHostname(host, tabRegisteredDomain));
14021408
if (sameSiteScript) {
14031409
// cookies set by scripts loaded on the same site as the site are not modified
14041410
debugHelper('ignore', '1p sameSite', setCookieContext);

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: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -674,8 +674,8 @@
674674

675675
// Only use globalThis for testing this breaks window.wrappedJSObject code in Firefox
676676
// eslint-disable-next-line no-global-assign
677-
const globalObj = typeof window === 'undefined' ? globalThis : window;
678-
const Error$1 = globalObj.Error;
677+
let globalObj = typeof window === 'undefined' ? globalThis : window;
678+
let Error$1 = globalObj.Error;
679679

680680
function getDataKeySync (sessionKey, domainKey, inputData) {
681681
// eslint-disable-next-line new-cap
@@ -1233,15 +1233,21 @@
12331233
return framingOrigin
12341234
}
12351235

1236+
/**
1237+
* Returns true if hostname is a subset of exceptionDomain or an exact match.
1238+
* @param {string} hostname
1239+
* @param {string} exceptionDomain
1240+
* @returns {boolean}
1241+
*/
1242+
function matchHostname (hostname, exceptionDomain) {
1243+
return hostname === exceptionDomain || hostname.endsWith(`.${exceptionDomain}`)
1244+
}
1245+
12361246
let protectionExempted = true;
12371247
const tabOrigin = getTabOrigin();
12381248
try {
12391249
const tabUrl = new URL(tabOrigin);
12401250

1241-
function matchHostname (hostname, exceptionDomain) {
1242-
return hostname === exceptionDomain || hostname.endsWith(`.${exceptionDomain}`)
1243-
}
1244-
12451251
const tabExempted = exceptions.some((exception) => {
12461252
return matchHostname(tabUrl.hostname, exception.domain)
12471253
});
@@ -1367,7 +1373,7 @@
13671373
debugHelper('ignore', 'disabled', setCookieContext);
13681374
return
13691375
}
1370-
const sameSiteScript = [...scriptOrigins].every((host) => host === tabRegisteredDomain || host.endsWith(`.${tabRegisteredDomain}`));
1376+
const sameSiteScript = [...scriptOrigins].every((host) => matchHostname(host, tabRegisteredDomain));
13711377
if (sameSiteScript) {
13721378
// cookies set by scripts loaded on the same site as the site are not modified
13731379
debugHelper('ignore', '1p sameSite', setCookieContext);

build/integration/contentScope.js

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -674,8 +674,8 @@
674674

675675
// Only use globalThis for testing this breaks window.wrappedJSObject code in Firefox
676676
// eslint-disable-next-line no-global-assign
677-
const globalObj = typeof window === 'undefined' ? globalThis : window;
678-
const Error$1 = globalObj.Error;
677+
let globalObj = typeof window === 'undefined' ? globalThis : window;
678+
let Error$1 = globalObj.Error;
679679

680680
function getDataKeySync (sessionKey, domainKey, inputData) {
681681
// eslint-disable-next-line new-cap
@@ -1215,15 +1215,21 @@
12151215
return framingOrigin
12161216
}
12171217

1218+
/**
1219+
* Returns true if hostname is a subset of exceptionDomain or an exact match.
1220+
* @param {string} hostname
1221+
* @param {string} exceptionDomain
1222+
* @returns {boolean}
1223+
*/
1224+
function matchHostname (hostname, exceptionDomain) {
1225+
return hostname === exceptionDomain || hostname.endsWith(`.${exceptionDomain}`)
1226+
}
1227+
12181228
let protectionExempted = true;
12191229
const tabOrigin = getTabOrigin();
12201230
try {
12211231
const tabUrl = new URL(tabOrigin);
12221232

1223-
function matchHostname (hostname, exceptionDomain) {
1224-
return hostname === exceptionDomain || hostname.endsWith(`.${exceptionDomain}`)
1225-
}
1226-
12271233
const tabExempted = exceptions.some((exception) => {
12281234
return matchHostname(tabUrl.hostname, exception.domain)
12291235
});
@@ -1349,7 +1355,7 @@
13491355
debugHelper('ignore', 'disabled', setCookieContext);
13501356
return
13511357
}
1352-
const sameSiteScript = [...scriptOrigins].every((host) => host === tabRegisteredDomain || host.endsWith(`.${tabRegisteredDomain}`));
1358+
const sameSiteScript = [...scriptOrigins].every((host) => matchHostname(host, tabRegisteredDomain));
13531359
if (sameSiteScript) {
13541360
// cookies set by scripts loaded on the same site as the site are not modified
13551361
debugHelper('ignore', '1p sameSite', setCookieContext);

src/features/cookie.js

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,15 +45,21 @@ function getTabOrigin () {
4545
return framingOrigin
4646
}
4747

48+
/**
49+
* Returns true if hostname is a subset of exceptionDomain or an exact match.
50+
* @param {string} hostname
51+
* @param {string} exceptionDomain
52+
* @returns {boolean}
53+
*/
54+
function matchHostname (hostname, exceptionDomain) {
55+
return hostname === exceptionDomain || hostname.endsWith(`.${exceptionDomain}`)
56+
}
57+
4858
let protectionExempted = true
4959
const tabOrigin = getTabOrigin()
5060
try {
5161
const tabUrl = new URL(tabOrigin)
5262

53-
function matchHostname (hostname, exceptionDomain) {
54-
return hostname === exceptionDomain || hostname.endsWith(`.${exceptionDomain}`)
55-
}
56-
5763
const tabExempted = exceptions.some((exception) => {
5864
return matchHostname(tabUrl.hostname, exception.domain)
5965
})
@@ -179,7 +185,7 @@ export function load (args) {
179185
debugHelper('ignore', 'disabled', setCookieContext)
180186
return
181187
}
182-
const sameSiteScript = [...scriptOrigins].every((host) => host === tabRegisteredDomain || host.endsWith(`.${tabRegisteredDomain}`))
188+
const sameSiteScript = [...scriptOrigins].every((host) => matchHostname(host, tabRegisteredDomain))
183189
if (sameSiteScript) {
184190
// cookies set by scripts loaded on the same site as the site are not modified
185191
debugHelper('ignore', '1p sameSite', setCookieContext)

src/utils.js

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,17 @@ import { sjcl } from '../lib/sjcl.js'
33

44
// Only use globalThis for testing this breaks window.wrappedJSObject code in Firefox
55
// eslint-disable-next-line no-global-assign
6-
const globalObj = typeof window === 'undefined' ? globalThis : window
7-
const Error = globalObj.Error
6+
let globalObj = typeof window === 'undefined' ? globalThis : window
7+
let Error = globalObj.Error
8+
9+
/**
10+
* Used for testing to override the globals used within this file.
11+
* @param {window} globalObjIn
12+
*/
13+
export function setGlobal (globalObjIn) {
14+
globalObj = globalObjIn
15+
Error = globalObj.Error
16+
}
817

918
// Tests don't define this variable so fallback to behave like chrome
1019
const hasMozProxies = typeof mozProxies !== 'undefined' ? mozProxies : false

0 commit comments

Comments
 (0)