Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Unified Diff: lib/domain.js

Issue 29987585: Issue 7232 - Move third-party request check into adblockpluscore (Closed) Base URL: https://hg.adblockplus.org/adblockpluscore/
Patch Set: Add tests Created Jan. 23, 2019, 3:22 a.m.
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | test/domain.js » ('j') | test/domain.js » ('J')
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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;
« no previous file with comments | « no previous file | test/domain.js » ('j') | test/domain.js » ('J')

Powered by Google App Engine
This is Rietveld