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

Delta Between Two Patch Sets: lib/content/snippets.js

Issue 29886700: Issue 6969 - Implement abort-on-property-read snippet (Closed) Base URL: https://hg.adblockplus.org/adblockpluscore/
Left Patch Set: Fix wrap property Created Oct. 29, 2018, 8:01 p.m.
Right Patch Set: Another round of changes Created Oct. 31, 2018, 8:43 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 | no next file » | 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-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 493 matching lines...) Expand 10 before | Expand all | Expand 10 after
504 // 60466176 is 36^5 504 // 60466176 is 36^5
505 // 2176782336 - 60466176 = 2116316160. This ensure to always have 6 505 // 2176782336 - 60466176 = 2116316160. This ensure to always have 6
506 // chars even if Math.random() returns its minimum value 0.0 506 // chars even if Math.random() returns its minimum value 0.0
507 // 507 //
508 return Math.floor(Math.random() * 2116316160 + 60466176).toString(36); 508 return Math.floor(Math.random() * 2116316160 + 60466176).toString(36);
509 } 509 }
510 510
511 function wrapPropertyAccess(object, property, descriptor) 511 function wrapPropertyAccess(object, property, descriptor)
512 { 512 {
513 let currentDescriptor = Object.getOwnPropertyDescriptor(object, property); 513 let currentDescriptor = Object.getOwnPropertyDescriptor(object, property);
514 if (!currentDescriptor) 514 if (currentDescriptor && !currentDescriptor.configurable)
515 { 515 return false;
Manish Jethani 2018/10/30 22:57:28 OK, so here's the thing: We don't need to use the
hub 2018/10/31 16:35:48 Done.
516 if (typeof descriptor.get == "undefined") 516
517 descriptor.get = currentDescriptor.get;
518 if (typeof descriptor.set == "undefined")
519 descriptor.set = currentDescriptor.set;
520 }
521 Object.defineProperty(object, property, descriptor); 517 Object.defineProperty(object, property, descriptor);
518 return true;
522 } 519 }
523 520
524 /** 521 /**
525 * Patches a property on the window object to abort execution when the 522 * Patches a property on the window object to abort execution when the
526 * property is read. 523 * property is read.
527 * No error will be printed in the console. 524 *
525 * No error is be printed to the console.
526 *
528 * The idea originates from 527 * The idea originates from
529 * {@link https://github.com/uBlockOrigin/uAssets/blob/80b195436f8f8d78ba713237b fc268ecfc9d9d2b/filters/resources.txt#L1703 uBlock Origin} 528 * {@link https://github.com/uBlockOrigin/uAssets/blob/80b195436f8f8d78ba713237b fc268ecfc9d9d2b/filters/resources.txt#L1703 uBlock Origin}.
530 * 529 *
531 * @param {string} property The name of the property. 530 * @param {string} property The name of the property.
532 */ 531 */
533 function abortOnPropertyRead(property) 532 function abortOnPropertyRead(property)
534 { 533 {
535 if (!property) 534 if (!property)
536 return; 535 return;
537 536
538 let rid = randomId(); 537 let rid = randomId();
539 538
540 function abort() 539 function abort()
541 { 540 {
542 throw new ReferenceError(rid); 541 throw new ReferenceError(rid);
543 } 542 }
544 543
545 let {onerror} = window; 544 let {onerror} = window;
546 window.onerror = (message, ...rest) => 545 if (wrapPropertyAccess(window, property, {get: abort, set() {}}))
547 { 546 {
548 if (typeof message == "string" && message.includes(rid)) 547 window.onerror = (message, ...rest) =>
549 return true; 548 {
550 if (typeof onerror == "function") 549 if (typeof message == "string" && message.includes(rid))
551 return (() => {}).call.call(onerror, this, message, ...rest); 550 return true;
552 }; 551 if (typeof onerror == "function")
553 552 return (() => {}).call.call(onerror, this, message, ...rest);
554 wrapPropertyAccess(window, property, {get: abort, set() {}}); 553 };
554 }
555 } 555 }
556 556
557 exports["abort-on-property-read"] = makeInjector(abortOnPropertyRead, 557 exports["abort-on-property-read"] = makeInjector(abortOnPropertyRead,
558 wrapPropertyAccess, 558 wrapPropertyAccess,
559 randomId); 559 randomId);
LEFTRIGHT
« no previous file | no next file » | Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Toggle Comments ('s')

Powered by Google App Engine
This is Rietveld