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 |
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
12 * GNU General Public License for more details. | 12 * GNU General Public License for more details. |
13 * | 13 * |
14 * You should have received a copy of the GNU General Public License | 14 * You should have received a copy of the GNU General Public License |
15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. | 15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. |
16 */ | 16 */ |
17 | 17 |
18 "use strict"; | 18 "use strict"; |
19 | 19 |
20 const {createSandbox} = require("./_common"); | 20 const {createSandbox} = require("./_common"); |
21 | 21 |
22 let Filter = null; | 22 let Filter = null; |
23 let InvalidFilter = null; | 23 let InvalidFilter = null; |
24 let CommentFilter = null; | 24 let CommentFilter = null; |
25 let ActiveFilter = null; | 25 let ActiveFilter = null; |
26 let RegExpFilter = null; | 26 let RegExpFilter = null; |
27 let BlockingFilter = null; | 27 let BlockingFilter = null; |
| 28 let ContentFilter = null; |
28 let WhitelistFilter = null; | 29 let WhitelistFilter = null; |
29 let ElemHideBase = null; | 30 let ElemHideBase = null; |
30 let ElemHideFilter = null; | 31 let ElemHideFilter = null; |
31 let ElemHideException = null; | 32 let ElemHideException = null; |
32 let ElemHideEmulationFilter = null; | 33 let ElemHideEmulationFilter = null; |
| 34 let SnippetFilter = null; |
33 | 35 |
34 let t = null; | 36 let t = null; |
35 let defaultTypes = null; | 37 let defaultTypes = null; |
36 | 38 |
37 exports.setUp = function(callback) | 39 exports.setUp = function(callback) |
38 { | 40 { |
39 let sandboxedRequire = createSandbox(); | 41 let sandboxedRequire = createSandbox(); |
40 ( | 42 ( |
41 {Filter, InvalidFilter, CommentFilter, ActiveFilter, RegExpFilter, | 43 {Filter, InvalidFilter, CommentFilter, ActiveFilter, RegExpFilter, |
42 BlockingFilter, WhitelistFilter, ElemHideBase, ElemHideFilter, | 44 BlockingFilter, WhitelistFilter, ContentFilter, ElemHideBase, |
43 ElemHideException, | 45 ElemHideFilter, ElemHideException, ElemHideEmulationFilter, |
44 ElemHideEmulationFilter} = sandboxedRequire("../lib/filterClasses") | 46 SnippetFilter} = sandboxedRequire("../lib/filterClasses") |
45 ); | 47 ); |
46 t = RegExpFilter.typeMap; | 48 t = RegExpFilter.typeMap; |
47 defaultTypes = 0x7FFFFFFF & ~(t.CSP | t.ELEMHIDE | t.DOCUMENT | t.POPUP | | 49 defaultTypes = 0x7FFFFFFF & ~(t.CSP | t.ELEMHIDE | t.DOCUMENT | t.POPUP | |
48 t.GENERICHIDE | t.GENERICBLOCK); | 50 t.GENERICHIDE | t.GENERICBLOCK); |
49 | 51 |
50 callback(); | 52 callback(); |
51 }; | 53 }; |
52 | 54 |
53 function serializeFilter(filter) | 55 function serializeFilter(filter) |
54 { | 56 { |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
87 | 89 |
88 let sitekeys = filter.sitekeys || []; | 90 let sitekeys = filter.sitekeys || []; |
89 result.push("sitekeys=" + sitekeys.slice().sort().join("|")); | 91 result.push("sitekeys=" + sitekeys.slice().sort().join("|")); |
90 | 92 |
91 result.push("thirdParty=" + filter.thirdParty); | 93 result.push("thirdParty=" + filter.thirdParty); |
92 if (filter instanceof BlockingFilter) | 94 if (filter instanceof BlockingFilter) |
93 { | 95 { |
94 result.push("type=filterlist"); | 96 result.push("type=filterlist"); |
95 result.push("collapse=" + filter.collapse); | 97 result.push("collapse=" + filter.collapse); |
96 result.push("csp=" + filter.csp); | 98 result.push("csp=" + filter.csp); |
| 99 result.push("rewrite=" + filter.rewrite); |
97 } | 100 } |
98 else if (filter instanceof WhitelistFilter) | 101 else if (filter instanceof WhitelistFilter) |
99 result.push("type=whitelist"); | 102 result.push("type=whitelist"); |
100 } | 103 } |
101 else if (filter instanceof ElemHideBase) | 104 else if (filter instanceof ElemHideBase) |
102 { | 105 { |
103 if (filter instanceof ElemHideFilter) | 106 if (filter instanceof ElemHideFilter) |
104 result.push("type=elemhide"); | 107 result.push("type=elemhide"); |
105 else if (filter instanceof ElemHideException) | 108 else if (filter instanceof ElemHideException) |
106 result.push("type=elemhideexception"); | 109 result.push("type=elemhideexception"); |
107 else if (filter instanceof ElemHideEmulationFilter) | 110 else if (filter instanceof ElemHideEmulationFilter) |
108 result.push("type=elemhideemulation"); | 111 result.push("type=elemhideemulation"); |
109 | 112 |
110 result.push("injectionDomain=" + (filter.injectionDomain || "")); | 113 result.push("selectorDomains=" + |
111 result.push("code=" + filter.code); | 114 [...filter.domains || []] |
| 115 .filter(([domain, isIncluded]) => isIncluded) |
| 116 .map(([domain]) => domain.toLowerCase())); |
| 117 result.push("selector=" + filter.selector); |
| 118 } |
| 119 else if (filter instanceof SnippetFilter) |
| 120 { |
| 121 result.push("type=snippet"); |
| 122 result.push("scriptDomains=" + |
| 123 [...filter.domains || []] |
| 124 .filter(([domain, isIncluded]) => isIncluded) |
| 125 .map(([domain]) => domain.toLowerCase())); |
| 126 result.push("script=" + filter.script); |
112 } | 127 } |
113 } | 128 } |
114 return result; | 129 return result; |
115 } | 130 } |
116 | 131 |
117 function addDefaults(expected) | 132 function addDefaults(expected) |
118 { | 133 { |
119 let type = null; | 134 let type = null; |
120 let hasProperty = {}; | 135 let hasProperty = {}; |
121 for (let entry of expected) | 136 for (let entry of expected) |
122 { | 137 { |
123 if (/^type=(.*)/.test(entry)) | 138 if (/^type=(.*)/.test(entry)) |
124 type = RegExp.$1; | 139 type = RegExp.$1; |
125 else if (/^(\w+)/.test(entry)) | 140 else if (/^(\w+)/.test(entry)) |
126 hasProperty[RegExp.$1] = true; | 141 hasProperty[RegExp.$1] = true; |
127 } | 142 } |
128 | 143 |
129 function addProperty(prop, value) | 144 function addProperty(prop, value) |
130 { | 145 { |
131 if (!(prop in hasProperty)) | 146 if (!(prop in hasProperty)) |
132 expected.push(prop + "=" + value); | 147 expected.push(prop + "=" + value); |
133 } | 148 } |
134 | 149 |
135 if (type == "whitelist" || type == "filterlist" || type == "elemhide" || | 150 if (type == "whitelist" || type == "filterlist" || type == "elemhide" || |
136 type == "elemhideexception" || type == "elemhideemulation") | 151 type == "elemhideexception" || type == "elemhideemulation" || |
| 152 type == "snippet") |
137 { | 153 { |
138 addProperty("disabled", "false"); | 154 addProperty("disabled", "false"); |
139 addProperty("lastHit", "0"); | 155 addProperty("lastHit", "0"); |
140 addProperty("hitCount", "0"); | 156 addProperty("hitCount", "0"); |
141 } | 157 } |
142 if (type == "whitelist" || type == "filterlist") | 158 if (type == "whitelist" || type == "filterlist") |
143 { | 159 { |
144 addProperty("contentType", 0x7FFFFFFF & ~( | 160 addProperty("contentType", 0x7FFFFFFF & ~( |
145 t.CSP | t.DOCUMENT | t.ELEMHIDE | t.POPUP | t.GENERICHIDE | t.GENERICBLOCK | 161 t.CSP | t.DOCUMENT | t.ELEMHIDE | t.POPUP | t.GENERICHIDE | t.GENERICBLOCK |
146 )); | 162 )); |
147 addProperty("matchCase", "false"); | 163 addProperty("matchCase", "false"); |
148 addProperty("thirdParty", "null"); | 164 addProperty("thirdParty", "null"); |
149 addProperty("domains", ""); | 165 addProperty("domains", ""); |
150 addProperty("sitekeys", ""); | 166 addProperty("sitekeys", ""); |
151 } | 167 } |
152 if (type == "filterlist") | 168 if (type == "filterlist") |
153 { | 169 { |
154 addProperty("collapse", "null"); | 170 addProperty("collapse", "null"); |
155 addProperty("csp", "null"); | 171 addProperty("csp", "null"); |
| 172 addProperty("rewrite", "null"); |
156 } | 173 } |
157 if (type == "elemhide" || type == "elemhideexception" || | 174 if (type == "elemhide" || type == "elemhideexception" || |
158 type == "elemhideemulation") | 175 type == "elemhideemulation") |
159 { | 176 { |
160 addProperty("injectionDomain", ""); | 177 addProperty("selectorDomains", ""); |
| 178 addProperty("domains", ""); |
| 179 } |
| 180 if (type == "snippet") |
| 181 { |
| 182 addProperty("scriptDomains", ""); |
161 addProperty("domains", ""); | 183 addProperty("domains", ""); |
162 } | 184 } |
163 } | 185 } |
164 | 186 |
165 function compareFilter(test, text, expected, postInit) | 187 function compareFilter(test, text, expected, postInit) |
166 { | 188 { |
167 addDefaults(expected); | 189 addDefaults(expected); |
168 | 190 |
169 let filter = Filter.fromText(text); | 191 let filter = Filter.fromText(text); |
170 if (postInit) | 192 if (postInit) |
(...skipping 22 matching lines...) Expand all Loading... |
193 } | 215 } |
194 | 216 |
195 exports.testFilterClassDefinitions = function(test) | 217 exports.testFilterClassDefinitions = function(test) |
196 { | 218 { |
197 test.equal(typeof Filter, "function", "typeof Filter"); | 219 test.equal(typeof Filter, "function", "typeof Filter"); |
198 test.equal(typeof InvalidFilter, "function", "typeof InvalidFilter"); | 220 test.equal(typeof InvalidFilter, "function", "typeof InvalidFilter"); |
199 test.equal(typeof CommentFilter, "function", "typeof CommentFilter"); | 221 test.equal(typeof CommentFilter, "function", "typeof CommentFilter"); |
200 test.equal(typeof ActiveFilter, "function", "typeof ActiveFilter"); | 222 test.equal(typeof ActiveFilter, "function", "typeof ActiveFilter"); |
201 test.equal(typeof RegExpFilter, "function", "typeof RegExpFilter"); | 223 test.equal(typeof RegExpFilter, "function", "typeof RegExpFilter"); |
202 test.equal(typeof BlockingFilter, "function", "typeof BlockingFilter"); | 224 test.equal(typeof BlockingFilter, "function", "typeof BlockingFilter"); |
| 225 test.equal(typeof ContentFilter, "function", "typeof ContentFilter"); |
203 test.equal(typeof WhitelistFilter, "function", "typeof WhitelistFilter"); | 226 test.equal(typeof WhitelistFilter, "function", "typeof WhitelistFilter"); |
204 test.equal(typeof ElemHideBase, "function", "typeof ElemHideBase"); | 227 test.equal(typeof ElemHideBase, "function", "typeof ElemHideBase"); |
205 test.equal(typeof ElemHideFilter, "function", "typeof ElemHideFilter"); | 228 test.equal(typeof ElemHideFilter, "function", "typeof ElemHideFilter"); |
206 test.equal(typeof ElemHideException, "function", "typeof ElemHideException"); | 229 test.equal(typeof ElemHideException, "function", "typeof ElemHideException"); |
207 test.equal(typeof ElemHideEmulationFilter, "function", | 230 test.equal(typeof ElemHideEmulationFilter, "function", |
208 "typeof ElemHideEmulationFilter"); | 231 "typeof ElemHideEmulationFilter"); |
| 232 test.equal(typeof SnippetFilter, "function", "typeof SnippetFilter"); |
209 | 233 |
210 test.done(); | 234 test.done(); |
211 }; | 235 }; |
212 | 236 |
213 exports.testComments = function(test) | 237 exports.testComments = function(test) |
214 { | 238 { |
215 compareFilter(test, "!asdf", ["type=comment", "text=!asdf"]); | 239 compareFilter(test, "!asdf", ["type=comment", "text=!asdf"]); |
216 compareFilter(test, "!foo#bar", ["type=comment", "text=!foo#bar"]); | 240 compareFilter(test, "!foo#bar", ["type=comment", "text=!foo#bar"]); |
217 compareFilter(test, "!foo##bar", ["type=comment", "text=!foo##bar"]); | 241 compareFilter(test, "!foo##bar", ["type=comment", "text=!foo##bar"]); |
218 | 242 |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
279 compareFilter(test, "@@/ddd|f?a[s]d/", ["type=whitelist", "text=@@/ddd|f?a[s]d
/", "regexp=ddd|f?a[s]d", "contentType=" + defaultTypes]); | 303 compareFilter(test, "@@/ddd|f?a[s]d/", ["type=whitelist", "text=@@/ddd|f?a[s]d
/", "regexp=ddd|f?a[s]d", "contentType=" + defaultTypes]); |
280 compareFilter(test, "@@*asdf*d**dd*", ["type=whitelist", "text=@@*asdf*d**dd*"
, "regexp=asdf.*d.*dd", "contentType=" + defaultTypes]); | 304 compareFilter(test, "@@*asdf*d**dd*", ["type=whitelist", "text=@@*asdf*d**dd*"
, "regexp=asdf.*d.*dd", "contentType=" + defaultTypes]); |
281 compareFilter(test, "@@|*asd|f*d**dd*|", ["type=whitelist", "text=@@|*asd|f*d*
*dd*|", "regexp=^.*asd\\|f.*d.*dd.*$", "contentType=" + defaultTypes]); | 305 compareFilter(test, "@@|*asd|f*d**dd*|", ["type=whitelist", "text=@@|*asd|f*d*
*dd*|", "regexp=^.*asd\\|f.*d.*dd.*$", "contentType=" + defaultTypes]); |
282 compareFilter(test, "@@dd[]{}$%<>&()d", ["type=whitelist", "text=@@dd[]{}$%<>&
()d", "regexp=dd\\[\\]\\{\\}\\$\\%\\<\\>\\&\\(\\)d", "contentType=" + defaultTyp
es]); | 306 compareFilter(test, "@@dd[]{}$%<>&()d", ["type=whitelist", "text=@@dd[]{}$%<>&
()d", "regexp=dd\\[\\]\\{\\}\\$\\%\\<\\>\\&\\(\\)d", "contentType=" + defaultTyp
es]); |
283 | 307 |
284 test.done(); | 308 test.done(); |
285 }; | 309 }; |
286 | 310 |
287 exports.testFilterOptions = function(test) | 311 exports.testFilterOptions = function(test) |
288 { | 312 { |
289 compareFilter(test, "bla$match-case,csp=first csp,script,other,third-party,dom
ain=foo.com,sitekey=foo", ["type=filterlist", "text=bla$match-case,csp=first csp
,script,other,third-party,domain=foo.com,sitekey=foo", "regexp=bla", "matchCase=
true", "contentType=" + (t.SCRIPT | t.OTHER | t.CSP), "thirdParty=true", "domain
s=FOO.COM", "sitekeys=FOO", "csp=first csp"]); | 313 compareFilter(test, "bla$match-case,csp=first csp,script,other,third-party,dom
ain=FOO.cOm,sitekey=foo", ["type=filterlist", "text=bla$match-case,csp=first csp
,script,other,third-party,domain=FOO.cOm,sitekey=foo", "regexp=bla", "matchCase=
true", "contentType=" + (t.SCRIPT | t.OTHER | t.CSP), "thirdParty=true", "domain
s=foo.com", "sitekeys=FOO", "csp=first csp"]); |
290 compareFilter(test, "bla$~match-case,~csp=csp,~script,~other,~third-party,doma
in=~bar.com", ["type=filterlist", "text=bla$~match-case,~csp=csp,~script,~other,
~third-party,domain=~bar.com", "regexp=bla", "contentType=" + (defaultTypes & ~(
t.SCRIPT | t.OTHER)), "thirdParty=false", "domains=~BAR.COM"]); | 314 compareFilter(test, "bla$~match-case,~csp=csp,~script,~other,~third-party,doma
in=~bAr.coM", ["type=filterlist", "text=bla$~match-case,~csp=csp,~script,~other,
~third-party,domain=~bAr.coM", "regexp=bla", "contentType=" + (defaultTypes & ~(
t.SCRIPT | t.OTHER)), "thirdParty=false", "domains=~bar.com"]); |
291 compareFilter(test, "@@bla$match-case,script,other,third-party,domain=foo.com|
bar.com|~bar.foo.com|~foo.bar.com,csp=c s p,sitekey=foo|bar", ["type=whitelist",
"text=@@bla$match-case,script,other,third-party,domain=foo.com|bar.com|~bar.foo
.com|~foo.bar.com,csp=c s p,sitekey=foo|bar", "regexp=bla", "matchCase=true", "c
ontentType=" + (t.SCRIPT | t.OTHER | t.CSP), "thirdParty=true", "domains=BAR.COM
|FOO.COM|~BAR.FOO.COM|~FOO.BAR.COM", "sitekeys=BAR|FOO"]); | 315 compareFilter(test, "@@bla$match-case,script,other,third-party,domain=foo.com|
bar.com|~bAR.foO.Com|~Foo.Bar.com,csp=c s p,sitekey=foo|bar", ["type=whitelist",
"text=@@bla$match-case,script,other,third-party,domain=foo.com|bar.com|~bAR.foO
.Com|~Foo.Bar.com,csp=c s p,sitekey=foo|bar", "regexp=bla", "matchCase=true", "c
ontentType=" + (t.SCRIPT | t.OTHER | t.CSP), "thirdParty=true", "domains=bar.com
|foo.com|~bar.foo.com|~foo.bar.com", "sitekeys=BAR|FOO"]); |
292 compareFilter(test, "@@bla$match-case,script,other,third-party,domain=foo.com|
bar.com|~bar.foo.com|~foo.bar.com,sitekey=foo|bar", ["type=whitelist", "text=@@b
la$match-case,script,other,third-party,domain=foo.com|bar.com|~bar.foo.com|~foo.
bar.com,sitekey=foo|bar", "regexp=bla", "matchCase=true", "contentType=" + (t.SC
RIPT | t.OTHER), "thirdParty=true", "domains=BAR.COM|FOO.COM|~BAR.FOO.COM|~FOO.B
AR.COM", "sitekeys=BAR|FOO"]); | 316 compareFilter(test, "@@bla$match-case,script,other,third-party,domain=foo.com|
bar.com|~bar.foo.com|~foo.bar.com,sitekey=foo|bar", ["type=whitelist", "text=@@b
la$match-case,script,other,third-party,domain=foo.com|bar.com|~bar.foo.com|~foo.
bar.com,sitekey=foo|bar", "regexp=bla", "matchCase=true", "contentType=" + (t.SC
RIPT | t.OTHER), "thirdParty=true", "domains=bar.com|foo.com|~bar.foo.com|~foo.b
ar.com", "sitekeys=BAR|FOO"]); |
| 317 compareFilter(test, "||content.server.com/files/*.php$rewrite=$1", ["type=filt
erlist", "text=||content.server.com/files/*.php$rewrite=$1", "regexp=^[\\w\\-]+:
\\/+(?!\\/)(?:[^\\/]+\\.)?content\\.server\\.com\\/files\\/.*\\.php", "matchCase
=false", "rewrite=$1", "contentType=" + (defaultTypes & ~(t.SCRIPT | t.SUBDOCUME
NT | t.OBJECT | t.OBJECT_SUBREQUEST))]); |
293 | 318 |
294 // background and image should be the same for backwards compatibility | 319 // background and image should be the same for backwards compatibility |
295 compareFilter(test, "bla$image", ["type=filterlist", "text=bla$image", "regexp
=bla", "contentType=" + (t.IMAGE)]); | 320 compareFilter(test, "bla$image", ["type=filterlist", "text=bla$image", "regexp
=bla", "contentType=" + (t.IMAGE)]); |
296 compareFilter(test, "bla$background", ["type=filterlist", "text=bla$background
", "regexp=bla", "contentType=" + (t.IMAGE)]); | 321 compareFilter(test, "bla$background", ["type=filterlist", "text=bla$background
", "regexp=bla", "contentType=" + (t.IMAGE)]); |
297 compareFilter(test, "bla$~image", ["type=filterlist", "text=bla$~image", "rege
xp=bla", "contentType=" + (defaultTypes & ~t.IMAGE)]); | 322 compareFilter(test, "bla$~image", ["type=filterlist", "text=bla$~image", "rege
xp=bla", "contentType=" + (defaultTypes & ~t.IMAGE)]); |
298 compareFilter(test, "bla$~background", ["type=filterlist", "text=bla$~backgrou
nd", "regexp=bla", "contentType=" + (defaultTypes & ~t.IMAGE)]); | 323 compareFilter(test, "bla$~background", ["type=filterlist", "text=bla$~backgrou
nd", "regexp=bla", "contentType=" + (defaultTypes & ~t.IMAGE)]); |
299 | 324 |
300 compareFilter(test, "@@bla$~script,~other", ["type=whitelist", "text=@@bla$~sc
ript,~other", "regexp=bla", "contentType=" + (defaultTypes & ~(t.SCRIPT | t.OTHE
R))]); | 325 compareFilter(test, "@@bla$~script,~other", ["type=whitelist", "text=@@bla$~sc
ript,~other", "regexp=bla", "contentType=" + (defaultTypes & ~(t.SCRIPT | t.OTHE
R))]); |
301 compareFilter(test, "@@http://bla$~script,~other", ["type=whitelist", "text=@@
http://bla$~script,~other", "regexp=http\\:\\/\\/bla", "contentType=" + (default
Types & ~(t.SCRIPT | t.OTHER))]); | 326 compareFilter(test, "@@http://bla$~script,~other", ["type=whitelist", "text=@@
http://bla$~script,~other", "regexp=http\\:\\/\\/bla", "contentType=" + (default
Types & ~(t.SCRIPT | t.OTHER))]); |
302 compareFilter(test, "@@|ftp://bla$~script,~other", ["type=whitelist", "text=@@
|ftp://bla$~script,~other", "regexp=^ftp\\:\\/\\/bla", "contentType=" + (default
Types & ~(t.SCRIPT | t.OTHER))]); | 327 compareFilter(test, "@@|ftp://bla$~script,~other", ["type=whitelist", "text=@@
|ftp://bla$~script,~other", "regexp=^ftp\\:\\/\\/bla", "contentType=" + (default
Types & ~(t.SCRIPT | t.OTHER))]); |
(...skipping 16 matching lines...) Expand all Loading... |
319 compareFilter(test, "bla$csp=foo,csp=referrer foo", ["type=invalid", "text=bla
$csp=foo,csp=referrer foo", "reason=filter_invalid_csp"]); | 344 compareFilter(test, "bla$csp=foo,csp=referrer foo", ["type=invalid", "text=bla
$csp=foo,csp=referrer foo", "reason=filter_invalid_csp"]); |
320 compareFilter(test, "bla$csp=foo,csp=base-uri", ["type=invalid", "text=bla$csp
=foo,csp=base-uri", "reason=filter_invalid_csp"]); | 345 compareFilter(test, "bla$csp=foo,csp=base-uri", ["type=invalid", "text=bla$csp
=foo,csp=base-uri", "reason=filter_invalid_csp"]); |
321 compareFilter(test, "bla$csp=foo,csp=upgrade-insecure-requests", ["type=invali
d", "text=bla$csp=foo,csp=upgrade-insecure-requests", "reason=filter_invalid_csp
"]); | 346 compareFilter(test, "bla$csp=foo,csp=upgrade-insecure-requests", ["type=invali
d", "text=bla$csp=foo,csp=upgrade-insecure-requests", "reason=filter_invalid_csp
"]); |
322 compareFilter(test, "bla$csp=foo,csp=ReFeRReR", ["type=invalid", "text=bla$csp
=foo,csp=ReFeRReR", "reason=filter_invalid_csp"]); | 347 compareFilter(test, "bla$csp=foo,csp=ReFeRReR", ["type=invalid", "text=bla$csp
=foo,csp=ReFeRReR", "reason=filter_invalid_csp"]); |
323 | 348 |
324 test.done(); | 349 test.done(); |
325 }; | 350 }; |
326 | 351 |
327 exports.testElementHidingRules = function(test) | 352 exports.testElementHidingRules = function(test) |
328 { | 353 { |
329 compareFilter(test, "##ddd", ["type=elemhide", "text=##ddd", "code=ddd"]); | 354 compareFilter(test, "##ddd", ["type=elemhide", "text=##ddd", "selector=ddd"]); |
330 compareFilter(test, "##body > div:first-child", ["type=elemhide", "text=##body
> div:first-child", "code=body > div:first-child"]); | 355 compareFilter(test, "##body > div:first-child", ["type=elemhide", "text=##body
> div:first-child", "selector=body > div:first-child"]); |
331 compareFilter(test, "foo##ddd", ["type=elemhide", "text=foo##ddd", "injectionD
omain=foo", "code=ddd", "domains=FOO"]); | 356 compareFilter(test, "fOO##ddd", ["type=elemhide", "text=fOO##ddd", "selectorDo
mains=foo", "selector=ddd", "domains=foo"]); |
332 compareFilter(test, "foo,bar##ddd", ["type=elemhide", "text=foo,bar##ddd", "in
jectionDomain=foo,bar", "code=ddd", "domains=BAR|FOO"]); | 357 compareFilter(test, "Foo,bAr##ddd", ["type=elemhide", "text=Foo,bAr##ddd", "se
lectorDomains=foo,bar", "selector=ddd", "domains=bar|foo"]); |
333 compareFilter(test, "foo,~bar##ddd", ["type=elemhide", "text=foo,~bar##ddd", "
injectionDomain=foo", "code=ddd", "domains=FOO|~BAR"]); | 358 compareFilter(test, "foo,~baR##ddd", ["type=elemhide", "text=foo,~baR##ddd", "
selectorDomains=foo", "selector=ddd", "domains=foo|~bar"]); |
334 compareFilter(test, "foo,~baz,bar##ddd", ["type=elemhide", "text=foo,~baz,bar#
#ddd", "injectionDomain=foo,bar", "code=ddd", "domains=BAR|FOO|~BAZ"]); | 359 compareFilter(test, "foo,~baz,bar##ddd", ["type=elemhide", "text=foo,~baz,bar#
#ddd", "selectorDomains=foo,bar", "selector=ddd", "domains=bar|foo|~baz"]); |
335 | 360 |
336 test.done(); | 361 test.done(); |
337 }; | 362 }; |
338 | 363 |
339 exports.testElementHidingExceptions = function(test) | 364 exports.testElementHidingExceptions = function(test) |
340 { | 365 { |
341 compareFilter(test, "#@#ddd", ["type=elemhideexception", "text=#@#ddd", "code=
ddd"]); | 366 compareFilter(test, "#@#ddd", ["type=elemhideexception", "text=#@#ddd", "selec
tor=ddd"]); |
342 compareFilter(test, "#@#body > div:first-child", ["type=elemhideexception", "t
ext=#@#body > div:first-child", "code=body > div:first-child"]); | 367 compareFilter(test, "#@#body > div:first-child", ["type=elemhideexception", "t
ext=#@#body > div:first-child", "selector=body > div:first-child"]); |
343 compareFilter(test, "foo#@#ddd", ["type=elemhideexception", "text=foo#@#ddd",
"injectionDomain=foo", "code=ddd", "domains=FOO"]); | 368 compareFilter(test, "fOO#@#ddd", ["type=elemhideexception", "text=fOO#@#ddd",
"selectorDomains=foo", "selector=ddd", "domains=foo"]); |
344 compareFilter(test, "foo,bar#@#ddd", ["type=elemhideexception", "text=foo,bar#
@#ddd", "injectionDomain=foo,bar", "code=ddd", "domains=BAR|FOO"]); | 369 compareFilter(test, "Foo,bAr#@#ddd", ["type=elemhideexception", "text=Foo,bAr#
@#ddd", "selectorDomains=foo,bar", "selector=ddd", "domains=bar|foo"]); |
345 compareFilter(test, "foo,~bar#@#ddd", ["type=elemhideexception", "text=foo,~ba
r#@#ddd", "injectionDomain=foo", "code=ddd", "domains=FOO|~BAR"]); | 370 compareFilter(test, "foo,~baR#@#ddd", ["type=elemhideexception", "text=foo,~ba
R#@#ddd", "selectorDomains=foo", "selector=ddd", "domains=foo|~bar"]); |
346 compareFilter(test, "foo,~baz,bar#@#ddd", ["type=elemhideexception", "text=foo
,~baz,bar#@#ddd", "injectionDomain=foo,bar", "code=ddd", "domains=BAR|FOO|~BAZ"]
); | 371 compareFilter(test, "foo,~baz,bar#@#ddd", ["type=elemhideexception", "text=foo
,~baz,bar#@#ddd", "selectorDomains=foo,bar", "selector=ddd", "domains=bar|foo|~b
az"]); |
347 | 372 |
348 test.done(); | 373 test.done(); |
349 }; | 374 }; |
350 | 375 |
351 exports.testElemHideEmulationFilters = function(test) | 376 exports.testElemHideEmulationFilters = function(test) |
352 { | 377 { |
353 // Check valid domain combinations | 378 // Check valid domain combinations |
354 compareFilter(test, "foo.com#?#:-abp-properties(abc)", ["type=elemhideemulatio
n", "text=foo.com#?#:-abp-properties(abc)", "injectionDomain=foo.com", "code=:-a
bp-properties(abc)", "domains=FOO.COM"]); | 379 compareFilter(test, "fOO.cOm#?#:-abp-properties(abc)", ["type=elemhideemulatio
n", "text=fOO.cOm#?#:-abp-properties(abc)", "selectorDomains=foo.com", "selector
=:-abp-properties(abc)", "domains=foo.com"]); |
355 compareFilter(test, "foo.com,~bar.com#?#:-abp-properties(abc)", ["type=elemhid
eemulation", "text=foo.com,~bar.com#?#:-abp-properties(abc)", "injectionDomain=f
oo.com", "code=:-abp-properties(abc)", "domains=FOO.COM|~BAR.COM"]); | 380 compareFilter(test, "Foo.com,~bAr.com#?#:-abp-properties(abc)", ["type=elemhid
eemulation", "text=Foo.com,~bAr.com#?#:-abp-properties(abc)", "selectorDomains=f
oo.com", "selector=:-abp-properties(abc)", "domains=foo.com|~bar.com"]); |
356 compareFilter(test, "foo.com,~bar#?#:-abp-properties(abc)", ["type=elemhideemu
lation", "text=foo.com,~bar#?#:-abp-properties(abc)", "injectionDomain=foo.com",
"code=:-abp-properties(abc)", "domains=FOO.COM|~BAR"]); | 381 compareFilter(test, "foo.com,~baR#?#:-abp-properties(abc)", ["type=elemhideemu
lation", "text=foo.com,~baR#?#:-abp-properties(abc)", "selectorDomains=foo.com",
"selector=:-abp-properties(abc)", "domains=foo.com|~bar"]); |
357 compareFilter(test, "~foo.com,bar.com#?#:-abp-properties(abc)", ["type=elemhid
eemulation", "text=~foo.com,bar.com#?#:-abp-properties(abc)", "injectionDomain=b
ar.com", "code=:-abp-properties(abc)", "domains=BAR.COM|~FOO.COM"]); | 382 compareFilter(test, "~foo.com,bar.com#?#:-abp-properties(abc)", ["type=elemhid
eemulation", "text=~foo.com,bar.com#?#:-abp-properties(abc)", "selectorDomains=b
ar.com", "selector=:-abp-properties(abc)", "domains=bar.com|~foo.com"]); |
358 | 383 |
359 // Check some special cases | 384 // Check some special cases |
360 compareFilter(test, "#?#:-abp-properties(abc)", ["type=invalid", "text=#?#:-ab
p-properties(abc)", "reason=filter_elemhideemulation_nodomain"]); | 385 compareFilter(test, "#?#:-abp-properties(abc)", ["type=invalid", "text=#?#:-ab
p-properties(abc)", "reason=filter_elemhideemulation_nodomain"]); |
361 compareFilter(test, "foo.com#?#abc", ["type=elemhideemulation", "text=foo.com#
?#abc", "injectionDomain=foo.com", "code=abc", "domains=FOO.COM"]); | 386 compareFilter(test, "foo.com#?#abc", ["type=elemhideemulation", "text=foo.com#
?#abc", "selectorDomains=foo.com", "selector=abc", "domains=foo.com"]); |
362 compareFilter(test, "foo.com#?#:-abp-foobar(abc)", ["type=elemhideemulation",
"text=foo.com#?#:-abp-foobar(abc)", "injectionDomain=foo.com", "code=:-abp-fooba
r(abc)", "domains=FOO.COM"]); | 387 compareFilter(test, "foo.com#?#:-abp-foobar(abc)", ["type=elemhideemulation",
"text=foo.com#?#:-abp-foobar(abc)", "selectorDomains=foo.com", "selector=:-abp-f
oobar(abc)", "domains=foo.com"]); |
363 compareFilter(test, "foo.com#?#aaa :-abp-properties(abc) bbb", ["type=elemhide
emulation", "text=foo.com#?#aaa :-abp-properties(abc) bbb", "injectionDomain=foo
.com", "code=aaa :-abp-properties(abc) bbb", "domains=FOO.COM"]); | 388 compareFilter(test, "foo.com#?#aaa :-abp-properties(abc) bbb", ["type=elemhide
emulation", "text=foo.com#?#aaa :-abp-properties(abc) bbb", "selectorDomains=foo
.com", "selector=aaa :-abp-properties(abc) bbb", "domains=foo.com"]); |
364 compareFilter(test, "foo.com#?#:-abp-properties(|background-image: url(data:*)
)", ["type=elemhideemulation", "text=foo.com#?#:-abp-properties(|background-imag
e: url(data:*))", "injectionDomain=foo.com", "code=:-abp-properties(|background-
image: url(data:*))", "domains=FOO.COM"]); | 389 compareFilter(test, "foo.com#?#:-abp-properties(|background-image: url(data:*)
)", ["type=elemhideemulation", "text=foo.com#?#:-abp-properties(|background-imag
e: url(data:*))", "selectorDomains=foo.com", "selector=:-abp-properties(|backgro
und-image: url(data:*))", "domains=foo.com"]); |
365 | 390 |
366 // Check conversion of legacy filters | 391 // Check conversion of legacy filters |
367 compareFilter(test, "foo.com##[-abp-properties='abc']", ["type=elemhideemulati
on", "text=foo.com#?#:-abp-properties(abc)", "injectionDomain=foo.com", "code=:-
abp-properties(abc)", "domains=FOO.COM"]); | 392 compareFilter(test, "foo.com##[-abp-properties='abc']", ["type=elemhideemulati
on", "text=foo.com#?#:-abp-properties(abc)", "selectorDomains=foo.com", "selecto
r=:-abp-properties(abc)", "domains=foo.com"]); |
368 test.equal(Filter.fromText("foo.com##[-abp-properties='abc']"), Filter.fromTex
t("foo.com#?#:-abp-properties(abc)")); | 393 test.equal(Filter.fromText("foo.com##[-abp-properties='abc']"), Filter.fromTex
t("foo.com#?#:-abp-properties(abc)")); |
369 compareFilter(test, "foo.com#@#[-abp-properties='abc']", ["type=elemhideexcept
ion", "text=foo.com#@#[-abp-properties='abc']", "injectionDomain=foo.com", "code
=[-abp-properties='abc']", "domains=FOO.COM"]); | 394 compareFilter(test, "foo.com#@#[-abp-properties='abc']", ["type=elemhideexcept
ion", "text=foo.com#@#[-abp-properties='abc']", "selectorDomains=foo.com", "sele
ctor=[-abp-properties='abc']", "domains=foo.com"]); |
370 compareFilter(test, "foo.com#?#[-abp-properties='abc']", ["type=elemhideemulat
ion", "text=foo.com#?#[-abp-properties='abc']", "injectionDomain=foo.com", "code
=[-abp-properties='abc']", "domains=FOO.COM"]); | 395 compareFilter(test, "foo.com#?#[-abp-properties='abc']", ["type=elemhideemulat
ion", "text=foo.com#?#[-abp-properties='abc']", "selectorDomains=foo.com", "sele
ctor=[-abp-properties='abc']", "domains=foo.com"]); |
371 compareFilter(test, "foo.com##aaa [-abp-properties='abc'] bbb", ["type=elemhid
eemulation", "text=foo.com#?#aaa :-abp-properties(abc) bbb", "injectionDomain=fo
o.com", "code=aaa :-abp-properties(abc) bbb", "domains=FOO.COM"]); | 396 compareFilter(test, "foo.com##aaa [-abp-properties='abc'] bbb", ["type=elemhid
eemulation", "text=foo.com#?#aaa :-abp-properties(abc) bbb", "selectorDomains=fo
o.com", "selector=aaa :-abp-properties(abc) bbb", "domains=foo.com"]); |
372 | 397 |
373 // test matching -abp-properties= (https://issues.adblockplus.org/ticket/5037)
. | 398 // test matching -abp-properties= (https://issues.adblockplus.org/ticket/5037)
. |
374 compareFilter(test, "foo.com##[-abp-properties-bogus='abc']", ["type=elemhide"
, "text=foo.com##[-abp-properties-bogus='abc']", "injectionDomain=foo.com", "cod
e=[-abp-properties-bogus='abc']", "domains=FOO.COM"]); | 399 compareFilter(test, "foo.com##[-abp-properties-bogus='abc']", ["type=elemhide"
, "text=foo.com##[-abp-properties-bogus='abc']", "selectorDomains=foo.com", "sel
ector=[-abp-properties-bogus='abc']", "domains=foo.com"]); |
375 | 400 |
376 test.done(); | 401 test.done(); |
377 }; | 402 }; |
378 | 403 |
379 exports.testEmptyElemHideDomains = function(test) | 404 exports.testEmptyElemHideDomains = function(test) |
380 { | 405 { |
381 let emptyDomainFilters = [ | 406 let emptyDomainFilters = [ |
382 ",##selector", ",,,##selector", "~,foo.com##selector", "foo.com,##selector", | 407 ",##selector", ",,,##selector", "~,foo.com##selector", "foo.com,##selector", |
383 ",foo.com##selector", "foo.com,~##selector", | 408 ",foo.com##selector", "foo.com,~##selector", |
384 "foo.com,,bar.com##selector", "foo.com,~,bar.com##selector" | 409 "foo.com,,bar.com##selector", "foo.com,~,bar.com##selector" |
385 ]; | 410 ]; |
386 | 411 |
387 for (let filterText of emptyDomainFilters) | 412 for (let filterText of emptyDomainFilters) |
388 { | 413 { |
389 let filter = Filter.fromText(filterText); | 414 let filter = Filter.fromText(filterText); |
390 test.ok(filter instanceof InvalidFilter); | 415 test.ok(filter instanceof InvalidFilter); |
391 test.equal(filter.reason, "filter_invalid_domain"); | 416 test.equal(filter.reason, "filter_invalid_domain"); |
392 } | 417 } |
393 | 418 |
394 test.done(); | 419 test.done(); |
395 }; | 420 }; |
396 | 421 |
397 exports.testElemHideRulesWithBraces = function(test) | 422 exports.testElemHideRulesWithBraces = function(test) |
398 { | 423 { |
399 compareFilter( | 424 compareFilter( |
400 test, "###foo{color: red}", [ | 425 test, "###foo{color: red}", [ |
401 "type=elemhide", | 426 "type=elemhide", |
402 "text=###foo{color: red}", | 427 "text=###foo{color: red}", |
403 "injectionDomain=", | 428 "selectorDomains=", |
404 "code=#foo\\7B color: red\\7D ", | 429 "selector=#foo\\7B color: red\\7D ", |
405 "domains=" | 430 "domains=" |
406 ] | 431 ] |
407 ); | 432 ); |
408 compareFilter( | 433 compareFilter( |
409 test, "foo.com#?#:-abp-properties(/margin: [3-4]{2}/)", [ | 434 test, "foo.com#?#:-abp-properties(/margin: [3-4]{2}/)", [ |
410 "type=elemhideemulation", | 435 "type=elemhideemulation", |
411 "text=foo.com#?#:-abp-properties(/margin: [3-4]{2}/)", | 436 "text=foo.com#?#:-abp-properties(/margin: [3-4]{2}/)", |
412 "injectionDomain=foo.com", | 437 "selectorDomains=foo.com", |
413 "code=:-abp-properties(/margin: [3-4]\\7B 2\\7D /)", | 438 "selector=:-abp-properties(/margin: [3-4]\\7B 2\\7D /)", |
414 "domains=FOO.COM" | 439 "domains=foo.com" |
415 ] | 440 ] |
416 ); | 441 ); |
| 442 test.done(); |
| 443 }; |
| 444 |
| 445 exports.testSnippetFilters = function(test) |
| 446 { |
| 447 compareFilter(test, "foo.com#$#abc", ["type=snippet", "text=foo.com#$#abc", "s
criptDomains=foo.com", "script=abc", "domains=foo.com"]); |
| 448 compareFilter(test, "foo.com,~bar.com#$#abc", ["type=snippet", "text=foo.com,~
bar.com#$#abc", "scriptDomains=foo.com", "script=abc", "domains=foo.com|~bar.com
"]); |
| 449 compareFilter(test, "foo.com,~bar#$#abc", ["type=snippet", "text=foo.com,~bar#
$#abc", "scriptDomains=foo.com", "script=abc", "domains=foo.com|~bar"]); |
| 450 compareFilter(test, "~foo.com,bar.com#$#abc", ["type=snippet", "text=~foo.com,
bar.com#$#abc", "scriptDomains=bar.com", "script=abc", "domains=bar.com|~foo.com
"]); |
| 451 |
417 test.done(); | 452 test.done(); |
418 }; | 453 }; |
419 | 454 |
420 exports.testFilterNormalization = function(test) | 455 exports.testFilterNormalization = function(test) |
421 { | 456 { |
422 // Line breaks etc | 457 // Line breaks etc |
423 test.equal(Filter.normalize("\n\t\nad\ns"), | 458 test.equal(Filter.normalize("\n\t\nad\ns"), |
424 "ads"); | 459 "ads"); |
425 | 460 |
426 // Comment filters | 461 // Comment filters |
(...skipping 22 matching lines...) Expand all Loading... |
449 test.equal(Filter.normalize(" domain.c om#@# # sele ctor "), | 484 test.equal(Filter.normalize(" domain.c om#@# # sele ctor "), |
450 "domain.com#@## sele ctor"); | 485 "domain.com#@## sele ctor"); |
451 | 486 |
452 // Incorrect syntax: the separator "#@#" cannot contain spaces; treated as a | 487 // Incorrect syntax: the separator "#@#" cannot contain spaces; treated as a |
453 // regular filter instead (not an element hiding filter either!), because | 488 // regular filter instead (not an element hiding filter either!), because |
454 // unlike the case with "# ?##" the "##" following the "@" is not considered | 489 // unlike the case with "# ?##" the "##" following the "@" is not considered |
455 // to be a separator | 490 // to be a separator |
456 test.equal(Filter.normalize(" domain.c om# @## sele ctor "), | 491 test.equal(Filter.normalize(" domain.c om# @## sele ctor "), |
457 "domain.com#@##selector"); | 492 "domain.com#@##selector"); |
458 | 493 |
| 494 // Snippet filters |
| 495 test.equal(Filter.normalize(" domain.c om#$# sni pp et "), |
| 496 "domain.com#$#sni pp et"); |
| 497 |
459 // Regular filters | 498 // Regular filters |
460 let normalized = Filter.normalize( | 499 let normalized = Filter.normalize( |
461 " b$l a$sitekey= foo ,domain= do main.com |foo .com,c sp= c s p
" | 500 " b$l a$sitekey= foo ,domain= do main.com |foo .com,c sp= c s p
" |
462 ); | 501 ); |
463 test.equal( | 502 test.equal( |
464 normalized, | 503 normalized, |
465 "b$la$sitekey=foo,domain=domain.com|foo.com,csp=c s p" | 504 "b$la$sitekey=foo,domain=domain.com|foo.com,csp=c s p" |
466 ); | 505 ); |
467 compareFilter( | 506 compareFilter( |
468 test, normalized, [ | 507 test, normalized, [ |
469 "type=filterlist", | 508 "type=filterlist", |
470 "text=" + normalized, | 509 "text=" + normalized, |
471 "csp=c s p", | 510 "csp=c s p", |
472 "domains=DOMAIN.COM|FOO.COM", | 511 "domains=domain.com|foo.com", |
473 "sitekeys=FOO", | 512 "sitekeys=FOO", |
474 "regexp=b\\$la", | 513 "regexp=b\\$la", |
475 "contentType=" + t.CSP | 514 "contentType=" + t.CSP |
476 ] | 515 ] |
477 ); | 516 ); |
478 | 517 |
479 // Some $csp edge cases | 518 // Some $csp edge cases |
480 test.equal(Filter.normalize("$csp= c s p"), | 519 test.equal(Filter.normalize("$csp= c s p"), |
481 "$csp=c s p"); | 520 "$csp=c s p"); |
482 test.equal(Filter.normalize("$$csp= c s p"), | 521 test.equal(Filter.normalize("$$csp= c s p"), |
483 "$$csp=c s p"); | 522 "$$csp=c s p"); |
484 test.equal(Filter.normalize("$$$csp= c s p"), | 523 test.equal(Filter.normalize("$$$csp= c s p"), |
485 "$$$csp=c s p"); | 524 "$$$csp=c s p"); |
486 test.equal(Filter.normalize("foo?csp=b a r$csp=script-src 'self'"), | 525 test.equal(Filter.normalize("foo?csp=b a r$csp=script-src 'self'"), |
487 "foo?csp=bar$csp=script-src 'self'"); | 526 "foo?csp=bar$csp=script-src 'self'"); |
488 test.equal(Filter.normalize("foo$bar=c s p = ba z,cs p = script-src 'self'"), | 527 test.equal(Filter.normalize("foo$bar=c s p = ba z,cs p = script-src 'self'"), |
489 "foo$bar=csp=baz,csp=script-src 'self'"); | 528 "foo$bar=csp=baz,csp=script-src 'self'"); |
490 test.equal(Filter.normalize("foo$csp=c s p csp= ba z,cs p = script-src 'self
'"), | 529 test.equal(Filter.normalize("foo$csp=c s p csp= ba z,cs p = script-src 'self
'"), |
491 "foo$csp=c s p csp= ba z,csp=script-src 'self'"); | 530 "foo$csp=c s p csp= ba z,csp=script-src 'self'"); |
492 test.equal(Filter.normalize("foo$csp=bar,$c sp=c s p"), | 531 test.equal(Filter.normalize("foo$csp=bar,$c sp=c s p"), |
493 "foo$csp=bar,$csp=c s p"); | 532 "foo$csp=bar,$csp=c s p"); |
494 test.equal(Filter.normalize(" f o o $ bar $csp=ba r"), | 533 test.equal(Filter.normalize(" f o o $ bar $csp=ba r"), |
495 "foo$bar$csp=ba r"); | 534 "foo$bar$csp=ba r"); |
496 test.equal(Filter.normalize("f $ o $ o $ csp=f o o "), | 535 test.equal(Filter.normalize("f $ o $ o $ csp=f o o "), |
497 "f$o$o$csp=f o o"); | 536 "f$o$o$csp=f o o"); |
498 test.equal(Filter.normalize("/foo$/$ csp = script-src http://example.com/?$1=
1&$2=2&$3=3"), | 537 test.equal(Filter.normalize("/foo$/$ csp = script-src http://example.com/?$1=
1&$2=2&$3=3"), |
499 "/foo$/$csp=script-src http://example.com/?$1=1&$2=2&$3=3"); | 538 "/foo$/$csp=script-src http://example.com/?$1=1&$2=2&$3=3"); |
500 | 539 test.equal(Filter.normalize("||content.server.com/files/*.php$rewrite= $1"), |
501 test.done(); | 540 "||content.server.com/files/*.php$rewrite=$1"); |
502 }; | 541 test.done(); |
| 542 }; |
| 543 |
| 544 |
| 545 exports.testFilterRewriteOption = function(test) |
| 546 { |
| 547 let text = "/(content\\.server\\/file\\/.*\\.txt)\\?.*$/$rewrite=$1"; |
| 548 |
| 549 let filter = Filter.fromText(text); |
| 550 |
| 551 test.equal(filter.rewrite, "$1"); |
| 552 // no rewrite occured: didn't match. |
| 553 test.equal(filter.rewriteUrl("foo"), "foo"); |
| 554 // rewrite occured: matched. |
| 555 test.equal(filter.rewriteUrl("http://content.server/file/foo.txt?bar"), |
| 556 "http://content.server/file/foo.txt"); |
| 557 |
| 558 // checking for same origin. |
| 559 let rewriteDiffOrigin = |
| 560 "/content\\.server(\\/file\\/.*\\.txt)\\?.*$/$rewrite=foo.com$1"; |
| 561 let filterDiffOrigin = Filter.fromText(rewriteDiffOrigin); |
| 562 |
| 563 // no rewrite occured because of a different origin. |
| 564 test.equal( |
| 565 filterDiffOrigin.rewriteUrl("http://content.server/file/foo.txt?bar"), |
| 566 "http://content.server/file/foo.txt?bar" |
| 567 ); |
| 568 |
| 569 // relative path. |
| 570 let rewriteRelative = "/(\\/file\\/.*\\.txt)\\?.*$/$rewrite=$1/disable"; |
| 571 let filterRelative = Filter.fromText(rewriteRelative); |
| 572 |
| 573 test.equal( |
| 574 filterRelative.rewriteUrl("http://content.server/file/foo.txt?bar"), |
| 575 "http://content.server/file/foo.txt/disable" |
| 576 ); |
| 577 test.equal( |
| 578 filterRelative.rewriteUrl("http://example.com/file/foo.txt?bar"), |
| 579 "http://example.com/file/foo.txt/disable" |
| 580 ); |
| 581 |
| 582 // Example from https://github.com/uBlockOrigin/uBlock-issues/issues/46#issuec
omment-391190533 |
| 583 // The rewrite shouldn't happen. |
| 584 let rewriteEvil = "/(^https?:\\/\\/[^/])/$script,rewrite=$1.evil.com"; |
| 585 let filterEvil = Filter.fromText(rewriteEvil); |
| 586 |
| 587 test.equal( |
| 588 filterEvil.rewriteUrl("https://www.adblockplus.org/script.js"), |
| 589 "https://www.adblockplus.org/script.js" |
| 590 ); |
| 591 |
| 592 test.done(); |
| 593 }; |
LEFT | RIGHT |