| LEFT | RIGHT |
| 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 |
| (...skipping 332 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 343 var observer = new MutationObserver(function() | 343 var observer = new MutationObserver(function() |
| 344 { | 344 { |
| 345 if (style.parentNode != parentNode) | 345 if (style.parentNode != parentNode) |
| 346 parentNode.appendChild(style); | 346 parentNode.appendChild(style); |
| 347 }); | 347 }); |
| 348 | 348 |
| 349 observer.observe(parentNode, {childList: true}); | 349 observer.observe(parentNode, {childList: true}); |
| 350 return observer; | 350 return observer; |
| 351 } | 351 } |
| 352 | 352 |
| 353 function injectJS(f) | 353 function runInPage(fn, arg) |
| 354 { | 354 { |
| 355 var args = JSON.stringify(Array.prototype.slice.call(arguments, 1)); | |
| 356 args = args.substring(1, args.length - 1); | |
| 357 var codeString = "(" + f.toString() + ")(" + args + ");"; | |
| 358 | |
| 359 var script = document.createElement("script"); | 355 var script = document.createElement("script"); |
| 356 script.type = "application/javascript"; |
| 360 script.async = false; | 357 script.async = false; |
| 361 script.textContent = codeString; | 358 script.textContent = "(" + fn + ")(" + JSON.stringify(arg) + ");"; |
| 362 document.documentElement.appendChild(script); | 359 document.documentElement.appendChild(script); |
| 363 document.documentElement.removeChild(script); | 360 document.documentElement.removeChild(script); |
| 364 } | 361 } |
| 365 | 362 |
| 366 function protectStyleSheet(document, style) | 363 function protectStyleSheet(document, style) |
| 367 { | 364 { |
| 368 style.id = id; | 365 style.id = id; |
| 369 | 366 |
| 370 var protector = function(id) | 367 runInPage(function(id) |
| 371 { | 368 { |
| 372 var style = document.getElementById(id) || | 369 var style = document.getElementById(id) || |
| 373 document.documentElement.shadowRoot.getElementById(id); | 370 document.documentElement.shadowRoot.getElementById(id); |
| 374 style.removeAttribute("id"); | 371 style.removeAttribute("id"); |
| 375 | 372 |
| 376 var i; | |
| 377 var disableables = [style, style.sheet]; | 373 var disableables = [style, style.sheet]; |
| 378 for (i = 0; i < disableables.length; i += 1) | 374 for (var i = 0; i < disableables.length; i++) |
| 379 Object.defineProperty(disableables[i], "disabled", | 375 Object.defineProperty(disableables[i], "disabled", |
| 380 {value: false, enumerable: true}); | 376 {value: false, enumerable: true}); |
| 381 | 377 |
| 382 var methods = ["deleteRule", "removeRule"]; | 378 ["deleteRule", "removeRule"].forEach(function(method) |
| 383 for (i = 0; i < methods.length; i += 1) | 379 { |
| 384 { | 380 var original = CSSStyleSheet.prototype[method]; |
| 385 if (methods[i] in CSSStyleSheet.prototype) | 381 CSSStyleSheet.prototype[method] = function(index) |
| 386 { | 382 { |
| 387 (function(method) | 383 if (this != style.sheet) |
| 388 { | 384 original.call(this, index); |
| 389 var original = CSSStyleSheet.prototype[method]; | 385 }; |
| 390 CSSStyleSheet.prototype[method] = function(index) | 386 }); |
| 391 { | 387 }, id); |
| 392 if (this != style.sheet) | |
| 393 original.call(this, index); | |
| 394 }; | |
| 395 }(methods[i])); | |
| 396 } | |
| 397 } | |
| 398 }; | |
| 399 | |
| 400 injectJS(protector, id); | |
| 401 } | 388 } |
| 402 | 389 |
| 403 // 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 |
| 404 // 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 |
| 405 // us. As a workaround we wrap WebSocket, closing connections that would have | 392 // us. As a workaround we wrap WebSocket, preventing blocked WebSocket |
| 406 // otherwise been blocked. | 393 // connections from being opened. |
| 407 // [1] - https://bugs.chromium.org/p/chromium/issues/detail?id=129353 | 394 // [1] - https://bugs.chromium.org/p/chromium/issues/detail?id=129353 |
| 408 function wrapWebSocket() | 395 function wrapWebSocket() |
| 409 { | 396 { |
| 410 if (typeof WebSocket == "undefined" || typeof WeakMap == "undefined") | 397 if (typeof WebSocket == "undefined") |
| 411 return; | 398 return; |
| 412 | 399 |
| 413 var eventName = "abpws-" + id; | 400 var eventName = "abpws-" + id; |
| 414 | 401 |
| 415 document.addEventListener(eventName, function(event) | 402 document.addEventListener(eventName, function(event) |
| 416 { | 403 { |
| 417 ext.backgroundPage.sendMessage({ | 404 ext.backgroundPage.sendMessage({ |
| 418 type: "websocket-request", | 405 type: "websocket-request", |
| 419 url: event.detail.url | 406 url: event.detail.url |
| 420 }, function (block) | 407 }, function (block) |
| 421 { | 408 { |
| 422 document.dispatchEvent( | 409 document.dispatchEvent( |
| 423 new CustomEvent(eventName + "-" + event.detail.url, {detail: block}) | 410 new CustomEvent(eventName + "-" + event.detail.url, {detail: block}) |
| 424 ); | 411 ); |
| 425 }); | 412 }); |
| 426 }); | 413 }); |
| 427 | 414 |
| 428 function wrapper(eventName) | 415 runInPage(function(eventName) |
| 429 { | 416 { |
| 417 // As far as possible we must track everything we use that could be |
| 418 // sabotaged by the website later in order to circumvent us. |
| 430 var RealWebSocket = WebSocket; | 419 var RealWebSocket = WebSocket; |
| 431 | 420 var closeWebSocket = Function.prototype.call.bind(RealWebSocket.prototype.cl
ose); |
| 432 function checkRequest(url, protocols, callback) | 421 var addEventListener = document.addEventListener.bind(document); |
| 422 var removeEventListener = document.removeEventListener.bind(document); |
| 423 var dispatchEvent = document.dispatchEvent.bind(document); |
| 424 var CustomEvent = window.CustomEvent; |
| 425 |
| 426 function checkRequest(url, callback) |
| 433 { | 427 { |
| 434 var incomingEventName = eventName + "-" + url; | 428 var incomingEventName = eventName + "-" + url; |
| 435 function listener(event) | 429 function listener(event) |
| 436 { | 430 { |
| 437 callback(event.detail); | 431 callback(event.detail); |
| 438 document.removeEventListener(incomingEventName, listener); | 432 removeEventListener(incomingEventName, listener); |
| 439 } | 433 } |
| 440 document.addEventListener(incomingEventName, listener); | 434 addEventListener(incomingEventName, listener); |
| 441 | 435 |
| 442 document.dispatchEvent(new CustomEvent(eventName, { | 436 dispatchEvent(new CustomEvent(eventName, { |
| 443 detail: {url: url, protocols: protocols} | 437 detail: {url: url} |
| 444 })); | 438 })); |
| 445 } | 439 } |
| 446 | 440 |
| 447 // To avoid detection we make our WebSocket wrapper as similar as possible. | 441 WebSocket = function WrappedWebSocket(url, protocols) |
| 448 // Most WebSocket properties and methods actually belong to | 442 { |
| 449 // WebSocket.prototype, but they require access to the instance state. To | 443 // Throw correct exceptions if the constructor is used improperly. |
| 450 // acheive this without adding extra properties we need to manage our own | 444 if (!(this instanceof WrappedWebSocket)) return RealWebSocket(); |
| 451 // storage here. To avoid leaking memory in the case that all other | 445 if (arguments.length < 1) return new RealWebSocket(); |
| 452 // references to a WebSocket have been deleted we must use a WeakMap. | 446 |
| 453 var instanceStorage = new WeakMap(); | 447 var websocket = new RealWebSocket(url, protocols); |
| 454 | 448 |
| 455 // We check if a WebSocket should be blocked before actually creating it. As | 449 checkRequest(websocket.url, function(blocked) |
| 456 // this is done asynchonously we must queue up any actions (method calls and | 450 { |
| 457 // assignments) that happen in the mean time. | |
| 458 // Once we have a block/allow result we create the WebSocket and perform the | |
| 459 // queued actions. (For blocked requests we only need to manage error and | |
| 460 // close listeners, other actions are all ignored. This is so real close and | |
| 461 // error events are fired, with the minimal risk of leaking messages.) | |
| 462 function processQueue(queue, websocket, blocked) | |
| 463 { | |
| 464 for (var i = 0; i < queue.length; i += 1) | |
| 465 { | |
| 466 var action = queue[i][0]; | |
| 467 var key = queue[i][1]; | |
| 468 var value = queue[i][2]; | |
| 469 | |
| 470 if (action == "set" && | |
| 471 (!blocked || key == "onclose" || key == "onerror")) | |
| 472 websocket[key] = value; | |
| 473 else if (action == "call" && | |
| 474 (!blocked || | |
| 475 ((key == "addEventListener" || key == "removeEventListener") &
& | |
| 476 (value[0] == "error" || value[0] == "close")))) | |
| 477 websocket[key].apply(websocket, value); | |
| 478 } | |
| 479 } | |
| 480 | |
| 481 var defaults = { | |
| 482 readyState: RealWebSocket.CONNECTING, | |
| 483 bufferedAmount: 0, | |
| 484 extensions: "", | |
| 485 binaryType: "blob" | |
| 486 }; | |
| 487 | |
| 488 WebSocket = function(url, protocols) | |
| 489 { | |
| 490 var storage = { | |
| 491 url: url, | |
| 492 protocol: protocols, | |
| 493 queue: [], | |
| 494 websocket: null | |
| 495 }; | |
| 496 instanceStorage.set(this, storage); | |
| 497 | |
| 498 checkRequest(url, protocols, function(blocked) | |
| 499 { | |
| 500 storage.websocket = new RealWebSocket(url, protocols); | |
| 501 processQueue(storage.queue, storage.websocket, blocked); | |
| 502 if (blocked) | 451 if (blocked) |
| 503 storage.websocket.close(); | 452 closeWebSocket(websocket); |
| 504 delete storage.queue; | |
| 505 }); | 453 }); |
| 506 }; | 454 |
| 507 | 455 return websocket; |
| 508 function proxyProperties(original) | 456 }.bind(); |
| 509 { | 457 |
| 510 var properties = Object.create(null); | 458 Object.defineProperties(WebSocket, { |
| 511 | 459 CONNECTING: {value: RealWebSocket.CONNECTING, enumerable: true}, |
| 512 Object.keys(original).map(function(key) | 460 OPEN: {value: RealWebSocket.OPEN, enumerable: true}, |
| 513 { | 461 CLOSING: {value: RealWebSocket.CLOSING, enumerable: true}, |
| 514 if (key == "prototype") | 462 CLOSED: {value: RealWebSocket.CLOSED, enumerable: true}, |
| 515 return; | 463 prototype: {value: RealWebSocket.prototype} |
| 516 | 464 }); |
| 517 var descriptor = Object.getOwnPropertyDescriptor(original, key); | 465 |
| 518 | 466 RealWebSocket.prototype.constructor = WebSocket; |
| 519 if (typeof descriptor.value == "function") | 467 }, eventName); |
| 520 { | |
| 521 descriptor.value = function() | |
| 522 { | |
| 523 var storage = instanceStorage.get(this); | |
| 524 if (!storage) | |
| 525 original[key].apply(original, arguments); | |
| 526 else if (storage.websocket) | |
| 527 storage.websocket[key].apply(storage.websocket, arguments); | |
| 528 else | |
| 529 storage.queue.push(["call", key, arguments]); | |
| 530 }; | |
| 531 } | |
| 532 else if (typeof descriptor.value == "undefined") | |
| 533 { | |
| 534 descriptor.get = function() | |
| 535 { | |
| 536 var storage = instanceStorage.get(this); | |
| 537 if (!storage) | |
| 538 return original[key]; | |
| 539 if (storage.websocket) | |
| 540 return storage.websocket[key]; | |
| 541 if (key == "url" || key == "protocol") | |
| 542 return storage[key]; | |
| 543 if (key in defaults) | |
| 544 return defaults[key]; | |
| 545 return null; | |
| 546 }; | |
| 547 descriptor.set = function(value) | |
| 548 { | |
| 549 var storage = instanceStorage.get(this); | |
| 550 if (!storage) | |
| 551 original[key] = value; | |
| 552 else if (storage.websocket) | |
| 553 storage.websocket[key] = value; | |
| 554 else | |
| 555 storage.queue.push(["set", key, value]); | |
| 556 return value; | |
| 557 }; | |
| 558 } | |
| 559 properties[key] = descriptor; | |
| 560 }); | |
| 561 return properties; | |
| 562 } | |
| 563 | |
| 564 Object.defineProperties(WebSocket, proxyProperties(RealWebSocket)); | |
| 565 WebSocket.prototype = Object.create( | |
| 566 RealWebSocket.prototype.__proto__, | |
| 567 proxyProperties(RealWebSocket.prototype) | |
| 568 ); | |
| 569 } | |
| 570 | |
| 571 injectJS(wrapper, eventName); | |
| 572 } | 468 } |
| 573 | 469 |
| 574 function init(document) | 470 function init(document) |
| 575 { | 471 { |
| 576 var shadow = null; | 472 var shadow = null; |
| 577 var style = null; | 473 var style = null; |
| 578 var observer = null; | 474 var observer = null; |
| 579 var tracer = null; | 475 var tracer = null; |
| 580 | 476 |
| 581 wrapWebSocket(); | 477 wrapWebSocket(); |
| (...skipping 161 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 743 }, true); | 639 }, true); |
| 744 | 640 |
| 745 return updateStylesheet; | 641 return updateStylesheet; |
| 746 } | 642 } |
| 747 | 643 |
| 748 if (document instanceof HTMLDocument) | 644 if (document instanceof HTMLDocument) |
| 749 { | 645 { |
| 750 checkSitekey(); | 646 checkSitekey(); |
| 751 window.updateStylesheet = init(document); | 647 window.updateStylesheet = init(document); |
| 752 } | 648 } |
| LEFT | RIGHT |