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

Side by Side Diff: lib/filterClasses.js

Issue 29680689: [$csp2 adblockpluscore] Issue 6329 - Add the CSP filter type (Closed)
Patch Set: Fully normalise whitespace, avoid strict equality Created March 12, 2018, 1:35 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 | test/filterClasses.js » ('j') | 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 79 matching lines...) Expand 10 before | Expand all | Expand 10 after
90 Filter.elemhideRegExp = /^([^/*|@"!]*?)#([@?])?#(.+)$/; 90 Filter.elemhideRegExp = /^([^/*|@"!]*?)#([@?])?#(.+)$/;
91 /** 91 /**
92 * Regular expression that RegExp filters specified as RegExps should match 92 * Regular expression that RegExp filters specified as RegExps should match
93 * @type {RegExp} 93 * @type {RegExp}
94 */ 94 */
95 Filter.regexpRegExp = /^(@@)?\/.*\/(?:\$~?[\w-]+(?:=[^,\s]+)?(?:,~?[\w-]+(?:=[^, \s]+)?)*)?$/; 95 Filter.regexpRegExp = /^(@@)?\/.*\/(?:\$~?[\w-]+(?:=[^,\s]+)?(?:,~?[\w-]+(?:=[^, \s]+)?)*)?$/;
96 /** 96 /**
97 * Regular expression that options on a RegExp filter should match 97 * Regular expression that options on a RegExp filter should match
98 * @type {RegExp} 98 * @type {RegExp}
99 */ 99 */
100 Filter.optionsRegExp = /\$(~?[\w-]+(?:=[^,\s]+)?(?:,~?[\w-]+(?:=[^,\s]+)?)*)$/; 100 Filter.optionsRegExp = /\$(~?[\w-]+(?:=[^,]+)?(?:,~?[\w-]+(?:=[^,]+)?)*)$/;
101 101
102 /** 102 /**
103 * Creates a filter of correct type from its text representation - does the 103 * Creates a filter of correct type from its text representation - does the
104 * basic parsing and calls the right constructor then. 104 * basic parsing and calls the right constructor then.
105 * 105 *
106 * @param {string} text as in Filter() 106 * @param {string} text as in Filter()
107 * @return {Filter} 107 * @return {Filter}
108 */ 108 */
109 Filter.fromText = function(text) 109 Filter.fromText = function(text)
110 { 110 {
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after
168 * @return {string} 168 * @return {string}
169 */ 169 */
170 Filter.normalize = function(text) 170 Filter.normalize = function(text)
171 { 171 {
172 if (!text) 172 if (!text)
173 return text; 173 return text;
174 174
175 // Remove line breaks and such 175 // Remove line breaks and such
176 text = text.replace(/[^\S ]/g, ""); 176 text = text.replace(/[^\S ]/g, "");
177 177
178 // Don't remove spaces inside comments
178 if (/^\s*!/.test(text)) 179 if (/^\s*!/.test(text))
180 return text.trim();
181
182 // Special treatment for element hiding filters, right side is allowed to
183 // contain spaces
184 if (Filter.elemhideRegExp.test(text))
179 { 185 {
180 // Don't remove spaces inside comments
181 return text.trim();
182 }
183 else if (Filter.elemhideRegExp.test(text))
184 {
185 // Special treatment for element hiding filters, right side is allowed to
186 // contain spaces
187 let [, domain, separator, selector] = /^(.*?)(#@?#?)(.*)$/.exec(text); 186 let [, domain, separator, selector] = /^(.*?)(#@?#?)(.*)$/.exec(text);
188 return domain.replace(/\s/g, "") + separator + selector.trim(); 187 return domain.replace(/\s/g, "") + separator + selector.trim();
189 } 188 }
190 return text.replace(/\s/g, ""); 189
190 // For most regexp filters we strip all whitespace, but the values of $csp
Manish Jethani 2018/03/12 18:53:59 So this doesn't work for the following cases: 1.
Sebastian Noack 2018/03/12 23:19:01 Splitting the options list into an array (for ever
Manish Jethani 2018/03/13 06:59:30 You mean in terms of memory consumption? I ran a t
Manish Jethani 2018/03/13 07:35:00 OK, so I inlined that bit, now this seems to perfo
kzar 2018/03/14 13:54:37 Well spotted, I've added some unit tests for those
Sebastian Noack 2018/03/14 20:59:20 Any reason, why this suggestion was ignored, witho
kzar 2018/03/15 10:26:24 I ignored that since it assumed the first '$' was
Manish Jethani 2018/03/15 10:42:41 I think Sebastian is referring to the rest of the
kzar 2018/03/15 11:38:50 So instead of splitting the options string by ","
Manish Jethani 2018/03/15 12:00:11 Yes, it just turned out to be faster.
Sebastian Noack 2018/03/15 17:25:41 I guess, if it's only in the code path hit for $cs
191 // filter options are allowed to contain single (non trailing) spaces.
192 let strippedText = text.replace(/\s/g, "");
Manish Jethani 2018/03/12 16:34:53 Since we have already stripped out all non-space w
kzar 2018/03/14 13:54:38 I think you're right, Done.
193 if (!/csp=/i.test(strippedText))
194 return strippedText;
195
196 let optionsMatch = Filter.optionsRegExp.exec(strippedText);
Manish Jethani 2018/03/12 16:34:53 We can just look for "$" here and if none is prese
kzar 2018/03/14 13:54:38 No, that's not good enough unfortunately. There ca
Manish Jethani 2018/03/14 18:10:49 OK, but I wonder if we shouldn't just look for a "
197 if (!optionsMatch)
198 return strippedText;
199
200 // We know where the options part starts in the filter text that's stripped
201 // of whitespace, next we must find the corresponding position in the original
202 // filter text.
203 let optionsPosition = 0;
Manish Jethani 2018/03/12 16:34:53 This part is not being used at all.
kzar 2018/03/14 13:54:38 Whoops, you're right. I've fixed that now.
204 let offset = 0;
205 while (offset > -1)
206 {
207 optionsPosition = text.substring(optionsPosition).indexOf("$");
208 offset = strippedText.substring(offset, optionsPosition).indexOf("$");
209 }
210
211 // Finally with that we can generally strip whitespace, being careful to not
212 // to for $csp filter values.
213 let parts = [];
214 let position = 0;
215 let cspRegexp = /(c\s*s\s*p\s*=)([^,]+)/ig;
216 let cspMatch;
217 while (cspMatch = cspRegexp.exec(text))
218 {
219 parts.push(
Manish Jethani 2018/03/12 16:34:53 We've already stripped whitespace once, now we're
kzar 2018/03/14 13:54:37 No because we're expecting thousands of non-csp fi
220 text.substring(position, cspMatch.index + cspMatch[1].length)
221 .replace(/\s/g, "")
222 );
223 parts.push(
224 text.substr(cspMatch.index + cspMatch[1].length, cspMatch[2].length)
225 .replace(/\s+/g, " ")
226 .trim()
227 );
228 position = cspMatch.index + cspMatch[0].length;
229 }
230 parts.push(text.substr(position).replace(/\s/g, ""));
231 return parts.join("");
191 }; 232 };
192 233
193 /** 234 /**
194 * @see filterToRegExp 235 * @see filterToRegExp
195 */ 236 */
196 Filter.toRegExp = filterToRegExp; 237 Filter.toRegExp = filterToRegExp;
197 238
198 /** 239 /**
199 * Class for invalid filters 240 * Class for invalid filters
200 * @param {string} text see Filter() 241 * @param {string} text see Filter()
(...skipping 519 matching lines...) Expand 10 before | Expand all | Expand 10 after
720 blocking = false; 761 blocking = false;
721 text = text.substr(2); 762 text = text.substr(2);
722 } 763 }
723 764
724 let contentType = null; 765 let contentType = null;
725 let matchCase = null; 766 let matchCase = null;
726 let domains = null; 767 let domains = null;
727 let sitekeys = null; 768 let sitekeys = null;
728 let thirdParty = null; 769 let thirdParty = null;
729 let collapse = null; 770 let collapse = null;
771 let csp = null;
730 let options; 772 let options;
731 let match = (text.indexOf("$") >= 0 ? Filter.optionsRegExp.exec(text) : null); 773 let match = (text.indexOf("$") >= 0 ? Filter.optionsRegExp.exec(text) : null);
732 if (match) 774 if (match)
733 { 775 {
734 options = match[1].toUpperCase().split(","); 776 options = match[1].split(",");
735 text = match.input.substr(0, match.index); 777 text = match.input.substr(0, match.index);
736 for (let option of options) 778 for (let option of options)
737 { 779 {
738 let value = null; 780 let value = null;
739 let separatorIndex = option.indexOf("="); 781 let separatorIndex = option.indexOf("=");
740 if (separatorIndex >= 0) 782 if (separatorIndex >= 0)
741 { 783 {
742 value = option.substr(separatorIndex + 1); 784 value = option.substr(separatorIndex + 1);
743 option = option.substr(0, separatorIndex); 785 option = option.substr(0, separatorIndex).toUpperCase();
786
787 if (option == "CSP")
788 value = value.trim();
789 else
790 value = value.replace(/\s/g, "");
744 } 791 }
792 else
793 option = option.toUpperCase();
794
745 option = option.replace(/-/, "_"); 795 option = option.replace(/-/, "_");
796
746 if (option in RegExpFilter.typeMap) 797 if (option in RegExpFilter.typeMap)
747 { 798 {
748 if (contentType == null) 799 if (contentType == null)
749 contentType = 0; 800 contentType = 0;
750 contentType |= RegExpFilter.typeMap[option]; 801 contentType |= RegExpFilter.typeMap[option];
802
803 if (option == "CSP" && typeof value != "undefined")
804 {
805 if (csp)
806 csp.push(value);
807 else
808 csp = [value];
809 }
751 } 810 }
752 else if (option[0] == "~" && option.substr(1) in RegExpFilter.typeMap) 811 else if (option[0] == "~" && option.substr(1) in RegExpFilter.typeMap)
753 { 812 {
754 if (contentType == null) 813 if (contentType == null)
755 ({contentType} = RegExpFilter.prototype); 814 ({contentType} = RegExpFilter.prototype);
756 contentType &= ~RegExpFilter.typeMap[option.substr(1)]; 815 contentType &= ~RegExpFilter.typeMap[option.substr(1)];
757 } 816 }
758 else if (option == "MATCH_CASE") 817 else if (option == "MATCH_CASE")
759 matchCase = true; 818 matchCase = true;
760 else if (option == "~MATCH_CASE") 819 else if (option == "~MATCH_CASE")
761 matchCase = false; 820 matchCase = false;
762 else if (option == "DOMAIN" && typeof value != "undefined") 821 else if (option == "DOMAIN" && typeof value != "undefined")
763 domains = value; 822 domains = value.toUpperCase();
764 else if (option == "THIRD_PARTY") 823 else if (option == "THIRD_PARTY")
765 thirdParty = true; 824 thirdParty = true;
766 else if (option == "~THIRD_PARTY") 825 else if (option == "~THIRD_PARTY")
767 thirdParty = false; 826 thirdParty = false;
768 else if (option == "COLLAPSE") 827 else if (option == "COLLAPSE")
769 collapse = true; 828 collapse = true;
770 else if (option == "~COLLAPSE") 829 else if (option == "~COLLAPSE")
771 collapse = false; 830 collapse = false;
772 else if (option == "SITEKEY" && typeof value != "undefined") 831 else if (option == "SITEKEY" && typeof value != "undefined")
773 sitekeys = value; 832 sitekeys = value.toUpperCase();
774 else 833 else
775 return new InvalidFilter(origText, "filter_unknown_option"); 834 return new InvalidFilter(origText, "filter_unknown_option");
776 } 835 }
777 } 836 }
837 text = text.replace(/\s/g, "");
778 838
779 try 839 try
780 { 840 {
781 if (blocking) 841 if (blocking)
782 { 842 {
843 if (csp)
844 {
845 csp = csp.join("; ").toLowerCase();
846
847 // Prevent filters from injecting report-uri or report-to directives
848 // since they are a privacy concern. Regexp based upon reBadCSP[1].
849 // [1] - https://github.com/gorhill/uBlock/blob/67e06f53b4d73df6179f6d32 0553a55da4ead40e/src/js/static-net-filtering.js#L1362
850 if (/(;|^)\s*report-(to|uri)\b/.test(csp))
851 return new InvalidFilter(origText, "filter_invalid_csp");
852 }
853
783 return new BlockingFilter(origText, text, contentType, matchCase, domains, 854 return new BlockingFilter(origText, text, contentType, matchCase, domains,
784 thirdParty, sitekeys, collapse); 855 thirdParty, sitekeys, collapse, csp);
785 } 856 }
786 return new WhitelistFilter(origText, text, contentType, matchCase, domains, 857 return new WhitelistFilter(origText, text, contentType, matchCase, domains,
787 thirdParty, sitekeys); 858 thirdParty, sitekeys);
788 } 859 }
789 catch (e) 860 catch (e)
790 { 861 {
791 return new InvalidFilter(origText, "filter_invalid_regexp"); 862 return new InvalidFilter(origText, "filter_invalid_regexp");
792 } 863 }
793 }; 864 };
794 865
795 /** 866 /**
796 * Maps type strings like "SCRIPT" or "OBJECT" to bit masks 867 * Maps type strings like "SCRIPT" or "OBJECT" to bit masks
797 */ 868 */
798 RegExpFilter.typeMap = { 869 RegExpFilter.typeMap = {
799 OTHER: 1, 870 OTHER: 1,
800 SCRIPT: 2, 871 SCRIPT: 2,
801 IMAGE: 4, 872 IMAGE: 4,
802 STYLESHEET: 8, 873 STYLESHEET: 8,
803 OBJECT: 16, 874 OBJECT: 16,
804 SUBDOCUMENT: 32, 875 SUBDOCUMENT: 32,
805 DOCUMENT: 64, 876 DOCUMENT: 64,
806 WEBSOCKET: 128, 877 WEBSOCKET: 128,
807 WEBRTC: 256, 878 WEBRTC: 256,
879 CSP: 512,
808 XBL: 1, 880 XBL: 1,
809 PING: 1024, 881 PING: 1024,
810 XMLHTTPREQUEST: 2048, 882 XMLHTTPREQUEST: 2048,
811 OBJECT_SUBREQUEST: 4096, 883 OBJECT_SUBREQUEST: 4096,
812 DTD: 1, 884 DTD: 1,
813 MEDIA: 16384, 885 MEDIA: 16384,
814 FONT: 32768, 886 FONT: 32768,
815 887
816 BACKGROUND: 4, // Backwards compat, same as IMAGE 888 BACKGROUND: 4, // Backwards compat, same as IMAGE
817 889
818 POPUP: 0x10000000, 890 POPUP: 0x10000000,
819 GENERICBLOCK: 0x20000000, 891 GENERICBLOCK: 0x20000000,
820 ELEMHIDE: 0x40000000, 892 ELEMHIDE: 0x40000000,
821 GENERICHIDE: 0x80000000 893 GENERICHIDE: 0x80000000
822 }; 894 };
823 895
824 // DOCUMENT, ELEMHIDE, POPUP, GENERICHIDE and GENERICBLOCK options shouldn't 896 // CSP, DOCUMENT, ELEMHIDE, POPUP, GENERICHIDE and GENERICBLOCK options
825 // be there by default 897 // shouldn't be there by default
826 RegExpFilter.prototype.contentType &= ~(RegExpFilter.typeMap.DOCUMENT | 898 RegExpFilter.prototype.contentType &= ~(RegExpFilter.typeMap.CSP |
899 RegExpFilter.typeMap.DOCUMENT |
827 RegExpFilter.typeMap.ELEMHIDE | 900 RegExpFilter.typeMap.ELEMHIDE |
828 RegExpFilter.typeMap.POPUP | 901 RegExpFilter.typeMap.POPUP |
829 RegExpFilter.typeMap.GENERICHIDE | 902 RegExpFilter.typeMap.GENERICHIDE |
830 RegExpFilter.typeMap.GENERICBLOCK); 903 RegExpFilter.typeMap.GENERICBLOCK);
831 904
832 /** 905 /**
833 * Class for blocking filters 906 * Class for blocking filters
834 * @param {string} text see Filter() 907 * @param {string} text see Filter()
835 * @param {string} regexpSource see RegExpFilter() 908 * @param {string} regexpSource see RegExpFilter()
836 * @param {number} contentType see RegExpFilter() 909 * @param {number} contentType see RegExpFilter()
837 * @param {boolean} matchCase see RegExpFilter() 910 * @param {boolean} matchCase see RegExpFilter()
838 * @param {string} domains see RegExpFilter() 911 * @param {string} domains see RegExpFilter()
839 * @param {boolean} thirdParty see RegExpFilter() 912 * @param {boolean} thirdParty see RegExpFilter()
840 * @param {string} sitekeys see RegExpFilter() 913 * @param {string} sitekeys see RegExpFilter()
841 * @param {boolean} collapse 914 * @param {boolean} collapse
842 * defines whether the filter should collapse blocked content, can be null 915 * defines whether the filter should collapse blocked content, can be null
916 * @param {string} [csp]
917 * Content Security Policy to inject when the filter matches
843 * @constructor 918 * @constructor
844 * @augments RegExpFilter 919 * @augments RegExpFilter
845 */ 920 */
846 function BlockingFilter(text, regexpSource, contentType, matchCase, domains, 921 function BlockingFilter(text, regexpSource, contentType, matchCase, domains,
847 thirdParty, sitekeys, collapse) 922 thirdParty, sitekeys, collapse, csp)
848 { 923 {
849 RegExpFilter.call(this, text, regexpSource, contentType, matchCase, domains, 924 RegExpFilter.call(this, text, regexpSource, contentType, matchCase, domains,
850 thirdParty, sitekeys); 925 thirdParty, sitekeys);
851 926
852 this.collapse = collapse; 927 this.collapse = collapse;
928 this.csp = csp;
853 } 929 }
854 exports.BlockingFilter = BlockingFilter; 930 exports.BlockingFilter = BlockingFilter;
855 931
856 BlockingFilter.prototype = extend(RegExpFilter, { 932 BlockingFilter.prototype = extend(RegExpFilter, {
857 type: "blocking", 933 type: "blocking",
858 934
859 /** 935 /**
860 * Defines whether the filter should collapse blocked content. 936 * Defines whether the filter should collapse blocked content.
861 * Can be null (use the global preference). 937 * Can be null (use the global preference).
862 * @type {boolean} 938 * @type {boolean}
863 */ 939 */
864 collapse: null 940 collapse: null,
941
942 /**
943 * Content Security Policy to inject for matching requests.
944 * @type {string}
945 */
946 csp: null
865 }); 947 });
866 948
867 /** 949 /**
868 * Class for whitelist filters 950 * Class for whitelist filters
869 * @param {string} text see Filter() 951 * @param {string} text see Filter()
870 * @param {string} regexpSource see RegExpFilter() 952 * @param {string} regexpSource see RegExpFilter()
871 * @param {number} contentType see RegExpFilter() 953 * @param {number} contentType see RegExpFilter()
872 * @param {boolean} matchCase see RegExpFilter() 954 * @param {boolean} matchCase see RegExpFilter()
873 * @param {string} domains see RegExpFilter() 955 * @param {string} domains see RegExpFilter()
874 * @param {boolean} thirdParty see RegExpFilter() 956 * @param {boolean} thirdParty see RegExpFilter()
(...skipping 144 matching lines...) Expand 10 before | Expand all | Expand 10 after
1019 */ 1101 */
1020 function ElemHideEmulationFilter(text, domains, selector) 1102 function ElemHideEmulationFilter(text, domains, selector)
1021 { 1103 {
1022 ElemHideBase.call(this, text, domains, selector); 1104 ElemHideBase.call(this, text, domains, selector);
1023 } 1105 }
1024 exports.ElemHideEmulationFilter = ElemHideEmulationFilter; 1106 exports.ElemHideEmulationFilter = ElemHideEmulationFilter;
1025 1107
1026 ElemHideEmulationFilter.prototype = extend(ElemHideBase, { 1108 ElemHideEmulationFilter.prototype = extend(ElemHideBase, {
1027 type: "elemhideemulation" 1109 type: "elemhideemulation"
1028 }); 1110 });
OLDNEW
« no previous file with comments | « no previous file | test/filterClasses.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld