| 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 |
| 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 assert = require("assert"); |
| 20 const {createSandbox} = require("./_common"); | 21 const {createSandbox} = require("./_common"); |
| 21 | 22 |
| 22 let Filter = null; | 23 let Filter = null; |
| 23 let RegExpFilter = null; | 24 let RegExpFilter = null; |
| 24 let CombinedMatcher = null; | 25 let CombinedMatcher = null; |
| 25 let defaultMatcher = null; | 26 let defaultMatcher = null; |
| 26 let Matcher = null; | 27 let Matcher = null; |
| 27 | 28 |
| 28 exports.setUp = function(callback) | 29 describe("Matcher", () => |
| 29 { | 30 { |
| 30 let sandboxedRequire = createSandbox(); | 31 beforeEach(() => |
| 31 ( | 32 { |
| 32 {Filter, RegExpFilter} = sandboxedRequire("../lib/filterClasses"), | 33 let sandboxedRequire = createSandbox(); |
| 33 {CombinedMatcher, defaultMatcher, Matcher} = sandboxedRequire("../lib/matche
r") | 34 ( |
| 34 ); | 35 {Filter, RegExpFilter} = sandboxedRequire("../lib/filterClasses"), |
| 35 | 36 {CombinedMatcher, defaultMatcher, Matcher} = sandboxedRequire("../lib/matc
her") |
| 36 callback(); | 37 ); |
| 37 }; | 38 }); |
| 38 | 39 |
| 39 function compareKeywords(test, text, expected) | 40 function compareKeywords(text, expected) |
| 40 { | 41 { |
| 41 for (let filter of [Filter.fromText(text), Filter.fromText("@@" + text)]) | 42 for (let filter of [Filter.fromText(text), Filter.fromText("@@" + text)]) |
| 43 { |
| 44 let matcher = new Matcher(); |
| 45 let result = []; |
| 46 for (let i = 0; i < expected.length; i++) |
| 47 { |
| 48 let keyword = matcher.findKeyword(filter); |
| 49 result.push(keyword); |
| 50 if (keyword) |
| 51 { |
| 52 let dummyFilter = Filter.fromText("^" + keyword + "^"); |
| 53 dummyFilter.filterCount = Infinity; |
| 54 matcher.add(dummyFilter); |
| 55 } |
| 56 } |
| 57 |
| 58 assert.equal(result.join(", "), expected.join(", "), "Keyword candidates f
or " + filter.text); |
| 59 } |
| 60 } |
| 61 |
| 62 function checkMatch(filters, location, contentType, docDomain, thirdParty, sit
ekey, specificOnly, expected, expectedFirstMatch = expected) |
| 42 { | 63 { |
| 43 let matcher = new Matcher(); | 64 let matcher = new Matcher(); |
| 44 let result = []; | |
| 45 for (let i = 0; i < expected.length; i++) | |
| 46 { | |
| 47 let keyword = matcher.findKeyword(filter); | |
| 48 result.push(keyword); | |
| 49 if (keyword) | |
| 50 { | |
| 51 let dummyFilter = Filter.fromText("^" + keyword + "^"); | |
| 52 dummyFilter.filterCount = Infinity; | |
| 53 matcher.add(dummyFilter); | |
| 54 } | |
| 55 } | |
| 56 | |
| 57 test.equal(result.join(", "), expected.join(", "), "Keyword candidates for "
+ filter.text); | |
| 58 } | |
| 59 } | |
| 60 | |
| 61 function checkMatch(test, filters, location, contentType, docDomain, thirdParty,
sitekey, specificOnly, expected, expectedFirstMatch = expected) | |
| 62 { | |
| 63 let matcher = new Matcher(); | |
| 64 for (let filter of filters) | |
| 65 matcher.add(Filter.fromText(filter)); | |
| 66 | |
| 67 let result = matcher.matchesAny(location, RegExpFilter.typeMap[contentType], d
ocDomain, thirdParty, sitekey, specificOnly); | |
| 68 if (result) | |
| 69 result = result.text; | |
| 70 | |
| 71 test.equal(result, expectedFirstMatch, "match(" + location + ", " + contentTyp
e + ", " + docDomain + ", " + (thirdParty ? "third-party" : "first-party") + ",
" + (sitekey || "no-sitekey") + ", " + (specificOnly ? "specificOnly" : "not-spe
cificOnly") + ") with:\n" + filters.join("\n")); | |
| 72 | |
| 73 let combinedMatcher = new CombinedMatcher(); | |
| 74 for (let i = 0; i < 2; i++) | |
| 75 { | |
| 76 for (let filter of filters) | 65 for (let filter of filters) |
| 77 combinedMatcher.add(Filter.fromText(filter)); | 66 matcher.add(Filter.fromText(filter)); |
| 78 | 67 |
| 79 result = combinedMatcher.matchesAny(location, RegExpFilter.typeMap[contentTy
pe], docDomain, thirdParty, sitekey, specificOnly); | 68 let result = matcher.matchesAny(location, RegExpFilter.typeMap[contentType],
docDomain, thirdParty, sitekey, specificOnly); |
| 80 if (result) | 69 if (result) |
| 81 result = result.text; | 70 result = result.text; |
| 82 | 71 |
| 83 test.equal(result, expected, "combinedMatch(" + location + ", " + contentTyp
e + ", " + docDomain + ", " + (thirdParty ? "third-party" : "first-party") + ",
" + (sitekey || "no-sitekey") + ", " + (specificOnly ? "specificOnly" : "not-spe
cificOnly") + ") with:\n" + filters.join("\n")); | 72 assert.equal(result, expectedFirstMatch, "match(" + location + ", " + conten
tType + ", " + docDomain + ", " + (thirdParty ? "third-party" : "first-party") +
", " + (sitekey || "no-sitekey") + ", " + (specificOnly ? "specificOnly" : "not
-specificOnly") + ") with:\n" + filters.join("\n")); |
| 84 | 73 |
| 85 // Generic whitelisting rules can match for specificOnly searches, so we | 74 let combinedMatcher = new CombinedMatcher(); |
| 86 // can't easily know which rule will match for these whitelisting tests | 75 for (let i = 0; i < 2; i++) |
| 87 if (specificOnly) | 76 { |
| 88 continue; | 77 for (let filter of filters) |
| 89 | 78 combinedMatcher.add(Filter.fromText(filter)); |
| 90 // For next run: add whitelisting filters for filters that aren't already | 79 |
| 91 filters = filters.map(text => text.substring(0, 2) == "@@" ? text : "@@" + t
ext); | 80 result = combinedMatcher.matchesAny(location, RegExpFilter.typeMap[content
Type], docDomain, thirdParty, sitekey, specificOnly); |
| 92 if (expected && expected.substring(0, 2) != "@@") | 81 if (result) |
| 93 expected = "@@" + expected; | 82 result = result.text; |
| 83 |
| 84 assert.equal(result, expected, "combinedMatch(" + location + ", " + conten
tType + ", " + docDomain + ", " + (thirdParty ? "third-party" : "first-party") +
", " + (sitekey || "no-sitekey") + ", " + (specificOnly ? "specificOnly" : "not
-specificOnly") + ") with:\n" + filters.join("\n")); |
| 85 |
| 86 // Generic whitelisting rules can match for specificOnly searches, so we |
| 87 // can't easily know which rule will match for these whitelisting tests |
| 88 if (specificOnly) |
| 89 continue; |
| 90 |
| 91 // For next run: add whitelisting filters for filters that aren't already |
| 92 filters = filters.map(text => text.substring(0, 2) == "@@" ? text : "@@" +
text); |
| 93 if (expected && expected.substring(0, 2) != "@@") |
| 94 expected = "@@" + expected; |
| 95 } |
| 94 } | 96 } |
| 95 } | 97 |
| 96 | 98 function checkSearch(filters, location, contentType, docDomain, |
| 97 function checkSearch(test, filters, location, contentType, docDomain, | 99 thirdParty, sitekey, specificOnly, filterType, |
| 98 thirdParty, sitekey, specificOnly, filterType, | 100 expected) |
| 99 expected) | 101 { |
| 100 { | 102 let matcher = new CombinedMatcher(); |
| 101 let matcher = new CombinedMatcher(); | 103 for (let filter of filters) |
| 102 for (let filter of filters) | 104 matcher.add(Filter.fromText(filter)); |
| 103 matcher.add(Filter.fromText(filter)); | 105 |
| 104 | 106 let result = matcher.search(location, RegExpFilter.typeMap[contentType], |
| 105 let result = matcher.search(location, RegExpFilter.typeMap[contentType], | 107 docDomain, thirdParty, sitekey, specificOnly, |
| 106 docDomain, thirdParty, sitekey, specificOnly, | 108 filterType); |
| 107 filterType); | 109 for (let key in result) |
| 108 for (let key in result) | 110 result[key] = result[key].map(filter => filter.text); |
| 109 result[key] = result[key].map(filter => filter.text); | 111 |
| 110 | 112 assert.deepEqual(result, expected, "search(" + location + ", " + |
| 111 test.deepEqual(result, expected, "search(" + location + ", " + | 113 contentType + ", " + docDomain + ", " + |
| 112 contentType + ", " + docDomain + ", " + | 114 (thirdParty ? "third-party" : "first-party") + ", " + |
| 113 (thirdParty ? "third-party" : "first-party") + ", " + | 115 (sitekey || "no-sitekey") + ", " + |
| 114 (sitekey || "no-sitekey") + ", " + | 116 (specificOnly ? "specificOnly" : "not-specificOnly") + ", " + |
| 115 (specificOnly ? "specificOnly" : "not-specificOnly") + ", " + | 117 filterType + ") with:\n" + filters.join("\n")); |
| 116 filterType + ") with:\n" + filters.join("\n")); | |
| 117 } | |
| 118 | |
| 119 function cacheCheck(test, matcher, location, contentType, docDomain, thirdParty,
expected) | |
| 120 { | |
| 121 let result = matcher.matchesAny(location, RegExpFilter.typeMap[contentType], d
ocDomain, thirdParty); | |
| 122 if (result) | |
| 123 result = result.text; | |
| 124 | |
| 125 test.equal(result, expected, "match(" + location + ", " + contentType + ", " +
docDomain + ", " + (thirdParty ? "third-party" : "first-party") + ") with stati
c filters"); | |
| 126 } | |
| 127 | |
| 128 exports.testMatcherClassDefinitions = function(test) | |
| 129 { | |
| 130 test.equal(typeof Matcher, "function", "typeof Matcher"); | |
| 131 test.equal(typeof CombinedMatcher, "function", "typeof CombinedMatcher"); | |
| 132 test.equal(typeof defaultMatcher, "object", "typeof defaultMatcher"); | |
| 133 test.ok(defaultMatcher instanceof CombinedMatcher, "defaultMatcher is a Combin
edMatcher instance"); | |
| 134 | |
| 135 test.done(); | |
| 136 }; | |
| 137 | |
| 138 exports.testKeywordExtraction = function(test) | |
| 139 { | |
| 140 compareKeywords(test, "*", []); | |
| 141 compareKeywords(test, "asdf", []); | |
| 142 compareKeywords(test, "/asdf/", []); | |
| 143 compareKeywords(test, "/asdf1234", []); | |
| 144 compareKeywords(test, "/asdf/1234", ["asdf"]); | |
| 145 compareKeywords(test, "/asdf/1234^", ["asdf", "1234"]); | |
| 146 compareKeywords(test, "/asdf/123456^", ["123456", "asdf"]); | |
| 147 compareKeywords(test, "^asdf^1234^56as^", ["asdf", "1234", "56as"]); | |
| 148 compareKeywords(test, "*asdf/1234^", ["1234"]); | |
| 149 compareKeywords(test, "|asdf,1234*", ["asdf"]); | |
| 150 compareKeywords(test, "||domain.example^", ["example", "domain"]); | |
| 151 compareKeywords(test, "&asdf=1234|", ["asdf", "1234"]); | |
| 152 compareKeywords(test, "^foo%2Ebar^", ["foo%2ebar"]); | |
| 153 compareKeywords(test, "^aSdF^1234", ["asdf"]); | |
| 154 compareKeywords(test, "_asdf_1234_", ["asdf", "1234"]); | |
| 155 compareKeywords(test, "+asdf-1234=", ["asdf", "1234"]); | |
| 156 compareKeywords(test, "/123^ad2&ad&", ["123", "ad2"]); | |
| 157 compareKeywords(test, "/123^ad2&ad$script,domain=example.com", ["123", "ad2"])
; | |
| 158 | |
| 159 test.done(); | |
| 160 }; | |
| 161 | |
| 162 exports.testFilterMatching = function(test) | |
| 163 { | |
| 164 checkMatch(test, [], "http://abc/def", "IMAGE", null, false, null, false, null
); | |
| 165 checkMatch(test, ["abc"], "http://abc/def", "IMAGE", null, false, null, false,
"abc"); | |
| 166 checkMatch(test, ["abc", "ddd"], "http://abc/def", "IMAGE", null, false, null,
false, "abc"); | |
| 167 checkMatch(test, ["ddd", "abc"], "http://abc/def", "IMAGE", null, false, null,
false, "abc"); | |
| 168 checkMatch(test, ["ddd", "abd"], "http://abc/def", "IMAGE", null, false, null,
false, null); | |
| 169 checkMatch(test, ["abc", "://abc/d"], "http://abc/def", "IMAGE", null, false,
null, false, "://abc/d"); | |
| 170 checkMatch(test, ["://abc/d", "abc"], "http://abc/def", "IMAGE", null, false,
null, false, "://abc/d"); | |
| 171 checkMatch(test, ["|http://"], "http://abc/def", "IMAGE", null, false, null, f
alse, "|http://"); | |
| 172 checkMatch(test, ["|http://abc"], "http://abc/def", "IMAGE", null, false, null
, false, "|http://abc"); | |
| 173 checkMatch(test, ["|abc"], "http://abc/def", "IMAGE", null, false, null, false
, null); | |
| 174 checkMatch(test, ["|/abc/def"], "http://abc/def", "IMAGE", null, false, null,
false, null); | |
| 175 checkMatch(test, ["/def|"], "http://abc/def", "IMAGE", null, false, null, fals
e, "/def|"); | |
| 176 checkMatch(test, ["/abc/def|"], "http://abc/def", "IMAGE", null, false, null,
false, "/abc/def|"); | |
| 177 checkMatch(test, ["/abc/|"], "http://abc/def", "IMAGE", null, false, null, fal
se, null); | |
| 178 checkMatch(test, ["http://abc/|"], "http://abc/def", "IMAGE", null, false, nul
l, false, null); | |
| 179 checkMatch(test, ["|http://abc/def|"], "http://abc/def", "IMAGE", null, false,
null, false, "|http://abc/def|"); | |
| 180 checkMatch(test, ["|/abc/def|"], "http://abc/def", "IMAGE", null, false, null,
false, null); | |
| 181 checkMatch(test, ["|http://abc/|"], "http://abc/def", "IMAGE", null, false, nu
ll, false, null); | |
| 182 checkMatch(test, ["|/abc/|"], "http://abc/def", "IMAGE", null, false, null, fa
lse, null); | |
| 183 checkMatch(test, ["||example.com/abc"], "http://example.com/abc/def", "IMAGE",
null, false, null, false, "||example.com/abc"); | |
| 184 checkMatch(test, ["||com/abc/def"], "http://example.com/abc/def", "IMAGE", nul
l, false, null, false, "||com/abc/def"); | |
| 185 checkMatch(test, ["||com/abc"], "http://example.com/abc/def", "IMAGE", null, f
alse, null, false, "||com/abc"); | |
| 186 checkMatch(test, ["||mple.com/abc"], "http://example.com/abc/def", "IMAGE", nu
ll, false, null, false, null); | |
| 187 checkMatch(test, ["||.com/abc/def"], "http://example.com/abc/def", "IMAGE", nu
ll, false, null, false, null); | |
| 188 checkMatch(test, ["||http://example.com/"], "http://example.com/abc/def", "IMA
GE", null, false, null, false, null); | |
| 189 checkMatch(test, ["||example.com/abc/def|"], "http://example.com/abc/def", "IM
AGE", null, false, null, false, "||example.com/abc/def|"); | |
| 190 checkMatch(test, ["||com/abc/def|"], "http://example.com/abc/def", "IMAGE", nu
ll, false, null, false, "||com/abc/def|"); | |
| 191 checkMatch(test, ["||example.com/abc|"], "http://example.com/abc/def", "IMAGE"
, null, false, null, false, null); | |
| 192 checkMatch(test, ["abc", "://abc/d", "asdf1234"], "http://abc/def", "IMAGE", n
ull, false, null, false, "://abc/d"); | |
| 193 checkMatch(test, ["foo*://abc/d", "foo*//abc/de", "://abc/de", "asdf1234"], "h
ttp://abc/def", "IMAGE", null, false, null, false, "://abc/de"); | |
| 194 checkMatch(test, ["abc$third-party", "abc$~third-party", "ddd"], "http://abc/d
ef", "IMAGE", null, false, null, false, "abc$~third-party"); | |
| 195 checkMatch(test, ["abc$third-party", "abc$~third-party", "ddd"], "http://abc/d
ef", "IMAGE", null, true, null, false, "abc$third-party"); | |
| 196 checkMatch(test, ["//abc/def$third-party", "//abc/def$~third-party", "//abc_de
f"], "http://abc/def", "IMAGE", null, false, null, false, "//abc/def$~third-part
y"); | |
| 197 checkMatch(test, ["//abc/def$third-party", "//abc/def$~third-party", "//abc_de
f"], "http://abc/def", "IMAGE", null, true, null, false, "//abc/def$third-party"
); | |
| 198 checkMatch(test, ["abc$third-party", "abc$~third-party", "//abc/def"], "http:/
/abc/def", "IMAGE", null, true, null, false, "//abc/def"); | |
| 199 checkMatch(test, ["//abc/def", "abc$third-party", "abc$~third-party"], "http:/
/abc/def", "IMAGE", null, true, null, false, "//abc/def"); | |
| 200 checkMatch(test, ["abc$third-party", "abc$~third-party", "//abc/def$third-part
y"], "http://abc/def", "IMAGE", null, true, null, false, "//abc/def$third-party"
); | |
| 201 checkMatch(test, ["abc$third-party", "abc$~third-party", "//abc/def$third-part
y"], "http://abc/def", "IMAGE", null, false, null, false, "abc$~third-party"); | |
| 202 checkMatch(test, ["abc$third-party", "abc$~third-party", "//abc/def$~third-par
ty"], "http://abc/def", "IMAGE", null, true, null, false, "abc$third-party"); | |
| 203 checkMatch(test, ["abc$image", "abc$script", "abc$~image"], "http://abc/def",
"IMAGE", null, false, null, false, "abc$image"); | |
| 204 checkMatch(test, ["abc$image", "abc$script", "abc$~script"], "http://abc/def",
"SCRIPT", null, false, null, false, "abc$script"); | |
| 205 checkMatch(test, ["abc$image", "abc$script", "abc$~image"], "http://abc/def",
"OTHER", null, false, null, false, "abc$~image"); | |
| 206 checkMatch(test, ["//abc/def$image", "//abc/def$script", "//abc/def$~image"],
"http://abc/def", "IMAGE", null, false, null, false, "//abc/def$image"); | |
| 207 checkMatch(test, ["//abc/def$image", "//abc/def$script", "//abc/def$~script"],
"http://abc/def", "SCRIPT", null, false, null, false, "//abc/def$script"); | |
| 208 checkMatch(test, ["//abc/def$image", "//abc/def$script", "//abc/def$~image"],
"http://abc/def", "OTHER", null, false, null, false, "//abc/def$~image"); | |
| 209 checkMatch(test, ["abc$image", "abc$~image", "//abc/def"], "http://abc/def", "
IMAGE", null, false, null, false, "//abc/def"); | |
| 210 checkMatch(test, ["//abc/def", "abc$image", "abc$~image"], "http://abc/def", "
IMAGE", null, false, null, false, "//abc/def"); | |
| 211 checkMatch(test, ["abc$image", "abc$~image", "//abc/def$image"], "http://abc/d
ef", "IMAGE", null, false, null, false, "//abc/def$image"); | |
| 212 checkMatch(test, ["abc$image", "abc$~image", "//abc/def$script"], "http://abc/
def", "IMAGE", null, false, null, false, "abc$image"); | |
| 213 checkMatch(test, ["abc$domain=foo.com", "abc$domain=bar.com", "abc$domain=~foo
.com|~bar.com"], "http://abc/def", "IMAGE", "foo.com", false, null, false, "abc$
domain=foo.com"); | |
| 214 checkMatch(test, ["abc$domain=foo.com", "abc$domain=bar.com", "abc$domain=~foo
.com|~bar.com"], "http://abc/def", "IMAGE", "bar.com", false, null, false, "abc$
domain=bar.com"); | |
| 215 checkMatch(test, ["abc$domain=foo.com", "abc$domain=bar.com", "abc$domain=~foo
.com|~bar.com"], "http://abc/def", "IMAGE", "baz.com", false, null, false, "abc$
domain=~foo.com|~bar.com"); | |
| 216 checkMatch(test, ["abc$domain=foo.com", "cba$domain=bar.com", "ccc$domain=~foo
.com|~bar.com"], "http://abc/def", "IMAGE", "foo.com", false, null, false, "abc$
domain=foo.com"); | |
| 217 checkMatch(test, ["abc$domain=foo.com", "cba$domain=bar.com", "ccc$domain=~foo
.com|~bar.com"], "http://abc/def", "IMAGE", "bar.com", false, null, false, null)
; | |
| 218 checkMatch(test, ["abc$domain=foo.com", "cba$domain=bar.com", "ccc$domain=~foo
.com|~bar.com"], "http://abc/def", "IMAGE", "baz.com", false, null, false, null)
; | |
| 219 checkMatch(test, ["abc$domain=foo.com", "cba$domain=bar.com", "ccc$domain=~foo
.com|~bar.com"], "http://ccc/def", "IMAGE", "baz.com", false, null, false, "ccc$
domain=~foo.com|~bar.com"); | |
| 220 checkMatch(test, ["abc$sitekey=foo-publickey", "abc$sitekey=bar-publickey"], "
http://abc/def", "IMAGE", "foo.com", false, "foo-publickey", false, "abc$sitekey
=foo-publickey"); | |
| 221 checkMatch(test, ["abc$sitekey=foo-publickey", "abc$sitekey=bar-publickey"], "
http://abc/def", "IMAGE", "bar.com", false, "bar-publickey", false, "abc$sitekey
=bar-publickey"); | |
| 222 checkMatch(test, ["abc$sitekey=foo-publickey", "cba$sitekey=bar-publickey"], "
http://abc/def", "IMAGE", "bar.com", false, "bar-publickey", false, null); | |
| 223 checkMatch(test, ["abc$sitekey=foo-publickey", "cba$sitekey=bar-publickey"], "
http://abc/def", "IMAGE", "baz.com", false, null, false, null); | |
| 224 checkMatch(test, ["abc$sitekey=foo-publickey,domain=foo.com", "abc$sitekey=bar
-publickey,domain=bar.com"], "http://abc/def", "IMAGE", "foo.com", false, "foo-p
ublickey", false, "abc$sitekey=foo-publickey,domain=foo.com"); | |
| 225 checkMatch(test, ["abc$sitekey=foo-publickey,domain=foo.com", "abc$sitekey=bar
-publickey,domain=bar.com"], "http://abc/def", "IMAGE", "foo.com", false, "bar-p
ublickey", false, null); | |
| 226 checkMatch(test, ["abc$sitekey=foo-publickey,domain=foo.com", "abc$sitekey=bar
-publickey,domain=bar.com"], "http://abc/def", "IMAGE", "bar.com", false, "foo-p
ublickey", false, null); | |
| 227 checkMatch(test, ["abc$sitekey=foo-publickey,domain=foo.com", "abc$sitekey=bar
-publickey,domain=bar.com"], "http://abc/def", "IMAGE", "bar.com", false, "bar-p
ublickey", false, "abc$sitekey=bar-publickey,domain=bar.com"); | |
| 228 checkMatch(test, ["@@foo.com$document"], "http://foo.com/bar", "DOCUMENT", "fo
o.com", false, null, false, "@@foo.com$document"); | |
| 229 checkMatch(test, ["@@foo.com$elemhide"], "http://foo.com/bar", "ELEMHIDE", "fo
o.com", false, null, false, "@@foo.com$elemhide"); | |
| 230 checkMatch(test, ["@@foo.com$generichide"], "http://foo.com/bar", "GENERICHIDE
", "foo.com", false, null, false, "@@foo.com$generichide"); | |
| 231 checkMatch(test, ["@@foo.com$genericblock"], "http://foo.com/bar", "GENERICBLO
CK", "foo.com", false, null, false, "@@foo.com$genericblock"); | |
| 232 checkMatch(test, ["@@bar.com$document"], "http://foo.com/bar", "DOCUMENT", "fo
o.com", false, null, false, null); | |
| 233 checkMatch(test, ["@@bar.com$elemhide"], "http://foo.com/bar", "ELEMHIDE", "fo
o.com", false, null, false, null); | |
| 234 checkMatch(test, ["@@bar.com$generichide"], "http://foo.com/bar", "GENERICHIDE
", "foo.com", false, null, false, null); | |
| 235 checkMatch(test, ["@@bar.com$genericblock"], "http://foo.com/bar", "GENERICBLO
CK", "foo.com", false, null, false, null); | |
| 236 checkMatch(test, ["/bar"], "http://foo.com/bar", "IMAGE", "foo.com", false, nu
ll, true, null); | |
| 237 checkMatch(test, ["/bar$domain=foo.com"], "http://foo.com/bar", "IMAGE", "foo.
com", false, null, true, "/bar$domain=foo.com"); | |
| 238 checkMatch(test, ["@@||foo.com^"], "http://foo.com/bar", "IMAGE", "foo.com", f
alse, null, false, null, "@@||foo.com^"); | |
| 239 checkMatch(test, ["/bar", "@@||foo.com^"], "http://foo.com/bar", "IMAGE", "foo
.com", false, null, false, "@@||foo.com^"); | |
| 240 checkMatch(test, ["/bar", "@@||foo.com^"], "http://foo.com/foo", "IMAGE", "foo
.com", false, null, false, null, "@@||foo.com^"); | |
| 241 checkMatch(test, ["||foo.com^$popup"], "http://foo.com/bar", "POPUP", "foo.com
", false, null, false, "||foo.com^$popup"); | |
| 242 checkMatch(test, ["@@||foo.com^$popup"], "http://foo.com/bar", "POPUP", "foo.c
om", false, null, false, null, "@@||foo.com^$popup"); | |
| 243 checkMatch(test, ["||foo.com^$popup", "@@||foo.com^$popup"], "http://foo.com/b
ar", "POPUP", "foo.com", false, null, false, "@@||foo.com^$popup", "||foo.com^$p
opup"); | |
| 244 checkMatch(test, ["||foo.com^$csp=script-src 'none'"], "http://foo.com/bar", "
CSP", "foo.com", false, null, false, "||foo.com^$csp=script-src 'none'"); | |
| 245 checkMatch(test, ["@@||foo.com^$csp"], "http://foo.com/bar", "CSP", "foo.com",
false, null, false, null, "@@||foo.com^$csp"); | |
| 246 checkMatch(test, ["||foo.com^$csp=script-src 'none'", "@@||foo.com^$csp"], "ht
tp://foo.com/bar", "CSP", "foo.com", false, null, false, "@@||foo.com^$csp", "||
foo.com^$csp=script-src 'none'"); | |
| 247 | |
| 248 // See #7312. | |
| 249 checkMatch(test, ["^foo/bar/$script"], "http://foo/bar/", "SCRIPT", "example.c
om", true, null, true, null); | |
| 250 checkMatch(test, ["^foo/bar/$script"], "http://foo/bar/", "SCRIPT", "example.c
om", true, null, false, "^foo/bar/$script"); | |
| 251 checkMatch(test, ["^foo/bar/$script,domain=example.com", "@@^foo/bar/$script"]
, "http://foo/bar/", "SCRIPT", "example.com", true, null, true, "@@^foo/bar/$scr
ipt", "^foo/bar/$script,domain=example.com"); | |
| 252 checkMatch(test, ["@@^foo/bar/$script", "^foo/bar/$script,domain=example.com"]
, "http://foo/bar/", "SCRIPT", "example.com", true, null, true, "@@^foo/bar/$scr
ipt", "^foo/bar/$script,domain=example.com"); | |
| 253 checkMatch(test, ["@@^foo/bar/$script", "^foo/bar/$script,domain=example.com"]
, "http://foo/bar/", "SCRIPT", "example.com", true, null, false, "@@^foo/bar/$sc
ript"); | |
| 254 | |
| 255 test.done(); | |
| 256 }; | |
| 257 | |
| 258 exports.testFilterSearch = function(test) | |
| 259 { | |
| 260 // Start with three filters: foo, bar, and @@foo | |
| 261 let filters = ["foo", "bar", "@@foo"]; | |
| 262 | |
| 263 checkSearch(test, filters, "http://example.com/foo", "IMAGE", "example.com", | |
| 264 false, null, false, "all", | |
| 265 {blocking: ["foo"], whitelist: ["@@foo"]}); | |
| 266 | |
| 267 // Blocking only. | |
| 268 checkSearch(test, filters, "http://example.com/foo", "IMAGE", "example.com", | |
| 269 false, null, false, "blocking", {blocking: ["foo"]}); | |
| 270 | |
| 271 // Whitelist only. | |
| 272 checkSearch(test, filters, "http://example.com/foo", "IMAGE", "example.com", | |
| 273 false, null, false, "whitelist", {whitelist: ["@@foo"]}); | |
| 274 | |
| 275 // Different URLs. | |
| 276 checkSearch(test, filters, "http://example.com/bar", "IMAGE", "example.com", | |
| 277 false, null, false, "all", {blocking: ["bar"], whitelist: []}); | |
| 278 checkSearch(test, filters, "http://example.com/foo/bar", "IMAGE", | |
| 279 "example.com", false, null, false, "all", | |
| 280 {blocking: ["foo", "bar"], whitelist: ["@@foo"]}); | |
| 281 | |
| 282 // Non-matching content type. | |
| 283 checkSearch(test, filters, "http://example.com/foo", "CSP", "example.com", | |
| 284 false, null, false, "all", {blocking: [], whitelist: []}); | |
| 285 | |
| 286 // Non-matching specificity. | |
| 287 checkSearch(test, filters, "http://example.com/foo", "IMAGE", "example.com", | |
| 288 false, null, true, "all", {blocking: [], whitelist: ["@@foo"]}); | |
| 289 | |
| 290 test.done(); | |
| 291 }; | |
| 292 | |
| 293 exports.testResultCacheChecks = function(test) | |
| 294 { | |
| 295 let matcher = new CombinedMatcher(); | |
| 296 matcher.add(Filter.fromText("abc$image")); | |
| 297 matcher.add(Filter.fromText("abc$script")); | |
| 298 matcher.add(Filter.fromText("abc$~image,~script,~media,~ping")); | |
| 299 matcher.add(Filter.fromText("cba$third-party")); | |
| 300 matcher.add(Filter.fromText("cba$~third-party,~script")); | |
| 301 matcher.add(Filter.fromText("http://def$image")); | |
| 302 matcher.add(Filter.fromText("http://def$script")); | |
| 303 matcher.add(Filter.fromText("http://def$~image,~script,~media,~ping")); | |
| 304 matcher.add(Filter.fromText("http://fed$third-party")); | |
| 305 matcher.add(Filter.fromText("http://fed$~third-party,~script")); | |
| 306 | |
| 307 cacheCheck(test, matcher, "http://abc", "IMAGE", null, false, "abc$image"); | |
| 308 cacheCheck(test, matcher, "http://abc", "SCRIPT", null, false, "abc$script"); | |
| 309 cacheCheck(test, matcher, "http://abc", "OTHER", null, false, "abc$~image,~scr
ipt,~media,~ping"); | |
| 310 cacheCheck(test, matcher, "http://cba", "IMAGE", null, false, "cba$~third-part
y,~script"); | |
| 311 cacheCheck(test, matcher, "http://cba", "IMAGE", null, true, "cba$third-party"
); | |
| 312 cacheCheck(test, matcher, "http://def", "IMAGE", null, false, "http://def$imag
e"); | |
| 313 cacheCheck(test, matcher, "http://def", "SCRIPT", null, false, "http://def$scr
ipt"); | |
| 314 cacheCheck(test, matcher, "http://def", "OTHER", null, false, "http://def$~ima
ge,~script,~media,~ping"); | |
| 315 cacheCheck(test, matcher, "http://fed", "IMAGE", null, false, "http://fed$~thi
rd-party,~script"); | |
| 316 cacheCheck(test, matcher, "http://fed", "IMAGE", null, true, "http://fed$third
-party"); | |
| 317 cacheCheck(test, matcher, "http://abc_cba", "MEDIA", null, false, "cba$~third-
party,~script"); | |
| 318 cacheCheck(test, matcher, "http://abc_cba", "MEDIA", null, true, "cba$third-pa
rty"); | |
| 319 cacheCheck(test, matcher, "http://abc_cba", "SCRIPT", null, false, "abc$script
"); | |
| 320 cacheCheck(test, matcher, "http://def?http://fed", "MEDIA", null, false, "http
://fed$~third-party,~script"); | |
| 321 cacheCheck(test, matcher, "http://def?http://fed", "MEDIA", null, true, "http:
//fed$third-party"); | |
| 322 cacheCheck(test, matcher, "http://def?http://fed", "SCRIPT", null, false, "htt
p://def$script"); | |
| 323 | |
| 324 test.done(); | |
| 325 }; | |
| 326 | |
| 327 exports.testWhitelisted = function(test) | |
| 328 { | |
| 329 let matcher = new CombinedMatcher(); | |
| 330 | |
| 331 test.ok(!matcher.isWhitelisted("https://example.com/foo", | |
| 332 RegExpFilter.typeMap.IMAGE)); | |
| 333 test.ok(!matcher.isWhitelisted("https://example.com/bar", | |
| 334 RegExpFilter.typeMap.IMAGE)); | |
| 335 test.ok(!matcher.isWhitelisted("https://example.com/foo", | |
| 336 RegExpFilter.typeMap.SUBDOCUMENT)); | |
| 337 | |
| 338 matcher.add(Filter.fromText("@@/foo^$image")); | |
| 339 | |
| 340 test.ok(matcher.isWhitelisted("https://example.com/foo", | |
| 341 RegExpFilter.typeMap.IMAGE)); | |
| 342 test.ok(!matcher.isWhitelisted("https://example.com/bar", | |
| 343 RegExpFilter.typeMap.IMAGE)); | |
| 344 test.ok(!matcher.isWhitelisted("https://example.com/foo", | |
| 345 RegExpFilter.typeMap.SUBDOCUMENT)); | |
| 346 | |
| 347 matcher.remove(Filter.fromText("@@/foo^$image")); | |
| 348 | |
| 349 test.ok(!matcher.isWhitelisted("https://example.com/foo", | |
| 350 RegExpFilter.typeMap.IMAGE)); | |
| 351 test.ok(!matcher.isWhitelisted("https://example.com/bar", | |
| 352 RegExpFilter.typeMap.IMAGE)); | |
| 353 test.ok(!matcher.isWhitelisted("https://example.com/foo", | |
| 354 RegExpFilter.typeMap.SUBDOCUMENT)); | |
| 355 | |
| 356 test.done(); | |
| 357 }; | |
| 358 | |
| 359 exports.testAddRemoveByKeyword = function(test) | |
| 360 { | |
| 361 let matcher = new CombinedMatcher(); | |
| 362 | |
| 363 matcher.add(Filter.fromText("||example.com/foo/bar/image.jpg^")); | |
| 364 | |
| 365 // Add the same filter a second time to make sure it doesn't get added again | |
| 366 // by a different keyword. | |
| 367 matcher.add(Filter.fromText("||example.com/foo/bar/image.jpg^")); | |
| 368 | |
| 369 test.ok(!!matcher.matchesAny("https://example.com/foo/bar/image.jpg", | |
| 370 RegExpFilter.typeMap.IMAGE)); | |
| 371 | |
| 372 matcher.remove(Filter.fromText("||example.com/foo/bar/image.jpg^")); | |
| 373 | |
| 374 // Make sure the filter got removed so there is no match. | |
| 375 test.ok(!matcher.matchesAny("https://example.com/foo/bar/image.jpg", | |
| 376 RegExpFilter.typeMap.IMAGE)); | |
| 377 | |
| 378 | |
| 379 // Map { "example" => { text: "||example.com^$~third-party" } } | |
| 380 matcher.add(Filter.fromText("||example.com^$~third-party")); | |
| 381 | |
| 382 test.equal(matcher._blacklist._filterDomainMapsByKeyword.size, 1); | |
| 383 | |
| 384 for (let [key, value] of matcher._blacklist._filterDomainMapsByKeyword) | |
| 385 { | |
| 386 test.equal(key, "example"); | |
| 387 test.deepEqual(value, Filter.fromText("||example.com^$~third-party")); | |
| 388 break; | |
| 389 } | 118 } |
| 390 | 119 |
| 391 test.ok(!!matcher.matchesAny("https://example.com/example/image.jpg", | 120 function cacheCheck(matcher, location, contentType, docDomain, thirdParty, exp
ected) |
| 392 RegExpFilter.typeMap.IMAGE, "example.com", | 121 { |
| 393 false)); | 122 let result = matcher.matchesAny(location, RegExpFilter.typeMap[contentType],
docDomain, thirdParty); |
| 394 | 123 if (result) |
| 395 // Map { | 124 result = result.text; |
| 396 // "example" => Map { | 125 |
| 397 // "" => Map { | 126 assert.equal(result, expected, "match(" + location + ", " + contentType + ",
" + docDomain + ", " + (thirdParty ? "third-party" : "first-party") + ") with s
tatic filters"); |
| 398 // { text: "||example.com^$~third-party" } => true, | |
| 399 // { text: "/example/*$~third-party" } => true | |
| 400 // } | |
| 401 // } | |
| 402 // } | |
| 403 matcher.add(Filter.fromText("/example/*$~third-party")); | |
| 404 | |
| 405 test.equal(matcher._blacklist._filterDomainMapsByKeyword.size, 1); | |
| 406 | |
| 407 for (let [key, value] of matcher._blacklist._filterDomainMapsByKeyword) | |
| 408 { | |
| 409 test.equal(key, "example"); | |
| 410 test.equal(value.size, 1); | |
| 411 | |
| 412 let map = value.get(""); | |
| 413 test.equal(map.size, 2); | |
| 414 test.equal(map.get(Filter.fromText("||example.com^$~third-party")), true); | |
| 415 test.equal(map.get(Filter.fromText("/example/*$~third-party")), true); | |
| 416 | |
| 417 break; | |
| 418 } | 127 } |
| 419 | 128 |
| 420 test.ok(!!matcher.matchesAny("https://example.com/example/image.jpg", | 129 it("Class Definitions", () => |
| 421 RegExpFilter.typeMap.IMAGE, "example.com", | 130 { |
| 422 false)); | 131 assert.equal(typeof Matcher, "function", "typeof Matcher"); |
| 423 | 132 assert.equal(typeof CombinedMatcher, "function", "typeof CombinedMatcher"); |
| 424 // Map { "example" => { text: "/example/*$~third-party" } } | 133 assert.equal(typeof defaultMatcher, "object", "typeof defaultMatcher"); |
| 425 matcher.remove(Filter.fromText("||example.com^$~third-party")); | 134 assert.ok(defaultMatcher instanceof CombinedMatcher, "defaultMatcher is a Co
mbinedMatcher instance"); |
| 426 | 135 }); |
| 427 test.equal(matcher._blacklist._filterDomainMapsByKeyword.size, 1); | 136 |
| 428 | 137 it("Keyword Extraction", () => |
| 429 for (let [key, value] of matcher._blacklist._filterDomainMapsByKeyword) | 138 { |
| 430 { | 139 compareKeywords("*", []); |
| 431 test.equal(key, "example"); | 140 compareKeywords("asdf", []); |
| 432 test.deepEqual(value, Filter.fromText("/example/*$~third-party")); | 141 compareKeywords("/asdf/", []); |
| 433 break; | 142 compareKeywords("/asdf1234", []); |
| 434 } | 143 compareKeywords("/asdf/1234", ["asdf"]); |
| 435 | 144 compareKeywords("/asdf/1234^", ["asdf", "1234"]); |
| 436 test.ok(!!matcher.matchesAny("https://example.com/example/image.jpg", | 145 compareKeywords("/asdf/123456^", ["123456", "asdf"]); |
| 437 RegExpFilter.typeMap.IMAGE, "example.com", | 146 compareKeywords("^asdf^1234^56as^", ["asdf", "1234", "56as"]); |
| 438 false)); | 147 compareKeywords("*asdf/1234^", ["1234"]); |
| 439 | 148 compareKeywords("|asdf,1234*", ["asdf"]); |
| 440 // Map {} | 149 compareKeywords("||domain.example^", ["example", "domain"]); |
| 441 matcher.remove(Filter.fromText("/example/*$~third-party")); | 150 compareKeywords("&asdf=1234|", ["asdf", "1234"]); |
| 442 | 151 compareKeywords("^foo%2Ebar^", ["foo%2ebar"]); |
| 443 test.equal(matcher._blacklist._filterDomainMapsByKeyword.size, 0); | 152 compareKeywords("^aSdF^1234", ["asdf"]); |
| 444 | 153 compareKeywords("_asdf_1234_", ["asdf", "1234"]); |
| 445 test.ok(!matcher.matchesAny("https://example.com/example/image.jpg", | 154 compareKeywords("+asdf-1234=", ["asdf", "1234"]); |
| 446 RegExpFilter.typeMap.IMAGE, "example.com", | 155 compareKeywords("/123^ad2&ad&", ["123", "ad2"]); |
| 447 false)); | 156 compareKeywords("/123^ad2&ad$script,domain=example.com", ["123", "ad2"]); |
| 448 | 157 }); |
| 449 test.done(); | 158 |
| 450 }; | 159 it("Filter Matching", () => |
| 160 { |
| 161 checkMatch([], "http://abc/def", "IMAGE", null, false, null, false, null); |
| 162 checkMatch(["abc"], "http://abc/def", "IMAGE", null, false, null, false, "ab
c"); |
| 163 checkMatch(["abc", "ddd"], "http://abc/def", "IMAGE", null, false, null, fal
se, "abc"); |
| 164 checkMatch(["ddd", "abc"], "http://abc/def", "IMAGE", null, false, null, fal
se, "abc"); |
| 165 checkMatch(["ddd", "abd"], "http://abc/def", "IMAGE", null, false, null, fal
se, null); |
| 166 checkMatch(["abc", "://abc/d"], "http://abc/def", "IMAGE", null, false, null
, false, "://abc/d"); |
| 167 checkMatch(["://abc/d", "abc"], "http://abc/def", "IMAGE", null, false, null
, false, "://abc/d"); |
| 168 checkMatch(["|http://"], "http://abc/def", "IMAGE", null, false, null, false
, "|http://"); |
| 169 checkMatch(["|http://abc"], "http://abc/def", "IMAGE", null, false, null, fa
lse, "|http://abc"); |
| 170 checkMatch(["|abc"], "http://abc/def", "IMAGE", null, false, null, false, nu
ll); |
| 171 checkMatch(["|/abc/def"], "http://abc/def", "IMAGE", null, false, null, fals
e, null); |
| 172 checkMatch(["/def|"], "http://abc/def", "IMAGE", null, false, null, false, "
/def|"); |
| 173 checkMatch(["/abc/def|"], "http://abc/def", "IMAGE", null, false, null, fals
e, "/abc/def|"); |
| 174 checkMatch(["/abc/|"], "http://abc/def", "IMAGE", null, false, null, false,
null); |
| 175 checkMatch(["http://abc/|"], "http://abc/def", "IMAGE", null, false, null, f
alse, null); |
| 176 checkMatch(["|http://abc/def|"], "http://abc/def", "IMAGE", null, false, nul
l, false, "|http://abc/def|"); |
| 177 checkMatch(["|/abc/def|"], "http://abc/def", "IMAGE", null, false, null, fal
se, null); |
| 178 checkMatch(["|http://abc/|"], "http://abc/def", "IMAGE", null, false, null,
false, null); |
| 179 checkMatch(["|/abc/|"], "http://abc/def", "IMAGE", null, false, null, false,
null); |
| 180 checkMatch(["||example.com/abc"], "http://example.com/abc/def", "IMAGE", nul
l, false, null, false, "||example.com/abc"); |
| 181 checkMatch(["||com/abc/def"], "http://example.com/abc/def", "IMAGE", null, f
alse, null, false, "||com/abc/def"); |
| 182 checkMatch(["||com/abc"], "http://example.com/abc/def", "IMAGE", null, false
, null, false, "||com/abc"); |
| 183 checkMatch(["||mple.com/abc"], "http://example.com/abc/def", "IMAGE", null,
false, null, false, null); |
| 184 checkMatch(["||.com/abc/def"], "http://example.com/abc/def", "IMAGE", null,
false, null, false, null); |
| 185 checkMatch(["||http://example.com/"], "http://example.com/abc/def", "IMAGE",
null, false, null, false, null); |
| 186 checkMatch(["||example.com/abc/def|"], "http://example.com/abc/def", "IMAGE"
, null, false, null, false, "||example.com/abc/def|"); |
| 187 checkMatch(["||com/abc/def|"], "http://example.com/abc/def", "IMAGE", null,
false, null, false, "||com/abc/def|"); |
| 188 checkMatch(["||example.com/abc|"], "http://example.com/abc/def", "IMAGE", nu
ll, false, null, false, null); |
| 189 checkMatch(["abc", "://abc/d", "asdf1234"], "http://abc/def", "IMAGE", null,
false, null, false, "://abc/d"); |
| 190 checkMatch(["foo*://abc/d", "foo*//abc/de", "://abc/de", "asdf1234"], "http:
//abc/def", "IMAGE", null, false, null, false, "://abc/de"); |
| 191 checkMatch(["abc$third-party", "abc$~third-party", "ddd"], "http://abc/def",
"IMAGE", null, false, null, false, "abc$~third-party"); |
| 192 checkMatch(["abc$third-party", "abc$~third-party", "ddd"], "http://abc/def",
"IMAGE", null, true, null, false, "abc$third-party"); |
| 193 checkMatch(["//abc/def$third-party", "//abc/def$~third-party", "//abc_def"],
"http://abc/def", "IMAGE", null, false, null, false, "//abc/def$~third-party"); |
| 194 checkMatch(["//abc/def$third-party", "//abc/def$~third-party", "//abc_def"],
"http://abc/def", "IMAGE", null, true, null, false, "//abc/def$third-party"); |
| 195 checkMatch(["abc$third-party", "abc$~third-party", "//abc/def"], "http://abc
/def", "IMAGE", null, true, null, false, "//abc/def"); |
| 196 checkMatch(["//abc/def", "abc$third-party", "abc$~third-party"], "http://abc
/def", "IMAGE", null, true, null, false, "//abc/def"); |
| 197 checkMatch(["abc$third-party", "abc$~third-party", "//abc/def$third-party"],
"http://abc/def", "IMAGE", null, true, null, false, "//abc/def$third-party"); |
| 198 checkMatch(["abc$third-party", "abc$~third-party", "//abc/def$third-party"],
"http://abc/def", "IMAGE", null, false, null, false, "abc$~third-party"); |
| 199 checkMatch(["abc$third-party", "abc$~third-party", "//abc/def$~third-party"]
, "http://abc/def", "IMAGE", null, true, null, false, "abc$third-party"); |
| 200 checkMatch(["abc$image", "abc$script", "abc$~image"], "http://abc/def", "IMA
GE", null, false, null, false, "abc$image"); |
| 201 checkMatch(["abc$image", "abc$script", "abc$~script"], "http://abc/def", "SC
RIPT", null, false, null, false, "abc$script"); |
| 202 checkMatch(["abc$image", "abc$script", "abc$~image"], "http://abc/def", "OTH
ER", null, false, null, false, "abc$~image"); |
| 203 checkMatch(["//abc/def$image", "//abc/def$script", "//abc/def$~image"], "htt
p://abc/def", "IMAGE", null, false, null, false, "//abc/def$image"); |
| 204 checkMatch(["//abc/def$image", "//abc/def$script", "//abc/def$~script"], "ht
tp://abc/def", "SCRIPT", null, false, null, false, "//abc/def$script"); |
| 205 checkMatch(["//abc/def$image", "//abc/def$script", "//abc/def$~image"], "htt
p://abc/def", "OTHER", null, false, null, false, "//abc/def$~image"); |
| 206 checkMatch(["abc$image", "abc$~image", "//abc/def"], "http://abc/def", "IMAG
E", null, false, null, false, "//abc/def"); |
| 207 checkMatch(["//abc/def", "abc$image", "abc$~image"], "http://abc/def", "IMAG
E", null, false, null, false, "//abc/def"); |
| 208 checkMatch(["abc$image", "abc$~image", "//abc/def$image"], "http://abc/def",
"IMAGE", null, false, null, false, "//abc/def$image"); |
| 209 checkMatch(["abc$image", "abc$~image", "//abc/def$script"], "http://abc/def"
, "IMAGE", null, false, null, false, "abc$image"); |
| 210 checkMatch(["abc$domain=foo.com", "abc$domain=bar.com", "abc$domain=~foo.com
|~bar.com"], "http://abc/def", "IMAGE", "foo.com", false, null, false, "abc$doma
in=foo.com"); |
| 211 checkMatch(["abc$domain=foo.com", "abc$domain=bar.com", "abc$domain=~foo.com
|~bar.com"], "http://abc/def", "IMAGE", "bar.com", false, null, false, "abc$doma
in=bar.com"); |
| 212 checkMatch(["abc$domain=foo.com", "abc$domain=bar.com", "abc$domain=~foo.com
|~bar.com"], "http://abc/def", "IMAGE", "baz.com", false, null, false, "abc$doma
in=~foo.com|~bar.com"); |
| 213 checkMatch(["abc$domain=foo.com", "cba$domain=bar.com", "ccc$domain=~foo.com
|~bar.com"], "http://abc/def", "IMAGE", "foo.com", false, null, false, "abc$doma
in=foo.com"); |
| 214 checkMatch(["abc$domain=foo.com", "cba$domain=bar.com", "ccc$domain=~foo.com
|~bar.com"], "http://abc/def", "IMAGE", "bar.com", false, null, false, null); |
| 215 checkMatch(["abc$domain=foo.com", "cba$domain=bar.com", "ccc$domain=~foo.com
|~bar.com"], "http://abc/def", "IMAGE", "baz.com", false, null, false, null); |
| 216 checkMatch(["abc$domain=foo.com", "cba$domain=bar.com", "ccc$domain=~foo.com
|~bar.com"], "http://ccc/def", "IMAGE", "baz.com", false, null, false, "ccc$doma
in=~foo.com|~bar.com"); |
| 217 checkMatch(["abc$sitekey=foo-publickey", "abc$sitekey=bar-publickey"], "http
://abc/def", "IMAGE", "foo.com", false, "foo-publickey", false, "abc$sitekey=foo
-publickey"); |
| 218 checkMatch(["abc$sitekey=foo-publickey", "abc$sitekey=bar-publickey"], "http
://abc/def", "IMAGE", "bar.com", false, "bar-publickey", false, "abc$sitekey=bar
-publickey"); |
| 219 checkMatch(["abc$sitekey=foo-publickey", "cba$sitekey=bar-publickey"], "http
://abc/def", "IMAGE", "bar.com", false, "bar-publickey", false, null); |
| 220 checkMatch(["abc$sitekey=foo-publickey", "cba$sitekey=bar-publickey"], "http
://abc/def", "IMAGE", "baz.com", false, null, false, null); |
| 221 checkMatch(["abc$sitekey=foo-publickey,domain=foo.com", "abc$sitekey=bar-pub
lickey,domain=bar.com"], "http://abc/def", "IMAGE", "foo.com", false, "foo-publi
ckey", false, "abc$sitekey=foo-publickey,domain=foo.com"); |
| 222 checkMatch(["abc$sitekey=foo-publickey,domain=foo.com", "abc$sitekey=bar-pub
lickey,domain=bar.com"], "http://abc/def", "IMAGE", "foo.com", false, "bar-publi
ckey", false, null); |
| 223 checkMatch(["abc$sitekey=foo-publickey,domain=foo.com", "abc$sitekey=bar-pub
lickey,domain=bar.com"], "http://abc/def", "IMAGE", "bar.com", false, "foo-publi
ckey", false, null); |
| 224 checkMatch(["abc$sitekey=foo-publickey,domain=foo.com", "abc$sitekey=bar-pub
lickey,domain=bar.com"], "http://abc/def", "IMAGE", "bar.com", false, "bar-publi
ckey", false, "abc$sitekey=bar-publickey,domain=bar.com"); |
| 225 checkMatch(["@@foo.com$document"], "http://foo.com/bar", "DOCUMENT", "foo.co
m", false, null, false, "@@foo.com$document"); |
| 226 checkMatch(["@@foo.com$elemhide"], "http://foo.com/bar", "ELEMHIDE", "foo.co
m", false, null, false, "@@foo.com$elemhide"); |
| 227 checkMatch(["@@foo.com$generichide"], "http://foo.com/bar", "GENERICHIDE", "
foo.com", false, null, false, "@@foo.com$generichide"); |
| 228 checkMatch(["@@foo.com$genericblock"], "http://foo.com/bar", "GENERICBLOCK",
"foo.com", false, null, false, "@@foo.com$genericblock"); |
| 229 checkMatch(["@@bar.com$document"], "http://foo.com/bar", "DOCUMENT", "foo.co
m", false, null, false, null); |
| 230 checkMatch(["@@bar.com$elemhide"], "http://foo.com/bar", "ELEMHIDE", "foo.co
m", false, null, false, null); |
| 231 checkMatch(["@@bar.com$generichide"], "http://foo.com/bar", "GENERICHIDE", "
foo.com", false, null, false, null); |
| 232 checkMatch(["@@bar.com$genericblock"], "http://foo.com/bar", "GENERICBLOCK",
"foo.com", false, null, false, null); |
| 233 checkMatch(["/bar"], "http://foo.com/bar", "IMAGE", "foo.com", false, null,
true, null); |
| 234 checkMatch(["/bar$domain=foo.com"], "http://foo.com/bar", "IMAGE", "foo.com"
, false, null, true, "/bar$domain=foo.com"); |
| 235 checkMatch(["@@||foo.com^"], "http://foo.com/bar", "IMAGE", "foo.com", false
, null, false, null, "@@||foo.com^"); |
| 236 checkMatch(["/bar", "@@||foo.com^"], "http://foo.com/bar", "IMAGE", "foo.com
", false, null, false, "@@||foo.com^"); |
| 237 checkMatch(["/bar", "@@||foo.com^"], "http://foo.com/foo", "IMAGE", "foo.com
", false, null, false, null, "@@||foo.com^"); |
| 238 checkMatch(["||foo.com^$popup"], "http://foo.com/bar", "POPUP", "foo.com", f
alse, null, false, "||foo.com^$popup"); |
| 239 checkMatch(["@@||foo.com^$popup"], "http://foo.com/bar", "POPUP", "foo.com",
false, null, false, null, "@@||foo.com^$popup"); |
| 240 checkMatch(["||foo.com^$popup", "@@||foo.com^$popup"], "http://foo.com/bar",
"POPUP", "foo.com", false, null, false, "@@||foo.com^$popup", "||foo.com^$popup
"); |
| 241 checkMatch(["||foo.com^$csp=script-src 'none'"], "http://foo.com/bar", "CSP"
, "foo.com", false, null, false, "||foo.com^$csp=script-src 'none'"); |
| 242 checkMatch(["@@||foo.com^$csp"], "http://foo.com/bar", "CSP", "foo.com", fal
se, null, false, null, "@@||foo.com^$csp"); |
| 243 checkMatch(["||foo.com^$csp=script-src 'none'", "@@||foo.com^$csp"], "http:/
/foo.com/bar", "CSP", "foo.com", false, null, false, "@@||foo.com^$csp", "||foo.
com^$csp=script-src 'none'"); |
| 244 |
| 245 // See #7312. |
| 246 checkMatch(["^foo/bar/$script"], "http://foo/bar/", "SCRIPT", "example.com",
true, null, true, null); |
| 247 checkMatch(["^foo/bar/$script"], "http://foo/bar/", "SCRIPT", "example.com",
true, null, false, "^foo/bar/$script"); |
| 248 checkMatch(["^foo/bar/$script,domain=example.com", "@@^foo/bar/$script"], "h
ttp://foo/bar/", "SCRIPT", "example.com", true, null, true, "@@^foo/bar/$script"
, "^foo/bar/$script,domain=example.com"); |
| 249 checkMatch(["@@^foo/bar/$script", "^foo/bar/$script,domain=example.com"], "h
ttp://foo/bar/", "SCRIPT", "example.com", true, null, true, "@@^foo/bar/$script"
, "^foo/bar/$script,domain=example.com"); |
| 250 checkMatch(["@@^foo/bar/$script", "^foo/bar/$script,domain=example.com"], "h
ttp://foo/bar/", "SCRIPT", "example.com", true, null, false, "@@^foo/bar/$script
"); |
| 251 }); |
| 252 |
| 253 it("Filter Search", () => |
| 254 { |
| 255 // Start with three filters: foo, bar, and @@foo |
| 256 let filters = ["foo", "bar", "@@foo"]; |
| 257 |
| 258 checkSearch(filters, "http://example.com/foo", "IMAGE", "example.com", |
| 259 false, null, false, "all", |
| 260 {blocking: ["foo"], whitelist: ["@@foo"]}); |
| 261 |
| 262 // Blocking only. |
| 263 checkSearch(filters, "http://example.com/foo", "IMAGE", "example.com", |
| 264 false, null, false, "blocking", {blocking: ["foo"]}); |
| 265 |
| 266 // Whitelist only. |
| 267 checkSearch(filters, "http://example.com/foo", "IMAGE", "example.com", |
| 268 false, null, false, "whitelist", {whitelist: ["@@foo"]}); |
| 269 |
| 270 // Different URLs. |
| 271 checkSearch(filters, "http://example.com/bar", "IMAGE", "example.com", |
| 272 false, null, false, "all", {blocking: ["bar"], whitelist: []}); |
| 273 checkSearch(filters, "http://example.com/foo/bar", "IMAGE", |
| 274 "example.com", false, null, false, "all", |
| 275 {blocking: ["foo", "bar"], whitelist: ["@@foo"]}); |
| 276 |
| 277 // Non-matching content type. |
| 278 checkSearch(filters, "http://example.com/foo", "CSP", "example.com", |
| 279 false, null, false, "all", {blocking: [], whitelist: []}); |
| 280 |
| 281 // Non-matching specificity. |
| 282 checkSearch(filters, "http://example.com/foo", "IMAGE", "example.com", |
| 283 false, null, true, "all", {blocking: [], whitelist: ["@@foo"]}); |
| 284 }); |
| 285 |
| 286 it("Result cache checks", () => |
| 287 { |
| 288 let matcher = new CombinedMatcher(); |
| 289 matcher.add(Filter.fromText("abc$image")); |
| 290 matcher.add(Filter.fromText("abc$script")); |
| 291 matcher.add(Filter.fromText("abc$~image,~script,~media,~ping")); |
| 292 matcher.add(Filter.fromText("cba$third-party")); |
| 293 matcher.add(Filter.fromText("cba$~third-party,~script")); |
| 294 matcher.add(Filter.fromText("http://def$image")); |
| 295 matcher.add(Filter.fromText("http://def$script")); |
| 296 matcher.add(Filter.fromText("http://def$~image,~script,~media,~ping")); |
| 297 matcher.add(Filter.fromText("http://fed$third-party")); |
| 298 matcher.add(Filter.fromText("http://fed$~third-party,~script")); |
| 299 |
| 300 cacheCheck(matcher, "http://abc", "IMAGE", null, false, "abc$image"); |
| 301 cacheCheck(matcher, "http://abc", "SCRIPT", null, false, "abc$script"); |
| 302 cacheCheck(matcher, "http://abc", "OTHER", null, false, "abc$~image,~script,
~media,~ping"); |
| 303 cacheCheck(matcher, "http://cba", "IMAGE", null, false, "cba$~third-party,~s
cript"); |
| 304 cacheCheck(matcher, "http://cba", "IMAGE", null, true, "cba$third-party"); |
| 305 cacheCheck(matcher, "http://def", "IMAGE", null, false, "http://def$image"); |
| 306 cacheCheck(matcher, "http://def", "SCRIPT", null, false, "http://def$script"
); |
| 307 cacheCheck(matcher, "http://def", "OTHER", null, false, "http://def$~image,~
script,~media,~ping"); |
| 308 cacheCheck(matcher, "http://fed", "IMAGE", null, false, "http://fed$~third-p
arty,~script"); |
| 309 cacheCheck(matcher, "http://fed", "IMAGE", null, true, "http://fed$third-par
ty"); |
| 310 cacheCheck(matcher, "http://abc_cba", "MEDIA", null, false, "cba$~third-part
y,~script"); |
| 311 cacheCheck(matcher, "http://abc_cba", "MEDIA", null, true, "cba$third-party"
); |
| 312 cacheCheck(matcher, "http://abc_cba", "SCRIPT", null, false, "abc$script"); |
| 313 cacheCheck(matcher, "http://def?http://fed", "MEDIA", null, false, "http://f
ed$~third-party,~script"); |
| 314 cacheCheck(matcher, "http://def?http://fed", "MEDIA", null, true, "http://fe
d$third-party"); |
| 315 cacheCheck(matcher, "http://def?http://fed", "SCRIPT", null, false, "http://
def$script"); |
| 316 }); |
| 317 |
| 318 it("Whitelisted", () => |
| 319 { |
| 320 let matcher = new CombinedMatcher(); |
| 321 |
| 322 assert.ok(!matcher.isWhitelisted("https://example.com/foo", |
| 323 RegExpFilter.typeMap.IMAGE)); |
| 324 assert.ok(!matcher.isWhitelisted("https://example.com/bar", |
| 325 RegExpFilter.typeMap.IMAGE)); |
| 326 assert.ok(!matcher.isWhitelisted("https://example.com/foo", |
| 327 RegExpFilter.typeMap.SUBDOCUMENT)); |
| 328 |
| 329 matcher.add(Filter.fromText("@@/foo^$image")); |
| 330 |
| 331 assert.ok(matcher.isWhitelisted("https://example.com/foo", |
| 332 RegExpFilter.typeMap.IMAGE)); |
| 333 assert.ok(!matcher.isWhitelisted("https://example.com/bar", |
| 334 RegExpFilter.typeMap.IMAGE)); |
| 335 assert.ok(!matcher.isWhitelisted("https://example.com/foo", |
| 336 RegExpFilter.typeMap.SUBDOCUMENT)); |
| 337 |
| 338 matcher.remove(Filter.fromText("@@/foo^$image")); |
| 339 |
| 340 assert.ok(!matcher.isWhitelisted("https://example.com/foo", |
| 341 RegExpFilter.typeMap.IMAGE)); |
| 342 assert.ok(!matcher.isWhitelisted("https://example.com/bar", |
| 343 RegExpFilter.typeMap.IMAGE)); |
| 344 assert.ok(!matcher.isWhitelisted("https://example.com/foo", |
| 345 RegExpFilter.typeMap.SUBDOCUMENT)); |
| 346 }); |
| 347 |
| 348 it("Add remove by keyword", () => |
| 349 { |
| 350 let matcher = new CombinedMatcher(); |
| 351 |
| 352 matcher.add(Filter.fromText("||example.com/foo/bar/image.jpg^")); |
| 353 |
| 354 // Add the same filter a second time to make sure it doesn't get added again |
| 355 // by a different keyword. |
| 356 matcher.add(Filter.fromText("||example.com/foo/bar/image.jpg^")); |
| 357 |
| 358 assert.ok(!!matcher.matchesAny("https://example.com/foo/bar/image.jpg", |
| 359 RegExpFilter.typeMap.IMAGE)); |
| 360 |
| 361 matcher.remove(Filter.fromText("||example.com/foo/bar/image.jpg^")); |
| 362 |
| 363 // Make sure the filter got removed so there is no match. |
| 364 assert.ok(!matcher.matchesAny("https://example.com/foo/bar/image.jpg", |
| 365 RegExpFilter.typeMap.IMAGE)); |
| 366 |
| 367 |
| 368 // Map { "example" => { text: "||example.com^$~third-party" } } |
| 369 matcher.add(Filter.fromText("||example.com^$~third-party")); |
| 370 |
| 371 assert.equal(matcher._blacklist._filterDomainMapsByKeyword.size, 1); |
| 372 |
| 373 for (let [key, value] of matcher._blacklist._filterDomainMapsByKeyword) |
| 374 { |
| 375 assert.equal(key, "example"); |
| 376 assert.deepEqual(value, Filter.fromText("||example.com^$~third-party")); |
| 377 break; |
| 378 } |
| 379 |
| 380 assert.ok(!!matcher.matchesAny("https://example.com/example/image.jpg", |
| 381 RegExpFilter.typeMap.IMAGE, "example.com", |
| 382 false)); |
| 383 |
| 384 // Map { |
| 385 // "example" => Map { |
| 386 // "" => Map { |
| 387 // { text: "||example.com^$~third-party" } => true, |
| 388 // { text: "/example/*$~third-party" } => true |
| 389 // } |
| 390 // } |
| 391 // } |
| 392 matcher.add(Filter.fromText("/example/*$~third-party")); |
| 393 |
| 394 assert.equal(matcher._blacklist._filterDomainMapsByKeyword.size, 1); |
| 395 |
| 396 for (let [key, value] of matcher._blacklist._filterDomainMapsByKeyword) |
| 397 { |
| 398 assert.equal(key, "example"); |
| 399 assert.equal(value.size, 1); |
| 400 |
| 401 let map = value.get(""); |
| 402 assert.equal(map.size, 2); |
| 403 assert.equal(map.get(Filter.fromText("||example.com^$~third-party")), true
); |
| 404 assert.equal(map.get(Filter.fromText("/example/*$~third-party")), true); |
| 405 |
| 406 break; |
| 407 } |
| 408 |
| 409 assert.ok(!!matcher.matchesAny("https://example.com/example/image.jpg", |
| 410 RegExpFilter.typeMap.IMAGE, "example.com", |
| 411 false)); |
| 412 |
| 413 // Map { "example" => { text: "/example/*$~third-party" } } |
| 414 matcher.remove(Filter.fromText("||example.com^$~third-party")); |
| 415 |
| 416 assert.equal(matcher._blacklist._filterDomainMapsByKeyword.size, 1); |
| 417 |
| 418 for (let [key, value] of matcher._blacklist._filterDomainMapsByKeyword) |
| 419 { |
| 420 assert.equal(key, "example"); |
| 421 assert.deepEqual(value, Filter.fromText("/example/*$~third-party")); |
| 422 break; |
| 423 } |
| 424 |
| 425 assert.ok(!!matcher.matchesAny("https://example.com/example/image.jpg", |
| 426 RegExpFilter.typeMap.IMAGE, "example.com", |
| 427 false)); |
| 428 |
| 429 // Map {} |
| 430 matcher.remove(Filter.fromText("/example/*$~third-party")); |
| 431 |
| 432 assert.equal(matcher._blacklist._filterDomainMapsByKeyword.size, 0); |
| 433 |
| 434 assert.ok(!matcher.matchesAny("https://example.com/example/image.jpg", |
| 435 RegExpFilter.typeMap.IMAGE, "example.com", |
| 436 false)); |
| 437 }); |
| 438 }); |
| OLD | NEW |