Skip to content

Commit 5d4eda9

Browse files
Add in more typing checks
1 parent d223cda commit 5d4eda9

File tree

13 files changed

+343
-78
lines changed

13 files changed

+343
-78
lines changed

build/apple/contentScope.js

Lines changed: 82 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -725,18 +725,30 @@
725725
// eslint-disable-next-line no-global-assign
726726
const globalObj = typeof window === 'undefined' ? globalThis : window;
727727

728+
/**
729+
* @param {string} sessionKey
730+
* @param {string} domainKey
731+
* @param {number} inputData
732+
*/
728733
function getDataKeySync (sessionKey, domainKey, inputData) {
729734
// eslint-disable-next-line new-cap
730735
const hmac = new sjcl.misc.hmac(sjcl.codec.utf8String.toBits(sessionKey + domainKey), sjcl.hash.sha256);
731736
return sjcl.codec.hex.fromBits(hmac.encrypt(inputData))
732737
}
733738

734-
// linear feedback shift register to find a random approximation
739+
/**
740+
* Linear feedback shift register to find a random approximation
741+
* @param {number} v
742+
*/
735743
function nextRandom (v) {
736744
return Math.abs((v >> 1) | (((v << 62) ^ (v << 61)) & (~(~0 << 63) << 62)))
737745
}
738746

