| 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 /** @module url */ | 18 /** @module url */ |
| 19 | 19 |
| 20 "use strict"; | 20 "use strict"; |
| 21 | 21 |
| 22 const {getDomain} = require("./tldjs"); | |
| 23 | |
| 24 /** | 22 /** |
| 25 * Gets the IDN-decoded hostname from the URL of a frame. | 23 * Gets the IDN-decoded hostname from the URL of a frame. |
| 26 * If the URL don't have host information (like "about:blank" | 24 * If the URL don't have host information (like "about:blank" |
| 27 * and "data:" URLs) it falls back to the parent frame. | 25 * and "data:" URLs) it falls back to the parent frame. |
| 28 * | 26 * |
| 29 * @param {?Frame} frame | 27 * @param {?Frame} frame |
| 30 * @param {URL} [originUrl] | 28 * @param {URL} [originUrl] |
| 31 * @return {string} | 29 * @return {string} |
| 32 */ | 30 */ |
| 33 exports.extractHostFromFrame = (frame, originUrl) => | 31 exports.extractHostFromFrame = (frame, originUrl) => |
| 34 { | 32 { |
| 35 for (; frame; frame = frame.parent) | 33 for (; frame; frame = frame.parent) |
| 36 { | 34 { |
| 37 let {hostname} = frame.url; | 35 let {hostname} = frame.url; |
| 38 if (hostname) | 36 if (hostname) |
| 39 return hostname; | 37 return hostname; |
| 40 } | 38 } |
| 41 | 39 |
| 42 return originUrl ? originUrl.hostname : ""; | 40 return originUrl ? originUrl.hostname : ""; |
| 43 }; | 41 }; |
| 44 | |
| 45 function isDomain(hostname) | |
| 46 { | |
| 47 // No hostname or IPv4 address, also considering hexadecimal octets. | |
| 48 if (/^((0x[\da-f]+|\d+)(\.|$))*$/i.test(hostname)) | |
| 49 return false; | |
| 50 | |
| 51 // IPv6 address. Since there can't be colons in domains, we can | |
| 52 // just check whether there are any colons to exclude IPv6 addresses. | |
| 53 return hostname.indexOf(":") == -1; | |
| 54 } | |
| 55 | |
| 56 /** | |
| 57 * Checks whether the request's origin is different from the document's origin. | |
| 58 * | |
| 59 * @param {URL} url The request URL | |
| 60 * @param {string} documentHost The IDN-decoded hostname of the document | |
| 61 * @return {Boolean} | |
| 62 */ | |
| 63 exports.isThirdParty = (url, documentHost) => | |
| 64 { | |
| 65 let requestHost = url.hostname.replace(/\.+$/, ""); | |
| 66 documentHost = documentHost.replace(/\.+$/, ""); | |
| 67 | |
| 68 if (requestHost == documentHost) | |
| 69 return false; | |
| 70 | |
| 71 if (!isDomain(requestHost) || !isDomain(documentHost)) | |
| 72 return true; | |
| 73 | |
| 74 return getDomain(requestHost) != getDomain(documentHost); | |
| 75 }; | |
| OLD | NEW |