Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Delta Between Two Patch Sets: lib/content/snippets.js

Issue 29828610: Issue 6791 - Implement abort-current-inline-script snippet (Closed) Base URL: https://hg.adblockplus.org/adblockpluscore/
Left Patch Set: Created July 12, 2018, 5:38 p.m.
Right Patch Set: Rebase Created July 13, 2018, 1 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « no previous file | no next file » | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
1 /* 1 /*
2 * This file is part of Adblock Plus <https://adblockplus.org/>, 2 * This file is part of Adblock Plus <https://adblockplus.org/>,
3 * Copyright (C) 2006-present eyeo GmbH 3 * Copyright (C) 2006-present eyeo GmbH
4 * 4 *
5 * Adblock Plus is free software: you can redistribute it and/or modify 5 * Adblock Plus is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 3 as 6 * it under the terms of the GNU General Public License version 3 as
7 * published by the Free Software Foundation. 7 * published by the Free Software Foundation.
8 * 8 *
9 * Adblock Plus is distributed in the hope that it will be useful, 9 * Adblock Plus is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details. 12 * GNU General Public License for more details.
13 * 13 *
14 * You should have received a copy of the GNU General Public License 14 * You should have received a copy of the GNU General Public License
15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. 15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>.
16 */ 16 */
17 17
18 /* eslint-env webextensions */
18 /* eslint no-console: "off" */ 19 /* eslint no-console: "off" */
19 20
20 "use strict"; 21 "use strict";
22
23 function regexEscape(s)
24 {
25 return s.replace(/[-/\\^$*+?.()|[\]{}]/g, "\\$&");
26 }
27
28 function findProperty(path, root = window)
29 {
30 let owner = root;
31
32 let chain = path.split(".");
33 let property = chain.pop();
34
35 for (let name of chain)
36 {
37 if (!owner)
38 break;
39
40 owner = owner[name];
41 }
42
43 if (!owner)
44 return null;
45
46 return {owner, property};
47 }
48
49 function injectPropertyAccessValidator(object, property, validate)
50 {
51 let value = object[property];
52
53 Object.defineProperty(object, property, {
54 get()
55 {
56 validate();
57 return value;
58 },
59 set(newValue)
60 {
61 validate();
62 value = newValue;
63 }
64 });
65 }
21 66
22 function injectCode(code) 67 function injectCode(code)
23 { 68 {
24 let script = document.createElement("script"); 69 let script = document.createElement("script");
25 70
26 script.type = "application/javascript"; 71 script.type = "application/javascript";
27 script.async = false; 72 script.async = false;
28 73
29 // Firefox 58 only bypasses site CSPs when assigning to 'src', 74 // Firefox 58 only bypasses site CSPs when assigning to 'src',
30 // while Chrome 67 only bypasses site CSPs when using 'textContent'. 75 // while Chrome 67 only bypasses site CSPs when using 'textContent'.
31 if (browser.runtime.getURL("").startsWith("chrome-extension://")) 76 if (browser.runtime.getURL("").startsWith("chrome-extension://"))
32 { 77 {
33 script.textContent = code; 78 script.textContent = code;
34 document.documentElement.appendChild(script); 79 document.documentElement.appendChild(script);
35 } 80 }
36 else 81 else
37 { 82 {
38 let url = URL.createObjectURL(new Blob([code])); 83 let url = URL.createObjectURL(new Blob([code]));
39 script.src = url; 84 script.src = url;
40 document.documentElement.appendChild(script); 85 document.documentElement.appendChild(script);
41 URL.revokeObjectURL(url); 86 URL.revokeObjectURL(url);
42 } 87 }
43 88
44 document.documentElement.removeChild(script); 89 document.documentElement.removeChild(script);
45 } 90 }
46 91
47 function stringifyFunctionCall(func, ...params) 92 function stringifyFunctionCall(func, ...params)
48 { 93 {
49 return `(${func})(${params.map(JSON.stringify).join(",")});`; 94 return `"use strict";(${func})(${params.map(JSON.stringify).join(",")});`;
50 } 95 }
51 96
52 function makeInjector(injectable) 97 function makeInjector(injectable)
53 { 98 {
54 return (...args) => injectCode(stringifyFunctionCall(injectable, ...args)); 99 return (...args) => injectCode(stringifyFunctionCall(injectable, ...args));
100 }
101
102 function getMagic()
103 {
104 return String.fromCharCode(Date.now() % 26 + 97) +
105 Math.floor(Math.random() * 982451653 + 982451653).toString(36);
106 }
107
108 function suppressMagicError(magic)
109 {
110 let {onerror} = window;
111 window.onerror = function(message, ...rest)
kzar 2018/07/16 16:33:15 The content script part of our snippet already run
112 {
113 if (typeof message == "string" && message.includes(magic))
114 return true;
115
116 if (typeof onerror instanceof Function)
117 return onerror.call(this, message, ...rest);
118 };
55 } 119 }
56 120
57 function log(...args) 121 function log(...args)
58 { 122 {
59 console.log(...args); 123 console.log(...args);
60 } 124 }
61 125
62 exports.log = log; 126 exports.log = log;
63 127
128 // This is an implementation of the uabinject-defuser technique used by uBlock
129 // Origin
130 // https://github.com/uBlockOrigin/uAssets/blob/c091f861b63cd2254b8e9e4628f6bdcd 89d43caa/filters/resources.txt#L640
64 function uabinjectDefuser() 131 function uabinjectDefuser()
65 { 132 {
66 window.trckd = window.uabpdl = window.uabInject = window.uabDetect = true; 133 window.trckd = true;
134 window.uabpdl = true;
135 window.uabInject = true;
136 window.uabDetect = true;
67 } 137 }
68 138
69 exports["uabinject-defuser"] = makeInjector(uabinjectDefuser); 139 exports["uabinject-defuser"] = makeInjector(uabinjectDefuser);
70 140
141 // This is an implementation of the abort-current-inline-script technique used
142 // by uBlock Origin
143 // https://github.com/uBlockOrigin/uAssets/blob/68cf8a364fb55deb96264c4e546b58f4 c851d782/filters/resources.txt#L1943
71 function abortCurrentInlineScript(target, needle) 144 function abortCurrentInlineScript(target, needle)
kzar 2018/07/16 16:33:15 Could you give me an example of `target` and `need
72 { 145 {
73 "use strict";
74
75 if (!target) 146 if (!target)
76 return; 147 return;
77 148
78 let reText = ".?"; 149 let re = null;
79 150
80 if (needle) 151 if (needle)
81 { 152 {
82 reText = /^\/.+\/$/.test(needle) ? 153 re = new RegExp(/^\/.+\/$/.test(needle) ?
83 needle.slice(1, -1) : 154 needle.slice(1, -1) :
84 needle.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"); 155 regexEscape(needle));
85 } 156 }
86 157
87 let re = new RegExp(reText); 158 let {owner, property} = findProperty(target) || {};
88 let chain = target.split(".");
89 159
90 let owner = window; 160 if (!owner)
91 let property = null; 161 return;
92
93 while (true)
94 {
95 property = chain.shift();
96 if (chain.length == 0)
97 break;
98
99 owner = owner[property];
100 if (!(owner instanceof Object))
101 return;
102 }
103 162
104 let descriptor = Object.getOwnPropertyDescriptor(owner, property); 163 let descriptor = Object.getOwnPropertyDescriptor(owner, property);
105 if (descriptor && descriptor.get) 164 if (descriptor && descriptor.get)
kzar 2018/07/16 16:33:15 What's the idea with this check?
106 return; 165 return;
107 166
108 let magic = String.fromCharCode(Date.now() % 26 + 97) + 167 let magic = getMagic();
109 Math.floor(Math.random() * 982451653 + 982451653).toString(36);
110 let value = owner[property];
111 168
112 let validate = () => 169 injectPropertyAccessValidator(owner, property, () =>
113 { 170 {
114 let element = document.currentScript; 171 let element = document.currentScript;
115 if (element instanceof HTMLScriptElement && 172 if (element instanceof HTMLScriptElement &&
116 element.src == "" && re.test(element.textContent)) 173 element.src == "" && (!re || re.test(element.textContent)))
117 { 174 {
118 throw new ReferenceError(magic); 175 throw new ReferenceError(magic);
kzar 2018/07/16 16:33:15 How come you've used `ReferenceError` out of inter
119 } 176 }
120 };
121
122 Object.defineProperty(owner, property, {
123 get()
124 {
125 validate();
126 return value;
127 },
128 set(newValue)
129 {
130 validate();
131 value = newValue;
132 }
133 }); 177 });
134 178
135 let {onerror} = window; 179 suppressMagicError(magic);
136 window.onerror = function(message, ...rest)
137 {
138 if (typeof message == "string" && message.indexOf(magic) != -1)
139 return true;
140
141 if (onerror instanceof Function)
142 return onerror.call(this, message, ...rest);
143 };
144 } 180 }
145 181
146 exports["abort-current-inline-script"] = makeInjector(abortCurrentInlineScript); 182 exports["abort-current-inline-script"] = makeInjector(abortCurrentInlineScript);
LEFTRIGHT
« no previous file | no next file » | Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Toggle Comments ('s')

Powered by Google App Engine
This is Rietveld