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

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

Issue 29995559: Issue 7236 - Handle sub properties in abort-on-property snippets (Closed) Base URL: https://hg.adblockplus.org/adblockpluscore/
Left Patch Set: added the tests back now that we can run them Created Feb. 6, 2019, 4:04 p.m.
Right Patch Set: tweak property names Created March 20, 2019, 4:18 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 | test/browser/_utils.js » ('j') | 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 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, magic) 701 function wrapPropertyAccess(object, property, descriptor)
702 { 702 {
703 let dot = property.indexOf("."); 703 let dotIndex = property.indexOf(".");
704 if (dot == -1) 704 if (dotIndex == -1)
705 { 705 {
706 // simple property case. 706 // simple property case.
707 let currentDescriptor = Object.getOwnPropertyDescriptor(object, property); 707 let currentDescriptor = Object.getOwnPropertyDescriptor(object, property);
708 if (currentDescriptor && !currentDescriptor.configurable) 708 if (currentDescriptor && !currentDescriptor.configurable)
709 return false; 709 return;
710 710
711 Object.defineProperty(object, property, descriptor); 711 Object.defineProperty(object, property, descriptor);
712 return true; 712 return;
713 } 713 }
714 let name = property.slice(0, dot); 714
715 property = property.slice(dot + 1); 715 let name = property.slice(0, dotIndex);
716 property = property.slice(dotIndex + 1);
716 let value = object[name]; 717 let value = object[name];
717 if (value && typeof value == "object") 718 if (value && typeof value == "object")
718 return wrapPropertyAccess(value, property, descriptor, magic); 719 wrapPropertyAccess(value, property, descriptor);
719 720
720 let currentDescriptor = Object.getOwnPropertyDescriptor(object, name); 721 let currentDescriptor = Object.getOwnPropertyDescriptor(object, name);
721 if (currentDescriptor) 722 if (currentDescriptor && !currentDescriptor.configurable)
722 { 723 return;
723 if (currentDescriptor.set && currentDescriptor.set.hasOwnProperty(magic)) 724
724 return true; 725 let setter = newValue =>
725 if (!currentDescriptor.configurable) 726 {
726 return false; 727 value = newValue;
727 } 728 if (newValue && typeof newValue == "object")
728 729 wrapPropertyAccess(newValue, property, descriptor);
729 let v;
730 let setter = a =>
731 {
732 v = a;
733 if (a && typeof a == "object")
734 wrapPropertyAccess(a, property, descriptor, magic);
735 }; 730 };
736 setter[magic] = undefined; 731
Manish Jethani 2019/02/08 10:49:48 I do not quite understand why this tag is necessar
hub 2019/02/08 14:14:03 See above line 723 where we check if it's here and
Manish Jethani 2019/02/12 16:12:38 Yes, but my question is more about why we need the
hub 2019/02/12 18:11:28 so we don't try to set it again if it has already
Manish Jethani 2019/02/15 14:45:24 Well in any case it's redundant because configurab
hub 2019/02/19 15:36:48 How do we know we are the one who set it?
Manish Jethani 2019/02/19 18:13:07 We don't know, but if it's not configurable we can
hub 2019/02/19 20:54:52 Done.
737 Object.defineProperty(object, name, {get: () => v, set: setter}); 732 Object.defineProperty(object, name, {get: () => value, set: setter});
738 return true;
739 } 733 }
740 734
741 /** 735 /**
742 * Overrides the <code>onerror</code> handler to discard tagged error messages 736 * Overrides the <code>onerror</code> handler to discard tagged error messages
743 * from our property wrapping. 737 * from our property wrapping.
744 * 738 *
745 * @param {string} magic The magic string that tags the error message. 739 * @param {string} magic The magic string that tags the error message.
746 */ 740 */
747 function overrideOnError(magic) 741 function overrideOnError(magic)
748 { 742 {
(...skipping 23 matching lines...) Expand all
772 if (!property) 766 if (!property)
773 return; 767 return;
774 768
775 let rid = randomId(); 769 let rid = randomId();
776 770
777 function abort() 771 function abort()
778 { 772 {
779 throw new ReferenceError(rid); 773 throw new ReferenceError(rid);
780 } 774 }
781 775
782 if (wrapPropertyAccess(window, property, {get: abort, set() {}}, rid)) 776 wrapPropertyAccess(window, property, {get: abort, set() {}});
783 overrideOnError(rid); 777 overrideOnError(rid);
784 } 778 }
785 779
786 exports["abort-on-property-read"] = makeInjector(abortOnPropertyRead, 780 exports["abort-on-property-read"] = makeInjector(abortOnPropertyRead,
787 wrapPropertyAccess, 781 wrapPropertyAccess,
788 overrideOnError, 782 overrideOnError,
789 randomId); 783 randomId);
790 784
791 /** 785 /**
792 * Patches a property on the window object to abort execution when the 786 * Patches a property on the window object to abort execution when the
793 * property is written. 787 * property is written.
(...skipping 10 matching lines...) Expand all
804 if (!property) 798 if (!property)
805 return; 799 return;
806 800
807 let rid = randomId(); 801 let rid = randomId();
808 802
809 function abort() 803 function abort()
810 { 804 {
811 throw new ReferenceError(rid); 805 throw new ReferenceError(rid);
812 } 806 }
813 807
814 if (wrapPropertyAccess(window, property, {set: abort}, rid)) 808 wrapPropertyAccess(window, property, {set: abort});
815 overrideOnError(rid); 809 overrideOnError(rid);
816 } 810 }
817 811
818 exports["abort-on-property-write"] = makeInjector(abortOnPropertyWrite, 812 exports["abort-on-property-write"] = makeInjector(abortOnPropertyWrite,
819 wrapPropertyAccess, 813 wrapPropertyAccess,
820 overrideOnError, 814 overrideOnError,
821 randomId); 815 randomId);
822 816
823 /** 817 /**
824 * Aborts the execution of an inline script. 818 * Aborts the execution of an inline script.
825 * 819 *
(...skipping 12 matching lines...) Expand all
838 832
839 833
840 let object = window; 834 let object = window;
841 let path = api.split("."); 835 let path = api.split(".");
842 let name = path.pop(); 836 let name = path.pop();
843 837
844 for (let node of path) 838 for (let node of path)
845 { 839 {
846 object = object[node]; 840 object = object[node];
847 841
848 if (!object || typeof object != "object") 842 if (!object || !(typeof object == "object" || typeof object == "function"))
849 return; 843 return;
850 } 844 }
851 845
852 let currentValue = object[name]; 846 let currentValue = object[name];
853 847
854 let abort = () => 848 let abort = () =>
855 { 849 {
856 let element = document.currentScript; 850 let element = document.currentScript;
857 if (element instanceof HTMLScriptElement && element.src == "" && 851 if (element instanceof HTMLScriptElement && element.src == "" &&
858 element != us && (!re || re.test(element.textContent))) 852 element != us && (!re || re.test(element.textContent)))
859 { 853 {
860 throw new ReferenceError(rid); 854 throw new ReferenceError(rid);
861 } 855 }
862 }; 856 };
863 857
864 let descriptor = { 858 let descriptor = {
865 get() 859 get()
866 { 860 {
867 abort(); 861 abort();
868 return currentValue; 862 return currentValue;
869 }, 863 },
870 set(value) 864 set(value)
871 { 865 {
872 abort(); 866 abort();
873 currentValue = value; 867 currentValue = value;
874 } 868 }
875 }; 869 };
876 870
877 if (wrapPropertyAccess(object, name, descriptor, rid)) 871 wrapPropertyAccess(object, name, descriptor);
878 overrideOnError(rid); 872 overrideOnError(rid);
879 } 873 }
880 874
881 exports["abort-current-inline-script"] = 875 exports["abort-current-inline-script"] =
882 makeInjector(abortCurrentInlineScript, wrapPropertyAccess, toRegExp, 876 makeInjector(abortCurrentInlineScript, wrapPropertyAccess, toRegExp,
883 overrideOnError, regexEscape, randomId); 877 overrideOnError, regexEscape, randomId);
878
879 /**
880 * Strips a query string parameter from <code>fetch()</code> calls.
881 *
882 * @param {string} name The name of the parameter.
883 * @param {?string} [urlPattern] An optional pattern that the URL must match.
884 */
885 function stripFetchQueryParameter(name, urlPattern = null)
886 {
887 let fetch_ = window.fetch;
888 if (typeof fetch_ != "function")
889 return;
890
891 let urlRegExp = urlPattern ? toRegExp(urlPattern) : null;
892 window.fetch = function fetch(...args)
893 {
894 let [source] = args;
895 if (typeof source == "string" &&
896 (!urlRegExp || urlRegExp.test(source)))
897 {
898 let url = new URL(source);
899 url.searchParams.delete(name);
900 args[0] = url.href;
901 }
902 return fetch_.apply(this, args);
903 };
904 }
905
906 exports["strip-fetch-query-parameter"] = makeInjector(stripFetchQueryParameter,
907 toRegExp, regexEscape);
LEFTRIGHT

Powered by Google App Engine
This is Rietveld