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

Delta Between Two Patch Sets: include.preload.js

Issue 29347034: Issue 1727 - Prevent circumvention via WebSocket (Closed)
Left Patch Set: Created June 26, 2016, 11:55 a.m.
Right Patch Set: Don't hardcode connection state values Created Aug. 10, 2016, 4:25 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 | lib/requestBlocker.js » ('j') | 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-2016 Eyeo GmbH 3 * Copyright (C) 2006-2016 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 var MutationObserver = window.MutationObserver || window.WebKitMutationObserver; 18 var MutationObserver = window.MutationObserver || window.WebKitMutationObserver;
19 var SELECTOR_GROUP_SIZE = 200; 19 var SELECTOR_GROUP_SIZE = 200;
20 var id = Math.random().toString(36).substr(2);
20 21
21 var typeMap = { 22 var typeMap = {
22 "img": "IMAGE", 23 "img": "IMAGE",
23 "input": "IMAGE", 24 "input": "IMAGE",
24 "picture": "IMAGE", 25 "picture": "IMAGE",
25 "audio": "MEDIA", 26 "audio": "MEDIA",
26 "video": "MEDIA", 27 "video": "MEDIA",
27 "frame": "SUBDOCUMENT", 28 "frame": "SUBDOCUMENT",
28 "iframe": "SUBDOCUMENT", 29 "iframe": "SUBDOCUMENT",
29 "object": "OBJECT", 30 "object": "OBJECT",
(...skipping 312 matching lines...) Expand 10 before | Expand all | Expand 10 after
342 var observer = new MutationObserver(function() 343 var observer = new MutationObserver(function()
343 { 344 {
344 if (style.parentNode != parentNode) 345 if (style.parentNode != parentNode)
345 parentNode.appendChild(style); 346 parentNode.appendChild(style);
346 }); 347 });
347 348
348 observer.observe(parentNode, {childList: true}); 349 observer.observe(parentNode, {childList: true});
349 return observer; 350 return observer;
350 } 351 }
351 352
352 function protectStyleSheet(document, style) 353 function runInPage(fn, arg)
353 { 354 {
354 var id = Math.random().toString(36).substr(2);
355 style.id = id;
356
357 var code = [
358 "(function()",
359 "{",
360 ' var style = document.getElementById("' + id + '") ||',
361 ' document.documentElement.shadowRoot.getElementById("' + id + '");',
362 ' style.removeAttribute("id");'
363 ];
364
365 var disableables = ["style", "style.sheet"];
366 for (var i = 0; i < disableables.length; i++)
367 {
368 code.push(" Object.defineProperty(" + disableables[i] + ', "disabled", '
369 + "{value: false, enumerable: true});") ;
370 }
371
372 var methods = ["deleteRule", "removeRule"];
373 for (var j = 0; j < methods.length; j++)
374 {
375 var method = methods[j];
376 if (method in CSSStyleSheet.prototype)
377 {
378 var origin = "CSSStyleSheet.prototype." + method;
379 code.push(" var " + method + " = " + origin + ";",
380 " " + origin + " = function(index)",
381 " {",
382 " if (this != style.sheet)",
383 " " + method + ".call(this, index);",
384 " }");
385 }
386 }
387
388 code.push("})();");
389
390 var script = document.createElement("script"); 355 var script = document.createElement("script");
356 script.type = "application/javascript";
391 script.async = false; 357 script.async = false;
392 script.textContent = code.join("\n"); 358 script.textContent = "(" + fn + ")(" + JSON.stringify(arg) + ");";
393 document.documentElement.appendChild(script); 359 document.documentElement.appendChild(script);
394 document.documentElement.removeChild(script); 360 document.documentElement.removeChild(script);
395 } 361 }
396 362
363 function protectStyleSheet(document, style)
364 {
365 style.id = id;
366
367 runInPage(function(id)
368 {
369 var style = document.getElementById(id) ||
370 document.documentElement.shadowRoot.getElementById(id);
371 style.removeAttribute("id");
372
373 var disableables = [style, style.sheet];
374 for (var i = 0; i < disableables.length; i++)
375 Object.defineProperty(disableables[i], "disabled",
376 {value: false, enumerable: true});
377
378 ["deleteRule", "removeRule"].forEach(function(method)
379 {
380 var original = CSSStyleSheet.prototype[method];
381 CSSStyleSheet.prototype[method] = function(index)
382 {
383 if (this != style.sheet)
384 original.call(this, index);
385 };
386 });
387 }, id);
388 }
389
397 // Neither Chrome[1] nor Safari allow us to intercept WebSockets, and therefore 390 // Neither Chrome[1] nor Safari allow us to intercept WebSockets, and therefore
398 // some ad networks are misusing them as a way to serve adverts and circumvent 391 // some ad networks are misusing them as a way to serve adverts and circumvent
399 // us. As a workaround we wrap WebSocket, closing connections that would have 392 // us. As a workaround we wrap WebSocket, preventing blocked WebSocket
400 // otherwise been blocked. 393 // connections from being opened.
401 // [1] - https://bugs.chromium.org/p/chromium/issues/detail?id=129353 394 // [1] - https://bugs.chromium.org/p/chromium/issues/detail?id=129353
402 function wrapWebSocket() 395 function wrapWebSocket()
403 { 396 {
404 if (typeof WebSocket == "undefined") 397 if (typeof WebSocket == "undefined")
405 return; 398 return;
406 399
407 var eventName = "abpws-" + Math.random().toString().substr(2); 400 var eventName = "abpws-" + id;
408 401
409 document.addEventListener(eventName, function(event) 402 document.addEventListener(eventName, function(event)
410 { 403 {
411 ext.backgroundPage.sendMessage({ 404 ext.backgroundPage.sendMessage({
412 type: "websocket-request", 405 type: "websocket-request",
413 url: event.detail.url 406 url: event.detail.url
414 }, function (block) 407 }, function (block)
415 { 408 {
416 document.dispatchEvent( 409 document.dispatchEvent(
417 new CustomEvent(eventName + "-" + event.detail.url, {detail: block}) 410 new CustomEvent(eventName + "-" + event.detail.url, {detail: block})
418 ); 411 );
419 }); 412 });
420 }); 413 });
421 414
422 function wrapper(eventName) 415 runInPage(function(eventName)
423 { 416 {
424 var originalWebSocket = WebSocket; 417 // As far as possible we must track everything we use that could be
425 var readyStates = { 418 // sabotaged by the website later in order to circumvent us.
426 CLOSED: {value: 3, enumerable: true}, 419 var RealWebSocket = WebSocket;
427 CLOSING: {value: 2, enumerable: true}, 420 var closeWebSocket = Function.prototype.call.bind(RealWebSocket.prototype.cl ose);
428 OPEN: {value: 1, enumerable: true}, 421 var addEventListener = document.addEventListener.bind(document);
429 CONNECTING: {value: 0, enumerable: true} 422 var removeEventListener = document.removeEventListener.bind(document);
430 }; 423 var dispatchEvent = document.dispatchEvent.bind(document);
431 424 var CustomEvent = window.CustomEvent;
432 WebSocket = function(url, protocol) 425
433 { 426 function checkRequest(url, callback)
434 var websocket = new originalWebSocket(url, protocol); 427 {
435 var properties = Object.create(null);
436
437 function getSet(key)
438 {
439 return {get: function() { return websocket[key]; },
440 set: function(value) { return websocket[key] = value; },
441 enumerable: true};
442 }
443 function funcWrap(key)
444 {
445 return {value: function() { websocket[key].apply(websocket, arguments); },
446 enumerable: true};
447 }
448
449 var key;
450 for (key of ["close", "send", "addEventListener", "removeEventListener"])
Sebastian Noack 2016/06/28 16:17:51 Is this script processed with jsHydra? It seems no
kzar 2016/06/29 13:40:30 Acknowledged.
451 properties[key] = funcWrap(key);
452 for (key of ["url", "protocol", "readyState", "extensions", "bufferedAmoun t",
453 "binaryType", "onopen", "onclose", "onerror", "onmessage"])
454 properties[key] = getSet(key);
455
456 Object.defineProperties(this, properties);
457
458 var incomingEventName = eventName + "-" + url; 428 var incomingEventName = eventName + "-" + url;
459 function listener(event) 429 function listener(event)
460 { 430 {
461 if (event.detail) 431 callback(event.detail);
462 websocket.close(); 432 removeEventListener(incomingEventName, listener);
Sebastian Noack 2016/06/28 16:17:51 As Lain pointed out, this approach allows WebSocke
kzar 2016/06/28 16:32:40 I actually tested this with WireShark and I found
Sebastian Noack 2016/06/28 17:04:58 I have two concerns here: 1. It might be a potent
kzar 2016/06/29 13:40:31 OK, done.
463 433 }
464 document.removeEventListener(incomingEventName, listener); 434 addEventListener(incomingEventName, listener);
465 } 435
466 document.addEventListener(incomingEventName, listener); 436 dispatchEvent(new CustomEvent(eventName, {
467 437 detail: {url: url}
468 document.dispatchEvent(new CustomEvent(eventName, {
469 detail: {url: url, protocol: protocol}
470 })); 438 }));
471 }; 439 }
472 WebSocket.prototype = Object.create(window.EventTarget.prototype, readyState s); 440
473 Object.defineProperties(WebSocket, readyStates); 441 WebSocket = function WrappedWebSocket(url, protocols)
474 442 {
475 var script = document.currentScript; 443 // Throw correct exceptions if the constructor is used improperly.
476 script.parentNode.removeChild(script); 444 if (!(this instanceof WrappedWebSocket)) return RealWebSocket();
477 } 445 if (arguments.length < 1) return new RealWebSocket();
478 446
479 var script = document.createElement("script"); 447 var websocket = new RealWebSocket(url, protocols);
480 script.textContent = "(" + wrapper.toString() + ")(\"" + eventName + "\");"; 448
Sebastian Noack 2016/06/28 16:17:51 Ideally, we inject only one script. Note that we a
kzar 2016/06/29 13:40:30 Done.
481 document.documentElement.appendChild(script); 449 checkRequest(websocket.url, function(blocked)
450 {
451 if (blocked)
452 closeWebSocket(websocket);
453 });
454
455 return websocket;
456 }.bind();
457
458 Object.defineProperties(WebSocket, {
459 CONNECTING: {value: RealWebSocket.CONNECTING, enumerable: true},
460 OPEN: {value: RealWebSocket.OPEN, enumerable: true},
461 CLOSING: {value: RealWebSocket.CLOSING, enumerable: true},
462 CLOSED: {value: RealWebSocket.CLOSED, enumerable: true},
463 prototype: {value: RealWebSocket.prototype}
464 });
465
466 RealWebSocket.prototype.constructor = WebSocket;
467 }, eventName);
482 } 468 }
483 469
484 function init(document) 470 function init(document)
485 { 471 {
486 var shadow = null; 472 var shadow = null;
487 var style = null; 473 var style = null;
488 var observer = null; 474 var observer = null;
489 var tracer = null; 475 var tracer = null;
490 476
491 wrapWebSocket(); 477 wrapWebSocket();
(...skipping 161 matching lines...) Expand 10 before | Expand all | Expand 10 after
653 }, true); 639 }, true);
654 640
655 return updateStylesheet; 641 return updateStylesheet;
656 } 642 }
657 643
658 if (document instanceof HTMLDocument) 644 if (document instanceof HTMLDocument)
659 { 645 {
660 checkSitekey(); 646 checkSitekey();
661 window.updateStylesheet = init(document); 647 window.updateStylesheet = init(document);
662 } 648 }
LEFTRIGHT

Powered by Google App Engine
This is Rietveld