OLD | NEW |
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 "use strict"; | 18 "use strict"; |
19 | 19 |
20 let randomEventName = "abp-request-" + Math.random().toString(36).substr(2); | 20 let randomEventName = "abp-request-" + Math.random().toString(36).substr(2); |
21 | 21 |
22 // Proxy "should we block?" messages from checkRequest inside the injected | 22 // Proxy "should we block?" messages from checkRequest inside the injected |
23 // code to the background page and back again. | 23 // code to the background page and back again. |
24 document.addEventListener(randomEventName, event => | 24 document.addEventListener(randomEventName, event => |
25 { | 25 { |
26 let {url, requestType} = event.detail; | 26 let {url} = event.detail; |
27 | 27 |
28 browser.runtime.sendMessage({ | 28 browser.runtime.sendMessage({ |
29 type: "request.blockedByWrapper", | 29 type: "request.blockedByRTCWrapper", |
30 requestType, | |
31 url | 30 url |
32 }, block => | 31 }, block => |
33 { | 32 { |
34 document.dispatchEvent(new CustomEvent( | 33 document.dispatchEvent(new CustomEvent( |
35 randomEventName + "-" + requestType + "-" + url, {detail: block} | 34 randomEventName + "-" + url, {detail: block} |
36 )); | 35 )); |
37 }); | 36 }); |
38 }); | 37 }); |
39 | 38 |
40 function injected(eventName, injectedIntoContentWindow) | 39 function injected(eventName, injectedIntoContentWindow) |
41 { | 40 { |
42 let checkRequest; | 41 let checkRequest; |
43 | 42 |
44 /* | 43 /* |
45 * Frame context wrapper | 44 * Frame context wrapper |
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
131 configurable: true, enumerable: true, get() | 130 configurable: true, enumerable: true, get() |
132 { | 131 { |
133 let thisShadow = shadowRoot(this); | 132 let thisShadow = shadowRoot(this); |
134 return thisShadow == ourShadowRoot ? null : thisShadow; | 133 return thisShadow == ourShadowRoot ? null : thisShadow; |
135 } | 134 } |
136 }); | 135 }); |
137 } | 136 } |
138 } | 137 } |
139 | 138 |
140 /* | 139 /* |
141 * Shared request checking code, used by both the WebSocket and | 140 * RTCPeerConnection wrapper |
142 * RTCPeerConnection wrappers. | 141 * |
| 142 * The webRequest API in Chrome does not yet allow the blocking of |
| 143 * WebRTC connections. |
| 144 * See https://bugs.chromium.org/p/chromium/issues/detail?id=707683 |
143 */ | 145 */ |
144 let RealCustomEvent = window.CustomEvent; | 146 let RealCustomEvent = window.CustomEvent; |
145 | 147 |
146 // If we've been injected into a frame via contentWindow then we can simply | 148 // If we've been injected into a frame via contentWindow then we can simply |
147 // grab the copy of checkRequest left for us by the parent document. Otherwise | 149 // grab the copy of checkRequest left for us by the parent document. Otherwise |
148 // we need to set it up now, along with the event handling functions. | 150 // we need to set it up now, along with the event handling functions. |
149 if (injectedIntoContentWindow) | 151 if (injectedIntoContentWindow) |
150 checkRequest = window[eventName]; | 152 checkRequest = window[eventName]; |
151 else | 153 else |
152 { | 154 { |
153 let addEventListener = document.addEventListener.bind(document); | 155 let addEventListener = document.addEventListener.bind(document); |
154 let dispatchEvent = document.dispatchEvent.bind(document); | 156 let dispatchEvent = document.dispatchEvent.bind(document); |
155 let removeEventListener = document.removeEventListener.bind(document); | 157 let removeEventListener = document.removeEventListener.bind(document); |
156 checkRequest = (requestType, url, callback) => | 158 checkRequest = (url, callback) => |
157 { | 159 { |
158 let incomingEventName = eventName + "-" + requestType + "-" + url; | 160 let incomingEventName = eventName + "-" + url; |
159 | 161 |
160 function listener(event) | 162 function listener(event) |
161 { | 163 { |
162 callback(event.detail); | 164 callback(event.detail); |
163 removeEventListener(incomingEventName, listener); | 165 removeEventListener(incomingEventName, listener); |
164 } | 166 } |
165 addEventListener(incomingEventName, listener); | 167 addEventListener(incomingEventName, listener); |
166 | 168 |
167 dispatchEvent(new RealCustomEvent(eventName, | 169 dispatchEvent(new RealCustomEvent(eventName, {detail: {url}})); |
168 {detail: {url, requestType}})); | |
169 }; | 170 }; |
170 } | 171 } |
171 | 172 |
172 // Only to be called before the page's code, not hardened. | 173 // Only to be called before the page's code, not hardened. |
173 function copyProperties(src, dest, properties) | 174 function copyProperties(src, dest, properties) |
174 { | 175 { |
175 for (let name of properties) | 176 for (let name of properties) |
176 { | 177 { |
177 if (src.hasOwnProperty(name)) | 178 if (src.hasOwnProperty(name)) |
178 { | 179 { |
179 Object.defineProperty(dest, name, | 180 Object.defineProperty(dest, name, |
180 Object.getOwnPropertyDescriptor(src, name)); | 181 Object.getOwnPropertyDescriptor(src, name)); |
181 } | 182 } |
182 } | 183 } |
183 } | 184 } |
184 | 185 |
185 /* | |
186 * WebSocket wrapper | |
187 * | |
188 * Required before Chrome 58, since the webRequest API didn't allow us to | |
189 * intercept WebSockets. | |
190 * See https://bugs.chromium.org/p/chromium/issues/detail?id=129353 | |
191 */ | |
192 let RealWebSocket = WebSocket; | |
193 let closeWebSocket = Function.prototype.call.bind( | |
194 RealWebSocket.prototype.close | |
195 ); | |
196 | |
197 function WrappedWebSocket(url, ...args) | |
198 { | |
199 // Throw correct exceptions if the constructor is used improperly. | |
200 if (!(this instanceof WrappedWebSocket)) return RealWebSocket(); | |
201 if (arguments.length < 1) return new RealWebSocket(); | |
202 | |
203 let websocket = new RealWebSocket(url, ...args); | |
204 | |
205 checkRequest("websocket", websocket.url, blocked => | |
206 { | |
207 if (blocked) | |
208 closeWebSocket(websocket); | |
209 }); | |
210 | |
211 return websocket; | |
212 } | |
213 WrappedWebSocket.prototype = RealWebSocket.prototype; | |
214 window.WebSocket = WrappedWebSocket.bind(); | |
215 copyProperties(RealWebSocket, WebSocket, | |
216 ["CONNECTING", "OPEN", "CLOSING", "CLOSED", "prototype"]); | |
217 RealWebSocket.prototype.constructor = WebSocket; | |
218 | |
219 /* | |
220 * RTCPeerConnection wrapper | |
221 * | |
222 * The webRequest API in Chrome does not yet allow the blocking of | |
223 * WebRTC connections. | |
224 * See https://bugs.chromium.org/p/chromium/issues/detail?id=707683 | |
225 */ | |
226 let RealRTCPeerConnection = window.RTCPeerConnection || | 186 let RealRTCPeerConnection = window.RTCPeerConnection || |
227 window.webkitRTCPeerConnection; | 187 window.webkitRTCPeerConnection; |
228 | 188 |
229 // Firefox has the option (media.peerconnection.enabled) to disable WebRTC | 189 // Firefox has the option (media.peerconnection.enabled) to disable WebRTC |
230 // in which case RealRTCPeerConnection is undefined. | 190 // in which case RealRTCPeerConnection is undefined. |
231 if (typeof RealRTCPeerConnection != "undefined") | 191 if (typeof RealRTCPeerConnection != "undefined") |
232 { | 192 { |
233 let closeRTCPeerConnection = Function.prototype.call.bind( | 193 let closeRTCPeerConnection = Function.prototype.call.bind( |
234 RealRTCPeerConnection.prototype.close | 194 RealRTCPeerConnection.prototype.close |
235 ); | 195 ); |
236 let RealArray = Array; | 196 let RealArray = Array; |
237 let RealString = String; | 197 let RealString = String; |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
298 return createObject(configuration, { | 258 return createObject(configuration, { |
299 iceServers: { | 259 iceServers: { |
300 configurable: false, enumerable: false, writable: false, | 260 configurable: false, enumerable: false, writable: false, |
301 value: iceServers | 261 value: iceServers |
302 } | 262 } |
303 }); | 263 }); |
304 }; | 264 }; |
305 | 265 |
306 let checkUrl = (peerconnection, url) => | 266 let checkUrl = (peerconnection, url) => |
307 { | 267 { |
308 checkRequest("webrtc", url, blocked => | 268 checkRequest(url, blocked => |
309 { | 269 { |
310 if (blocked) | 270 if (blocked) |
311 { | 271 { |
312 // Calling .close() throws if already closed. | 272 // Calling .close() throws if already closed. |
313 try | 273 try |
314 { | 274 { |
315 closeRTCPeerConnection(peerconnection); | 275 closeRTCPeerConnection(peerconnection); |
316 } | 276 } |
317 catch (e) {} | 277 catch (e) {} |
318 } | 278 } |
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
408 // Firefox 58 only bypasses site CSPs when assigning to 'src'. | 368 // Firefox 58 only bypasses site CSPs when assigning to 'src'. |
409 let url = URL.createObjectURL(new Blob([ | 369 let url = URL.createObjectURL(new Blob([ |
410 "(" + injected + ")('" + randomEventName + "');" | 370 "(" + injected + ")('" + randomEventName + "');" |
411 ])); | 371 ])); |
412 script.src = url; | 372 script.src = url; |
413 document.documentElement.appendChild(script); | 373 document.documentElement.appendChild(script); |
414 document.documentElement.removeChild(script); | 374 document.documentElement.removeChild(script); |
415 URL.revokeObjectURL(url); | 375 URL.revokeObjectURL(url); |
416 } | 376 } |
417 } | 377 } |
OLD | NEW |