Left: | ||
Right: |
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 475 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
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 /** | 495 /** |
496 * Generate an alpha-numeric id 6 chars long, in the range 100000..zzzzzz. | 496 * Generates a random alphanumeric ID consisting of 6 base-36 digits |
Manish Jethani
2018/10/24 23:02:10
Some nits:
s/Generate/Generates/
s/an alpha-nume
hub
2018/10/25 14:32:27
Done.
| |
497 * | 497 * from the range 100000..zzzzzz (both inclusive). |
498 * @returns {string} the random id. | 498 * |
499 * @returns {string} The random ID. | |
499 */ | 500 */ |
500 function randomId() | 501 function randomId() |
501 { | 502 { |
502 // 2176782336 is 36^6 which mean 6 chars [a-z0-9] | 503 // 2176782336 is 36^6 which mean 6 chars [a-z0-9] |
503 // 60466176 is 36^5 | 504 // 60466176 is 36^5 |
504 // 2176782336 - 60466176 = 2116316160. This ensure to always have 6 | 505 // 2176782336 - 60466176 = 2116316160. This ensure to always have 6 |
505 // chars even if Math.random() returns its minimum value 0.0 | 506 // chars even if Math.random() returns its minimum value 0.0 |
507 // | |
506 return Math.floor(Math.random() * 2116316160 + 60466176).toString(36); | 508 return Math.floor(Math.random() * 2116316160 + 60466176).toString(36); |
507 } | 509 } |
508 | 510 |
509 function wrapPropertyAccess(object, property, descriptor) | 511 function wrapPropertyAccess(object, property, descriptor) |
510 { | 512 { |
511 // simple property | |
Manish Jethani
2018/10/24 23:02:10
Do we need this comment? The code is quite straigh
hub
2018/10/25 14:32:28
was carried over from other changes. removed it.
| |
512 let currentDescriptor = Object.getOwnPropertyDescriptor(object, property); | 513 let currentDescriptor = Object.getOwnPropertyDescriptor(object, property); |
513 if (!currentDescriptor) | 514 if (currentDescriptor && !currentDescriptor.configurable) |
514 currentDescriptor = {}; | 515 return false; |
515 if (typeof descriptor.get != "undefined") | 516 |
516 { | 517 Object.defineProperty(object, property, descriptor); |
517 if (currentDescriptor.get != descriptor.get) | 518 return true; |
Manish Jethani
2018/10/24 23:02:10
I'm not seeing the point of this check.
hub
2018/10/25 14:32:28
Done.
| |
518 currentDescriptor.get = descriptor.get; | 519 } |
519 } | 520 |
520 if (typeof descriptor.set != "undefined") | 521 /** |
521 { | 522 * Patches a property on the window object to abort execution when the |
522 if (currentDescriptor.set != descriptor.set) | 523 * property is read. |
523 currentDescriptor.set = descriptor.set; | 524 * |
524 } | 525 * No error is be printed to the console. |
525 Object.defineProperty(object, property, currentDescriptor); | 526 * |
526 } | 527 * The idea originates from |
527 | 528 * {@link https://github.com/uBlockOrigin/uAssets/blob/80b195436f8f8d78ba713237b fc268ecfc9d9d2b/filters/resources.txt#L1703 uBlock Origin}. |
528 /** | 529 * |
529 * Will patch a property on the window object to abort when read. | 530 * @param {string} property The name of the property. |
530 * It will intercept the onerror callback and block it if tagged. | |
531 * | |
532 * @todo handle properties of properties. | |
533 * | |
534 * @param {string} property the name of the property. | |
535 */ | 531 */ |
536 function abortOnPropertyRead(property) | 532 function abortOnPropertyRead(property) |
537 { | 533 { |
538 if (!property) | 534 if (!property) |
539 return; | 535 return; |
540 | 536 |
541 let rid = randomId(); | 537 let rid = randomId(); |
542 | 538 |
543 function abort() | 539 function abort() |
544 { | 540 { |
545 throw new ReferenceError(rid); | 541 throw new ReferenceError(rid); |
546 } | 542 } |
547 | 543 |
548 let {onerror} = window; | 544 let {onerror} = window; |
549 window.onerror = (message, ...rest) => | 545 if (wrapPropertyAccess(window, property, {get: abort, set() {}})) |
550 { | 546 { |
551 if (typeof message == "string" && message.includes(rid)) | 547 window.onerror = (message, ...rest) => |
552 return true; | 548 { |
553 if (typeof onerror == "function") | 549 if (typeof message == "string" && message.includes(rid)) |
554 return onerror(message, ...rest); | 550 return true; |
555 }; | 551 if (typeof onerror == "function") |
556 | 552 return (() => {}).call.call(onerror, this, message, ...rest); |
557 wrapPropertyAccess(window, property, {get: abort, set() {}}); | 553 }; |
554 } | |
558 } | 555 } |
559 | 556 |
560 exports["abort-on-property-read"] = makeInjector(abortOnPropertyRead, | 557 exports["abort-on-property-read"] = makeInjector(abortOnPropertyRead, |
561 wrapPropertyAccess, | 558 wrapPropertyAccess, |
562 randomId); | 559 randomId); |
LEFT | RIGHT |