| 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-2016 Eyeo GmbH | 3  * Copyright (C) 2006-2016 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 (function() | 
| 20 { | 21 { | 
| 21   let {getDecodedHostname, | 22   let {getDecodedHostname, | 
| 22        extractHostFromFrame, | 23        extractHostFromFrame, | 
| 23        stringifyURL, | 24        stringifyURL, | 
| 24        isThirdParty} = require("url"); | 25        isThirdParty} = require("url"); | 
| 25 | 26 | 
| 26   module("URL/host tools"); | 27   module("URL/host tools"); | 
| 27 | 28 | 
| 28   test("Extracting hostname from URL", () => | 29   test("Extracting hostname from URL", () => | 
| 29   { | 30   { | 
| 30     function testURLHostname(url, expectedHostname, message) | 31     function testURLHostname(url, expectedHostname, message) | 
| 31     { | 32     { | 
| 32       equal(getDecodedHostname(new URL(url)), expectedHostname, message); | 33       equal(getDecodedHostname(new URL(url)), expectedHostname, message); | 
| 33     } | 34     } | 
| 34 | 35 | 
| 35     testURLHostname("http://example.com/foo", "example.com", "with path"); | 36     testURLHostname("http://example.com/foo", "example.com", "with path"); | 
| 36     testURLHostname("http://example.com/?foo=bar", "example.com", "with query"); | 37     testURLHostname("http://example.com/?foo=bar", "example.com", "with query"); | 
| 37     testURLHostname("http://example.com/#top", "example.com", "with hash"); | 38     testURLHostname("http://example.com/#top", "example.com", "with hash"); | 
| 38     testURLHostname("http://example.com:8080/", "example.com", "with port"); | 39     testURLHostname("http://example.com:8080/", "example.com", "with port"); | 
| 39     testURLHostname("http://user:password@example.com/", "example.com", "with au
     th credentials"); | 40     testURLHostname("http://user:password@example.com/", "example.com", | 
| 40     testURLHostname("http://xn--f-1gaa.com/", "f\u00f6\u00f6.com", "with punycod
     e"); | 41                     "with auth credentials"); | 
|  | 42     testURLHostname("http://xn--f-1gaa.com/", "f\u00f6\u00f6.com", | 
|  | 43                     "with punycode"); | 
| 41     testURLHostname("about:blank", "", "about:blank"); | 44     testURLHostname("about:blank", "", "about:blank"); | 
| 42     testURLHostname("data:text/plain,foo", "", "data: URL"); | 45     testURLHostname("data:text/plain,foo", "", "data: URL"); | 
| 43     testURLHostname("ftp://example.com/", "example.com", "ftp: URL"); | 46     testURLHostname("ftp://example.com/", "example.com", "ftp: URL"); | 
| 44     testURLHostname("http://1.2.3.4:8000/", "1.2.3.4", "IPv4 address"); | 47     testURLHostname("http://1.2.3.4:8000/", "1.2.3.4", "IPv4 address"); | 
| 45     testURLHostname("http://[2001:db8:85a3::8a2e:370:7334]/", "[2001:db8:85a3::8
     a2e:370:7334]", "IPv6 address"); | 48     testURLHostname("http://[2001:db8:85a3::8a2e:370:7334]/", | 
|  | 49                     "[2001:db8:85a3::8a2e:370:7334]", "IPv6 address"); | 
| 46   }); | 50   }); | 
| 47 | 51 | 
| 48   test("Extracting hostname from frame", () => | 52   test("Extracting hostname from frame", () => | 
| 49   { | 53   { | 
| 50     function testFrameHostname(hierarchy, expectedHostname, message) | 54     function testFrameHostname(hierarchy, expectedHostname, message) | 
| 51     { | 55     { | 
| 52       let frame = null; | 56       let frame = null; | 
| 53 | 57 | 
| 54       for (let url of hierarchy) | 58       for (let url of hierarchy) | 
| 55         frame = {parent: frame, url: new URL(url)}; | 59         frame = {parent: frame, url: new URL(url)}; | 
| 56 | 60 | 
| 57       equal(extractHostFromFrame(frame), expectedHostname, message); | 61       equal(extractHostFromFrame(frame), expectedHostname, message); | 
| 58     } | 62     } | 
| 59 | 63 | 
| 60     testFrameHostname(["http://example.com/"], "example.com", "single frame"); | 64     testFrameHostname(["http://example.com/"], "example.com", "single frame"); | 
| 61     testFrameHostname(["http://example.com/", "http://example.org/"], "example.o
     rg", "with parent frame"); | 65     testFrameHostname(["http://example.com/", "http://example.org/"], | 
| 62     testFrameHostname(["http://example.com/", "data:text/plain,foo"], "example.c
     om", "data: URL, hostname in parent"); | 66                       "example.org", "with parent frame"); | 
| 63     testFrameHostname(["http://example.com/", "about:blank", "about:blank"], "ex
     ample.com", "about:blank, hostname in ancestor"); | 67     testFrameHostname(["http://example.com/", "data:text/plain,foo"], | 
| 64     testFrameHostname(["about:blank", "about:blank"], "", "about:blank, no hostn
     ame"); | 68                       "example.com", "data: URL, hostname in parent"); | 
| 65     testFrameHostname(["http://xn--f-1gaa.com/"], "f\u00f6\u00f6.com", "with pun
     ycode"); | 69     testFrameHostname(["http://example.com/", "about:blank", "about:blank"], | 
|  | 70                       "example.com", "about:blank, hostname in ancestor"); | 
|  | 71     testFrameHostname(["about:blank", "about:blank"], "", | 
|  | 72                       "about:blank, no hostname"); | 
|  | 73     testFrameHostname(["http://xn--f-1gaa.com/"], "f\u00f6\u00f6.com", | 
|  | 74                       "with punycode"); | 
| 66   }); | 75   }); | 
| 67 | 76 | 
| 68   test("Stringifying URLs", () => | 77   test("Stringifying URLs", () => | 
| 69   { | 78   { | 
| 70     function testNormalizedURL(url, expectedURL, message) | 79     function testNormalizedURL(url, expectedURL, message) | 
| 71     { | 80     { | 
| 72       equal(stringifyURL(new URL(url)), expectedURL, message); | 81       equal(stringifyURL(new URL(url)), expectedURL, message); | 
| 73     } | 82     } | 
| 74 | 83 | 
| 75     function testPreservedURL(url, message) | 84     function testPreservedURL(url, message) | 
| 76     { | 85     { | 
| 77       testNormalizedURL(url, url, message); | 86       testNormalizedURL(url, url, message); | 
| 78     } | 87     } | 
| 79 | 88 | 
| 80     testPreservedURL("http://example.com/foo", "includes path"); | 89     testPreservedURL("http://example.com/foo", "includes path"); | 
| 81     testPreservedURL("http://example.com/?foo=bar", "includes query"); | 90     testPreservedURL("http://example.com/?foo=bar", "includes query"); | 
| 82     testPreservedURL("http://example.com:8080/", "includes port"); | 91     testPreservedURL("http://example.com:8080/", "includes port"); | 
| 83     testPreservedURL("http://example.com/?", "with empty query string"); | 92     testPreservedURL("http://example.com/?", "with empty query string"); | 
| 84     testNormalizedURL("http://example.com/#top","http://example.com/", "stripped
      hash"); | 93     testNormalizedURL("http://example.com/#top", "http://example.com/", | 
| 85     testNormalizedURL("http://example.com/#top?", "http://example.com/", "stripp
     ed hash with trailing question mark"); | 94                       "stripped hash"); | 
| 86     testNormalizedURL("http://xn--f-1gaa.com/","http://f\u00f6\u00f6.com/", "dec
     oded punycode"); | 95     testNormalizedURL("http://example.com/#top?", "http://example.com/", | 
|  | 96                       "stripped hash with trailing question mark"); | 
|  | 97     testNormalizedURL("http://xn--f-1gaa.com/", "http://f\u00f6\u00f6.com/", | 
|  | 98                       "decoded punycode"); | 
| 87     testPreservedURL("about:blank", "about:blank"); | 99     testPreservedURL("about:blank", "about:blank"); | 
| 88     testPreservedURL("data:text/plain,foo", "data: URL"); | 100     testPreservedURL("data:text/plain,foo", "data: URL"); | 
| 89   }); | 101   }); | 
| 90 | 102 | 
| 91   test("Third-party checks", () => | 103   test("Third-party checks", () => | 
| 92   { | 104   { | 
| 93     function hostnameToURL(hostname) | 105     function hostnameToURL(hostname) | 
| 94     { | 106     { | 
| 95       return new URL("http://" + hostname); | 107       return new URL("http://" + hostname); | 
| 96     } | 108     } | 
| 97 | 109 | 
| 98     function testThirdParty(requestHost, documentHost, expected, message) | 110     function testThirdParty(requestHost, documentHost, expected, message) | 
| 99     { | 111     { | 
| 100       equal( | 112       equal( | 
| 101         isThirdParty( | 113         isThirdParty( | 
| 102           hostnameToURL(requestHost), | 114           hostnameToURL(requestHost), | 
| 103 | 115 | 
| 104           // Chrome's URL object normalizes IP addresses. So some test | 116           // Chrome's URL object normalizes IP addresses. So some test | 
| 105           // will fail if we don't normalize the document host as well. | 117           // will fail if we don't normalize the document host as well. | 
| 106           getDecodedHostname(hostnameToURL(documentHost)) | 118           getDecodedHostname(hostnameToURL(documentHost)) | 
| 107         ), | 119         ), | 
| 108         expected, | 120         expected, | 
| 109         message | 121         message | 
| 110       ); | 122       ); | 
| 111     } | 123     } | 
| 112 | 124 | 
| 113     testThirdParty("foo", "foo", false, "same domain isn't third-party"); | 125     testThirdParty("foo", "foo", false, "same domain isn't third-party"); | 
| 114     testThirdParty("foo", "bar", true, "different domain is third-party"); | 126     testThirdParty("foo", "bar", true, "different domain is third-party"); | 
| 115     testThirdParty("foo.com", "foo.com", false, "same domain with TLD (.com) isn
     't third-party"); | 127     testThirdParty("foo.com", "foo.com", false, | 
| 116     testThirdParty("foo.com", "bar.com", true, "same TLD (.com) but different do
     main is third-party"); | 128                    "same domain with TLD (.com) isn't third-party"); | 
| 117     testThirdParty("foo.com", "www.foo.com", false, "same domain but differend s
     ubdomain isn't third-party"); | 129     testThirdParty("foo.com", "bar.com", true, | 
| 118     testThirdParty("foo.example.com", "bar.example.com", false, "same basedomain
      (example.com) isn't third-party"); | 130                    "same TLD (.com) but different domain is third-party"); | 
| 119     testThirdParty("foo.uk", "bar.uk", true, "same TLD (.uk) but different domai
     n is third-party"); | 131     testThirdParty("foo.com", "www.foo.com", false, | 
| 120     testThirdParty("foo.co.uk", "bar.co.uk", true, "same TLD (.co.uk) but differ
     ent domain is third-party"); | 132                    "same domain but differend subdomain isn't third-party"); | 
| 121     testThirdParty("foo.example.co.uk", "bar.example.co.uk", false, "same basedo
     main (example.co.uk) isn't third-party"); | 133     testThirdParty("foo.example.com", "bar.example.com", false, | 
| 122     testThirdParty("1.2.3.4", "1.2.3.4", false, "same IPv4 address isn't third-p
     arty"); | 134                    "same basedomain (example.com) isn't third-party"); | 
| 123     testThirdParty("1.1.1.1", "2.1.1.1", true, "different IPv4 address is third-
     party"); | 135     testThirdParty("foo.uk", "bar.uk", true, | 
| 124     testThirdParty("0x01ff0101", "0x01ff0101", false, "same IPv4 hexadecimal add
     ress isn't third-party"); | 136                    "same TLD (.uk) but different domain is third-party"); | 
| 125     testThirdParty("0x01ff0101", "0x01ff0102", true, "different IPv4 hexadecimal
      address is third-party"); | 137     testThirdParty("foo.co.uk", "bar.co.uk", true, | 
| 126     testThirdParty("1.0xff.3.4", "1.0xff.3.4", false, "same IPv4 address with he
     xadecimal octet isn't third-party"); | 138                    "same TLD (.co.uk) but different domain is third-party"); | 
| 127     testThirdParty("1.0xff.1.1", "2.0xff.1.1", true, "different IPv4 address wit
     h hexadecimal octet is third-party"); | 139     testThirdParty("foo.example.co.uk", "bar.example.co.uk", false, | 
| 128     testThirdParty("0xff.example.com", "example.com", false, "domain starts like
      a hexadecimal IPv4 address but isn't one"); | 140                    "same basedomain (example.co.uk) isn't third-party"); | 
| 129     testThirdParty("[2001:db8:85a3::8a2e:370:7334]", "[2001:db8:85a3::8a2e:370:7
     334]", false, "same IPv6 address isn't third-party"); | 141     testThirdParty("1.2.3.4", "1.2.3.4", false, | 
| 130     testThirdParty("[2001:db8:85a3::8a2e:370:7334]", "[5001:db8:85a3::8a2e:370:7
     334]", true, "different IPv6 address is third-party"); | 142                    "same IPv4 address isn't third-party"); | 
| 131     testThirdParty("[::ffff:192.0.2.128]", "[::ffff:192.0.2.128]", false, "same 
     IPv4-mapped IPv6 address isn't third-party"); | 143     testThirdParty("1.1.1.1", "2.1.1.1", true, | 
| 132     testThirdParty("[::ffff:192.0.2.128]", "[::ffff:192.1.2.128]", true, "differ
     ent IPv4-mapped IPv6 address is third-party"); | 144                    "different IPv4 address is third-party"); | 
| 133     testThirdParty("xn--f-1gaa.com", "f\u00f6\u00f6.com", false, "same IDN isn't
      third-party"); | 145     testThirdParty("0x01ff0101", "0x01ff0101", false, | 
| 134     testThirdParty("example.com..", "example.com....", false, "traling dots are 
     ignored"); | 146                    "same IPv4 hexadecimal address isn't third-party"); | 
|  | 147     testThirdParty("0x01ff0101", "0x01ff0102", true, | 
|  | 148                    "different IPv4 hexadecimal address is third-party"); | 
|  | 149     testThirdParty( | 
|  | 150       "1.0xff.3.4", "1.0xff.3.4", false, | 
|  | 151       "same IPv4 address with hexadecimal octet isn't third-party" | 
|  | 152     ); | 
|  | 153     testThirdParty( | 
|  | 154       "1.0xff.1.1", "2.0xff.1.1", true, | 
|  | 155       "different IPv4 address with hexadecimal octet is third-party" | 
|  | 156     ); | 
|  | 157     testThirdParty( | 
|  | 158       "0xff.example.com", "example.com", false, | 
|  | 159       "domain starts like a hexadecimal IPv4 address but isn't one" | 
|  | 160     ); | 
|  | 161     testThirdParty( | 
|  | 162       "[2001:db8:85a3::8a2e:370:7334]", "[2001:db8:85a3::8a2e:370:7334]", false, | 
|  | 163       "same IPv6 address isn't third-party" | 
|  | 164     ); | 
|  | 165     testThirdParty( | 
|  | 166       "[2001:db8:85a3::8a2e:370:7334]", "[5001:db8:85a3::8a2e:370:7334]", true, | 
|  | 167       "different IPv6 address is third-party" | 
|  | 168     ); | 
|  | 169     testThirdParty( | 
|  | 170       "[::ffff:192.0.2.128]", "[::ffff:192.0.2.128]", false, | 
|  | 171       "same IPv4-mapped IPv6 address isn't third-party" | 
|  | 172     ); | 
|  | 173     testThirdParty( | 
|  | 174       "[::ffff:192.0.2.128]", "[::ffff:192.1.2.128]", true, | 
|  | 175       "different IPv4-mapped IPv6 address is third-party" | 
|  | 176     ); | 
|  | 177     testThirdParty("xn--f-1gaa.com", "f\u00f6\u00f6.com", false, | 
|  | 178                    "same IDN isn't third-party"); | 
|  | 179     testThirdParty("example.com..", "example.com....", false, | 
|  | 180                    "traling dots are ignored"); | 
| 135   }); | 181   }); | 
| 136 } | 182 }()); | 
| OLD | NEW | 
|---|