From 61dd7225c7b6a2bb9346c76926b5e96264f831b8 Mon Sep 17 00:00:00 2001 From: Amin Bandali Date: Wed, 8 Apr 2020 21:52:58 -0400 Subject: Update HTTPS Everywhere to 2020.3.16. --- .../background-scripts/util.js | 71 ++++++++++++++++++++++ 1 file changed, 71 insertions(+) (limited to 'data/extensions/https-everywhere@eff.org/background-scripts/util.js') diff --git a/data/extensions/https-everywhere@eff.org/background-scripts/util.js b/data/extensions/https-everywhere@eff.org/background-scripts/util.js index 1946e14..5a4097c 100644 --- a/data/extensions/https-everywhere@eff.org/background-scripts/util.js +++ b/data/extensions/https-everywhere@eff.org/background-scripts/util.js @@ -60,6 +60,73 @@ function loadExtensionFile(url, returnType) { return xhr.responseText; } +/** + * Remove tailing dots from hostname, e.g. "www.example.com." + */ +function getNormalisedHostname(hostname) { + while (hostname && hostname[hostname.length - 1] === '.' && hostname !== '.') { + hostname = hostname.slice(0, -1); + } + return hostname; +} + +// Empty iterable singleton to reduce memory usage +const nullIterable = Object.create(null, { + [Symbol.iterator]: { + value: function* () { + // do nothing + } + }, + + size: { + value: 0 + }, +}); + +/** + * Return true if host is well-formed (RFC 1035) + */ +function isValidHostname(host) { + if (host && host.length > 0 && host.length <= 255 && host.indexOf("..") === -1) { + return true; + } + return false; +} + +/** + * Return a list of wildcard expressions which support + * the host under HTTPS Everywhere's implementation + */ +function getWildcardExpressions(host) { + // Ensure host is well-formed (RFC 1035) + if (!isValidHostname(host)) { + return nullIterable; + } + + // Ensure host does not contain a wildcard itself + if (host.indexOf("*") != -1) { + return nullIterable; + } + + let results = []; + + // Replace www.example.com with www.example.* + // eat away from the right for once and only once + let segmented = host.split("."); + if (segmented.length > 1) { + const tmp = [...segmented.slice(0, segmented.length - 1), "*"].join("."); + results.push(tmp); + } + + // now eat away from the left, with *, so that for x.y.z.google.com we + // check *.y.z.google.com, *.z.google.com and *.google.com + for (let i = 1; i < segmented.length - 1; i++) { + const tmp = ["*", ...segmented.slice(i, segmented.length)].join("."); + results.push(tmp); + } + return results; +} + /** * Convert an ArrayBuffer to string * @@ -84,6 +151,10 @@ Object.assign(exports, { NOTE, WARN, log, + nullIterable, + isValidHostname, + getNormalisedHostname, + getWildcardExpressions, setDefaultLogLevel, getDefaultLogLevel, loadExtensionFile, -- cgit v1.2.3