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

Delta Between Two Patch Sets: lib/filterClasses.js

Issue 29680689: [$csp2 adblockpluscore] Issue 6329 - Add the CSP filter type (Closed)
Left Patch Set: Improve comments Created March 16, 2018, 11:10 a.m.
Right Patch Set: Addressed Sebastian's feedback Created March 21, 2018, 4:51 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/filterClasses.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 80 matching lines...) Expand 10 before | Expand all | Expand 10 after
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-]+(?:=[^,]+)?(?:,~?[\w-]+(?:=[^,]+)?)*)$/; 100 Filter.optionsRegExp = /\$(~?[\w-]+(?:=[^,]+)?(?:,~?[\w-]+(?:=[^,]+)?)*)$/;
101 /**
102 * Regular expression that matches an invalid Content Security Policy
103 * @type {RegExp}
104 */
105 Filter.invalidCSPRegExp = /(;|^) ?(base-uri|referrer|report-to|report-uri|upgrad e-insecure-requests)\b/i;
101 106
102 /** 107 /**
103 * Creates a filter of correct type from its text representation - does the 108 * Creates a filter of correct type from its text representation - does the
104 * basic parsing and calls the right constructor then. 109 * basic parsing and calls the right constructor then.
105 * 110 *
106 * @param {string} text as in Filter() 111 * @param {string} text as in Filter()
107 * @return {Filter} 112 * @return {Filter}
108 */ 113 */
109 Filter.fromText = function(text) 114 Filter.fromText = function(text)
110 { 115 {
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after
166 * the input parameter is null. 171 * the input parameter is null.
167 * @param {string} text 172 * @param {string} text
168 * @return {string} 173 * @return {string}
169 */ 174 */
170 Filter.normalize = function(text) 175 Filter.normalize = function(text)
171 { 176 {
172 if (!text) 177 if (!text)
173 return text; 178 return text;
174 179
175 // Remove line breaks, tabs etc 180 // Remove line breaks, tabs etc
176 text = text.replace(/[^\S ]+/g, ""); 181 text = text.replace(/[^\S ]+/g, "");
sergei 2018/03/16 16:49:25 This change (including the same changes below) see
kzar 2018/03/17 14:35:06 I would rather just land this all together (same b
sergei 2018/03/19 15:47:31 OK, however if it gets a bit longer then could you
177 182
178 // Don't remove spaces inside comments 183 // Don't remove spaces inside comments
179 if (/^ *!/.test(text)) 184 if (/^ *!/.test(text))
sergei 2018/03/16 16:49:24 This change seems unrelated too and to tell the tr
kzar 2018/03/17 14:35:07 It is the case since we've already replaced other
sergei 2018/03/19 15:47:32 Acknowledged.
180 return text.trim(); 185 return text.trim();
181 186
182 // Special treatment for element hiding filters, right side is allowed to 187 // Special treatment for element hiding filters, right side is allowed to
183 // contain spaces 188 // contain spaces
184 if (Filter.elemhideRegExp.test(text)) 189 if (Filter.elemhideRegExp.test(text))
185 { 190 {
186 let [, domain, separator, selector] = /^(.*?)(#@?#?)(.*)$/.exec(text); 191 let [, domain, separator, selector] = /^(.*?)(#@?#?)(.*)$/.exec(text);
187 return domain.replace(/ +/g, "") + separator + selector.trim(); 192 return domain.replace(/ +/g, "") + separator + selector.trim();
188 } 193 }
189 194
190 // For most regexp filters we strip all spaces, but $csp filter options 195 // For most regexp filters we strip all spaces, but $csp filter options
191 // are allowed to contain single (non trailing) spaces. 196 // are allowed to contain single (non trailing) spaces.
192 let strippedText = text.replace(/ +/g, ""); 197 let strippedText = text.replace(/ +/g, "");
193 if (!strippedText.includes("$") || !/\bcsp=/i.test(strippedText)) 198 if (!strippedText.includes("$") || !/\bcsp=/i.test(strippedText))
194 return strippedText; 199 return strippedText;
195 200
196 let optionsMatch = Filter.optionsRegExp.exec(strippedText); 201 let optionsMatch = Filter.optionsRegExp.exec(strippedText);
sergei 2018/03/16 16:49:25 I would like to carefully double check what is hap
sergei 2018/03/16 16:49:25 Since parsing time is very important could you ple
kzar 2018/03/17 14:35:07 None in EasyList, 24 in the uBlock filter list[1].
sergei 2018/03/19 15:47:31 Acknowledged. BTW, now it also additionally execut
kzar 2018/03/19 16:38:59 Yes I know.
197 if (!optionsMatch) 202 if (!optionsMatch)
198 return strippedText; 203 return strippedText;
199 204
200 // For $csp filters we must first separate out the options part of the 205 // For $csp filters we must first separate out the options part of the
201 // text, being careful to preserve its spaces. 206 // text, being careful to preserve its spaces.
202 let beforeOptions = strippedText.substring(0, optionsMatch.index); 207 let beforeOptions = strippedText.substring(0, optionsMatch.index);
203 let optionsText = text; 208 let strippedDollarIndex = -1;
204 for (let i = beforeOptions.split("$").length; i > 0; i--) 209 let dollarIndex = -1;
205 optionsText = optionsText.substr(optionsText.indexOf("$") + 1); 210 do
211 {
212 strippedDollarIndex = beforeOptions.indexOf("$", strippedDollarIndex + 1);
213 dollarIndex = text.indexOf("$", dollarIndex + 1);
214 }
215 while (strippedDollarIndex != -1);
216 let optionsText = text.substr(dollarIndex + 1);
206 217
207 // Then we can normalize spaces in the options part safely 218 // Then we can normalize spaces in the options part safely
208 let options = optionsText.split(","); 219 let options = optionsText.split(",");
209 for (let i = 0; i < options.length; i++) 220 for (let i = 0; i < options.length; i++)
210 { 221 {
211 let option = options[i]; 222 let option = options[i];
212 let cspMatch = /^ *c *s *p *=/i.exec(option); 223 let cspMatch = /^ *c *s *p *=/i.exec(option);
213 if (cspMatch) 224 if (cspMatch)
214 { 225 {
215 options[i] = cspMatch[0].replace(/ +/g, "") + 226 options[i] = cspMatch[0].replace(/ +/g, "") +
(...skipping 560 matching lines...) Expand 10 before | Expand all | Expand 10 after
776 option = option.substr(0, separatorIndex); 787 option = option.substr(0, separatorIndex);
777 } 788 }
778 option = option.replace(/-/, "_").toUpperCase(); 789 option = option.replace(/-/, "_").toUpperCase();
779 if (option in RegExpFilter.typeMap) 790 if (option in RegExpFilter.typeMap)
780 { 791 {
781 if (contentType == null) 792 if (contentType == null)
782 contentType = 0; 793 contentType = 0;
783 contentType |= RegExpFilter.typeMap[option]; 794 contentType |= RegExpFilter.typeMap[option];
784 795
785 if (option == "CSP" && typeof value != "undefined") 796 if (option == "CSP" && typeof value != "undefined")
786 { 797 csp = value;
787 if (csp)
788 csp.push(value);
789 else
790 csp = [value];
791 }
792 } 798 }
793 else if (option[0] == "~" && option.substr(1) in RegExpFilter.typeMap) 799 else if (option[0] == "~" && option.substr(1) in RegExpFilter.typeMap)
794 { 800 {
795 if (contentType == null) 801 if (contentType == null)
796 ({contentType} = RegExpFilter.prototype); 802 ({contentType} = RegExpFilter.prototype);
797 contentType &= ~RegExpFilter.typeMap[option.substr(1)]; 803 contentType &= ~RegExpFilter.typeMap[option.substr(1)];
798 } 804 }
799 else if (option == "MATCH_CASE") 805 else if (option == "MATCH_CASE")
800 matchCase = true; 806 matchCase = true;
801 else if (option == "~MATCH_CASE") 807 else if (option == "~MATCH_CASE")
802 matchCase = false; 808 matchCase = false;
803 else if (option == "DOMAIN" && typeof value != "undefined") 809 else if (option == "DOMAIN" && typeof value != "undefined")
804 domains = value.toUpperCase(); 810 domains = value.toUpperCase();
sergei 2018/03/16 16:49:24 Is this change related to the issue? When I was pr
kzar 2018/03/17 14:35:06 Yes.
805 else if (option == "THIRD_PARTY") 811 else if (option == "THIRD_PARTY")
806 thirdParty = true; 812 thirdParty = true;
807 else if (option == "~THIRD_PARTY") 813 else if (option == "~THIRD_PARTY")
808 thirdParty = false; 814 thirdParty = false;
809 else if (option == "COLLAPSE") 815 else if (option == "COLLAPSE")
810 collapse = true; 816 collapse = true;
811 else if (option == "~COLLAPSE") 817 else if (option == "~COLLAPSE")
812 collapse = false; 818 collapse = false;
813 else if (option == "SITEKEY" && typeof value != "undefined") 819 else if (option == "SITEKEY" && typeof value != "undefined")
814 sitekeys = value.toUpperCase(); 820 sitekeys = value.toUpperCase();
815 else 821 else
816 return new InvalidFilter(origText, "filter_unknown_option"); 822 return new InvalidFilter(origText, "filter_unknown_option");
817 } 823 }
818 } 824 }
819 825
820 try 826 try
821 { 827 {
822 if (blocking) 828 if (blocking)
823 { 829 {
824 if (csp) 830 if (csp && Filter.invalidCSPRegExp.test(csp))
825 { 831 return new InvalidFilter(origText, "filter_invalid_csp");
826 csp = csp.join("; ").toLowerCase();
827
828 // Prevent injecting directives which could be a privacy concern
829 if (/(;|^) ?(report-to|report-uri|referrer)\b/.test(csp))
830 return new InvalidFilter(origText, "filter_invalid_csp");
831 }
832 832
833 return new BlockingFilter(origText, text, contentType, matchCase, domains, 833 return new BlockingFilter(origText, text, contentType, matchCase, domains,
834 thirdParty, sitekeys, collapse, csp); 834 thirdParty, sitekeys, collapse, csp);
835 } 835 }
836 return new WhitelistFilter(origText, text, contentType, matchCase, domains, 836 return new WhitelistFilter(origText, text, contentType, matchCase, domains,
837 thirdParty, sitekeys); 837 thirdParty, sitekeys);
838 } 838 }
839 catch (e) 839 catch (e)
840 { 840 {
841 return new InvalidFilter(origText, "filter_invalid_regexp"); 841 return new InvalidFilter(origText, "filter_invalid_regexp");
(...skipping 238 matching lines...) Expand 10 before | Expand all | Expand 10 after
1080 */ 1080 */
1081 function ElemHideEmulationFilter(text, domains, selector) 1081 function ElemHideEmulationFilter(text, domains, selector)
1082 { 1082 {
1083 ElemHideBase.call(this, text, domains, selector); 1083 ElemHideBase.call(this, text, domains, selector);
1084 } 1084 }
1085 exports.ElemHideEmulationFilter = ElemHideEmulationFilter; 1085 exports.ElemHideEmulationFilter = ElemHideEmulationFilter;
1086 1086
1087 ElemHideEmulationFilter.prototype = extend(ElemHideBase, { 1087 ElemHideEmulationFilter.prototype = extend(ElemHideBase, {
1088 type: "elemhideemulation" 1088 type: "elemhideemulation"
1089 }); 1089 });
LEFTRIGHT

Powered by Google App Engine
This is Rietveld