| 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   test.done(); | 
 |  252 }; | 
 |  253  | 
|   67 exports.testNormalizeHostname = function(test) |  254 exports.testNormalizeHostname = function(test) | 
|   68 { |  255 { | 
|   69   test.equal(normalizeHostname("example.com"), "example.com"); |  256   test.equal(normalizeHostname("example.com"), "example.com"); | 
|   70   test.equal(normalizeHostname("example.com."), "example.com"); |  257   test.equal(normalizeHostname("example.com."), "example.com"); | 
|   71   test.equal(normalizeHostname("example.com.."), "example.com"); |  258   test.equal(normalizeHostname("example.com.."), "example.com"); | 
|   72   test.equal(normalizeHostname("example.com..."), "example.com"); |  259   test.equal(normalizeHostname("example.com..."), "example.com"); | 
|   73  |  260  | 
|   74   test.equal(normalizeHostname("Example.com"), "example.com"); |  261   test.equal(normalizeHostname("Example.com"), "example.com"); | 
|   75   test.equal(normalizeHostname("ExaMple.Com"), "example.com"); |  262   test.equal(normalizeHostname("ExaMple.Com"), "example.com"); | 
|   76   test.equal(normalizeHostname("ExaMple.Com.."), "example.com"); |  263   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" |  475     "us-east-1.amazonaws.com" | 
|  289   ); |  476   ); | 
|  290   test.equal(getBaseDomain("example.amazonaws.com"), "amazonaws.com"); |  477   test.equal(getBaseDomain("example.amazonaws.com"), "amazonaws.com"); | 
|  291   test.equal(getBaseDomain("amazonaws.com"), "amazonaws.com"); |  478   test.equal(getBaseDomain("amazonaws.com"), "amazonaws.com"); | 
|  292  |  479  | 
|  293   // Edge case. |  480   // Edge case. | 
|  294   test.equal(getBaseDomain(""), ""); |  481   test.equal(getBaseDomain(""), ""); | 
|  295  |  482  | 
|  296   test.done(); |  483   test.done(); | 
|  297 }; |  484 }; | 
| OLD | NEW |