Left: | ||
Right: |
LEFT | RIGHT |
---|---|
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 Loading... | |
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 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
165 * Removes unnecessary whitespaces from filter text, will only return null if | 170 * Removes unnecessary whitespaces from filter text, will only return null if |
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 and such | 180 // Remove line breaks, tabs etc |
176 text = text.replace(/[^\S ]/g, ""); | 181 text = text.replace(/[^\S ]+/g, ""); |
177 | 182 |
178 if (/^\s*!/.test(text)) | 183 // Don't remove spaces inside comments |
179 { | 184 if (/^ *!/.test(text)) |
180 // Don't remove spaces inside comments | |
181 return text.trim(); | 185 return text.trim(); |
182 } | 186 |
183 else if (Filter.elemhideRegExp.test(text)) | 187 // Special treatment for element hiding filters, right side is allowed to |
184 { | 188 // contain spaces |
185 // Special treatment for element hiding filters, right side is allowed to | 189 if (Filter.elemhideRegExp.test(text)) |
186 // contain spaces | 190 { |
187 let [, domain, separator, selector] = /^(.*?)(#@?#?)(.*)$/.exec(text); | 191 let [, domain, separator, selector] = /^(.*?)(#@?#?)(.*)$/.exec(text); |
188 return domain.replace(/\s/g, "") + separator + selector.trim(); | 192 return domain.replace(/ +/g, "") + separator + selector.trim(); |
189 } | 193 } |
190 return text.trim().replace(/\s+/g, " "); | 194 |
195 // For most regexp filters we strip all spaces, but $csp filter options | |
196 // are allowed to contain single (non trailing) spaces. | |
197 let strippedText = text.replace(/ +/g, ""); | |
198 if (!strippedText.includes("$") || !/\bcsp=/i.test(strippedText)) | |
199 return strippedText; | |
200 | |
201 let optionsMatch = Filter.optionsRegExp.exec(strippedText); | |
202 if (!optionsMatch) | |
203 return strippedText; | |
204 | |
205 // For $csp filters we must first separate out the options part of the | |
206 // text, being careful to preserve its spaces. | |
207 let beforeOptions = strippedText.substring(0, optionsMatch.index); | |
208 let strippedDollarIndex = -1; | |
209 let dollarIndex = -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); | |
217 | |
218 // Then we can normalize spaces in the options part safely | |
219 let options = optionsText.split(","); | |
220 for (let i = 0; i < options.length; i++) | |
221 { | |
222 let option = options[i]; | |
223 let cspMatch = /^ *c *s *p *=/i.exec(option); | |
224 if (cspMatch) | |
225 { | |
226 options[i] = cspMatch[0].replace(/ +/g, "") + | |
227 option.substr(cspMatch[0].length).trim().replace(/ +/g, " "); | |
228 } | |
229 else | |
230 options[i] = option.replace(/ +/g, ""); | |
231 } | |
232 | |
233 return beforeOptions + "$" + options.join(); | |
191 }; | 234 }; |
192 | 235 |
193 /** | 236 /** |
194 * @see filterToRegExp | 237 * @see filterToRegExp |
195 */ | 238 */ |
196 Filter.toRegExp = filterToRegExp; | 239 Filter.toRegExp = filterToRegExp; |
197 | 240 |
198 /** | 241 /** |
199 * Class for invalid filters | 242 * Class for invalid filters |
200 * @param {string} text see Filter() | 243 * @param {string} text see Filter() |
(...skipping 533 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
734 { | 777 { |
735 options = match[1].split(","); | 778 options = match[1].split(","); |
736 text = match.input.substr(0, match.index); | 779 text = match.input.substr(0, match.index); |
737 for (let option of options) | 780 for (let option of options) |
738 { | 781 { |
739 let value = null; | 782 let value = null; |
740 let separatorIndex = option.indexOf("="); | 783 let separatorIndex = option.indexOf("="); |
741 if (separatorIndex >= 0) | 784 if (separatorIndex >= 0) |
742 { | 785 { |
743 value = option.substr(separatorIndex + 1); | 786 value = option.substr(separatorIndex + 1); |
744 option = option.substr(0, separatorIndex).toUpperCase(); | 787 option = option.substr(0, separatorIndex); |
745 | |
746 if (option == "CSP") | |
Manish Jethani
2018/03/07 00:22:13
I've left a comment on the other patch, I think we
kzar
2018/03/12 13:35:58
Done.
| |
747 value = value.trim(); | |
748 else | |
749 value = value.replace(/\s/g, ""); | |
750 } | 788 } |
751 else | 789 option = option.replace(/-/, "_").toUpperCase(); |
752 option = option.toUpperCase(); | |
753 | |
754 option = option.replace(/-/, "_"); | |
755 | |
756 if (option in RegExpFilter.typeMap) | 790 if (option in RegExpFilter.typeMap) |
757 { | 791 { |
758 if (contentType == null) | 792 if (contentType == null) |
759 contentType = 0; | 793 contentType = 0; |
760 contentType |= RegExpFilter.typeMap[option]; | 794 contentType |= RegExpFilter.typeMap[option]; |
761 | 795 |
762 if (option == "CSP" && typeof value != "undefined") | 796 if (option == "CSP" && typeof value != "undefined") |
763 { | 797 csp = value; |
764 if (csp === null) | |
Manish Jethani
2018/03/07 00:22:13
As far as I can tell the strict equality is unnece
kzar
2018/03/12 13:35:58
Done.
| |
765 csp = [value]; | |
766 else | |
767 csp.push(value); | |
768 } | |
769 } | 798 } |
770 else if (option[0] == "~" && option.substr(1) in RegExpFilter.typeMap) | 799 else if (option[0] == "~" && option.substr(1) in RegExpFilter.typeMap) |
771 { | 800 { |
772 if (contentType == null) | 801 if (contentType == null) |
773 ({contentType} = RegExpFilter.prototype); | 802 ({contentType} = RegExpFilter.prototype); |
774 contentType &= ~RegExpFilter.typeMap[option.substr(1)]; | 803 contentType &= ~RegExpFilter.typeMap[option.substr(1)]; |
775 } | 804 } |
776 else if (option == "MATCH_CASE") | 805 else if (option == "MATCH_CASE") |
777 matchCase = true; | 806 matchCase = true; |
778 else if (option == "~MATCH_CASE") | 807 else if (option == "~MATCH_CASE") |
779 matchCase = false; | 808 matchCase = false; |
780 else if (option == "DOMAIN" && typeof value != "undefined") | 809 else if (option == "DOMAIN" && typeof value != "undefined") |
781 domains = value.toUpperCase(); | 810 domains = value.toUpperCase(); |
782 else if (option == "THIRD_PARTY") | 811 else if (option == "THIRD_PARTY") |
783 thirdParty = true; | 812 thirdParty = true; |
784 else if (option == "~THIRD_PARTY") | 813 else if (option == "~THIRD_PARTY") |
785 thirdParty = false; | 814 thirdParty = false; |
786 else if (option == "COLLAPSE") | 815 else if (option == "COLLAPSE") |
787 collapse = true; | 816 collapse = true; |
788 else if (option == "~COLLAPSE") | 817 else if (option == "~COLLAPSE") |
789 collapse = false; | 818 collapse = false; |
790 else if (option == "SITEKEY" && typeof value != "undefined") | 819 else if (option == "SITEKEY" && typeof value != "undefined") |
791 sitekeys = value.toUpperCase(); | 820 sitekeys = value.toUpperCase(); |
792 else | 821 else |
793 return new InvalidFilter(origText, "filter_unknown_option"); | 822 return new InvalidFilter(origText, "filter_unknown_option"); |
794 } | 823 } |
795 } | 824 } |
796 text = text.replace(/\s/g, ""); | |
797 | 825 |
798 try | 826 try |
799 { | 827 { |
800 if (blocking) | 828 if (blocking) |
801 { | 829 { |
802 if (csp !== null) | 830 if (csp && Filter.invalidCSPRegExp.test(csp)) |
803 { | 831 return new InvalidFilter(origText, "filter_invalid_csp"); |
804 csp = csp.join("; ").toLowerCase(); | |
805 | |
806 // Prevent filters from injecting report-uri or report-to directives | |
807 // since they are a privacy concern. Regexp based upon reBadCSP[1]. | |
808 // [1] - https://github.com/gorhill/uBlock/blob/67e06f53b4d73df6179f6d32 0553a55da4ead40e/src/js/static-net-filtering.js#L1362 | |
809 if (/(;|^)\s*report-(to|uri)\b/.test(csp)) | |
810 return new InvalidFilter(origText, "filter_invalid_csp"); | |
811 } | |
812 | 832 |
813 return new BlockingFilter(origText, text, contentType, matchCase, domains, | 833 return new BlockingFilter(origText, text, contentType, matchCase, domains, |
814 thirdParty, sitekeys, collapse, csp); | 834 thirdParty, sitekeys, collapse, csp); |
815 } | 835 } |
816 return new WhitelistFilter(origText, text, contentType, matchCase, domains, | 836 return new WhitelistFilter(origText, text, contentType, matchCase, domains, |
817 thirdParty, sitekeys); | 837 thirdParty, sitekeys); |
818 } | 838 } |
819 catch (e) | 839 catch (e) |
820 { | 840 { |
821 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 Loading... | |
1060 */ | 1080 */ |
1061 function ElemHideEmulationFilter(text, domains, selector) | 1081 function ElemHideEmulationFilter(text, domains, selector) |
1062 { | 1082 { |
1063 ElemHideBase.call(this, text, domains, selector); | 1083 ElemHideBase.call(this, text, domains, selector); |
1064 } | 1084 } |
1065 exports.ElemHideEmulationFilter = ElemHideEmulationFilter; | 1085 exports.ElemHideEmulationFilter = ElemHideEmulationFilter; |
1066 | 1086 |
1067 ElemHideEmulationFilter.prototype = extend(ElemHideBase, { | 1087 ElemHideEmulationFilter.prototype = extend(ElemHideBase, { |
1068 type: "elemhideemulation" | 1088 type: "elemhideemulation" |
1069 }); | 1089 }); |
LEFT | RIGHT |