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: Remove the magic from the setter. Created Feb. 19, 2019, 8:54 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 682 matching lines...) Expand 10 before | Expand all | Expand 10 after
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)
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); 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.configurable) 722 if (currentDescriptor && !currentDescriptor.configurable)
722 return false; 723 return;
723 724
724 let v; 725 let setter = newValue =>
Manish Jethani 2019/02/20 06:20:46 There are two things missing in this implementatio
Manish Jethani 2019/02/20 06:28:53 Let me elaborate a little ...
hub 2019/02/22 17:36:19 Fixed them.
725 let setter = a => 726 {
726 { 727 value = newValue;
727 v = a; 728 if (newValue && typeof newValue == "object")
728 if (a && typeof a == "object") 729 wrapPropertyAccess(newValue, property, descriptor);
729 wrapPropertyAccess(a, property, descriptor);
730 }; 730 };
731 Object.defineProperty(object, name, {get: () => v, set: setter}); 731
732 return true; 732 Object.defineProperty(object, name, {get: () => value, set: setter});
733 } 733 }
734 734
735 /** 735 /**
736 * Overrides the <code>onerror</code> handler to discard tagged error messages 736 * Overrides the <code>onerror</code> handler to discard tagged error messages
737 * from our property wrapping. 737 * from our property wrapping.
738 * 738 *
739 * @param {string} magic The magic string that tags the error message. 739 * @param {string} magic The magic string that tags the error message.
740 */ 740 */
741 function overrideOnError(magic) 741 function overrideOnError(magic)
742 { 742 {
(...skipping 23 matching lines...) Expand all
766 if (!property) 766 if (!property)
767 return; 767 return;
768 768
769 let rid = randomId(); 769 let rid = randomId();
770 770
771 function abort() 771 function abort()
772 { 772 {
773 throw new ReferenceError(rid); 773 throw new ReferenceError(rid);
774 } 774 }
775 775
776 if (wrapPropertyAccess(window, property, {get: abort, set() {}})) 776 wrapPropertyAccess(window, property, {get: abort, set() {}});
777 overrideOnError(rid); 777 overrideOnError(rid);
778 } 778 }
779 779
780 exports["abort-on-property-read"] = makeInjector(abortOnPropertyRead, 780 exports["abort-on-property-read"] = makeInjector(abortOnPropertyRead,
781 wrapPropertyAccess, 781 wrapPropertyAccess,
782 overrideOnError, 782 overrideOnError,
783 randomId); 783 randomId);
784 784
785 /** 785 /**
786 * 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
787 * property is written. 787 * property is written.
(...skipping 10 matching lines...) Expand all
798 if (!property) 798 if (!property)
799 return; 799 return;
800 800
801 let rid = randomId(); 801 let rid = randomId();
802 802
803 function abort() 803 function abort()
804 { 804 {
805 throw new ReferenceError(rid); 805 throw new ReferenceError(rid);
806 } 806 }
807 807
808 if (wrapPropertyAccess(window, property, {set: abort})) 808 wrapPropertyAccess(window, property, {set: abort});
809 overrideOnError(rid); 809 overrideOnError(rid);
810 } 810 }
811 811
812 exports["abort-on-property-write"] = makeInjector(abortOnPropertyWrite, 812 exports["abort-on-property-write"] = makeInjector(abortOnPropertyWrite,
813 wrapPropertyAccess, 813 wrapPropertyAccess,
814 overrideOnError, 814 overrideOnError,
815 randomId); 815 randomId);
816 816
817 /** 817 /**
818 * Aborts the execution of an inline script. 818 * Aborts the execution of an inline script.
819 * 819 *
(...skipping 12 matching lines...) Expand all
832 832
833 833
834 let object = window; 834 let object = window;
835 let path = api.split("."); 835 let path = api.split(".");
836 let name = path.pop(); 836 let name = path.pop();
837 837
838 for (let node of path) 838 for (let node of path)
839 { 839 {
840 object = object[node]; 840 object = object[node];
841 841
842 if (!object || typeof object != "object") 842 if (!object || !(typeof object == "object" || typeof object == "function"))
843 return; 843 return;
844 } 844 }
845 845
846 let currentValue = object[name]; 846 let currentValue = object[name];
847 847
848 let abort = () => 848 let abort = () =>
849 { 849 {
850 let element = document.currentScript; 850 let element = document.currentScript;
851 if (element instanceof HTMLScriptElement && element.src == "" && 851 if (element instanceof HTMLScriptElement && element.src == "" &&
852 element != us && (!re || re.test(element.textContent))) 852 element != us && (!re || re.test(element.textContent)))
853 { 853 {
854 throw new ReferenceError(rid); 854 throw new ReferenceError(rid);
855 } 855 }
856 }; 856 };
857 857
858 let descriptor = { 858 let descriptor = {
859 get() 859 get()
860 { 860 {
861 abort(); 861 abort();
862 return currentValue; 862 return currentValue;
863 }, 863 },
864 set(value) 864 set(value)
865 { 865 {
866 abort(); 866 abort();
867 currentValue = value; 867 currentValue = value;
868 } 868 }
869 }; 869 };
870 870
871 if (wrapPropertyAccess(object, name, descriptor)) 871 wrapPropertyAccess(object, name, descriptor);
872 overrideOnError(rid); 872 overrideOnError(rid);
873 } 873 }
874 874
875 exports["abort-current-inline-script"] = 875 exports["abort-current-inline-script"] =
876 makeInjector(abortCurrentInlineScript, wrapPropertyAccess, toRegExp, 876 makeInjector(abortCurrentInlineScript, wrapPropertyAccess, toRegExp,
877 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