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: use typeof instead. Remove test. Created Feb. 4, 2019, 7:54 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 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 name = property.slice(0, dot);
715 property = property.slice(dot + 1);
716 let value = object[name];
717 if (value && typeof value == "object")
718 return wrapPropertyAccess(value, property, descriptor);
Manish Jethani 2019/02/05 14:47:17 Don't we have to pass along magic here?
hub 2019/02/05 15:20:20 Yes we do. Done.
719
720 let currentDescriptor = Object.getOwnPropertyDescriptor(object, name);
721 if (currentDescriptor && currentDescriptor.set &&
722 magic && currentDescriptor.set.hasOwnProperty(magic))
Manish Jethani 2019/02/05 14:47:17 Here check for magic being truthy seems redundant
hub 2019/02/05 15:20:20 might as well remove it. It's a corner case anyway
723 {
724 return true;
725 }
726
727 let v;
728 let setter = a =>
729 {
730 v = a;
731 if (a && typeof a == "object")
732 wrapPropertyAccess(a, property, descriptor, magic);
733 };
734 setter[magic] = undefined;
735 Object.defineProperty(object, name, {get: () => v, set: setter});
709 } 736 }
Manish Jethani 2019/02/05 14:47:17 What about the return value here?
hub 2019/02/05 15:20:20 Done.
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 {
719 let {onerror} = window; 746 let {onerror} = window;
(...skipping 22 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 | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld