Skip to content

Commit

Permalink
Merge branch 'release/v1.7' into feature/AG-13337
Browse files Browse the repository at this point in the history
  • Loading branch information
stanislav-atr committed Oct 25, 2022
2 parents 4ffc6ac + af22d63 commit 009b6a7
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 8 deletions.
20 changes: 17 additions & 3 deletions src/helpers/get-descriptor-addon.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { randomId } from './random-id';
/**
* Prevent infinite loops when trapping props that could be used by scriptlet's own helpers
* Example: window.RegExp, that is used by matchStackTrace > toRegExp
*
* https://github.com/AdguardTeam/Scriptlets/issues/251
* https://github.com/AdguardTeam/Scriptlets/issues/226
* https://github.com/AdguardTeam/Scriptlets/issues/232
*
Expand All @@ -12,9 +14,21 @@ export function getDescriptorAddon() {
isAbortingSuspended: false,
isolateCallback(cb, ...args) {
this.isAbortingSuspended = true;
const result = cb(...args);
this.isAbortingSuspended = false;
return result;
// try...catch is required in case if there are more than one inline scripts
// which should be aborted.
// so after the first successful abortion, `cb(...args);` will throw error,
// and we should not stop on that and continue to abort other scripts
try {
const result = cb(...args);
this.isAbortingSuspended = false;
return result;
} catch {
this.isAbortingSuspended = false;
const rid = randomId();
// It's necessary to throw error
// otherwise script will be not aborted
throw new ReferenceError(rid);
}
},
};
}
4 changes: 3 additions & 1 deletion src/redirects/googlesyndication-adsbygoogle.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@ export function GoogleSyndicationAdsByGoogle(source) {
for (const key of Object.keys(arg)) {
if (typeof arg[key] === 'function') {
try {
arg[key].call();
// https://github.com/AdguardTeam/Scriptlets/issues/252
// argument "{}" is needed to fix issue with undefined argument
arg[key].call(this, {});
} catch {
/* empty */
}
Expand Down
13 changes: 11 additions & 2 deletions tests/redirects/googlesyndication-adsbygoogle.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,17 @@ test('Redirect testing', (assert) => {

assert.strictEqual(window.adsbygoogle.length, undefined, 'adsbygoogle.length check');
assert.strictEqual(window.adsbygoogle.push.length, 1, 'push.length check');
const pushCallback = () => {
assert.ok(true, 'callback was called');
const pushCallback = (arg) => {
try {
// Test for https://github.com/AdguardTeam/Scriptlets/issues/252
// If arg is not defined then error will be thrown
if (arg.whatever) {
arg.whatever = 1;
}
assert.ok(typeof arg !== 'undefined', 'arg is defined');
} catch (error) {
assert.ok(false, 'something went wrong');
}
};
const pushArg = {
test: 'test',
Expand Down
25 changes: 23 additions & 2 deletions tests/scriptlets/abort-current-inline-script.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ module(name, { beforeEach, afterEach });

const onError = (assert) => (message) => {
const browserErrorMessage = 'Script error.';
const nodePuppeteerErrorMessageRgx = /Reference error/g;
const nodePuppeteerErrorMessageRgx = /Reference error|ReferenceError/g;
const checkResult = message === browserErrorMessage
|| message.test(nodePuppeteerErrorMessageRgx);
|| nodePuppeteerErrorMessageRgx.test(message);
assert.ok(checkResult);
};

Expand Down Expand Up @@ -246,3 +246,24 @@ test('Protected from infinite loop when prop is used in a helper', (assert) => {

assert.strictEqual(window.hit, undefined, 'hit should NOT fire');
});

test('searches script by regexp - abort few inline scripts', (assert) => {
window.onerror = onError(assert);
window.shouldBeAborted = true;
window.shouldNotBeAborted = false;
const property = 'console.log';
const shouldBeAborted = 'shouldBeAborted';
const shouldNotBeAborted = 'shouldNotBeAborted';
const search = '/test|abcd|1234|qwerty/';
const scriptletArgs = [property, search];
runScriptlet(name, scriptletArgs);
addAndRemoveInlineScript(`window.${property}('test'); window.${shouldBeAborted} = false;`);
addAndRemoveInlineScript(`window.${property}('abcd'); window.${shouldBeAborted} = false;`);
addAndRemoveInlineScript(`window.${property}('1234'); window.${shouldBeAborted} = false;`);
addAndRemoveInlineScript(`window.${property}('should not be aborted'); window.${shouldNotBeAborted} = true;`);
addAndRemoveInlineScript(`window.${property}('qwerty'); window.${shouldBeAborted} = false;`);

assert.strictEqual(window.shouldBeAborted, true, 'initial value of shouldBeAborted has not changed');
assert.strictEqual(window.shouldNotBeAborted, true, 'value of shouldBeAborted has been changed from false to true');
assert.strictEqual(window.hit, 'FIRED', 'hit fired');
});

0 comments on commit 009b6a7

Please sign in to comment.