Index: lib/domain.js |
=================================================================== |
--- a/lib/domain.js |
+++ b/lib/domain.js |
@@ -15,22 +15,39 @@ |
* along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. |
*/ |
"use strict"; |
const publicSuffixes = require("../data/publicSuffixList.json"); |
/** |
+ * Checks whether the given hostname is a domain. |
+ * |
+ * @param {string} hostname |
+ * @returns {boolean} |
+ */ |
+function isDomain(hostname) |
+{ |
+ // No hostname or IPv4 address, also considering hexadecimal octets. |
+ if (/^((0x[\da-f]+|\d+)(\.|$))*$/i.test(hostname)) |
+ return false; |
+ |
+ // IPv6 address. Since there can't be colons in domains, we can |
+ // just check whether there are any colons to exclude IPv6 addresses. |
+ return hostname.indexOf(":") == -1; |
+} |
+ |
+/** |
* Gets the base domain for the given hostname. |
* |
* @param {string} hostname |
* @returns {string} |
*/ |
-exports.getDomain = hostname => |
+function getDomain(hostname) |
{ |
let bits = hostname.split("."); |
let cutoff = bits.length - 2; |
for (let i = 0; i < bits.length; i++) |
{ |
let offset = publicSuffixes[bits.slice(i).join(".")]; |
@@ -40,9 +57,33 @@ |
break; |
} |
} |
if (cutoff <= 0) |
return hostname; |
return bits.slice(cutoff).join("."); |
-}; |
+} |
+ |
+/** |
+ * Checks whether a request's origin is different from its document's origin. |
+ * |
+ * @param {URL} url The request URL. |
+ * @param {string} documentHostname The IDNA-encoded hostname of the document. |
+ * |
+ * @returns {boolean} |
+ */ |
+function isThirdParty(url, documentHostname) |
+{ |
+ let requestHostname = url.hostname.replace(/\.+$/, ""); |
+ documentHostname = documentHostname.replace(/\.+$/, ""); |
+ |
+ if (requestHostname == documentHostname) |
+ return false; |
+ |
+ if (!isDomain(requestHostname) || !isDomain(documentHostname)) |
+ return true; |
+ |
+ return getDomain(requestHostname) != getDomain(documentHostname); |
+} |
+ |
+exports.isThirdParty = isThirdParty; |