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 // The function expects a normalized URL. |
| 55 url = new URL(url).href; |
| 56 |
| 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/"); |
| 92 testURLParsing(test, "https://example.com/foo"); |
| 93 testURLParsing(test, "https://example.com/foo/bar"); |
| 94 testURLParsing( |
| 95 test, |
| 96 "https://example.com/foo/bar?https://random/foo/bar" |
| 97 ); |
| 98 |
| 99 testURLParsing(test, "https://example.com:8080"); |
| 100 testURLParsing(test, "https://example.com:8080/"); |
| 101 testURLParsing(test, "https://example.com:8080/foo"); |
| 102 testURLParsing(test, "https://example.com:8080/foo/bar"); |
| 103 testURLParsing( |
| 104 test, |
| 105 "https://example.com:8080/foo/bar?https://random/foo/bar" |
| 106 ); |
| 107 |
| 108 testURLParsing(test, "http://localhost"); |
| 109 testURLParsing(test, "http://localhost/"); |
| 110 testURLParsing(test, "http://localhost/foo"); |
| 111 testURLParsing(test, "http://localhost/foo/bar"); |
| 112 testURLParsing( |
| 113 test, |
| 114 "http://localhost/foo/bar?https://random/foo/bar" |
| 115 ); |
| 116 |
| 117 testURLParsing(test, "https://user@example.com"); |
| 118 testURLParsing(test, "https://user@example.com/"); |
| 119 testURLParsing(test, "https://user@example.com/foo"); |
| 120 testURLParsing(test, "https://user@example.com/foo/bar"); |
| 121 testURLParsing( |
| 122 test, |
| 123 "https://user@example.com/foo/bar?https://random/foo/bar" |
| 124 ); |
| 125 |
| 126 testURLParsing(test, "https://user@example.com:8080"); |
| 127 testURLParsing(test, "https://user@example.com:8080/"); |
| 128 testURLParsing(test, "https://user@example.com:8080/foo"); |
| 129 testURLParsing(test, "https://user@example.com:8080/foo/bar"); |
| 130 testURLParsing( |
| 131 test, |
| 132 "https://user@example.com:8080/foo/bar?https://random/foo/bar" |
| 133 ); |
| 134 |
| 135 testURLParsing(test, "https://user:pass@example.com"); |
| 136 testURLParsing(test, "https://user:pass@example.com/"); |
| 137 testURLParsing(test, "https://user:pass@example.com/foo"); |
| 138 testURLParsing(test, "https://user:pass@example.com/foo/bar"); |
| 139 testURLParsing( |
| 140 test, |
| 141 "https://user:pass@example.com/foo/bar?https://random/foo/bar" |
| 142 ); |
| 143 |
| 144 testURLParsing(test, "https://user:pass@example.com:8080"); |
| 145 testURLParsing(test, "https://user:pass@example.com:8080/"); |
| 146 testURLParsing(test, "https://user:pass@example.com:8080/foo"); |
| 147 testURLParsing(test, "https://user:pass@example.com:8080/foo/bar"); |
| 148 testURLParsing( |
| 149 test, |
| 150 "https://user:pass@example.com:8080/foo/bar?https://random/foo/bar" |
| 151 ); |
| 152 |
| 153 testURLParsing(test, "https://us%40er:pa%40ss@example.com"); |
| 154 testURLParsing(test, "https://us%40er:pa%40ss@example.com/"); |
| 155 testURLParsing(test, "https://us%40er:pa%40ss@example.com/foo"); |
| 156 testURLParsing(test, "https://us%40er:pa%40ss@example.com/foo/bar"); |
| 157 testURLParsing( |
| 158 test, |
| 159 "https://us%40er:pa%40ss@example.com/foo/bar?https://random/foo/bar" |
| 160 ); |
| 161 |
| 162 testURLParsing(test, "https://us%40er:pa%40ss@example.com:8080"); |
| 163 testURLParsing(test, "https://us%40er:pa%40ss@example.com:8080/"); |
| 164 testURLParsing(test, "https://us%40er:pa%40ss@example.com:8080/foo"); |
| 165 testURLParsing(test, "https://us%40er:pa%40ss@example.com:8080/foo/bar"); |
| 166 testURLParsing( |
| 167 test, |
| 168 "https://us%40er:pa%40ss@example.com:8080/foo/bar?https://random/foo/bar" |
| 169 ); |
| 170 |
| 171 testURLParsing(test, "http://192.168.1.1"); |
| 172 testURLParsing(test, "http://192.168.1.1/"); |
| 173 testURLParsing(test, "http://192.168.1.1/foo"); |
| 174 testURLParsing(test, "http://192.168.1.1/foo/bar"); |
| 175 testURLParsing( |
| 176 test, |
| 177 "http://192.168.1.1/foo/bar?https://random/foo/bar" |
| 178 ); |
| 179 testURLParsing( |
| 180 test, |
| 181 "http://192.168.1.1:8080/foo/bar?https://random/foo/bar" |
| 182 ); |
| 183 testURLParsing( |
| 184 test, |
| 185 "http://user@192.168.1.1:8080/foo/bar?https://random/foo/bar" |
| 186 ); |
| 187 testURLParsing( |
| 188 test, |
| 189 "http://user:pass@192.168.1.1:8080/foo/bar?https://random/foo/bar" |
| 190 ); |
| 191 |
| 192 testURLParsing(test, "http://[2001:0db8:0000:0042:0000:8a2e:0370:7334]"); |
| 193 testURLParsing(test, "http://[2001:0db8:0000:0042:0000:8a2e:0370:7334]/"); |
| 194 testURLParsing(test, "http://[2001:0db8:0000:0042:0000:8a2e:0370:7334]/foo"); |
| 195 testURLParsing( |
| 196 test, |
| 197 "http://[2001:0db8:0000:0042:0000:8a2e:0370:7334]/foo/bar" |
| 198 ); |
| 199 testURLParsing( |
| 200 test, |
| 201 "http://[2001:0db8:0000:0042:0000:8a2e:0370:7334]/foo/bar?https://random/foo
/bar" |
| 202 ); |
| 203 testURLParsing( |
| 204 test, |
| 205 "http://[2001:0db8:0000:0042:0000:8a2e:0370:7334]:8080/foo/bar?https://rando
m/foo/bar" |
| 206 ); |
| 207 testURLParsing( |
| 208 test, |
| 209 "http://user@[2001:0db8:0000:0042:0000:8a2e:0370:7334]:8080/foo/bar?https://
random/foo/bar" |
| 210 ); |
| 211 testURLParsing( |
| 212 test, |
| 213 "http://user:pass@[2001:0db8:0000:0042:0000:8a2e:0370:7334]:8080/foo/bar?htt
ps://random/foo/bar" |
| 214 ); |
| 215 |
| 216 testURLParsing(test, "ftp://user:pass@example.com:8021"); |
| 217 testURLParsing(test, "ftp://user:pass@example.com:8021/"); |
| 218 testURLParsing(test, "ftp://user:pass@example.com:8021/foo"); |
| 219 testURLParsing(test, "ftp://user:pass@example.com:8021/foo/bar"); |
| 220 |
| 221 testURLParsing(test, "about:blank"); |
| 222 testURLParsing(test, "chrome://extensions"); |
| 223 testURLParsing( |
| 224 test, |
| 225 "chrome-extension://bhignfpcigccnlfapldlodmhlidjaion/options.html" |
| 226 ); |
| 227 testURLParsing(test, "mailto:john.doe@mail.example.com"); |
| 228 |
| 229 testURLParsing(test, "news:newsgroup"); |
| 230 testURLParsing(test, "news:message-id"); |
| 231 testURLParsing(test, "nntp://example.com:8119/newsgroup"); |
| 232 testURLParsing(test, "nntp://example.com:8119/message-id"); |
| 233 |
| 234 testURLParsing(test, "data:,"); |
| 235 testURLParsing( |
| 236 test, |
| 237 "data:text/vnd-example+xyz;foo=bar;base64,R0lGODdh" |
| 238 ); |
| 239 testURLParsing( |
| 240 test, |
| 241 "data:text/plain;charset=UTF-8;page=21,the%20data:1234,5678" |
| 242 ); |
| 243 |
| 244 testURLParsing(test, "javascript:"); |
| 245 testURLParsing(test, "javascript:alert();"); |
| 246 testURLParsing(test, "javascript:foo/bar/"); |
| 247 testURLParsing(test, "javascript://foo/bar/"); |
| 248 |
| 249 testURLParsing(test, "file:///dev/random"); |
| 250 |
| 251 testURLParsing(test, "wss://example.com/"); |
| 252 testURLParsing(test, "wss://example.com:8080/"); |
| 253 testURLParsing(test, "wss://user@example.com:8080/"); |
| 254 testURLParsing(test, "wss://user:pass@example.com:8080/"); |
| 255 |
| 256 testURLParsing(test, "stuns:stuns.example.com/"); |
| 257 testURLParsing(test, "stuns:stuns.example.com:8080/"); |
| 258 testURLParsing(test, "stuns:user@stuns.example.com:8080/"); |
| 259 testURLParsing(test, "stuns:user:pass@stuns.example.com:8080/"); |
| 260 |
| 261 // The following tests are based on |
| 262 // https://cs.chromium.org/chromium/src/url/gurl_unittest.cc?rcl=9ec7bc85e0f6a
0bf28eff6b2eca678067da547e9 |
| 263 // Note: We do not check for "canonicalization" (normalization). parseURL() |
| 264 // should be used with normalized URLs only. |
| 265 |
| 266 testURLParsing(test, "something:///example.com/"); |
| 267 testURLParsing(test, "something://example.com/"); |
| 268 |
| 269 testURLParsing(test, "file:///C:/foo.txt"); |
| 270 testURLParsing(test, "file://server/foo.txt"); |
| 271 |
| 272 testURLParsing(test, "http://user:pass@example.com:99/foo;bar?q=a#ref"); |
| 273 |
| 274 testURLParsing(test, "http://user:%40!$&'()*+,%3B%3D%3A@example.com:12345/"); |
| 275 |
| 276 testURLParsing(test, "filesystem:http://example.com/temporary/"); |
| 277 testURLParsing( |
| 278 test, |
| 279 "filesystem:http://user:%40!$&'()*+,%3B%3D%3A@example.com:12345/" |
| 280 ); |
| 281 |
| 282 testURLParsing(test, "javascript:window.alert('hello, world');"); |
| 283 testURLParsing(test, "javascript:#"); |
| 284 |
| 285 testURLParsing( |
| 286 test, |
| 287 "blob:https://example.com/7ce70a1e-9681-4148-87a8-43cb9171b994" |
| 288 ); |
| 289 |
| 290 testURLParsing(test, "http://[2001:db8::1]"); |
| 291 testURLParsing(test, "http://[2001:db8::1]:8080"); |
| 292 testURLParsing(test, "http://[::]:8080"); |
| 293 |
| 294 testURLParsing(test, "not-a-standard-scheme:this is arbitrary content"); |
| 295 testURLParsing(test, "view-source:http://example.com/path"); |
| 296 |
| 297 testURLParsing( |
| 298 test, |
| 299 "data:text/html,Question?%3Cdiv%20style=%22color:%20#bad%22%3Eidea%3C/div%3E
" |
| 300 ); |
| 301 |
| 302 test.done(); |
| 303 }; |
| 304 |
67 exports.testNormalizeHostname = function(test) | 305 exports.testNormalizeHostname = function(test) |
68 { | 306 { |
69 test.equal(normalizeHostname("example.com"), "example.com"); | 307 test.equal(normalizeHostname("example.com"), "example.com"); |
70 test.equal(normalizeHostname("example.com."), "example.com"); | 308 test.equal(normalizeHostname("example.com."), "example.com"); |
71 test.equal(normalizeHostname("example.com.."), "example.com"); | 309 test.equal(normalizeHostname("example.com.."), "example.com"); |
72 test.equal(normalizeHostname("example.com..."), "example.com"); | 310 test.equal(normalizeHostname("example.com..."), "example.com"); |
73 | 311 |
74 test.equal(normalizeHostname("Example.com"), "example.com"); | 312 test.equal(normalizeHostname("Example.com"), "example.com"); |
75 test.equal(normalizeHostname("ExaMple.Com"), "example.com"); | 313 test.equal(normalizeHostname("ExaMple.Com"), "example.com"); |
76 test.equal(normalizeHostname("ExaMple.Com.."), "example.com"); | 314 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" | 526 "us-east-1.amazonaws.com" |
289 ); | 527 ); |
290 test.equal(getBaseDomain("example.amazonaws.com"), "amazonaws.com"); | 528 test.equal(getBaseDomain("example.amazonaws.com"), "amazonaws.com"); |
291 test.equal(getBaseDomain("amazonaws.com"), "amazonaws.com"); | 529 test.equal(getBaseDomain("amazonaws.com"), "amazonaws.com"); |
292 | 530 |
293 // Edge case. | 531 // Edge case. |
294 test.equal(getBaseDomain(""), ""); | 532 test.equal(getBaseDomain(""), ""); |
295 | 533 |
296 test.done(); | 534 test.done(); |
297 }; | 535 }; |
OLD | NEW |