diff options
author | hackademix | 2018-08-23 17:48:38 +0200 |
---|---|---|
committer | hackademix | 2018-08-23 17:48:38 +0200 |
commit | 286acd20792601efd8699aded61a05fd72899311 (patch) | |
tree | c124387b1bf0195cafd8f5ad86409e26a58f7888 /src/common | |
parent | 60c299c4f1b1abb2f494c04d6b42aa683ef0098c (diff) | |
download | noscript-286acd20792601efd8699aded61a05fd72899311.tar.gz noscript-286acd20792601efd8699aded61a05fd72899311.tar.xz noscript-286acd20792601efd8699aded61a05fd72899311.zip |
More resilient and optimized version of Sites.domainImplies().
Diffstat (limited to 'src/common')
-rw-r--r-- | src/common/Policy.js | 15 |
1 files changed, 12 insertions, 3 deletions
diff --git a/src/common/Policy.js b/src/common/Policy.js index dae369a..3c31c71 100644 --- a/src/common/Policy.js +++ b/src/common/Policy.js @@ -5,7 +5,9 @@ var {Permissions, Policy, Sites} = (() => { const SECURE_DOMAIN_RX = new RegExp(`^${SECURE_DOMAIN_PREFIX}`); const DOMAIN_RX = new RegExp(`(?:^\\w+://|${SECURE_DOMAIN_PREFIX})?([^/]*)`, "i"); const SKIP_RX = /^(?:(?:about|chrome|resource|moz-.*):|\[System)/; - + + let rxQuote = s => s.replace(/[.?*+^$[\]\\(){}|-]/g, "\\$&"); + class Sites extends Map { static secureDomainKey(domain) { return domain.includes(":") ? domain : `${SECURE_DOMAIN_PREFIX}${domain}`; @@ -27,12 +29,19 @@ var {Permissions, Policy, Sites} = (() => { } static domainImplies(domainKey, site, protocol ="https?") { + if (!site.includes(domainKey)) return false; + if (Sites.isSecureDomainKey(domainKey)) { protocol = "https"; domainKey = Sites.toggleSecureDomainKey(domainKey, false); } - return new RegExp(`^${protocol}://([^/?#:]+\\.)?${domainKey.replace(/\./g, "\\.")}(?:[:/]|$)`) - .test(site); + try { + return new RegExp(`^${protocol}://([^/?#:]+\\.)?${rxQuote(domainKey)}(?:[:/]|$)`) + .test(site); + } catch (e) { + error(e, `Cannot check if ${domainKey} implies ${site}`); + return false; + } } static isImplied(site, byKey) { |