Skip to content

Commit fef50e5

Browse files
committed
Improve abort-current-script scriptlet
Prevent webpages from tampering with `textContent`. Borrow solution from: https://github.com/AdguardTeam/Scriptlets/blob/c2d7378e4de8ed79fcd7cc17c0e883e48dcd2f2a/src/scriptlets/abort-current-inline-script.js#L98-L99
1 parent fed7f4a commit fef50e5

File tree

1 file changed

+7
-6
lines changed

1 file changed

+7
-6
lines changed

src/js/resources/scriptlets.js

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,8 @@ function shouldDebug(details) {
7777
/******************************************************************************/
7878

7979
builtinScriptlets.push({
80-
name: 'abort-current-script-core.fn',
81-
fn: abortCurrentScriptCore,
80+
name: 'abort-current-script.fn',
81+
fn: abortCurrentScriptFn,
8282
dependencies: [
8383
'get-exception-token.fn',
8484
'safe-self.fn',
@@ -87,7 +87,7 @@ builtinScriptlets.push({
8787
});
8888
// Issues to mind before changing anything:
8989
// https://github.com/uBlockOrigin/uBlock-issues/issues/2154
90-
function abortCurrentScriptCore(
90+
function abortCurrentScriptFn(
9191
target = '',
9292
needle = '',
9393
context = ''
@@ -122,8 +122,9 @@ function abortCurrentScriptCore(
122122
const debug = shouldDebug(extraArgs);
123123
const exceptionToken = getExceptionTokenFn();
124124
const scriptTexts = new WeakMap();
125+
const textContentGetter = Object.getOwnPropertyDescriptor(Node.prototype, 'textContent').get;
125126
const getScriptText = elem => {
126-
let text = elem.textContent;
127+
let text = textContentGetter.call(elem);
127128
if ( text.trim() !== '' ) { return text; }
128129
if ( scriptTexts.has(elem) ) { return scriptTexts.get(elem); }
129130
const [ , mime, content ] =
@@ -596,15 +597,15 @@ builtinScriptlets.push({
596597
],
597598
fn: abortCurrentScript,
598599
dependencies: [
599-
'abort-current-script-core.fn',
600+
'abort-current-script.fn',
600601
'run-at-html-element.fn',
601602
],
602603
});
603604
// Issues to mind before changing anything:
604605
// https://github.com/uBlockOrigin/uBlock-issues/issues/2154
605606
function abortCurrentScript(...args) {
606607
runAtHtmlElementFn(( ) => {
607-
abortCurrentScriptCore(...args);
608+
abortCurrentScriptFn(...args);
608609
});
609610
}
610611

0 commit comments

Comments
 (0)