739747
const exemptionLists = {};
748+
/**
749+
* @param {string | number} type
750+
* @param {string} url
751+
*/
740752
function shouldExemptUrl (type, url) {
741753
for (const regex of exemptionLists[type]) {
742754
if (regex.test(url)) {
@@ -748,6 +760,9 @@
748760

749761
let debug = false;
750762

763+
/**
764+
* @param {{ debug?: any; stringExemptionLists?: any; }} args
765+
*/
751766
function initStringExemptionLists (args) {
752767
const { stringExemptionLists } = args;
753768
debug = args.debug;
@@ -759,14 +774,17 @@
759774
}
760775
}
761776

762-
// Checks the stack trace if there are known libraries that are broken.
777+
/**
778+
* Checks the stack trace if there are known libraries that are broken.
779+
* @param {string} type
780+
*/
763781
function shouldExemptMethod (type) {
764782
// Short circuit stack tracing if we don't have checks
765783
if (!(type in exemptionLists) || exemptionLists[type].length === 0) {
766784
return false
767785
}
768786
try {
769-
const errorLines = new Error().stack.split('\n');
787+
const errorLines = new Error().stack?.split('\n') || [];
770788
const errorFiles = new Set();
771789
// Should cater for Chrome and Firefox stacks, we only care about https? resources.
772790
const lineTest = /(\()?(http[^)]+):[0-9]+:[0-9]+(\))?/;
@@ -790,7 +808,11 @@
790808
return false
791809
}
792810

793-
// Iterate through the key, passing an item index and a byte to be modified
811+
/**
812+
* Iterate through the key, passing an item index and a byte to be modified
813+
* @param {any} key
814+
* @param {{ (item: any, byte: any): void; (arg0: any, arg1: any): any; }} callback
815+
*/
794816
function iterateDataKey (key, callback) {
795817
let item = key.charCodeAt(0);
796818
for (const i in key) {
@@ -811,12 +833,18 @@
811833
}
812834
}
813835

836+
/**
837+
* @param {{ site: { isBroken: any; allowlisted: any; enabledFeatures: string | any[]; }; }} args
838+
* @param {string} feature
839+
*/
814840
function isFeatureBroken (args, feature) {
815841
return args.site.isBroken || args.site.allowlisted || !args.site.enabledFeatures.includes(feature)
816842
}
817843

818844
/**
819845
* For each property defined on the object, update it with the target value.
846+
* @param {string} name
847+
* @param {{ object: any; origValue: any; targetValue: any; }} prop
820848
*/
821849
function overrideProperty (name, prop) {
822850
// Don't update if existing value is undefined or null
@@ -840,14 +868,22 @@
840868
return prop.origValue
841869
}
842870

871+
/**
872+
* @param {typeof globalThis} object
873+
* @param {PropertyKey} propertyName
874+
* @param {PropertyDescriptor & ThisType<any>} descriptor
875+
*/
843876
function defineProperty (object, propertyName, descriptor) {
844877
{
845878
Object.defineProperty(object, propertyName, descriptor);
846879
}
847880
}
848881

882+
/**
883+
* @param {string} dashCaseText
884+
*/
849885
function camelcase (dashCaseText) {
850-
return dashCaseText.replace(/-(.)/g, (match, letter) => {
886+
return dashCaseText.replace(/-(.)/g, (/** @type {any} */ match, /** @type {string} */ letter) => {
851887
return letter.toUpperCase()
852888
})
853889
}
@@ -874,10 +910,15 @@
874910
return result === 'enabled'
875911
}
876912

913+
/**
914+
* @template {object} P
915+
* @typedef {(target: object, thisArg: P, args: object) => void} ApplyMethod<P>
916+
*/
917+
877918
/**
878919
* @template {object} P
879920
* @typedef {object} ProxyObject<P>
880-
* @property {(target?: object, thisArg?: P, args?: object) => void} apply
921+
* @property {ApplyMethod<P>} apply?
881922
*/
882923

883924
/**
@@ -895,14 +936,15 @@
895936
this.property = property;
896937
this.featureName = featureName;
897938
this.camelFeatureName = camelcase(this.featureName);
939+
/** @type ApplyMethod<P> */
898940
const outputHandler = (...args) => {
899941
const isExempt = shouldExemptMethod(this.camelFeatureName);
900942
if (debug) {
901943
postDebugMessage(this.camelFeatureName, {
902944
action: isExempt ? 'ignore' : 'restrict',
903945
kind: this.property,
904946
documentUrl: document.location.href,
905-
stack: new Error().stack,
947+
stack: new Error().stack || '',
906948
args: JSON.stringify(args[2])
907949
});
908950
}
@@ -928,6 +970,10 @@
928970
}
929971
}
930972

973+
/**
974+
* @param {any} feature
975+
* @param {{ action: string; kind: string; documentUrl: string; stack: string; args: string; }} message
976+
*/
931977
function postDebugMessage (feature, message) {
932978
globalObj.postMessage({
933979
action: feature,
@@ -1056,7 +1102,7 @@
10561102
const featureName = 'fingerprinting-audio';
10571103

10581104
// In place modify array data to remove fingerprinting
1059-
function transformArrayData (channelData, domainKey, sessionKey, thisArg) {
1105+
function transformArrayData (channelData, domainKey, sessionKey, thisArg, args) {
10601106
let { audioKey } = getCachedResponse(thisArg, args);
10611107
if (!audioKey) {
10621108
let cdSum = 0;
@@ -1164,7 +1210,7 @@
11641210
* as well as prevent any script from listening to events.
11651211
*/
11661212
function init$b (args) {
1167-
if (globalThis.navigator.getBattery) {
1213+
if ('getBattery' in globalThis.navigator) {
11681214
const BatteryManager = globalThis.BatteryManager;
11691215

11701216
const spoofedValues = {
@@ -2157,23 +2203,35 @@
21572203

21582204
var seedrandom = sr;
21592205

2206+
/**
2207+
* @typedef {CanvasRenderingContext2D | WebGL2RenderingContext | WebGLRenderingContext} CanvasContext
2208+
*/
2209+
21602210
/**
21612211
* @param {HTMLCanvasElement} canvas
21622212
* @param {string} domainKey
21632213
* @param {string} sessionKey
21642214
* @param {any} getImageDataProxy
2165-
* @param {CanvasRenderingContext2D | WebGL2RenderingContext | WebGLRenderingContext} ctx?
2215+
* @param {CanvasContext} ctx?
2216+
* @return {{offScreenCanvas: HTMLCanvasElement, offScreenCtx: CanvasContext}?}
21662217
*/
21672218
function computeOffScreenCanvas (canvas, domainKey, sessionKey, getImageDataProxy, ctx) {
21682219
if (!ctx) {
2169-
ctx = canvas.getContext('2d');
2220+
const newCtx = canvas.getContext('2d');
2221+
if (newCtx === null) {
2222+
return null
2223+
}
2224+
ctx = newCtx;
21702225
}
21712226

21722227
// Make a off-screen canvas and put the data there
21732228
const offScreenCanvas = document.createElement('canvas');
21742229
offScreenCanvas.width = canvas.width;
21752230
offScreenCanvas.height = canvas.height;
21762231
const offScreenCtx = offScreenCanvas.getContext('2d');
2232+
if (offScreenCtx === null) {
2233+
return null
2234+
}
21772235

21782236
let rasterizedCtx = ctx;
21792237
// If we're not a 2d canvas we need to rasterise first into 2d
@@ -2517,6 +2575,7 @@
25172575

25182576
overrideProperty('keyboard', {
25192577
object: Navigator.prototype,
2578+
// @ts-ignore
25202579
origValue: navigator.keyboard,
25212580
targetValue: undefined
25222581
});
@@ -2527,6 +2586,7 @@
25272586
});
25282587
overrideProperty('deviceMemory', {
25292588
object: Navigator.prototype,
2589+
// @ts-ignore
25302590
origValue: navigator.deviceMemory,
25312591
targetValue: 8
25322592
});
@@ -2587,11 +2647,12 @@
25872647
setWindowPropertyValue('screenTop', normalizedY);
25882648
}
25892649

2590-
if (top.window.outerHeight >= origPropertyValues.availHeight - 1) {
2591-
setWindowPropertyValue('outerHeight', top.window.screen.height);
2650+
const outerHeight = top?.window.outerHeight || 0;
2651+
if (outerHeight >= origPropertyValues.availHeight - 1) {
2652+
setWindowPropertyValue('outerHeight', top?.window.screen.height);
25922653
} else {
25932654
try {
2594-
setWindowPropertyValue('outerHeight', top.window.outerHeight);
2655+
setWindowPropertyValue('outerHeight', top?.window.outerHeight);
25952656
} catch (e) {
25962657
// top not accessible to certain iFrames, so ignore.
25972658
}
@@ -2605,11 +2666,12 @@
26052666
setWindowPropertyValue('screenLeft', normalizedX);
26062667
}
26072668

2608-
if (top.window.outerWidth >= origPropertyValues.availWidth - 1) {
2609-
setWindowPropertyValue('outerWidth', top.window.screen.width);
2669+
const outerWidth = top?.window.outerWidth || 0;
2670+
if (outerWidth >= origPropertyValues.availWidth - 1) {
2671+
setWindowPropertyValue('outerWidth', top?.window.screen.width);
26102672
} else {
26112673
try {
2612-
setWindowPropertyValue('outerWidth', top.window.outerWidth);
2674+
setWindowPropertyValue('outerWidth', top?.window.outerWidth);
26132675
} catch (e) {
26142676
// top not accessible to certain iFrames, so ignore.
26152677
}
@@ -2619,17 +2681,20 @@
26192681
}
26202682
}
26212683

2684+
// @ts-ignore
26222685
function init$8 (args) {
26232686
const Screen = globalThis.Screen;
26242687
const screen = globalThis.screen;
26252688

26262689
origPropertyValues.availTop = overrideProperty('availTop', {
26272690
object: Screen.prototype,
2691+
// @ts-ignore
26282692
origValue: screen.availTop,
26292693
targetValue: 0
26302694
});
26312695
origPropertyValues.availLeft = overrideProperty('availLeft', {
26322696
object: Screen.prototype,
2697+
// @ts-ignore
26332698
origValue: screen.availLeft,
26342699
targetValue: 0
26352700
});

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.

0 commit comments

Comments
 (0)