OLD | NEW |
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 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
77 } | 77 } |
78 }; | 78 }; |
79 | 79 |
80 /** | 80 /** |
81 * Cache for known filters, maps string representation to filter objects. | 81 * Cache for known filters, maps string representation to filter objects. |
82 * @type {Map.<string,Filter>} | 82 * @type {Map.<string,Filter>} |
83 */ | 83 */ |
84 Filter.knownFilters = new Map(); | 84 Filter.knownFilters = new Map(); |
85 | 85 |
86 /** | 86 /** |
87 * Regular expression that element hiding filters should match | 87 * Regular expression that content filters should match |
88 * @type {RegExp} | 88 * @type {RegExp} |
89 */ | 89 */ |
90 Filter.elemhideRegExp = /^([^/*|@"!]*?)#([@?])?#(.+)$/; | 90 Filter.contentRegExp = /^([^/*|@"!]*?)#([@?$])?#(.+)$/; |
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 /** | 101 /** |
102 * Regular expression that matches an invalid Content Security Policy | 102 * Regular expression that matches an invalid Content Security Policy |
103 * @type {RegExp} | 103 * @type {RegExp} |
104 */ | 104 */ |
105 Filter.invalidCSPRegExp = /(;|^) ?(base-uri|referrer|report-to|report-uri|upgrad
e-insecure-requests)\b/i; | 105 Filter.invalidCSPRegExp = /(;|^) ?(base-uri|referrer|report-to|report-uri|upgrad
e-insecure-requests)\b/i; |
106 | 106 |
107 /** | 107 /** |
108 * 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 |
109 * basic parsing and calls the right constructor then. | 109 * basic parsing and calls the right constructor then. |
110 * | 110 * |
111 * @param {string} text as in Filter() | 111 * @param {string} text as in Filter() |
112 * @return {Filter} | 112 * @return {Filter} |
113 */ | 113 */ |
114 Filter.fromText = function(text) | 114 Filter.fromText = function(text) |
115 { | 115 { |
116 let filter = Filter.knownFilters.get(text); | 116 let filter = Filter.knownFilters.get(text); |
117 if (filter) | 117 if (filter) |
118 return filter; | 118 return filter; |
119 | 119 |
120 let match = (text.includes("#") ? Filter.elemhideRegExp.exec(text) : null); | 120 let match = text.includes("#") ? Filter.contentRegExp.exec(text) : null; |
121 if (match) | 121 if (match) |
122 { | 122 { |
123 let propsMatch; | 123 let propsMatch; |
124 if (!match[2] && | 124 if (!match[2] && |
125 (propsMatch = /\[-abp-properties=(["'])([^"']+)\1\]/.exec(match[3]))) | 125 (propsMatch = /\[-abp-properties=(["'])([^"']+)\1\]/.exec(match[3]))) |
126 { | 126 { |
127 // This is legacy CSS properties syntax, convert to current syntax | 127 // This is legacy CSS properties syntax, convert to current syntax |
128 let prefix = match[3].substr(0, propsMatch.index); | 128 let prefix = match[3].substr(0, propsMatch.index); |
129 let expression = propsMatch[2]; | 129 let expression = propsMatch[2]; |
130 let suffix = match[3].substr(propsMatch.index + propsMatch[0].length); | 130 let suffix = match[3].substr(propsMatch.index + propsMatch[0].length); |
131 return Filter.fromText(`${match[1]}#?#` + | 131 return Filter.fromText(`${match[1]}#?#` + |
132 `${prefix}:-abp-properties(${expression})${suffix}`); | 132 `${prefix}:-abp-properties(${expression})${suffix}`); |
133 } | 133 } |
134 | 134 |
135 filter = ElemHideBase.fromText( | 135 filter = ContentFilter.fromText(text, match[1], match[2], match[3]); |
136 text, match[1], match[2], match[3] | |
137 ); | |
138 } | 136 } |
139 else if (text[0] == "!") | 137 else if (text[0] == "!") |
140 filter = new CommentFilter(text); | 138 filter = new CommentFilter(text); |
141 else | 139 else |
142 filter = RegExpFilter.fromText(text); | 140 filter = RegExpFilter.fromText(text); |
143 | 141 |
144 Filter.knownFilters.set(filter.text, filter); | 142 Filter.knownFilters.set(filter.text, filter); |
145 return filter; | 143 return filter; |
146 }; | 144 }; |
147 | 145 |
(...skipping 29 matching lines...) Expand all Loading... |
177 if (!text) | 175 if (!text) |
178 return text; | 176 return text; |
179 | 177 |
180 // Remove line breaks, tabs etc | 178 // Remove line breaks, tabs etc |
181 text = text.replace(/[^\S ]+/g, ""); | 179 text = text.replace(/[^\S ]+/g, ""); |
182 | 180 |
183 // Don't remove spaces inside comments | 181 // Don't remove spaces inside comments |
184 if (/^ *!/.test(text)) | 182 if (/^ *!/.test(text)) |
185 return text.trim(); | 183 return text.trim(); |
186 | 184 |
187 // Special treatment for element hiding filters, right side is allowed to | 185 // Special treatment for content filters, right side is allowed to contain |
188 // contain spaces | 186 // spaces |
189 if (Filter.elemhideRegExp.test(text)) | 187 if (Filter.contentRegExp.test(text)) |
190 { | 188 { |
191 let [, domains, separator, selector] = /^(.*?)(#[@?]?#?)(.*)$/.exec(text); | 189 let [, domains, separator, script] = /^(.*?)(#[@?$]?#?)(.*)$/.exec(text); |
192 return domains.replace(/ +/g, "") + separator + selector.trim(); | 190 return domains.replace(/ +/g, "") + separator + script.trim(); |
193 } | 191 } |
194 | 192 |
195 // For most regexp filters we strip all spaces, but $csp filter options | 193 // For most regexp filters we strip all spaces, but $csp filter options |
196 // are allowed to contain single (non trailing) spaces. | 194 // are allowed to contain single (non trailing) spaces. |
197 let strippedText = text.replace(/ +/g, ""); | 195 let strippedText = text.replace(/ +/g, ""); |
198 if (!strippedText.includes("$") || !/\bcsp=/i.test(strippedText)) | 196 if (!strippedText.includes("$") || !/\bcsp=/i.test(strippedText)) |
199 return strippedText; | 197 return strippedText; |
200 | 198 |
201 let optionsMatch = Filter.optionsRegExp.exec(strippedText); | 199 let optionsMatch = Filter.optionsRegExp.exec(strippedText); |
202 if (!optionsMatch) | 200 if (!optionsMatch) |
(...skipping 777 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
980 RegExpFilter.call(this, text, regexpSource, contentType, matchCase, domains, | 978 RegExpFilter.call(this, text, regexpSource, contentType, matchCase, domains, |
981 thirdParty, sitekeys); | 979 thirdParty, sitekeys); |
982 } | 980 } |
983 exports.WhitelistFilter = WhitelistFilter; | 981 exports.WhitelistFilter = WhitelistFilter; |
984 | 982 |
985 WhitelistFilter.prototype = extend(RegExpFilter, { | 983 WhitelistFilter.prototype = extend(RegExpFilter, { |
986 type: "whitelist" | 984 type: "whitelist" |
987 }); | 985 }); |
988 | 986 |
989 /** | 987 /** |
990 * Base class for element hiding filters | 988 * Base class for content filters |
991 * @param {string} text see {@link Filter Filter()} | 989 * @param {string} text see {@link Filter Filter()} |
992 * @param {string} [domains] Host names or domains the filter should be | 990 * @param {string} [domains] Host names or domains the filter should be |
993 * restricted to | 991 * restricted to |
994 * @param {string} selector CSS selector for the HTML elements that should be | 992 * @param {string} script Script that should be executed |
995 * hidden | |
996 * @constructor | 993 * @constructor |
997 * @augments ActiveFilter | 994 * @augments ActiveFilter |
998 */ | 995 */ |
999 function ElemHideBase(text, domains, selector) | 996 function ContentFilter(text, domains, script) |
1000 { | 997 { |
1001 ActiveFilter.call(this, text, domains || null); | 998 ActiveFilter.call(this, text, domains || null); |
1002 | 999 |
1003 // Braces are being escaped to prevent CSS rule injection. | 1000 this.script = script; |
1004 this.selector = selector.replace("{", "\\7B ").replace("}", "\\7D "); | |
1005 } | 1001 } |
1006 exports.ElemHideBase = ElemHideBase; | 1002 exports.ContentFilter = ContentFilter; |
1007 | 1003 |
1008 ElemHideBase.prototype = extend(ActiveFilter, { | 1004 ContentFilter.prototype = extend(ActiveFilter, { |
1009 /** | 1005 /** |
1010 * @see ActiveFilter.domainSeparator | 1006 * @see ActiveFilter.domainSeparator |
1011 */ | 1007 */ |
1012 domainSeparator: ",", | 1008 domainSeparator: ",", |
1013 | 1009 |
1014 /** | 1010 /** |
1015 * CSS selector for the HTML elements that should be hidden | 1011 * Script that should be executed |
1016 * @type {string} | 1012 * @type {string} |
1017 */ | 1013 */ |
1018 selector: null | 1014 script: null |
1019 }); | 1015 }); |
1020 | 1016 |
1021 /** | 1017 /** |
1022 * Creates an element hiding filter from a pre-parsed text representation | 1018 * Creates a content filter from a pre-parsed text representation |
1023 * | 1019 * |
1024 * @param {string} text same as in Filter() | 1020 * @param {string} text same as in Filter() |
1025 * @param {string} [domains] | 1021 * @param {string} [domains] |
1026 * domains part of the text representation | 1022 * domains part of the text representation |
1027 * @param {string} [type] | 1023 * @param {string} [type] |
1028 * rule type, either empty or @ (exception) or ? (emulation rule) | 1024 * rule type, either empty or @ (exception) or ? (emulation rule) or |
1029 * @param {string} selector raw CSS selector | 1025 * $ (snippet) |
| 1026 * @param {string} script |
| 1027 * script part of the text representation, either a CSS selector or a snippet |
| 1028 * script |
1030 * @return {ElemHideFilter|ElemHideException| | 1029 * @return {ElemHideFilter|ElemHideException| |
1031 * ElemHideEmulationFilter|InvalidFilter} | 1030 * ElemHideEmulationFilter|SnippetFilter|InvalidFilter} |
1032 */ | 1031 */ |
1033 ElemHideBase.fromText = function(text, domains, type, selector) | 1032 ContentFilter.fromText = function(text, domains, type, script) |
1034 { | 1033 { |
1035 // We don't allow ElemHide filters which have any empty domains. | 1034 // We don't allow content filters which have any empty domains. |
1036 // Note: The ElemHide.prototype.domainSeparator is duplicated here, if that | 1035 // Note: The ContentFilter.prototype.domainSeparator is duplicated here, if |
1037 // changes this must be changed too. | 1036 // that changes this must be changed too. |
1038 if (domains && /(^|,)~?(,|$)/.test(domains)) | 1037 if (domains && /(^|,)~?(,|$)/.test(domains)) |
1039 return new InvalidFilter(text, "filter_invalid_domain"); | 1038 return new InvalidFilter(text, "filter_invalid_domain"); |
1040 | 1039 |
1041 if (type == "@") | 1040 if (type == "@") |
1042 return new ElemHideException(text, domains, selector); | 1041 return new ElemHideException(text, domains, script); |
| 1042 |
| 1043 if (type == "$") |
| 1044 return new SnippetFilter(text, domains, script); |
1043 | 1045 |
1044 if (type == "?") | 1046 if (type == "?") |
1045 { | 1047 { |
1046 // Element hiding emulation filters are inefficient so we need to make sure | 1048 // Element hiding emulation filters are inefficient so we need to make sure |
1047 // that they're only applied if they specify active domains | 1049 // that they're only applied if they specify active domains |
1048 if (!/,[^~][^,.]*\.[^,]/.test("," + domains)) | 1050 if (!/,[^~][^,.]*\.[^,]/.test("," + domains)) |
1049 return new InvalidFilter(text, "filter_elemhideemulation_nodomain"); | 1051 return new InvalidFilter(text, "filter_elemhideemulation_nodomain"); |
1050 | 1052 |
1051 return new ElemHideEmulationFilter(text, domains, selector); | 1053 return new ElemHideEmulationFilter(text, domains, script); |
1052 } | 1054 } |
1053 | 1055 |
1054 return new ElemHideFilter(text, domains, selector); | 1056 return new ElemHideFilter(text, domains, script); |
1055 }; | 1057 }; |
1056 | 1058 |
| 1059 /* |
| 1060 * Base class for element hiding filters |
| 1061 * @param {string} text see {@link Filter Filter()} |
| 1062 * @param {string} [domains] see {@link ContentFilter ContentFilter()} |
| 1063 * @param {string} selector CSS selector for the HTML elements that should be |
| 1064 * hidden |
| 1065 * @constructor |
| 1066 * @augments ContentFilter |
| 1067 */ |
| 1068 function ElemHideBase(text, domains, selector) |
| 1069 { |
| 1070 ContentFilter.call(this, text, domains, selector); |
| 1071 } |
| 1072 exports.ElemHideBase = ElemHideBase; |
| 1073 |
| 1074 ElemHideBase.prototype = extend(ContentFilter, { |
| 1075 /** |
| 1076 * CSS selector for the HTML elements that should be hidden |
| 1077 * @type {string} |
| 1078 */ |
| 1079 get selector() |
| 1080 { |
| 1081 // Braces are being escaped to prevent CSS rule injection. |
| 1082 return this.script.replace("{", "\\7B ").replace("}", "\\7D "); |
| 1083 } |
| 1084 }); |
| 1085 |
1057 /** | 1086 /** |
1058 * Class for element hiding filters | 1087 * Class for element hiding filters |
1059 * @param {string} text see {@link Filter Filter()} | 1088 * @param {string} text see {@link Filter Filter()} |
1060 * @param {string} [domains] see {@link ElemHideBase ElemHideBase()} | 1089 * @param {string} [domains] see {@link ElemHideBase ElemHideBase()} |
1061 * @param {string} selector see {@link ElemHideBase ElemHideBase()} | 1090 * @param {string} selector see {@link ElemHideBase ElemHideBase()} |
1062 * @constructor | 1091 * @constructor |
1063 * @augments ElemHideBase | 1092 * @augments ElemHideBase |
1064 */ | 1093 */ |
1065 function ElemHideFilter(text, domains, selector) | 1094 function ElemHideFilter(text, domains, selector) |
1066 { | 1095 { |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1100 */ | 1129 */ |
1101 function ElemHideEmulationFilter(text, domains, selector) | 1130 function ElemHideEmulationFilter(text, domains, selector) |
1102 { | 1131 { |
1103 ElemHideBase.call(this, text, domains, selector); | 1132 ElemHideBase.call(this, text, domains, selector); |
1104 } | 1133 } |
1105 exports.ElemHideEmulationFilter = ElemHideEmulationFilter; | 1134 exports.ElemHideEmulationFilter = ElemHideEmulationFilter; |
1106 | 1135 |
1107 ElemHideEmulationFilter.prototype = extend(ElemHideBase, { | 1136 ElemHideEmulationFilter.prototype = extend(ElemHideBase, { |
1108 type: "elemhideemulation" | 1137 type: "elemhideemulation" |
1109 }); | 1138 }); |
| 1139 |
| 1140 /** |
| 1141 * Class for snippet filters |
| 1142 * @param {string} text see Filter() |
| 1143 * @param {string} [domains] see ContentFilter() |
| 1144 * @param {string} script Script that should be executed |
| 1145 * @constructor |
| 1146 * @augments ContentFilter |
| 1147 */ |
| 1148 function SnippetFilter(text, domains, script) |
| 1149 { |
| 1150 ContentFilter.call(this, text, domains, script); |
| 1151 } |
| 1152 exports.SnippetFilter = SnippetFilter; |
| 1153 |
| 1154 SnippetFilter.prototype = extend(ContentFilter, { |
| 1155 type: "snippet" |
| 1156 }); |
OLD | NEW |