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: Pass magic to wrapPropertyAccess. Fix return value. Created Feb. 5, 2019, 3:19 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 && currentDescriptor.set && 722 if (currentDescriptor && !currentDescriptor.configurable)
722 currentDescriptor.set.hasOwnProperty(magic)) 723 return;
723 { 724
724 return true; 725 let setter = newValue =>
725 } 726 {
726 727 value = newValue;
727 let v; 728 if (newValue && typeof newValue == "object")
728 let setter = a => 729 wrapPropertyAccess(newValue, property, descriptor);
729 {
730 v = a;
731 if (a && typeof a == "object")
732 wrapPropertyAccess(a, property, descriptor, magic);
733 }; 730 };
734 setter[magic] = undefined; 731
735 Object.defineProperty(object, name, {get: () => v, set: setter}); 732 Object.defineProperty(object, name, {get: () => value, set: setter});
Manish Jethani 2019/02/05 15:39:08 What if object.name is non-configurable? Similar t
hub 2019/02/05 19:22:26 Exception will be thrown. I'll address this.
736 return true;
737 } 733 }
738 734
739 /** 735 /**
740 * Overrides the <code>onerror</code> handler to discard tagged error messages 736 * Overrides the <code>onerror</code> handler to discard tagged error messages
741 * from our property wrapping. 737 * from our property wrapping.
742 * 738 *
743 * @param {string} magic The magic string that tags the error message. 739 * @param {string} magic The magic string that tags the error message.
744 */ 740 */
745 function overrideOnError(magic) 741 function overrideOnError(magic)
746 { 742 {
(...skipping 23 matching lines...) Expand all
770 if (!property) 766 if (!property)
771 return; 767 return;
772 768
773 let rid = randomId(); 769 let rid = randomId();
774 770
775 function abort() 771 function abort()
776 { 772 {
777 throw new ReferenceError(rid); 773 throw new ReferenceError(rid);
778 } 774 }
779 775
780 if (wrapPropertyAccess(window, property, {get: abort, set() {}}, rid)) 776 wrapPropertyAccess(window, property, {get: abort, set() {}});
781 overrideOnError(rid); 777 overrideOnError(rid);
782 } 778 }
783 779
784 exports["abort-on-property-read"] = makeInjector(abortOnPropertyRead, 780 exports["abort-on-property-read"] = makeInjector(abortOnPropertyRead,
785 wrapPropertyAccess, 781 wrapPropertyAccess,
786 overrideOnError, 782 overrideOnError,
787 randomId); 783 randomId);
788 784
789 /** 785 /**
790 * 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
791 * property is written. 787 * property is written.
(...skipping 10 matching lines...) Expand all
802 if (!property) 798 if (!property)
803 return; 799 return;
804 800
805 let rid = randomId(); 801 let rid = randomId();
806 802
807 function abort() 803 function abort()
808 { 804 {
809 throw new ReferenceError(rid); 805 throw new ReferenceError(rid);
810 } 806 }
811 807
812 if (wrapPropertyAccess(window, property, {set: abort}, rid)) 808 wrapPropertyAccess(window, property, {set: abort});
813 overrideOnError(rid); 809 overrideOnError(rid);
814 } 810 }
815 811
816 exports["abort-on-property-write"] = makeInjector(abortOnPropertyWrite, 812 exports["abort-on-property-write"] = makeInjector(abortOnPropertyWrite,
817 wrapPropertyAccess, 813 wrapPropertyAccess,
818 overrideOnError, 814 overrideOnError,
819 randomId); 815 randomId);
820 816
821 /** 817 /**
822 * Aborts the execution of an inline script. 818 * Aborts the execution of an inline script.
823 * 819 *
(...skipping 12 matching lines...) Expand all
836 832
837 833
838 let object = window; 834 let object = window;
839 let path = api.split("."); 835 let path = api.split(".");
840 let name = path.pop(); 836 let name = path.pop();
841 837
842 for (let node of path) 838 for (let node of path)
843 { 839 {
844 object = object[node]; 840 object = object[node];
845 841
846 if (!object || typeof object != "object") 842 if (!object || !(typeof object == "object" || typeof object == "function"))
847 return; 843 return;
848 } 844 }
849 845
850 let currentValue = object[name]; 846 let currentValue = object[name];
851 847
852 let abort = () => 848 let abort = () =>
853 { 849 {
854 let element = document.currentScript; 850 let element = document.currentScript;
855 if (element instanceof HTMLScriptElement && element.src == "" && 851 if (element instanceof HTMLScriptElement && element.src == "" &&
856 element != us && (!re || re.test(element.textContent))) 852 element != us && (!re || re.test(element.textContent)))
857 { 853 {
858 throw new ReferenceError(rid); 854 throw new ReferenceError(rid);
859 } 855 }
860 }; 856 };
861 857
862 let descriptor = { 858 let descriptor = {
863 get() 859 get()
864 { 860 {
865 abort(); 861 abort();
866 return currentValue; 862 return currentValue;
867 }, 863 },
868 set(value) 864 set(value)
869 { 865 {
870 abort(); 866 abort();
871 currentValue = value; 867 currentValue = value;
872 } 868 }
873 }; 869 };
874 870
875 if (wrapPropertyAccess(object, name, descriptor, rid)) 871 wrapPropertyAccess(object, name, descriptor);
876 overrideOnError(rid); 872 overrideOnError(rid);
877 } 873 }
878 874
879 exports["abort-current-inline-script"] = 875 exports["abort-current-inline-script"] =
880 makeInjector(abortCurrentInlineScript, wrapPropertyAccess, toRegExp, 876 makeInjector(abortCurrentInlineScript, wrapPropertyAccess, toRegExp,
881 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