Skip to content

Commit

Permalink
AG-17027 fix issue with isAbortingSuspended in abort-current-inline-s…
Browse files Browse the repository at this point in the history
…cript

Squashed commit of the following:

commit bde3e4b
Author: Adam <adam@adguard.com>
Date:   Tue Oct 18 15:04:48 2022 +0200

    Update comment

commit 0117103
Author: Adam <adam@adguard.com>
Date:   Tue Oct 18 14:57:09 2022 +0200

    Add comments to test

commit 71d257b
Author: Adam <adam@adguard.com>
Date:   Tue Oct 18 14:45:41 2022 +0200

    Revert changes in wiki

commit 8e1ea58
Author: Adam <adam@adguard.com>
Date:   Tue Oct 18 14:38:20 2022 +0200

    Revert changes in wiki

commit 0a48e7a
Author: Adam <adam@adguard.com>
Date:   Tue Oct 18 14:12:53 2022 +0200

    Fix typo

commit f0cf3ee
Author: Adam <adam@adguard.com>
Date:   Tue Oct 18 14:07:38 2022 +0200

    Fix issue with isAbortingSuspended in abort-current-inline-script
  • Loading branch information
AdamWr committed Oct 18, 2022
1 parent e1489ee commit 932ff82
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 5 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);
}
},
};
}
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 932ff82

Please # to comment.