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"); | 22 const {getDomain} = require("./tldjs"); |
23 const punycode = require("./punycode"); | |
24 | |
25 let getDecodedHostname = | |
26 /** | |
27 * Gets the IDN-decoded hostname from a URL object. | |
28 * | |
29 * @param {URL} url | |
30 * @return {string} | |
31 * @static | |
32 */ | |
33 exports.getDecodedHostname = url => | |
34 { | |
35 let {hostname} = url; | |
36 | |
37 if (hostname.indexOf("xn--") == -1) | |
38 return hostname; | |
39 | |
40 return punycode.toUnicode(hostname); | |
41 }; | |
42 | 23 |
43 /** | 24 /** |
44 * Gets the IDN-decoded hostname from the URL of a frame. | 25 * Gets the IDN-decoded hostname from the URL of a frame. |
45 * If the URL don't have host information (like "about:blank" | 26 * If the URL don't have host information (like "about:blank" |
46 * and "data:" URLs) it falls back to the parent frame. | 27 * and "data:" URLs) it falls back to the parent frame. |
47 * | 28 * |
48 * @param {?Frame} frame | 29 * @param {?Frame} frame |
49 * @param {URL} [originUrl] | 30 * @param {URL} [originUrl] |
50 * @return {string} | 31 * @return {string} |
51 */ | 32 */ |
52 exports.extractHostFromFrame = (frame, originUrl) => | 33 exports.extractHostFromFrame = (frame, originUrl) => |
53 { | 34 { |
54 for (; frame; frame = frame.parent) | 35 for (; frame; frame = frame.parent) |
55 { | 36 { |
56 let hostname = getDecodedHostname(frame.url); | 37 let hostname = frame.url.hostname; |
57 if (hostname) | 38 if (hostname) |
58 return hostname; | 39 return hostname; |
59 } | 40 } |
60 | 41 |
61 return originUrl ? getDecodedHostname(originUrl) : ""; | 42 return originUrl ? originUrl.hostname : ""; |
62 }; | |
63 | |
64 /** | |
65 * Converts a URL object into a string. For HTTP(S) URLs | |
66 * the hostname gets IDN-decoded and the hash is stripped. | |
67 * | |
68 * @param {URL} url | |
69 * @return {string} | |
70 */ | |
71 exports.stringifyURL = url => | |
72 { | |
73 let {protocol, href} = url; | |
74 | |
75 if (protocol == "http:" || protocol == "https:") | |
76 { | |
77 let {hostname} = url; | |
78 if (hostname.indexOf("xn--") != -1) | |
79 href = href.replace(hostname, punycode.toUnicode(hostname)); | |
80 | |
81 let hash = href.indexOf("#"); | |
82 if (hash != -1) | |
83 href = href.substr(0, hash); | |
84 } | |
85 | |
86 return href; | |
87 }; | 43 }; |
88 | 44 |
89 function isDomain(hostname) | 45 function isDomain(hostname) |
90 { | 46 { |
91 // No hostname or IPv4 address, also considering hexadecimal octets. | 47 // No hostname or IPv4 address, also considering hexadecimal octets. |
92 if (/^((0x[\da-f]+|\d+)(\.|$))*$/i.test(hostname)) | 48 if (/^((0x[\da-f]+|\d+)(\.|$))*$/i.test(hostname)) |
93 return false; | 49 return false; |
94 | 50 |
95 // IPv6 address. Since there can't be colons in domains, we can | 51 // IPv6 address. Since there can't be colons in domains, we can |
96 // just check whether there are any colons to exclude IPv6 addresses. | 52 // just check whether there are any colons to exclude IPv6 addresses. |
97 return hostname.indexOf(":") == -1; | 53 return hostname.indexOf(":") == -1; |
98 } | 54 } |
99 | 55 |
100 /** | 56 /** |
101 * Checks whether the request's origin is different from the document's origin. | 57 * Checks whether the request's origin is different from the document's origin. |
102 * | 58 * |
103 * @param {URL} url The request URL | 59 * @param {URL} url The request URL |
104 * @param {string} documentHost The IDN-decoded hostname of the document | 60 * @param {string} documentHost The IDN-decoded hostname of the document |
105 * @return {Boolean} | 61 * @return {Boolean} |
106 */ | 62 */ |
107 exports.isThirdParty = (url, documentHost) => | 63 exports.isThirdParty = (url, documentHost) => |
108 { | 64 { |
109 let requestHost = getDecodedHostname(url).replace(/\.+$/, ""); | 65 let requestHost = url.hostname.replace(/\.+$/, ""); |
110 documentHost = documentHost.replace(/\.+$/, ""); | 66 documentHost = documentHost.replace(/\.+$/, ""); |
111 | 67 |
112 if (requestHost == documentHost) | 68 if (requestHost == documentHost) |
113 return false; | 69 return false; |
114 | 70 |
115 if (!isDomain(requestHost) || !isDomain(documentHost)) | 71 if (!isDomain(requestHost) || !isDomain(documentHost)) |
116 return true; | 72 return true; |
117 | 73 |
118 return getDomain(requestHost) != getDomain(documentHost); | 74 return getDomain(requestHost) != getDomain(documentHost); |
119 }; | 75 }; |
OLD | NEW |