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 // Only starting NodeJS 10 that URL is in the global space. | 20 // Only starting NodeJS 10 that URL is in the global space. |
21 const {URL} = require("url"); | 21 const {URL} = require("url"); |
22 const {createSandbox} = require("./_common"); | 22 const {createSandbox} = require("./_common"); |
23 | 23 |
24 const publicSuffixes = require("../data/publicSuffixList.json"); | 24 const publicSuffixes = require("../data/publicSuffixList.json"); |
25 | 25 |
| 26 let parseURL = null; |
26 let normalizeHostname = null; | 27 let normalizeHostname = null; |
27 let domainSuffixes = null; | 28 let domainSuffixes = null; |
28 let isThirdParty = null; | 29 let isThirdParty = null; |
29 let getBaseDomain = null; | 30 let getBaseDomain = null; |
30 | 31 |
31 exports.setUp = function(callback) | 32 exports.setUp = function(callback) |
32 { | 33 { |
33 let sandboxedRequire = createSandbox({ | 34 let sandboxedRequire = createSandbox({ |
34 extraExports: { | 35 extraExports: { |
35 domain: ["getBaseDomain"] | 36 domain: ["getBaseDomain"] |
36 } | 37 } |
37 }); | 38 }); |
38 ( | 39 ( |
39 {normalizeHostname, domainSuffixes, isThirdParty, | 40 {parseURL, normalizeHostname, domainSuffixes, isThirdParty, |
40 getBaseDomain} = sandboxedRequire("../lib/url") | 41 getBaseDomain} = sandboxedRequire("../lib/url") |
41 ); | 42 ); |
42 | 43 |
43 callback(); | 44 callback(); |
44 }; | 45 }; |
45 | 46 |
46 function hostnameToURL(hostname) | 47 function hostnameToURL(hostname) |
47 { | 48 { |
48 return new URL("http://" + hostname); | 49 return new URL("http://" + hostname); |
49 } | 50 } |
50 | 51 |
| 52 function testURLParsing(test, url) |
| 53 { |
| 54 // Note: The function expects a normalized URL. |
| 55 // e.g. "http:example.com:80?foo" should already be normalized to |
| 56 // "http://example.com/?foo". If not, the tests will fail. |
| 57 let urlInfo = parseURL(url); |
| 58 |
| 59 // We need to ensure only that our implementation matches that of the URL |
| 60 // object. |
| 61 let urlObject = new URL(url); |
| 62 |
| 63 test.equal(urlInfo.href, urlObject.href); |
| 64 test.equal(urlInfo.protocol, urlObject.protocol); |
| 65 test.equal(urlInfo.hostname, urlObject.hostname); |
| 66 |
| 67 test.equal(urlInfo.toString(), urlObject.toString()); |
| 68 test.equal(String(urlInfo), String(urlObject)); |
| 69 test.equal(urlInfo + "", urlObject + ""); |
| 70 } |
| 71 |
51 function testThirdParty(test, requestHostname, documentHostname, expected, | 72 function testThirdParty(test, requestHostname, documentHostname, expected, |
52 message) | 73 message) |
53 { | 74 { |
54 test.equal( | 75 test.equal( |
55 isThirdParty( | 76 isThirdParty( |
56 hostnameToURL(requestHostname), | 77 hostnameToURL(requestHostname), |
57 | 78 |
58 // Chrome's URL object normalizes IP addresses. So some test | 79 // Chrome's URL object normalizes IP addresses. So some test |
59 // will fail if we don't normalize the document host as well. | 80 // will fail if we don't normalize the document host as well. |
60 hostnameToURL(documentHostname).hostname | 81 hostnameToURL(documentHostname).hostname |
61 ), | 82 ), |
62 expected, | 83 expected, |
63 message | 84 message |
64 ); | 85 ); |
65 } | 86 } |
66 | 87 |
| 88 exports.testParseURL = function(test) |
| 89 { |
| 90 testURLParsing(test, "https://example.com/"); |
| 91 testURLParsing(test, "https://example.com/foo"); |
| 92 testURLParsing(test, "https://example.com/foo/bar"); |
| 93 testURLParsing( |
| 94 test, |
| 95 "https://example.com/foo/bar?https://random/foo/bar" |
| 96 ); |
| 97 |
| 98 testURLParsing(test, "https://example.com:8080/"); |
| 99 testURLParsing(test, "https://example.com:8080/foo"); |
| 100 testURLParsing(test, "https://example.com:8080/foo/bar"); |
| 101 testURLParsing( |
| 102 test, |
| 103 "https://example.com:8080/foo/bar?https://random/foo/bar" |
| 104 ); |
| 105 |
| 106 testURLParsing(test, "http://localhost/"); |
| 107 testURLParsing(test, "http://localhost/foo"); |
| 108 testURLParsing(test, "http://localhost/foo/bar"); |
| 109 testURLParsing( |
| 110 test, |
| 111 "http://localhost/foo/bar?https://random/foo/bar" |
| 112 ); |
| 113 |
| 114 testURLParsing(test, "https://user@example.com/"); |
| 115 testURLParsing(test, "https://user@example.com/foo"); |
| 116 testURLParsing(test, "https://user@example.com/foo/bar"); |
| 117 testURLParsing( |
| 118 test, |
| 119 "https://user@example.com/foo/bar?https://random/foo/bar" |
| 120 ); |
| 121 |
| 122 testURLParsing(test, "https://user@example.com:8080/"); |
| 123 testURLParsing(test, "https://user@example.com:8080/foo"); |
| 124 testURLParsing(test, "https://user@example.com:8080/foo/bar"); |
| 125 testURLParsing( |
| 126 test, |
| 127 "https://user@example.com:8080/foo/bar?https://random/foo/bar" |
| 128 ); |
| 129 |
| 130 testURLParsing(test, "https://user:pass@example.com/"); |
| 131 testURLParsing(test, "https://user:pass@example.com/foo"); |
| 132 testURLParsing(test, "https://user:pass@example.com/foo/bar"); |
| 133 testURLParsing( |
| 134 test, |
| 135 "https://user:pass@example.com/foo/bar?https://random/foo/bar" |
| 136 ); |
| 137 |
| 138 testURLParsing(test, "https://user:pass@example.com:8080/"); |
| 139 testURLParsing(test, "https://user:pass@example.com:8080/foo"); |
| 140 testURLParsing(test, "https://user:pass@example.com:8080/foo/bar"); |
| 141 testURLParsing( |
| 142 test, |
| 143 "https://user:pass@example.com:8080/foo/bar?https://random/foo/bar" |
| 144 ); |
| 145 |
| 146 testURLParsing(test, "https://us%40er:pa%40ss@example.com/"); |
| 147 testURLParsing(test, "https://us%40er:pa%40ss@example.com/foo"); |
| 148 testURLParsing(test, "https://us%40er:pa%40ss@example.com/foo/bar"); |
| 149 testURLParsing( |
| 150 test, |
| 151 "https://us%40er:pa%40ss@example.com/foo/bar?https://random/foo/bar" |
| 152 ); |
| 153 |
| 154 testURLParsing(test, "https://us%40er:pa%40ss@example.com:8080/"); |
| 155 testURLParsing(test, "https://us%40er:pa%40ss@example.com:8080/foo"); |
| 156 testURLParsing(test, "https://us%40er:pa%40ss@example.com:8080/foo/bar"); |
| 157 testURLParsing( |
| 158 test, |
| 159 "https://us%40er:pa%40ss@example.com:8080/foo/bar?https://random/foo/bar" |
| 160 ); |
| 161 |
| 162 testURLParsing(test, "http://192.168.1.1/"); |
| 163 testURLParsing(test, "http://192.168.1.1/foo"); |
| 164 testURLParsing(test, "http://192.168.1.1/foo/bar"); |
| 165 testURLParsing( |
| 166 test, |
| 167 "http://192.168.1.1/foo/bar?https://random/foo/bar" |
| 168 ); |
| 169 testURLParsing( |
| 170 test, |
| 171 "http://192.168.1.1:8080/foo/bar?https://random/foo/bar" |
| 172 ); |
| 173 testURLParsing( |
| 174 test, |
| 175 "http://user@192.168.1.1:8080/foo/bar?https://random/foo/bar" |
| 176 ); |
| 177 testURLParsing( |
| 178 test, |
| 179 "http://user:pass@192.168.1.1:8080/foo/bar?https://random/foo/bar" |
| 180 ); |
| 181 |
| 182 testURLParsing(test, "http://[2001:db8:0:42:0:8a2e:370:7334]/"); |
| 183 testURLParsing(test, "http://[2001:db8:0:42:0:8a2e:370:7334]/foo"); |
| 184 testURLParsing( |
| 185 test, |
| 186 "http://[2001:db8:0:42:0:8a2e:370:7334]/foo/bar" |
| 187 ); |
| 188 testURLParsing( |
| 189 test, |
| 190 "http://[2001:db8:0:42:0:8a2e:370:7334]/foo/bar?https://random/foo/bar" |
| 191 ); |
| 192 testURLParsing( |
| 193 test, |
| 194 "http://[2001:db8:0:42:0:8a2e:370:7334]:8080/foo/bar?https://random/foo/bar" |
| 195 ); |
| 196 testURLParsing( |
| 197 test, |
| 198 "http://user@[2001:db8:0:42:0:8a2e:370:7334]:8080/foo/bar?https://random/foo
/bar" |
| 199 ); |
| 200 testURLParsing( |
| 201 test, |
| 202 "http://user:pass@[2001:db8:0:42:0:8a2e:370:7334]:8080/foo/bar?https://rando
m/foo/bar" |
| 203 ); |
| 204 |
| 205 testURLParsing(test, "ftp://user:pass@example.com:8021/"); |
| 206 testURLParsing(test, "ftp://user:pass@example.com:8021/foo"); |
| 207 testURLParsing(test, "ftp://user:pass@example.com:8021/foo/bar"); |
| 208 |
| 209 testURLParsing(test, "about:blank"); |
| 210 testURLParsing(test, "chrome://extensions"); |
| 211 testURLParsing( |
| 212 test, |
| 213 "chrome-extension://bhignfpcigccnlfapldlodmhlidjaion/options.html" |
| 214 ); |
| 215 testURLParsing(test, "mailto:john.doe@mail.example.com"); |
| 216 |
| 217 testURLParsing(test, "news:newsgroup"); |
| 218 testURLParsing(test, "news:message-id"); |
| 219 testURLParsing(test, "nntp://example.com:8119/newsgroup"); |
| 220 testURLParsing(test, "nntp://example.com:8119/message-id"); |
| 221 |
| 222 testURLParsing(test, "data:,"); |
| 223 testURLParsing( |
| 224 test, |
| 225 "data:text/vnd-example+xyz;foo=bar;base64,R0lGODdh" |
| 226 ); |
| 227 testURLParsing( |
| 228 test, |
| 229 "data:text/plain;charset=UTF-8;page=21,the%20data:1234,5678" |
| 230 ); |
| 231 |
| 232 testURLParsing(test, "javascript:"); |
| 233 testURLParsing(test, "javascript:alert();"); |
| 234 testURLParsing(test, "javascript:foo/bar/"); |
| 235 testURLParsing(test, "javascript://foo/bar/"); |
| 236 |
| 237 testURLParsing(test, "file:///dev/random"); |
| 238 |
| 239 testURLParsing(test, "wss://example.com/"); |
| 240 testURLParsing(test, "wss://example.com:8080/"); |
| 241 testURLParsing(test, "wss://user@example.com:8080/"); |
| 242 testURLParsing(test, "wss://user:pass@example.com:8080/"); |
| 243 |
| 244 testURLParsing(test, "stuns:stuns.example.com/"); |
| 245 testURLParsing(test, "stuns:stuns.example.com:8080/"); |
| 246 testURLParsing(test, "stuns:user@stuns.example.com:8080/"); |
| 247 testURLParsing(test, "stuns:user:pass@stuns.example.com:8080/"); |
| 248 |
| 249 // The following tests are based on |
| 250 // https://cs.chromium.org/chromium/src/url/gurl_unittest.cc?rcl=9ec7bc85e0f6a
0bf28eff6b2eca678067da547e9 |
| 251 // Note: We do not check for "canonicalization" (normalization). parseURL() |
| 252 // should be used with normalized URLs only. |
| 253 |
| 254 testURLParsing(test, "something:///example.com/"); |
| 255 testURLParsing(test, "something://example.com/"); |
| 256 |
| 257 testURLParsing(test, "file:///C:/foo.txt"); |
| 258 testURLParsing(test, "file://server/foo.txt"); |
| 259 |
| 260 testURLParsing(test, "http://user:pass@example.com:99/foo;bar?q=a#ref"); |
| 261 |
| 262 testURLParsing(test, "http://user:%40!$&'()*+,%3B%3D%3A@example.com:12345/"); |
| 263 |
| 264 testURLParsing(test, "filesystem:http://example.com/temporary/"); |
| 265 testURLParsing( |
| 266 test, |
| 267 "filesystem:http://user:%40!$&'()*+,%3B%3D%3A@example.com:12345/" |
| 268 ); |
| 269 |
| 270 testURLParsing(test, "javascript:window.alert('hello, world');"); |
| 271 testURLParsing(test, "javascript:#"); |
| 272 |
| 273 testURLParsing( |
| 274 test, |
| 275 "blob:https://example.com/7ce70a1e-9681-4148-87a8-43cb9171b994" |
| 276 ); |
| 277 |
| 278 testURLParsing(test, "http://[2001:db8::1]/"); |
| 279 testURLParsing(test, "http://[2001:db8::1]:8080/"); |
| 280 testURLParsing(test, "http://[::]:8080/"); |
| 281 |
| 282 testURLParsing(test, "not-a-standard-scheme:this is arbitrary content"); |
| 283 testURLParsing(test, "view-source:http://example.com/path"); |
| 284 |
| 285 testURLParsing( |
| 286 test, |
| 287 "data:text/html,Question?%3Cdiv%20style=%22color:%20#bad%22%3Eidea%3C/div%3E
" |
| 288 ); |
| 289 |
| 290 test.done(); |
| 291 }; |
| 292 |
67 exports.testNormalizeHostname = function(test) | 293 exports.testNormalizeHostname = function(test) |
68 { | 294 { |
69 test.equal(normalizeHostname("example.com"), "example.com"); | 295 test.equal(normalizeHostname("example.com"), "example.com"); |
70 test.equal(normalizeHostname("example.com."), "example.com"); | 296 test.equal(normalizeHostname("example.com."), "example.com"); |
71 test.equal(normalizeHostname("example.com.."), "example.com"); | 297 test.equal(normalizeHostname("example.com.."), "example.com"); |
72 test.equal(normalizeHostname("example.com..."), "example.com"); | 298 test.equal(normalizeHostname("example.com..."), "example.com"); |
73 | 299 |
74 test.equal(normalizeHostname("Example.com"), "example.com"); | 300 test.equal(normalizeHostname("Example.com"), "example.com"); |
75 test.equal(normalizeHostname("ExaMple.Com"), "example.com"); | 301 test.equal(normalizeHostname("ExaMple.Com"), "example.com"); |
76 test.equal(normalizeHostname("ExaMple.Com.."), "example.com"); | 302 test.equal(normalizeHostname("ExaMple.Com.."), "example.com"); |
(...skipping 211 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
288 "us-east-1.amazonaws.com" | 514 "us-east-1.amazonaws.com" |
289 ); | 515 ); |
290 test.equal(getBaseDomain("example.amazonaws.com"), "amazonaws.com"); | 516 test.equal(getBaseDomain("example.amazonaws.com"), "amazonaws.com"); |
291 test.equal(getBaseDomain("amazonaws.com"), "amazonaws.com"); | 517 test.equal(getBaseDomain("amazonaws.com"), "amazonaws.com"); |
292 | 518 |
293 // Edge case. | 519 // Edge case. |
294 test.equal(getBaseDomain(""), ""); | 520 test.equal(getBaseDomain(""), ""); |
295 | 521 |
296 test.done(); | 522 test.done(); |
297 }; | 523 }; |
OLD | NEW |