From e0e669822a596bca4f4e917632b0d899b6232333 Mon Sep 17 00:00:00 2001 From: Stanislav A Date: Thu, 15 Dec 2022 19:24:40 +0300 Subject: [PATCH] add typecheck for delay + tests --- src/helpers/prevent-utils.js | 7 +++-- tests/scriptlets/prevent-setInterval.test.js | 32 ++++++++++++++++++++ tests/scriptlets/prevent-setTimeout.test.js | 32 ++++++++++++++++++++ 3 files changed, 68 insertions(+), 3 deletions(-) diff --git a/src/helpers/prevent-utils.js b/src/helpers/prevent-utils.js index 08546bdb..8868215b 100644 --- a/src/helpers/prevent-utils.js +++ b/src/helpers/prevent-utils.js @@ -46,7 +46,8 @@ export const isPreventionNeeded = ({ const { isInvertedDelayMatch, delayMatch } = parseDelayArg(matchDelay); // https://github.com/AdguardTeam/Scriptlets/issues/247 - const roundedDelay = Math.floor(delay); + // type check is required to pass non-number values like null + const parsedDelay = typeof delay === 'number' ? Math.floor(delay) : delay; let shouldPrevent = false; // https://github.com/AdguardTeam/Scriptlets/issues/105 @@ -54,10 +55,10 @@ export const isPreventionNeeded = ({ if (delayMatch === null) { shouldPrevent = matchRegexp.test(callbackStr) !== isInvertedMatch; } else if (!matchCallback) { - shouldPrevent = (roundedDelay === delayMatch) !== isInvertedDelayMatch; + shouldPrevent = (parsedDelay === delayMatch) !== isInvertedDelayMatch; } else { shouldPrevent = matchRegexp.test(callbackStr) !== isInvertedMatch - && (roundedDelay === delayMatch) !== isInvertedDelayMatch; + && (parsedDelay === delayMatch) !== isInvertedDelayMatch; } return shouldPrevent; }; diff --git a/tests/scriptlets/prevent-setInterval.test.js b/tests/scriptlets/prevent-setInterval.test.js index bf6a54c7..2c5ffc4e 100644 --- a/tests/scriptlets/prevent-setInterval.test.js +++ b/tests/scriptlets/prevent-setInterval.test.js @@ -404,3 +404,35 @@ test('match any callback + decimal delay', (assert) => { const intervalTest3 = setInterval(third, 10.95); testIntervals.push(intervalTest3); }); + +test('match any callback + non-number delay', (assert) => { + const done = assert.async(); + window.one = 'old one'; + window.two = 'old two'; + window.three = 'old three'; + // We need to run our assertion after all timeouts + setTimeout(() => { + assert.equal(window.one, 'old one', 'property \'one\' should NOT be changed'); + assert.equal(window.two, 'NEW TWO', 'property \'two\' should be changed'); + assert.equal(window.three, 'NEW THREE', 'property \'three\' should be changed'); + assert.strictEqual(window.hit, 'FIRED', 'hit fired'); + done(); + }, 100); + + // run scriptlet code + const scriptletArgs = ['', '25']; + runScriptlet(name, scriptletArgs); + + // only this one SHOULD NOT be prevented because of delay mismatch + const one = () => { window.one = 'NEW ONE'; }; + const intervalTest1 = setInterval(one, 25.123); + testIntervals.push(intervalTest1); + + const second = () => { window.two = 'NEW TWO'; }; + const intervalTest2 = setInterval(second, null); + testIntervals.push(intervalTest2); + + const third = () => { window.three = 'NEW THREE'; }; + const intervalTest3 = setInterval(third, false); + testIntervals.push(intervalTest3); +}); diff --git a/tests/scriptlets/prevent-setTimeout.test.js b/tests/scriptlets/prevent-setTimeout.test.js index 031442b2..91534be3 100644 --- a/tests/scriptlets/prevent-setTimeout.test.js +++ b/tests/scriptlets/prevent-setTimeout.test.js @@ -405,3 +405,35 @@ test('match any callback + decimal delay', (assert) => { const timeoutTest3 = setTimeout(third, 10.95); testTimeouts.push(timeoutTest3); }); + +test('match any callback + non-number delay', (assert) => { + const done = assert.async(); + window.one = 'one'; + window.two = 'two'; + window.three = 'three'; + // We need to run our assertion after all timeouts + nativeSetTimeout(() => { + assert.equal(window.one, 'one', 'property \'one\' should NOT be changed'); + assert.equal(window.two, 'NEW TWO', 'property \'two\' should be changed'); + assert.equal(window.three, 'NEW THREE', 'property \'three\' should be changed'); + assert.strictEqual(window.hit, 'FIRED', 'hit fired'); + done(); + }, 100); + + // run scriptlet code + const scriptletArgs = ['', '30']; + runScriptlet(name, scriptletArgs); + + // only this one SHOULD NOT be prevented because of delay mismatch + const first = () => { window.one = 'NEW ONE'; }; + const timeoutTest1 = setTimeout(first, 30.153); + testTimeouts.push(timeoutTest1); + + const second = () => { window.two = 'NEW TWO'; }; + const timeoutTest2 = setTimeout(second, null); + testTimeouts.push(timeoutTest2); + + const third = () => { window.three = 'NEW THREE'; }; + const timeoutTest3 = setTimeout(third, true); + testTimeouts.push(timeoutTest3); +});