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: Split out the property wrap Created Oct. 23, 2018, 6:22 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 function randomId()
496 {
497 // 2176782336 is 36^6 which mean 6 chars [a-z0-9]
498 // 60466176 is 36^5
499 // 2176782336 - 60466176 = 2116316160. This ensure always 6 chars
500 // for when Math.random() returns 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
501 return Math.floor(Math.random() * 2116316160 + 60466176).toString(36);
502 }
503
504 function wrapPropertyAccess(object, property, desc)
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 {
506 // simple property
507 let d = Object.getOwnPropertyDescriptor(object, property);
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)
509 d = {};
510 if (typeof desc.get != "undefined")
511 {
512 if (d.get != desc.get)
513 d.get = desc.get;
514 }
515 if (typeof desc.set != "undefined")
516 {
517 if (d.set != desc.set)
518 d.set = desc.set;
519 }
520 Object.defineProperty(object, property, d);
521 }
522 /**
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.
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.
525 *
526 * @todo handle properties of properties.
527 *
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;
534
535 let rid = randomId();
536
537 function canceller()
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 {
539 throw new ReferenceError(rid);
540 }
541
542 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) =>
544 {
545 if (typeof message == "string" && message.includes(rid))
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;
547 if (onerror && typeof onerror == "function")
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);
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 };
550
551 let d = {get: canceller, set() {}};
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
553 wrapPropertyAccess(window, prop, d);
554 }
555
556 exports["abort-on-property-read"] = makeInjector(abortOnPropertyRead,
557 wrapPropertyAccess,
558 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