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: Added more tests, blacklisted directives and added a tiny optimisation Created March 17, 2018, 2:17 p.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 /**
103 * Forbidden Content Security Policy directives
104 * @type {Set<string>}
105 */
106 Filter.cspDirectiveBlacklist = new Set([
107 "base-uri", "referrer", "report-to", "report-uri", "base-uri",
108 "upgrade-insecure-requests"
109 ]);
110 /** 101 /**
111 * Regular expression that matches an invalid Content Security Policy 102 * Regular expression that matches an invalid Content Security Policy
112 * @type {RegExp} 103 * @type {RegExp}
113 */ 104 */
114 Filter.invalidCSPRegExp = new RegExp( 105 Filter.invalidCSPRegExp = /(;|^) ?(base-uri|referrer|report-to|report-uri|upgrad e-insecure-requests)\b/i;
115 "(;|^) ?(" + Array.from(Filter.cspDirectiveBlacklist).join("|") + ")\\b"
116 );
117 106
118 /** 107 /**
119 * 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
120 * basic parsing and calls the right constructor then. 109 * basic parsing and calls the right constructor then.
121 * 110 *
122 * @param {string} text as in Filter() 111 * @param {string} text as in Filter()
123 * @return {Filter} 112 * @return {Filter}
124 */ 113 */
125 Filter.fromText = function(text) 114 Filter.fromText = function(text)
126 { 115 {
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after
209 if (!strippedText.includes("$") || !/\bcsp=/i.test(strippedText)) 198 if (!strippedText.includes("$") || !/\bcsp=/i.test(strippedText))
210 return strippedText; 199 return strippedText;
211 200
212 let optionsMatch = Filter.optionsRegExp.exec(strippedText); 201 let optionsMatch = Filter.optionsRegExp.exec(strippedText);
213 if (!optionsMatch) 202 if (!optionsMatch)
214 return strippedText; 203 return strippedText;
215 204
216 // 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
217 // text, being careful to preserve its spaces. 206 // text, being careful to preserve its spaces.
218 let beforeOptions = strippedText.substring(0, optionsMatch.index); 207 let beforeOptions = strippedText.substring(0, optionsMatch.index);
219 let optionsText = text; 208 let strippedDollarIndex = -1;
220 for (let i = -2; i != -1; i = beforeOptions.indexOf("$", i + 1)) 209 let dollarIndex = -1;
kzar 2018/03/17 14:35:08 Here you go Manish, I still don't see the point pe
Manish Jethani 2018/03/19 18:03:38 Acknowledged.
221 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);
222 217
223 // Then we can normalize spaces in the options part safely 218 // Then we can normalize spaces in the options part safely
224 let options = optionsText.split(","); 219 let options = optionsText.split(",");
225 for (let i = 0; i < options.length; i++) 220 for (let i = 0; i < options.length; i++)
226 { 221 {
227 let option = options[i]; 222 let option = options[i];
228 let cspMatch = /^ *c *s *p *=/i.exec(option); 223 let cspMatch = /^ *c *s *p *=/i.exec(option);
229 if (cspMatch) 224 if (cspMatch)
230 { 225 {
231 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
792 option = option.substr(0, separatorIndex); 787 option = option.substr(0, separatorIndex);
793 } 788 }
794 option = option.replace(/-/, "_").toUpperCase(); 789 option = option.replace(/-/, "_").toUpperCase();
795 if (option in RegExpFilter.typeMap) 790 if (option in RegExpFilter.typeMap)
796 { 791 {
797 if (contentType == null) 792 if (contentType == null)
798 contentType = 0; 793 contentType = 0;
799 contentType |= RegExpFilter.typeMap[option]; 794 contentType |= RegExpFilter.typeMap[option];
800 795
801 if (option == "CSP" && typeof value != "undefined") 796 if (option == "CSP" && typeof value != "undefined")
802 { 797 csp = value;
803 if (csp)
804 csp.push(value);
805 else
806 csp = [value];
807 }
808 } 798 }
809 else if (option[0] == "~" && option.substr(1) in RegExpFilter.typeMap) 799 else if (option[0] == "~" && option.substr(1) in RegExpFilter.typeMap)
810 { 800 {
811 if (contentType == null) 801 if (contentType == null)
812 ({contentType} = RegExpFilter.prototype); 802 ({contentType} = RegExpFilter.prototype);
813 contentType &= ~RegExpFilter.typeMap[option.substr(1)]; 803 contentType &= ~RegExpFilter.typeMap[option.substr(1)];
814 } 804 }
815 else if (option == "MATCH_CASE") 805 else if (option == "MATCH_CASE")
816 matchCase = true; 806 matchCase = true;
817 else if (option == "~MATCH_CASE") 807 else if (option == "~MATCH_CASE")
(...skipping 12 matching lines...) Expand all
830 sitekeys = value.toUpperCase(); 820 sitekeys = value.toUpperCase();
831 else 821 else
832 return new InvalidFilter(origText, "filter_unknown_option"); 822 return new InvalidFilter(origText, "filter_unknown_option");
833 } 823 }
834 } 824 }
835 825
836 try 826 try
837 { 827 {
838 if (blocking) 828 if (blocking)
839 { 829 {
840 if (csp) 830 if (csp && Filter.invalidCSPRegExp.test(csp))
841 { 831 return new InvalidFilter(origText, "filter_invalid_csp");
842 csp = csp.join("; ").toLowerCase();
843
844 if (Filter.invalidCSPRegExp.test(csp))
845 return new InvalidFilter(origText, "filter_invalid_csp");
846 }
847 832
848 return new BlockingFilter(origText, text, contentType, matchCase, domains, 833 return new BlockingFilter(origText, text, contentType, matchCase, domains,
849 thirdParty, sitekeys, collapse, csp); 834 thirdParty, sitekeys, collapse, csp);
850 } 835 }
851 return new WhitelistFilter(origText, text, contentType, matchCase, domains, 836 return new WhitelistFilter(origText, text, contentType, matchCase, domains,
852 thirdParty, sitekeys); 837 thirdParty, sitekeys);
853 } 838 }
854 catch (e) 839 catch (e)
855 { 840 {
856 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
1095 */ 1080 */
1096 function ElemHideEmulationFilter(text, domains, selector) 1081 function ElemHideEmulationFilter(text, domains, selector)
1097 { 1082 {
1098 ElemHideBase.call(this, text, domains, selector); 1083 ElemHideBase.call(this, text, domains, selector);
1099 } 1084 }
1100 exports.ElemHideEmulationFilter = ElemHideEmulationFilter; 1085 exports.ElemHideEmulationFilter = ElemHideEmulationFilter;
1101 1086
1102 ElemHideEmulationFilter.prototype = extend(ElemHideBase, { 1087 ElemHideEmulationFilter.prototype = extend(ElemHideBase, {
1103 type: "elemhideemulation" 1088 type: "elemhideemulation"
1104 }); 1089 });
LEFTRIGHT

Powered by Google App Engine
This is Rietveld