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