Skip to content

Commit 0df7faf

Browse files
committed
Improve noeval-if scriptlet
Related feedback: uBlockOrigin/uBlock-issues#2907 (comment)
1 parent 17c6603 commit 0df7faf

File tree

2 files changed

+59
-34
lines changed

2 files changed

+59
-34
lines changed

src/js/resources/noeval.js

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*******************************************************************************
2+
3+
uBlock Origin - a comprehensive, efficient content blocker
4+
Copyright (C) 2019-present Raymond Hill
5+
6+
This program is free software: you can redistribute it and/or modify
7+
it under the terms of the GNU General Public License as published by
8+
the Free Software Foundation, either version 3 of the License, or
9+
(at your option) any later version.
10+
11+
This program is distributed in the hope that it will be useful,
12+
but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
GNU General Public License for more details.
15+
16+
You should have received a copy of the GNU General Public License
17+
along with this program. If not, see {http://www.gnu.org/licenses/}.
18+
19+
Home: https://github.com/gorhill/uBlock
20+
21+
*/
22+
23+
import { proxyApplyFn } from './proxy-apply.js';
24+
import { registerScriptlet } from './base.js';
25+
import { safeSelf } from './safe-self.js';
26+
27+
/******************************************************************************/
28+
29+
function noEvalIf(
30+
needle = ''
31+
) {
32+
if ( typeof needle !== 'string' ) { return; }
33+
const safe = safeSelf();
34+
const logPrefix = safe.makeLogPrefix('noeval-if', needle);
35+
const reNeedle = safe.patternToRegex(needle);
36+
proxyApplyFn('eval', function(context) {
37+
const { callArgs } = context;
38+
const a = String(callArgs[0]);
39+
if ( needle !== '' && reNeedle.test(a) ) {
40+
safe.uboLog(logPrefix, 'Prevented:\n', a);
41+
return;
42+
}
43+
if ( needle === '' || safe.logLevel > 1 ) {
44+
safe.uboLog(logPrefix, 'Not prevented:\n', a);
45+
}
46+
return context.reflect();
47+
});
48+
}
49+
registerScriptlet(noEvalIf, {
50+
name: 'noeval-if.js',
51+
aliases: [
52+
'prevent-eval-if.js',
53+
],
54+
dependencies: [
55+
proxyApplyFn,
56+
safeSelf,
57+
],
58+
});

src/js/resources/scriptlets.js

Lines changed: 1 addition & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
import './attribute.js';
2424
import './href-sanitizer.js';
25+
import './noeval.js';
2526
import './replace-argument.js';
2627
import './spoof-css.js';
2728
import './prevent-settimeout.js';
@@ -1598,40 +1599,6 @@ function adjustSetTimeout(
15981599

15991600
/******************************************************************************/
16001601

1601-
builtinScriptlets.push({
1602-
name: 'noeval-if.js',
1603-
aliases: [
1604-
'prevent-eval-if.js',
1605-
],
1606-
fn: noEvalIf,
1607-
dependencies: [
1608-
'safe-self.fn',
1609-
],
1610-
});
1611-
function noEvalIf(
1612-
needle = ''
1613-
) {
1614-
if ( typeof needle !== 'string' ) { return; }
1615-
const safe = safeSelf();
1616-
const logPrefix = safe.makeLogPrefix('noeval-if', needle);
1617-
const reNeedle = safe.patternToRegex(needle);
1618-
window.eval = new Proxy(window.eval, { // jshint ignore: line
1619-
apply: function(target, thisArg, args) {
1620-
const a = String(args[0]);
1621-
if ( needle !== '' && reNeedle.test(a) ) {
1622-
safe.uboLog(logPrefix, 'Prevented:\n', a);
1623-
return;
1624-
}
1625-
if ( needle === '' || safe.logLevel > 1 ) {
1626-
safe.uboLog(logPrefix, 'Not prevented:\n', a);
1627-
}
1628-
return Reflect.apply(target, thisArg, args);
1629-
}
1630-
});
1631-
}
1632-
1633-
/******************************************************************************/
1634-
16351602
builtinScriptlets.push({
16361603
name: 'prevent-fetch.js',
16371604
aliases: [

0 commit comments

Comments
 (0)