From 48053d96d44abbfa3c659e5fc817d998dda2bb35 Mon Sep 17 00:00:00 2001 From: hackademix Date: Mon, 10 Sep 2018 19:10:32 +0200 Subject: Better IPV6 support. --- src/bg/ChildPolicies.js | 25 ++++++++++++++++--------- 1 file changed, 16 insertions(+), 9 deletions(-) (limited to 'src/bg/ChildPolicies.js') diff --git a/src/bg/ChildPolicies.js b/src/bg/ChildPolicies.js index 7f94ce0..7df3253 100644 --- a/src/bg/ChildPolicies.js +++ b/src/bg/ChildPolicies.js @@ -56,7 +56,7 @@ let protocolRx = /^(\w+):/i; let pathRx = /(?:[^:/]\/|:\/{3})$/; let portRx = /:\d+(?=\/|$)/; - let validMatchPatternRx = /^(?:\*|(?:http|ws|ftp)s?|file):\/\/(?:\*|(?:\*\.)?[\w\u0100-\uf000][\w\u0100-\uf000.-]*)?\/(\*|[^*]*)$/; + let validMatchPatternRx = /^(?:\*|(?:http|ws|ftp)s?|file):\/\/(?:\*|(?:\*\.)?[\w\u0100-\uf000][\w\u0100-\uf000.-]*|\[[\w:]+\])?\/(\*|[^*]*)$/; let validMatchPattern = mp => validMatchPatternRx.test(mp); @@ -67,23 +67,30 @@ mp = Sites.cleanUrl(mp); if (!mp) return false; } else { - let protocol = Sites.isSecureDomainKey(site) ? "https://" : "*://"; - mp = `${protocol}*`; + mp = Sites.isSecureDomainKey(site) ? "https://" : "*://"; let hostname = Sites.toggleSecureDomainKey(site, false).replace(portRx, ''); if (hostname && hostname !== ".") { - if (!tld.preserveFQDNs) hostname = tld.normalize(hostname); - mp += hostname.startsWith(".") ? hostname : `.${hostname}`; + if (tld.isIp(hostname) || hostname.includes("*")) { + mp += hostname; + } else { + if (!tld.preserveFQDNs) hostname = tld.normalize(hostname); + mp += hostname.startsWith(".") ? `*${hostname}` : `*.${hostname}`; + } + } else { + mp += "*"; } if (!(hostname && hostname.includes("/"))) mp += "/"; } - return validMatchPatternRx.test(mp) && ( - mp.endsWith("/") ? `${mp}*` : [mp, `${mp}?*`, `${mp}#*`]); + return validMatchPattern(mp) && + (mp.endsWith("/") ? `${mp}*` : [mp, `${mp}?*`, `${mp}#*`]); }; let withFQDNs = patterns => { - return tld.preserveFQDNs ? patterns : patterns.concat( - patterns.map(p => p.replace(/^(?:\w+|\*):\/\/[^/]*[^.*/]/, '$&.') + if (tld.preserveFQDNs) return patterns; + let rx = /^(?:\w+|\*):\/\/([^/]*[^.*/])/; + return patterns.concat( + patterns.map(p => p.replace(rx, (m, host) => tld.isIp(host) ? m : m + ".") ).filter(validMatchPattern) ); } -- cgit v1.2.3