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-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 |
(...skipping 474 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
485 (!re || re.test(target.textContent))) | 485 (!re || re.test(target.textContent))) |
486 { | 486 { |
487 event.preventDefault(); | 487 event.preventDefault(); |
488 } | 488 } |
489 }, | 489 }, |
490 true); | 490 true); |
491 } | 491 } |
492 | 492 |
493 exports["prevent-inline-scripts"] = preventInlineScripts; | 493 exports["prevent-inline-scripts"] = preventInlineScripts; |
494 | 494 |
| 495 /** |
| 496 * Generates a random alphanumeric ID consisting of 6 base-36 digits |
| 497 * from the range 100000..zzzzzz (both inclusive). |
| 498 * |
| 499 * @returns {string} The random ID. |
| 500 */ |
495 function randomId() | 501 function randomId() |
496 { | 502 { |
497 // 2176782336 is 36^6 which mean 6 chars [a-z0-9] | 503 // 2176782336 is 36^6 which mean 6 chars [a-z0-9] |
498 // 60466176 is 36^5 | 504 // 60466176 is 36^5 |
499 // 2176782336 - 60466176 = 2116316160. This ensure always 6 chars | 505 // 2176782336 - 60466176 = 2116316160. This ensure to always have 6 |
500 // for when Math.random() returns 0.0 | 506 // chars even if Math.random() returns its minimum value 0.0 |
| 507 // |
501 return Math.floor(Math.random() * 2116316160 + 60466176).toString(36); | 508 return Math.floor(Math.random() * 2116316160 + 60466176).toString(36); |
502 } | 509 } |
503 | 510 |
504 /** | 511 function wrapPropertyAccess(object, property, descriptor) |
505 * Will patch a property on the window object to abort when read. | 512 { |
506 * It will intercept the onerror callback and block it if tagged. | 513 let currentDescriptor = Object.getOwnPropertyDescriptor(object, property); |
507 * | 514 if (currentDescriptor && !currentDescriptor.configurable) |
508 * @todo handle properties of properties. | 515 return false; |
509 * | 516 |
510 * @param {string} prop the name of the property. | 517 Object.defineProperty(object, property, descriptor); |
511 */ | 518 return true; |
512 function abortOnPropertyRead(prop) | 519 } |
513 { | 520 |
514 if (!prop) | 521 /** |
| 522 * Patches a property on the window object to abort execution when the |
| 523 * property is read. |
| 524 * |
| 525 * No error is be printed to the console. |
| 526 * |
| 527 * The idea originates from |
| 528 * {@link https://github.com/uBlockOrigin/uAssets/blob/80b195436f8f8d78ba713237b
fc268ecfc9d9d2b/filters/resources.txt#L1703 uBlock Origin}. |
| 529 * |
| 530 * @param {string} property The name of the property. |
| 531 */ |
| 532 function abortOnPropertyRead(property) |
| 533 { |
| 534 if (!property) |
515 return; | 535 return; |
516 | 536 |
517 let magic = randomId(); | 537 let rid = randomId(); |
518 | 538 |
519 let abort = function() | 539 function abort() |
520 { | 540 { |
521 throw new ReferenceError(magic); | 541 throw new ReferenceError(rid); |
522 }; | 542 } |
523 | 543 |
524 let {onerror} = window; | 544 let {onerror} = window; |
525 window.onerror = (message, ...rest) => | 545 if (wrapPropertyAccess(window, property, {get: abort, set() {}})) |
526 { | 546 { |
527 if (typeof message == "string" && message.includes(magic)) | 547 window.onerror = (message, ...rest) => |
528 return true; | 548 { |
529 if (onerror && typeof onerro == "function") | 549 if (typeof message == "string" && message.includes(rid)) |
530 return onerror(this, message, ...rest); | 550 return true; |
531 }; | 551 if (typeof onerror == "function") |
532 | 552 return (() => {}).call.call(onerror, this, message, ...rest); |
533 (function(o, p) | 553 }; |
534 { | 554 } |
535 // simple property | 555 } |
536 let d = Object.getOwnPropertyDescriptor(o, p); | 556 |
537 if (!d || d.get != abort) | 557 exports["abort-on-property-read"] = makeInjector(abortOnPropertyRead, |
538 Object.defineProperty(o, p, {get: abort, set() {}}); | 558 wrapPropertyAccess, |
539 })(window, prop); | 559 randomId); |
540 } | |
541 | |
542 exports["abort-on-property-read"] = makeInjector(abortOnPropertyRead, randomId); | |
LEFT | RIGHT |