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

Side by Side Diff: lib/content/snippets.js

Issue 29886700: Issue 6969 - Implement abort-on-property-read snippet (Closed) Base URL: https://hg.adblockplus.org/adblockpluscore/
Patch Set: Rework wrapPropertyAccess Created Oct. 31, 2018, 4:35 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 473 matching lines...) Expand 10 before | Expand all | Expand 10 after
484 if ((!selector || target.matches(selector)) && 484 if ((!selector || target.matches(selector)) &&
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
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 */
501 function randomId()
502 {
503 // 2176782336 is 36^6 which mean 6 chars [a-z0-9]
504 // 60466176 is 36^5
505 // 2176782336 - 60466176 = 2116316160. This ensure to always have 6
506 // chars even if Math.random() returns its minimum value 0.0
507 //
508 return Math.floor(Math.random() * 2116316160 + 60466176).toString(36);
509 }
510
511 function wrapPropertyAccess(object, property, descriptor)
512 {
513 let currentDescriptor = Object.getOwnPropertyDescriptor(object, property);
514 if (currentDescriptor && !currentDescriptor.configurable)
515 return false;
516
517 Object.defineProperty(object, property, descriptor);
518 return true;
519 }
520
521 /**
522 * Patches a property on the window object to abort execution when the
523 * property is read.
524 * No error will be printed in the console.
Manish Jethani 2018/10/31 19:54:49 Nit: Again, is the "No error will be printed" part
Manish Jethani 2018/10/31 19:54:49 Nit: Even though it doesn't matter to the generate
hub 2018/10/31 20:44:16 Done.
hub 2018/10/31 20:44:17 Done.
525 * The idea originates from
Manish Jethani 2018/10/31 19:54:49 Nit: Period at the end of the sentence.
hub 2018/10/31 20:44:16 Done.
526 * {@link https://github.com/uBlockOrigin/uAssets/blob/80b195436f8f8d78ba713237b fc268ecfc9d9d2b/filters/resources.txt#L1703 uBlock Origin}
527 *
528 * @param {string} property The name of the property.
529 */
530 function abortOnPropertyRead(property)
531 {
532 if (!property)
533 return;
534
535 let rid = randomId();
536
537 function abort()
538 {
539 throw new ReferenceError(rid);
540 }
541
542 if (wrapPropertyAccess(window, property, {get: abort, set() {}}))
543 {
544 let {onerror} = window;
Manish Jethani 2018/10/31 19:38:16 Can we move this line outside the if block? In the
hub 2018/10/31 20:44:16 It can be moved. But it would still bomb if it wa
Manish Jethani 2018/10/31 20:52:22 Alright, fair enough.
545 window.onerror = (message, ...rest) =>
546 {
547 if (typeof message == "string" && message.includes(rid))
548 return true;
549 if (typeof onerror == "function")
550 return (() => {}).call.call(onerror, this, message, ...rest);
551 };
552 }
553 }
554
555 exports["abort-on-property-read"] = makeInjector(abortOnPropertyRead,
556 wrapPropertyAccess,
557 randomId);
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld