Skip to content

Commit 8967d9b

Browse files
committed
Improve 'prevent-eval-if' — return original value for 'eval.toString()'
1 parent aca8013 commit 8967d9b

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

src/scriptlets/prevent-eval-if.js

+3
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@ export function preventEvalIf(source, search) {
4141
hit(source, payload);
4242
return undefined;
4343
}.bind(window);
44+
45+
// Protect window.eval from native code check
46+
window.eval.toString = nativeEval.toString.bind(nativeEval);
4447
}
4548

4649
export const preventEvalIfNames = [

tests/scriptlets/prevent-eval-if.test.js

+18
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,24 @@ test('AG prevent-eval-if works', (assert) => {
5252
assert.strictEqual(secondActual, undefined, 'result of eval evaluation should be undefined');
5353
});
5454

55+
test('AG prevent-eval-if works, check toString', (assert) => {
56+
const originalEvalString = window.eval.toString();
57+
58+
runScriptlet(name, ['/adblock/']);
59+
60+
const agPreventEvalIf = 'agPreventEvalIf';
61+
62+
const evalWrapper = eval;
63+
const firstActual = evalWrapper(`(function () {return '${agPreventEvalIf}'})()`);
64+
assert.strictEqual(window.hit, undefined, 'hit function should not fire for not matched function');
65+
assert.strictEqual(firstActual, agPreventEvalIf, 'result of eval evaluation should exist');
66+
67+
const secondActual = evalWrapper(`(function () {const adblock = true; return '${agPreventEvalIf}'})()`);
68+
assert.strictEqual(window.hit, 'FIRED', 'hit function should fire');
69+
assert.strictEqual(secondActual, undefined, 'result of eval evaluation should be undefined');
70+
assert.strictEqual(window.eval.toString(), originalEvalString, 'toString should not be changed');
71+
});
72+
5573
test('does not work -- invalid regexp pattern', (assert) => {
5674
runScriptlet(name, ['/\\/']);
5775

0 commit comments

Comments
 (0)