Skip to content

Commit cfeac10

Browse files
committed
[mv3] Add support for abort-on-property-read scriptlet
1 parent 70a0de9 commit cfeac10

File tree

1 file changed

+132
-0
lines changed

1 file changed

+132
-0
lines changed
Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
/*******************************************************************************
2+
3+
uBlock Origin - a browser extension to block requests.
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+
The scriptlets below are meant to be injected only into a
22+
web page context.
23+
*/
24+
25+
/* jshint esversion:11 */
26+
27+
'use strict';
28+
29+
/******************************************************************************/
30+
31+
/// name abort-on-property-read
32+
/// alias aopr
33+
34+
/******************************************************************************/
35+
36+
// Important!
37+
// Isolate from global scope
38+
(function() {
39+
40+
/******************************************************************************/
41+
42+
const ObjGetOwnPropertyDescriptor = Object.getOwnPropertyDescriptor;
43+
const ObjDefineProperty = Object.defineProperty;
44+
45+
const magic =
46+
String.fromCharCode(Date.now() % 26 + 97) +
47+
Math.floor(Math.random() * 982451653 + 982451653).toString(36);
48+
49+
const abort = function() {
50+
throw new ReferenceError(magic);
51+
};
52+
53+
const makeProxy = function(owner, chain) {
54+
const pos = chain.indexOf('.');
55+
if ( pos === -1 ) {
56+
const desc = ObjGetOwnPropertyDescriptor(owner, chain);
57+
if ( !desc || desc.get !== abort ) {
58+
ObjDefineProperty(owner, chain, {
59+
get: abort,
60+
set: function(){}
61+
});
62+
}
63+
return;
64+
}
65+
66+
const prop = chain.slice(0, pos);
67+
let v = owner[prop];
68+
chain = chain.slice(pos + 1);
69+
if ( v ) {
70+
makeProxy(v, chain);
71+
return;
72+
}
73+
74+
const desc = ObjGetOwnPropertyDescriptor(owner, prop);
75+
if ( desc && desc.set !== undefined ) { return; }
76+
77+
ObjDefineProperty(owner, prop, {
78+
get: function() { return v; },
79+
set: function(a) {
80+
v = a;
81+
if ( a instanceof Object ) {
82+
makeProxy(a, chain);
83+
}
84+
}
85+
});
86+
};
87+
88+
const scriptlet = (
89+
chain = ''
90+
) => {
91+
const owner = window;
92+
makeProxy(owner, chain);
93+
const oe = window.onerror;
94+
window.onerror = function(msg, src, line, col, error) {
95+
if ( typeof msg === 'string' && msg.indexOf(magic) !== -1 ) {
96+
return true;
97+
}
98+
if ( oe instanceof Function ) {
99+
return oe(msg, src, line, col, error);
100+
}
101+
}.bind();
102+
};
103+
104+
/******************************************************************************/
105+
106+
const argsMap = new Map(self.$argsMap$);
107+
108+
const hostnamesMap = new Map(self.$hostnamesMap$);
109+
110+
let hn;
111+
try { hn = document.location.hostname; } catch(ex) { }
112+
while ( hn ) {
113+
if ( hostnamesMap.has(hn) ) {
114+
let argsHashes = hostnamesMap.get(hn);
115+
if ( typeof argsHashes === 'number' ) { argsHashes = [ argsHashes ]; }
116+
for ( const argsHash of argsHashes ) {
117+
const details = argsMap.get(argsHash);
118+
if ( details.n && details.n.includes(hn) ) { continue; }
119+
try { scriptlet(...details.a); } catch(ex) {}
120+
}
121+
}
122+
const pos = hn.indexOf('.');
123+
if ( pos === -1 ) { break; }
124+
hn = hn.slice(pos + 1);
125+
}
126+
127+
/******************************************************************************/
128+
129+
})();
130+
131+
/******************************************************************************/
132+

0 commit comments

Comments
 (0)