| 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-2015 Eyeo GmbH | 3 * Copyright (C) 2006-2015 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 |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 46 } | 46 } |
| 47 | 47 |
| 48 return URL; | 48 return URL; |
| 49 })(); | 49 })(); |
| 50 | 50 |
| 51 /** | 51 /** |
| 52 * Gets the IDN-decoded hostname from a URL object. | 52 * Gets the IDN-decoded hostname from a URL object. |
| 53 * | 53 * |
| 54 * @param {URL} url | 54 * @param {URL} url |
| 55 * @return {string} | 55 * @return {string} |
| 56 * @static |
| 56 */ | 57 */ |
| 57 function getDecodedHostname(url) | 58 function getDecodedHostname(url) |
| 58 { | 59 { |
| 59 let hostname = url.hostname; | 60 let hostname = url.hostname; |
| 60 | 61 |
| 61 if (hostname.indexOf("xn--") == -1) | 62 if (hostname.indexOf("xn--") == -1) |
| 62 return hostname; | 63 return hostname; |
| 63 | 64 |
| 64 return punycode.toUnicode(hostname); | 65 return punycode.toUnicode(hostname); |
| 65 } | 66 } |
| 66 exports.getDecodedHostname = getDecodedHostname; | 67 exports.getDecodedHostname = getDecodedHostname; |
| 67 | 68 |
| 68 /** | 69 /** |
| 69 * Gets the IDN-decoded hostname from the URL of a frame. | 70 * Gets the IDN-decoded hostname from the URL of a frame. |
| 70 * If the URL don't have host information (like "about:blank" | 71 * If the URL don't have host information (like "about:blank" |
| 71 * and "data:" URLs) it falls back to the parent frame. | 72 * and "data:" URLs) it falls back to the parent frame. |
| 72 * | 73 * |
| 73 * @param {Frame} frame | 74 * @param {Frame} frame |
| 74 * @return {string} | 75 * @return {string} |
| 75 */ | 76 */ |
| 76 function extractHostFromFrame(frame) | 77 exports.extractHostFromFrame = function(frame) |
| 77 { | 78 { |
| 78 for (; frame; frame = frame.parent) | 79 for (; frame; frame = frame.parent) |
| 79 { | 80 { |
| 80 let hostname = getDecodedHostname(frame.url); | 81 let hostname = getDecodedHostname(frame.url); |
| 81 if (hostname) | 82 if (hostname) |
| 82 return hostname; | 83 return hostname; |
| 83 } | 84 } |
| 84 | 85 |
| 85 return ""; | 86 return ""; |
| 86 } | 87 }; |
| 87 exports.extractHostFromFrame = extractHostFromFrame; | |
| 88 | 88 |
| 89 /** | 89 /** |
| 90 * Converts a URL object into a string. For HTTP(S) URLs | 90 * Converts a URL object into a string. For HTTP(S) URLs |
| 91 * the hostname gets IDN-decoded and the hash is stripped. | 91 * the hostname gets IDN-decoded and the hash is stripped. |
| 92 * | 92 * |
| 93 * @param {URL} url | 93 * @param {URL} url |
| 94 * @return {string} | 94 * @return {string} |
| 95 */ | 95 */ |
| 96 function stringifyURL(url) | 96 exports.stringifyURL = function(url) |
| 97 { | 97 { |
| 98 let protocol = url.protocol; | 98 let protocol = url.protocol; |
| 99 let href = url.href; | 99 let href = url.href; |
| 100 | 100 |
| 101 if (protocol == "http:" || protocol == "https:") | 101 if (protocol == "http:" || protocol == "https:") |
| 102 { | 102 { |
| 103 let hostname = url.hostname; | 103 let hostname = url.hostname; |
| 104 if (hostname.indexOf("xn--") != -1) | 104 if (hostname.indexOf("xn--") != -1) |
| 105 href = href.replace(hostname, punycode.toUnicode(hostname)); | 105 href = href.replace(hostname, punycode.toUnicode(hostname)); |
| 106 | 106 |
| 107 let hash = href.indexOf("#"); | 107 let hash = href.indexOf("#"); |
| 108 if (hash != -1) | 108 if (hash != -1) |
| 109 href = href.substr(0, hash); | 109 href = href.substr(0, hash); |
| 110 } | 110 } |
| 111 | 111 |
| 112 return href; | 112 return href; |
| 113 } | 113 }; |
| 114 | |
| 115 exports.stringifyURL = stringifyURL; | |
| 116 | 114 |
| 117 function isDomain(hostname) | 115 function isDomain(hostname) |
| 118 { | 116 { |
| 119 // No hostname or IPv4 address, also considering hexadecimal octets. | 117 // No hostname or IPv4 address, also considering hexadecimal octets. |
| 120 if (/^((0x[\da-f]+|\d+)(\.|$))*$/i.test(hostname)) | 118 if (/^((0x[\da-f]+|\d+)(\.|$))*$/i.test(hostname)) |
| 121 return false; | 119 return false; |
| 122 | 120 |
| 123 // IPv6 address. Since there can't be colons in domains, we can | 121 // IPv6 address. Since there can't be colons in domains, we can |
| 124 // just check whether there are any colons to exclude IPv6 addresses. | 122 // just check whether there are any colons to exclude IPv6 addresses. |
| 125 return hostname.indexOf(":") == -1; | 123 return hostname.indexOf(":") == -1; |
| (...skipping 21 matching lines...) Expand all Loading... |
| 147 return bits.slice(cutoff).join("."); | 145 return bits.slice(cutoff).join("."); |
| 148 } | 146 } |
| 149 | 147 |
| 150 /** | 148 /** |
| 151 * Checks whether the request's origin is different from the document's origin. | 149 * Checks whether the request's origin is different from the document's origin. |
| 152 * | 150 * |
| 153 * @param {URL} url The request URL | 151 * @param {URL} url The request URL |
| 154 * @param {string} documentHost The IDN-decoded hostname of the document | 152 * @param {string} documentHost The IDN-decoded hostname of the document |
| 155 * @return {Boolean} | 153 * @return {Boolean} |
| 156 */ | 154 */ |
| 157 function isThirdParty(url, documentHost) | 155 exports.isThirdParty = function(url, documentHost) |
| 158 { | 156 { |
| 159 let requestHost = getDecodedHostname(url).replace(/\.+$/, ""); | 157 let requestHost = getDecodedHostname(url).replace(/\.+$/, ""); |
| 160 documentHost = documentHost.replace(/\.+$/, ""); | 158 documentHost = documentHost.replace(/\.+$/, ""); |
| 161 | 159 |
| 162 if (requestHost == documentHost) | 160 if (requestHost == documentHost) |
| 163 return false; | 161 return false; |
| 164 | 162 |
| 165 if (!isDomain(requestHost) || !isDomain(documentHost)) | 163 if (!isDomain(requestHost) || !isDomain(documentHost)) |
| 166 return true; | 164 return true; |
| 167 | 165 |
| 168 return getBaseDomain(requestHost) != getBaseDomain(documentHost); | 166 return getBaseDomain(requestHost) != getBaseDomain(documentHost); |
| 169 } | 167 }; |
| 170 exports.isThirdParty = isThirdParty; | |
| OLD | NEW |