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