summaryrefslogtreecommitdiff
path: root/src/lib
diff options
context:
space:
mode:
authorhackademix2018-09-04 18:48:14 +0200
committerhackademix2018-09-04 19:28:16 +0200
commit26470b84f681b7db9e500bb4503eab7b8b202879 (patch)
tree842228c86ab61eb149868752479b53f19b05d9c9 /src/lib
parentdf149a5a5578f3d34ba404836fccca8d2cfa508e (diff)
downloadnoscript-26470b84f681b7db9e500bb4503eab7b8b202879.tar.gz
noscript-26470b84f681b7db9e500bb4503eab7b8b202879.tar.xz
noscript-26470b84f681b7db9e500bb4503eab7b8b202879.zip
Transparent support for FQDNs and better management of file:/// URLs.
Diffstat (limited to 'src/lib')
-rw-r--r--src/lib/restricted.js2
-rw-r--r--src/lib/tld.js28
2 files changed, 19 insertions, 11 deletions
diff --git a/src/lib/restricted.js b/src/lib/restricted.js
index ae85ba8..6415221 100644
--- a/src/lib/restricted.js
+++ b/src/lib/restricted.js
@@ -21,7 +21,7 @@
function isRestrictedURL(u) {
try {
if (typeof u === "string") u = new URL(u);
- return u.protocol === "https:" && domains.includes(u.hostname);
+ return u.protocol === "https:" && domains.includes(tld.normalize(u.hostname || ""));
} catch (e) {
return false;
}
diff --git a/src/lib/tld.js b/src/lib/tld.js
index 397fe81..15d42e5 100644
--- a/src/lib/tld.js
+++ b/src/lib/tld.js
@@ -1,12 +1,18 @@
var tld = {
- normalize(d) { return d; },
+ preserveFQDNs: false,
+
+ // remove trailing dots from FQDNs
+ normalize(d) { return d.endsWith(".") ? d.slice(0, -1) : d },
isIp(d) { return this._ipRx.test(d); },
- getDomain(domain) {
- if (domain === "localhost" || this.isIp(domain)) return domain;
+ getDomain(aDomain) {
+ if (aDomain === "localhost" || this.isIp(aDomain)) return aDomain;
+
+ domain = this.normalize(aDomain);
+
+ let resultDomain = this.preserveFQDNs ? aDomain : domain;
- domain = this.normalize(domain);
var pos = domain.search(this._tldEx);
if(pos === -1 ) {
pos = domain.search(this._tldRx);
@@ -16,20 +22,22 @@ var tld = {
pos = domain.lastIndexOf(".");
if (pos === -1) {
// No dots at all? Likely a private domain in a LAN.
- return domain;
+ return resultDomain;
}
}
pos = domain.lastIndexOf(".", pos - 1) + 1;
} else if(domain[pos] == ".") {
++pos;
}
- return pos <= 0 ? domain : domain.substring(pos);
+ return pos <= 0 ? resultDomain : resultDomain.substring(pos);
},
- getPublicSuffix(domain) {
- if (this.isIp(domain)) return "";
+ getPublicSuffix(aDomain) {
+ if (this.isIp(aDomain)) return "";
+
+ domain = this.normalize(aDomain);
+ let resultDomain = this.preserveFQDNs ? aDomain : domain;
- domain = this.normalize(domain);
var pos = domain.search(this._tldEx);
if(pos < 0) {
pos = domain.search(this._tldRx);
@@ -37,7 +45,7 @@ var tld = {
} else {
pos = domain.indexOf(".", pos + 1) + 1;
}
- return pos < 0 ? "" : domain.substring(pos);
+ return pos < 0 ? "" : resultDomain.substring(pos);
},
_ipRx: /^(?:0\.|[1-9]\d{0,2}\.){3}(?:0|[1-9]\d{0,2})$|:.*:/i,