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 /** @module url */ | 18 /** @module url */ |
19 | 19 |
| 20 let getDomain = require("tldjs").getDomain; |
| 21 |
20 let getDecodedHostname = | 22 let getDecodedHostname = |
21 /** | 23 /** |
22 * Gets the IDN-decoded hostname from a URL object. | 24 * Gets the IDN-decoded hostname from a URL object. |
23 * | 25 * |
24 * @param {URL} url | 26 * @param {URL} url |
25 * @return {string} | 27 * @return {string} |
26 * @static | 28 * @static |
27 */ | 29 */ |
28 exports.getDecodedHostname = function(url) | 30 exports.getDecodedHostname = function(url) |
29 { | 31 { |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
85 { | 87 { |
86 // No hostname or IPv4 address, also considering hexadecimal octets. | 88 // No hostname or IPv4 address, also considering hexadecimal octets. |
87 if (/^((0x[\da-f]+|\d+)(\.|$))*$/i.test(hostname)) | 89 if (/^((0x[\da-f]+|\d+)(\.|$))*$/i.test(hostname)) |
88 return false; | 90 return false; |
89 | 91 |
90 // IPv6 address. Since there can't be colons in domains, we can | 92 // IPv6 address. Since there can't be colons in domains, we can |
91 // just check whether there are any colons to exclude IPv6 addresses. | 93 // just check whether there are any colons to exclude IPv6 addresses. |
92 return hostname.indexOf(":") == -1; | 94 return hostname.indexOf(":") == -1; |
93 } | 95 } |
94 | 96 |
95 function getBaseDomain(hostname) | |
96 { | |
97 let bits = hostname.split("."); | |
98 let cutoff = bits.length - 2; | |
99 | |
100 for (let i = 0; i < bits.length; i++) | |
101 { | |
102 let offset = publicSuffixes[bits.slice(i).join(".")]; | |
103 | |
104 if (typeof offset != "undefined") | |
105 { | |
106 cutoff = i - offset; | |
107 break; | |
108 } | |
109 } | |
110 | |
111 if (cutoff <= 0) | |
112 return hostname; | |
113 | |
114 return bits.slice(cutoff).join("."); | |
115 } | |
116 | |
117 /** | 97 /** |
118 * Checks whether the request's origin is different from the document's origin. | 98 * Checks whether the request's origin is different from the document's origin. |
119 * | 99 * |
120 * @param {URL} url The request URL | 100 * @param {URL} url The request URL |
121 * @param {string} documentHost The IDN-decoded hostname of the document | 101 * @param {string} documentHost The IDN-decoded hostname of the document |
122 * @return {Boolean} | 102 * @return {Boolean} |
123 */ | 103 */ |
124 exports.isThirdParty = function(url, documentHost) | 104 exports.isThirdParty = function(url, documentHost) |
125 { | 105 { |
126 let requestHost = getDecodedHostname(url).replace(/\.+$/, ""); | 106 let requestHost = getDecodedHostname(url).replace(/\.+$/, ""); |
127 documentHost = documentHost.replace(/\.+$/, ""); | 107 documentHost = documentHost.replace(/\.+$/, ""); |
128 | 108 |
129 if (requestHost == documentHost) | 109 if (requestHost == documentHost) |
130 return false; | 110 return false; |
131 | 111 |
132 if (!isDomain(requestHost) || !isDomain(documentHost)) | 112 if (!isDomain(requestHost) || !isDomain(documentHost)) |
133 return true; | 113 return true; |
134 | 114 |
135 return getBaseDomain(requestHost) != getBaseDomain(documentHost); | 115 return getDomain(requestHost) != getDomain(documentHost); |
136 }; | 116 }; |
OLD | NEW |