Skip to content

Commit 6772215

Browse files
committed
Improve prevent-bab scriptlet
Related issue: uBlockOrigin/uBlock-issues#4054
1 parent b123c23 commit 6772215

3 files changed

Lines changed: 49 additions & 38 deletions

File tree

src/js/redirect-resources.js

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -118,10 +118,6 @@ export default new Map([
118118
[ 'nitropay_ads.js', {
119119
data: 'text',
120120
} ],
121-
[ 'nobab.js', {
122-
alias: [ 'bab-defuser.js', 'prevent-bab.js' ],
123-
data: 'text',
124-
} ],
125121
[ 'nobab2.js', {
126122
data: 'text',
127123
} ],
Lines changed: 48 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,15 @@
1919
Home: https://github.com/gorhill/uBlock
2020
*/
2121

22-
(function() {
23-
'use strict';
22+
import { proxyApplyFn } from './proxy-apply.js';
23+
import { registerScriptlet } from './base.js';
24+
import { safeSelf } from './safe-self.js';
25+
26+
/******************************************************************************/
27+
28+
function preventBab() {
29+
const safe = safeSelf();
30+
const logPrefix = safe.makeLogPrefix('prevent-bab');
2431
const signatures = [
2532
[ 'blockadblock' ],
2633
[ 'babasbm' ],
@@ -40,48 +47,55 @@
4047
'clientWidth',
4148
'localStorage',
4249
'Math',
43-
'random'
50+
'random',
4451
],
4552
];
4653
const check = function(s) {
47-
for ( let i = 0; i < signatures.length; i++ ) {
48-
const tokens = signatures[i];
54+
if ( typeof s !== 'string' ) { return false; }
55+
for ( const tokens of signatures ) {
4956
let match = 0;
50-
for ( let j = 0; j < tokens.length; j++ ) {
51-
const token = tokens[j];
52-
const pos = token instanceof RegExp
53-
? s.search(token)
54-
: s.indexOf(token);
55-
if ( pos !== -1 ) { match += 1; }
57+
for ( const token of tokens ) {
58+
const hit = token instanceof RegExp
59+
? token.test(s)
60+
: s.includes(token);
61+
if ( hit ) { match += 1; }
5662
}
5763
if ( (match / tokens.length) >= 0.8 ) { return true; }
5864
}
5965
return false;
6066
};
61-
window.eval = new Proxy(window.eval, { // jshint ignore: line
62-
apply: function(target, thisArg, args) {
63-
const a = args[0];
64-
if ( typeof a !== 'string' || !check(a) ) {
65-
return target.apply(thisArg, args);
66-
}
67-
if ( document.body ) {
68-
document.body.style.removeProperty('visibility');
69-
}
70-
let el = document.getElementById('babasbmsgx');
71-
if ( el ) {
72-
el.parentNode.removeChild(el);
73-
}
67+
proxyApplyFn('eval', function(context) {
68+
const a = context.callArgs[0];
69+
if ( !check(a) ) {
70+
return context.reflect();
71+
}
72+
safe.uboLog(logPrefix, 'Prevented');
73+
if ( document.body ) {
74+
document.body.style.removeProperty('visibility');
75+
}
76+
const el = document.getElementById('babasbmsgx');
77+
if ( el ) {
78+
el.parentNode.removeChild(el);
7479
}
7580
});
76-
window.setTimeout = new Proxy(window.setTimeout, {
77-
apply: function(target, thisArg, args) {
78-
const a = args[0];
79-
if (
80-
typeof a !== 'string' ||
81-
/\.bab_elementid.$/.test(a) === false
82-
) {
83-
return target.apply(thisArg, args);
84-
}
81+
proxyApplyFn('setTimeout', function(context) {
82+
const { callArgs } = context;
83+
const a = callArgs[0];
84+
if ( typeof a === 'string' && /\.bab_elementid.$/.test(a) ) {
85+
callArgs[0] = ( ) => { };
86+
safe.uboLog(logPrefix, 'Prevented');
8587
}
88+
return context.reflect();
8689
});
87-
})();
90+
}
91+
registerScriptlet(preventBab, {
92+
name: 'prevent-bab.js',
93+
aliases: [
94+
'bab-defuser.js',
95+
'nobab.js',
96+
],
97+
dependencies: [
98+
proxyApplyFn,
99+
safeSelf,
100+
],
101+
});

src/js/resources/scriptlets.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import './json-prune.js';
2828
import './noeval.js';
2929
import './object-prune.js';
3030
import './prevent-addeventlistener.js';
31+
import './prevent-bab.js';
3132
import './prevent-dialog.js';
3233
import './prevent-fetch.js';
3334
import './prevent-innerHTML.js';

0 commit comments

Comments
 (0)