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 script filters should match |
88 * @type {RegExp} | 88 * @type {RegExp} |
89 */ | 89 */ |
90 Filter.elemhideRegExp = /^([^/*|@"!]*?)#([@?])?#(.+)$/; | 90 Filter.scriptRegExp = /^([^/*|@"!]*?)#([@?$])?#(.+)$/; |
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.scriptRegExp.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 if (match[2] == "$") |
136 text, match[1], match[2], match[3] | 136 filter = new SnippetFilter(text, match[1], match[3]); |
137 ); | 137 else |
| 138 filter = ElemHideBase.fromText(text, match[1], match[2], match[3]); |
138 } | 139 } |
139 else if (text[0] == "!") | 140 else if (text[0] == "!") |
140 filter = new CommentFilter(text); | 141 filter = new CommentFilter(text); |
141 else | 142 else |
142 filter = RegExpFilter.fromText(text); | 143 filter = RegExpFilter.fromText(text); |
143 | 144 |
144 Filter.knownFilters.set(filter.text, filter); | 145 Filter.knownFilters.set(filter.text, filter); |
145 return filter; | 146 return filter; |
146 }; | 147 }; |
147 | 148 |
(...skipping 29 matching lines...) Expand all Loading... |
177 if (!text) | 178 if (!text) |
178 return text; | 179 return text; |
179 | 180 |
180 // Remove line breaks, tabs etc | 181 // Remove line breaks, tabs etc |
181 text = text.replace(/[^\S ]+/g, ""); | 182 text = text.replace(/[^\S ]+/g, ""); |
182 | 183 |
183 // Don't remove spaces inside comments | 184 // Don't remove spaces inside comments |
184 if (/^ *!/.test(text)) | 185 if (/^ *!/.test(text)) |
185 return text.trim(); | 186 return text.trim(); |
186 | 187 |
187 // Special treatment for element hiding filters, right side is allowed to | 188 // Special treatment for script filters, right side is allowed to contain |
188 // contain spaces | 189 // spaces |
189 if (Filter.elemhideRegExp.test(text)) | 190 if (Filter.scriptRegExp.test(text)) |
190 { | 191 { |
191 let [, domains, separator, selector] = /^(.*?)(#[@?]?#?)(.*)$/.exec(text); | 192 let [, domains, separator, script] = /^(.*?)(#[@?$]?#?)(.*)$/.exec(text); |
192 return domains.replace(/ +/g, "") + separator + selector.trim(); | 193 return domains.replace(/ +/g, "") + separator + script.trim(); |
193 } | 194 } |
194 | 195 |
195 // For most regexp filters we strip all spaces, but $csp filter options | 196 // For most regexp filters we strip all spaces, but $csp filter options |
196 // are allowed to contain single (non trailing) spaces. | 197 // are allowed to contain single (non trailing) spaces. |
197 let strippedText = text.replace(/ +/g, ""); | 198 let strippedText = text.replace(/ +/g, ""); |
198 if (!strippedText.includes("$") || !/\bcsp=/i.test(strippedText)) | 199 if (!strippedText.includes("$") || !/\bcsp=/i.test(strippedText)) |
199 return strippedText; | 200 return strippedText; |
200 | 201 |
201 let optionsMatch = Filter.optionsRegExp.exec(strippedText); | 202 let optionsMatch = Filter.optionsRegExp.exec(strippedText); |
202 if (!optionsMatch) | 203 if (!optionsMatch) |
(...skipping 773 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
976 RegExpFilter.call(this, text, regexpSource, contentType, matchCase, domains, | 977 RegExpFilter.call(this, text, regexpSource, contentType, matchCase, domains, |
977 thirdParty, sitekeys); | 978 thirdParty, sitekeys); |
978 } | 979 } |
979 exports.WhitelistFilter = WhitelistFilter; | 980 exports.WhitelistFilter = WhitelistFilter; |
980 | 981 |
981 WhitelistFilter.prototype = extend(RegExpFilter, { | 982 WhitelistFilter.prototype = extend(RegExpFilter, { |
982 type: "whitelist" | 983 type: "whitelist" |
983 }); | 984 }); |
984 | 985 |
985 /** | 986 /** |
986 * Base class for element hiding filters | 987 * Base class for script filters |
987 * @param {string} text see Filter() | 988 * @param {string} text see Filter() |
988 * @param {string} [domains] Host names or domains the filter should be | 989 * @param {string} [domains] Host names or domains the filter should be |
989 * restricted to | 990 * restricted to |
990 * @param {string} selector CSS selector for the HTML elements that should be | 991 * @param {string} script Script that should be executed |
991 * hidden | |
992 * @constructor | 992 * @constructor |
993 * @augments ActiveFilter | 993 * @augments ActiveFilter |
994 */ | 994 */ |
995 function ElemHideBase(text, domains, selector) | 995 function ScriptFilter(text, domains, script) |
996 { | 996 { |
997 ActiveFilter.call(this, text, domains || null); | 997 ActiveFilter.call(this, text, domains || null); |
998 | 998 |
999 if (domains) | 999 if (domains) |
1000 { | 1000 { |
1001 this.selectorDomains = domains.replace(/,~[^,]+/g, "") | 1001 this.scriptDomains = domains.replace(/,~[^,]+/g, "") |
1002 .replace(/^~[^,]+,?/, "").toLowerCase(); | 1002 .replace(/^~[^,]+,?/, "").toLowerCase(); |
1003 } | 1003 } |
1004 | 1004 |
1005 // Braces are being escaped to prevent CSS rule injection. | 1005 this.script = script; |
1006 this.selector = selector.replace("{", "\\7B ").replace("}", "\\7D "); | |
1007 } | 1006 } |
1008 exports.ElemHideBase = ElemHideBase; | 1007 exports.ScriptFilter = ScriptFilter; |
1009 | 1008 |
1010 ElemHideBase.prototype = extend(ActiveFilter, { | 1009 ScriptFilter.prototype = extend(ActiveFilter, { |
1011 /** | 1010 /** |
1012 * @see ActiveFilter.domainSeparator | 1011 * @see ActiveFilter.domainSeparator |
1013 */ | 1012 */ |
1014 domainSeparator: ",", | 1013 domainSeparator: ",", |
1015 | 1014 |
1016 /** | 1015 /** |
1017 * @see ActiveFilter.ignoreTrailingDot | 1016 * @see ActiveFilter.ignoreTrailingDot |
1018 */ | 1017 */ |
1019 ignoreTrailingDot: false, | 1018 ignoreTrailingDot: false, |
1020 | 1019 |
1021 /** | 1020 /** |
1022 * Host names or domains the filter should be restricted to (can be null for | 1021 * Host names or domains the filter should be restricted to (can be null for |
1023 * no restriction) | 1022 * no restriction) |
1024 * @type {?string} | 1023 * @type {?string} |
1025 */ | 1024 */ |
1026 selectorDomains: null, | 1025 scriptDomains: null, |
| 1026 |
| 1027 /** |
| 1028 * Script that should be executed |
| 1029 * @type {string} |
| 1030 */ |
| 1031 script: null |
| 1032 }); |
| 1033 |
| 1034 /* |
| 1035 * Base class for element hiding filters |
| 1036 * @param {string} text see Filter() |
| 1037 * @param {string} [domains] see ScriptFilter() |
| 1038 * @param {string} selector CSS selector for the HTML elements that should be |
| 1039 * hidden |
| 1040 * @constructor |
| 1041 * @augments ScriptFilter |
| 1042 */ |
| 1043 function ElemHideBase(text, domains, selector) |
| 1044 { |
| 1045 ScriptFilter.call(this, text, domains, selector); |
| 1046 |
| 1047 // Braces are being escaped to prevent CSS rule injection. |
| 1048 this.script = this.script.replace("{", "\\7B ").replace("}", "\\7D "); |
| 1049 } |
| 1050 exports.ElemHideBase = ElemHideBase; |
| 1051 |
| 1052 ElemHideBase.prototype = extend(ScriptFilter, { |
| 1053 /** |
| 1054 * @see ScriptFilter.scriptDomains |
| 1055 */ |
| 1056 get selectorDomains() |
| 1057 { |
| 1058 return this.scriptDomains; |
| 1059 }, |
| 1060 |
1027 /** | 1061 /** |
1028 * CSS selector for the HTML elements that should be hidden | 1062 * CSS selector for the HTML elements that should be hidden |
1029 * @type {string} | 1063 * @type {string} |
1030 */ | 1064 */ |
1031 selector: null | 1065 get selector() |
| 1066 { |
| 1067 return this.script; |
| 1068 } |
1032 }); | 1069 }); |
1033 | 1070 |
1034 /** | 1071 /** |
1035 * Creates an element hiding filter from a pre-parsed text representation | 1072 * Creates an element hiding filter from a pre-parsed text representation |
1036 * | 1073 * |
1037 * @param {string} text same as in Filter() | 1074 * @param {string} text same as in Filter() |
1038 * @param {string} [domains] | 1075 * @param {string} [domains] |
1039 * domains part of the text representation | 1076 * domains part of the text representation |
1040 * @param {string} [type] | 1077 * @param {string} [type] |
1041 * rule type, either empty or @ (exception) or ? (emulation rule) | 1078 * rule type, either empty or @ (exception) or ? (emulation rule) |
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1113 */ | 1150 */ |
1114 function ElemHideEmulationFilter(text, domains, selector) | 1151 function ElemHideEmulationFilter(text, domains, selector) |
1115 { | 1152 { |
1116 ElemHideBase.call(this, text, domains, selector); | 1153 ElemHideBase.call(this, text, domains, selector); |
1117 } | 1154 } |
1118 exports.ElemHideEmulationFilter = ElemHideEmulationFilter; | 1155 exports.ElemHideEmulationFilter = ElemHideEmulationFilter; |
1119 | 1156 |
1120 ElemHideEmulationFilter.prototype = extend(ElemHideBase, { | 1157 ElemHideEmulationFilter.prototype = extend(ElemHideBase, { |
1121 type: "elemhideemulation" | 1158 type: "elemhideemulation" |
1122 }); | 1159 }); |
| 1160 |
| 1161 /** |
| 1162 * Class for snippet filters |
| 1163 * @param {string} text see Filter() |
| 1164 * @param {string} [domains] see ScriptFilter() |
| 1165 * @param {string} script Script that should be executed |
| 1166 * @constructor |
| 1167 * @augments ScriptFilter |
| 1168 */ |
| 1169 function SnippetFilter(text, domains, script) |
| 1170 { |
| 1171 ScriptFilter.call(this, text, domains, script); |
| 1172 } |
| 1173 exports.SnippetFilter = SnippetFilter; |
| 1174 |
| 1175 SnippetFilter.prototype = extend(ScriptFilter, { |
| 1176 type: "snippet" |
| 1177 }); |
OLD | NEW |