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: Split out the property wrap Created Oct. 23, 2018, 6:22 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 474 matching lines...) Expand 10 before | Expand all | Expand 10 after
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
Manish Jethani 2018/10/24 20:47:45 The last line of the comment here, it's a bit misl
hub 2018/10/24 22:17:38 reworded the comments. Also added a proper JSDoc a
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 function wrapPropertyAccess(object, property, desc) 511 function wrapPropertyAccess(object, property, descriptor)
Manish Jethani 2018/10/24 20:47:44 At least for function parameters, let's stick with
hub 2018/10/24 22:17:37 Done.
505 { 512 {
506 // simple property 513 let currentDescriptor = Object.getOwnPropertyDescriptor(object, property);
507 let d = Object.getOwnPropertyDescriptor(object, property); 514 if (currentDescriptor && !currentDescriptor.configurable)
Manish Jethani 2018/10/24 20:47:45 Maybe we should call it "currentDescriptor" instea
hub 2018/10/24 22:17:38 Done.
508 if (!d) 515 return false;
509 d = {}; 516
510 if (typeof desc.get != "undefined") 517 Object.defineProperty(object, property, descriptor);
511 { 518 return true;
512 if (d.get != desc.get) 519 }
513 d.get = desc.get; 520
514 } 521 /**
515 if (typeof desc.set != "undefined") 522 * Patches a property on the window object to abort execution when the
516 { 523 * property is read.
517 if (d.set != desc.set) 524 *
518 d.set = desc.set; 525 * No error is be printed to the console.
519 } 526 *
520 Object.defineProperty(object, property, d); 527 * The idea originates from
521 } 528 * {@link https://github.com/uBlockOrigin/uAssets/blob/80b195436f8f8d78ba713237b fc268ecfc9d9d2b/filters/resources.txt#L1703 uBlock Origin}.
522 /** 529 *
Manish Jethani 2018/10/24 20:47:45 Nit: We should leave a blank line here.
hub 2018/10/24 22:17:38 Done.
523 * Will patch a property on the window object to abort when read. 530 * @param {string} property The name of the property.
Manish Jethani 2018/10/24 20:47:44 Regarding the JSDoc comment for this snippet, my t
hub 2018/10/24 22:17:37 I disagree. This is documenting this code. It is e
Manish Jethani 2018/10/24 23:02:09 Alright, so we need to credit uBlock Origin for th
hub 2018/10/25 14:32:25 Added a link to uBlock.
524 * It will intercept the onerror callback and block it if tagged. 531 */
525 * 532 function abortOnPropertyRead(property)
526 * @todo handle properties of properties. 533 {
527 * 534 if (!property)
528 * @param {string} prop the name of the property.
529 */
530 function abortOnPropertyRead(prop)
Manish Jethani 2018/10/24 20:47:45 Let's rename "prop" to "property" since we are usi
hub 2018/10/24 22:17:37 Done.
531 {
532 if (!prop)
533 return; 535 return;
534 536
535 let rid = randomId(); 537 let rid = randomId();
536 538
537 function canceller() 539 function abort()
Manish Jethani 2018/10/24 20:47:45 How about we just call this function "abort", whic
hub 2018/10/24 22:17:38 Done.
538 { 540 {
539 throw new ReferenceError(rid); 541 throw new ReferenceError(rid);
540 } 542 }
541 543
542 let {onerror} = window; 544 let {onerror} = window;
Manish Jethani 2018/10/24 21:01:48 Hmm ... I think we're going to need to repeat this
Manish Jethani 2018/10/24 21:12:28 Looking at the other patch, yeah I think we should
hub 2018/10/24 22:17:38 I think it is.
hub 2018/10/24 22:17:38 All of this make the whole thing more complicated,
Manish Jethani 2018/10/24 23:02:09 So you intend to repeat this window.onerror boiler
543 window.onerror = (message, ...rest) => 545 if (wrapPropertyAccess(window, property, {get: abort, set() {}}))
544 { 546 {
545 if (typeof message == "string" && message.includes(rid)) 547 window.onerror = (message, ...rest) =>
Manish Jethani 2018/10/24 23:02:09 I think we should check specifically for "Referenc
hub 2018/10/25 14:32:26 No. Because the message format isn't part of any s
Manish Jethani 2018/10/25 16:12:15 Well it's OK for this to not work and for an error
hub 2018/10/25 20:26:09 I'll make 5 times these. Should be random enough.
546 return true; 548 {
547 if (onerror && typeof onerror == "function") 549 if (typeof message == "string" && message.includes(rid))
Manish Jethani 2018/10/24 20:47:45 I don't think the first condition here is necessar
hub 2018/10/24 22:17:37 Done.
548 return onerror.call(this, message, ...rest); 550 return true;
Manish Jethani 2018/10/24 20:47:45 I just realized that you can have a function that
hub 2018/10/24 22:17:38 Actually I think I went a bit overboard. I don't e
Manish Jethani 2018/10/24 23:02:09 You did not go overboard, because in strict mode t
hub 2018/10/25 14:32:25 No. If the original handler is bound to something,
Manish Jethani 2018/10/25 16:12:15 How can we assume that the original handler will b
hub 2018/10/25 20:26:09 So call it is. BTW if the function object has no `
Manish Jethani 2018/10/25 21:32:25 The call method is a feature of the API, it is not
hub 2018/10/26 15:19:05 Done.
549 }; 551 if (typeof onerror == "function")
550 552 return (() => {}).call.call(onerror, this, message, ...rest);
551 let d = {get: canceller, set() {}}; 553 };
Manish Jethani 2018/10/24 20:47:44 We don't need this temporary variable, in my opini
Manish Jethani 2018/10/24 20:47:45 I feel like we're going to need this noop function
hub 2018/10/24 22:17:38 Done.
hub 2018/10/24 22:17:38 I don't think this make it any clearer.
Manish Jethani 2018/10/24 23:02:09 Acknowledged.
552 554 }
553 wrapPropertyAccess(window, prop, d);
554 } 555 }
555 556
556 exports["abort-on-property-read"] = makeInjector(abortOnPropertyRead, 557 exports["abort-on-property-read"] = makeInjector(abortOnPropertyRead,
557 wrapPropertyAccess, 558 wrapPropertyAccess,
558 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