Index: lib/domain.js |
=================================================================== |
--- a/lib/domain.js |
+++ b/lib/domain.js |
@@ -14,23 +14,34 @@ |
* You should have received a copy of the GNU General Public License |
* along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. |
*/ |
"use strict"; |
const publicSuffixes = require("../data/publicSuffixList.json"); |
+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) |
Manish Jethani
2019/01/23 03:25:25
There's no need to export this now.
|
{ |
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 +51,32 @@ |
break; |
} |
} |
if (cutoff <= 0) |
return hostname; |
return bits.slice(cutoff).join("."); |
-}; |
+} |
+ |
+/** |
+ * Checks whether the request's origin is different from the document's origin. |
+ * |
+ * @param {URL} url The request URL |
+ * @param {string} documentHost The IDN-decoded hostname of the document |
+ * @return {Boolean} |
+ */ |
+function isThirdParty(url, documentHost) |
+{ |
+ let requestHost = url.hostname.replace(/\.+$/, ""); |
+ documentHost = documentHost.replace(/\.+$/, ""); |
+ |
+ if (requestHost == documentHost) |
+ return false; |
+ |
+ if (!isDomain(requestHost) || !isDomain(documentHost)) |
+ return true; |
+ |
+ return getDomain(requestHost) != getDomain(documentHost); |
+} |
+ |
+exports.isThirdParty = isThirdParty; |