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

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

Issue 29995559: Issue 7236 - Handle sub properties in abort-on-property snippets (Closed) Base URL: https://hg.adblockplus.org/adblockpluscore/
Patch Set: Created Feb. 1, 2019, 8:14 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 | test/browser/snippets.js » ('j') | 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 680 matching lines...) Expand 10 before | Expand all | Expand 10 after
691 function randomId() 691 function randomId()
692 { 692 {
693 // 2176782336 is 36^6 which mean 6 chars [a-z0-9] 693 // 2176782336 is 36^6 which mean 6 chars [a-z0-9]
694 // 60466176 is 36^5 694 // 60466176 is 36^5
695 // 2176782336 - 60466176 = 2116316160. This ensure to always have 6 695 // 2176782336 - 60466176 = 2116316160. This ensure to always have 6
696 // chars even if Math.random() returns its minimum value 0.0 696 // chars even if Math.random() returns its minimum value 0.0
697 // 697 //
698 return Math.floor(Math.random() * 2116316160 + 60466176).toString(36); 698 return Math.floor(Math.random() * 2116316160 + 60466176).toString(36);
699 } 699 }
700 700
701 function wrapPropertyAccess(object, property, descriptor) 701 function wrapPropertyAccess(object, property, descriptor, magic)
702 { 702 {
703 let currentDescriptor = Object.getOwnPropertyDescriptor(object, property); 703 let dot = property.indexOf(".");
704 if (currentDescriptor && !currentDescriptor.configurable) 704 if (dot == -1)
705 return false; 705 {
706 // simple property case.
707 let currentDescriptor = Object.getOwnPropertyDescriptor(object, property);
708 if (currentDescriptor && !currentDescriptor.configurable)
709 return false;
706 710
707 Object.defineProperty(object, property, descriptor); 711 Object.defineProperty(object, property, descriptor);
708 return true; 712 return true;
713 }
714 let prop = property.slice(0, dot);
Manish Jethani 2019/02/03 14:30:38 We could just call this `name` instead of `prop`.
hub 2019/02/04 15:48:02 Done.
715 property = property.slice(dot + 1);
716 let value = object[prop];
717 if (typeof value != "undefined")
Manish Jethani 2019/02/03 14:30:38 What if `value` is `null`? I think we want to chec
hub 2019/02/04 15:48:02 Actually I think we want `value intanceof Object`
718 return wrapPropertyAccess(value, property, descriptor);
719
720 let currentDescriptor = Object.getOwnPropertyDescriptor(object, prop);
721 if (currentDescriptor && currentDescriptor.set &&
722 magic && currentDescriptor.set.hasOwnProperty(magic))
723 {
724 return true;
725 }
726
727 let v;
728 let setter = a =>
729 {
730 v = a;
731 if (a instanceof Object)
Manish Jethani 2019/02/03 14:30:38 This check is inconsistent with the one above. I t
hub 2019/02/04 15:48:02 `a instanceof Object` is false if a is null. Unlik
Manish Jethani 2019/02/04 17:56:33 We've been here before: https://hg.adblockplus.or
hub 2019/02/04 19:54:36 So be it. And this actually make the test pass on
732 wrapPropertyAccess(a, property, descriptor, magic);
733 };
734 setter[magic] = undefined;
Manish Jethani 2019/02/03 14:30:38 If we just want to tag the setter, I think it's be
hub 2019/02/04 15:48:03 undefined is good. We check it with hasOwnProperty
735 Object.defineProperty(object, prop, {get: () => v, set: setter});
709 } 736 }
710 737
711 /** 738 /**
712 * Overrides the <code>onerror</code> handler to discard tagged error messages 739 * Overrides the <code>onerror</code> handler to discard tagged error messages
713 * from our property wrapping. 740 * from our property wrapping.
714 * 741 *
715 * @param {string} magic The magic string that tags the error message. 742 * @param {string} magic The magic string that tags the error message.
716 */ 743 */
717 function overrideOnError(magic) 744 function overrideOnError(magic)
718 { 745 {
(...skipping 23 matching lines...) Expand all
742 if (!property) 769 if (!property)
743 return; 770 return;
744 771
745 let rid = randomId(); 772 let rid = randomId();
746 773
747 function abort() 774 function abort()
748 { 775 {
749 throw new ReferenceError(rid); 776 throw new ReferenceError(rid);
750 } 777 }
751 778
752 if (wrapPropertyAccess(window, property, {get: abort, set() {}})) 779 if (wrapPropertyAccess(window, property, {get: abort, set() {}}, rid))
753 overrideOnError(rid); 780 overrideOnError(rid);
754 } 781 }
755 782
756 exports["abort-on-property-read"] = makeInjector(abortOnPropertyRead, 783 exports["abort-on-property-read"] = makeInjector(abortOnPropertyRead,
757 wrapPropertyAccess, 784 wrapPropertyAccess,
758 overrideOnError, 785 overrideOnError,
759 randomId); 786 randomId);
760 787
761 /** 788 /**
762 * Patches a property on the window object to abort execution when the 789 * Patches a property on the window object to abort execution when the
(...skipping 11 matching lines...) Expand all
774 if (!property) 801 if (!property)
775 return; 802 return;
776 803
777 let rid = randomId(); 804 let rid = randomId();
778 805
779 function abort() 806 function abort()
780 { 807 {
781 throw new ReferenceError(rid); 808 throw new ReferenceError(rid);
782 } 809 }
783 810
784 if (wrapPropertyAccess(window, property, {set: abort})) 811 if (wrapPropertyAccess(window, property, {set: abort}, rid))
785 overrideOnError(rid); 812 overrideOnError(rid);
786 } 813 }
787 814
788 exports["abort-on-property-write"] = makeInjector(abortOnPropertyWrite, 815 exports["abort-on-property-write"] = makeInjector(abortOnPropertyWrite,
789 wrapPropertyAccess, 816 wrapPropertyAccess,
790 overrideOnError, 817 overrideOnError,
791 randomId); 818 randomId);
792 819
793 /** 820 /**
794 * Aborts the execution of an inline script. 821 * Aborts the execution of an inline script.
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
837 abort(); 864 abort();
838 return currentValue; 865 return currentValue;
839 }, 866 },
840 set(value) 867 set(value)
841 { 868 {
842 abort(); 869 abort();
843 currentValue = value; 870 currentValue = value;
844 } 871 }
845 }; 872 };
846 873
847 if (wrapPropertyAccess(object, name, descriptor)) 874 if (wrapPropertyAccess(object, name, descriptor, rid))
848 overrideOnError(rid); 875 overrideOnError(rid);
849 } 876 }
850 877
851 exports["abort-current-inline-script"] = 878 exports["abort-current-inline-script"] =
852 makeInjector(abortCurrentInlineScript, wrapPropertyAccess, toRegExp, 879 makeInjector(abortCurrentInlineScript, wrapPropertyAccess, toRegExp,
853 overrideOnError, regexEscape, randomId); 880 overrideOnError, regexEscape, randomId);
OLDNEW
« no previous file with comments | « no previous file | test/browser/snippets.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld