Skip to content

Commit

Permalink
fix: fixed performance with silent ban lookup
Browse files Browse the repository at this point in the history
  • Loading branch information
titanism committed Dec 12, 2024
1 parent 0978092 commit 79dcf9d
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 7 deletions.
9 changes: 6 additions & 3 deletions helpers/is-allowlisted.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const parseHostFromDomainOrAddress = require('#helpers/parse-host-from-domain-or
const parseRootDomain = require('#helpers/parse-root-domain');

// eslint-disable-next-line complexity
async function isAllowlisted(val, client, resolver) {
async function isAllowlisted(val, client, resolver, ignoreRedis = false) {
const lowerCased = punycode.toASCII(val).toLowerCase().trim();

// check hard-coded allowlist
Expand Down Expand Up @@ -70,12 +70,13 @@ async function isAllowlisted(val, client, resolver) {
const [clientHostname] = await resolver.reverse(val);
if (isFQDN(clientHostname)) {
// check domain
if (await isAllowlisted(clientHostname, client, resolver)) return true;
if (await isAllowlisted(clientHostname, client, resolver, ignoreRedis))
return true;
// check root domain (if differed)
const root = parseRootDomain(clientHostname);
if (
clientHostname !== root &&
(await isAllowlisted(root, client, resolver))
(await isAllowlisted(root, client, resolver, ignoreRedis))
)
return true;
}
Expand All @@ -88,6 +89,8 @@ async function isAllowlisted(val, client, resolver) {
}
}

if (ignoreRedis) return false;

const result = await client.get(`allowlist:${lowerCased}`);

return boolean(result);
Expand Down
14 changes: 10 additions & 4 deletions helpers/is-silent-banned.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,23 @@ const isAllowlisted = require('#helpers/is-allowlisted');
async function isSilentBanned(value, client, resolver) {
if (!Array.isArray(value)) value = [value];

// lowercase and trim all the values
value = value.map((v) => punycode.toASCII(v).toLowerCase().trim());

// `value` can be anything arbitrary (IP, hostname, email address)
// and it can be an Array of Strings or a String

// filter out values from array if they were allowlisted
const filtered = await pFilter(
value,
async (v) => {
const allowlisted = await isAllowlisted(v, client, resolver);
//
// NOTE: `true` as the last arg makes it so this ignores redis cache and only
// checks for hard-coded allowlist/truth source values (and our own hostname)
//
const allowlisted = await isAllowlisted(
punycode.toASCII(v).toLowerCase().trim(),
client,
resolver,
true
);
return !allowlisted;
},
{ concurrency: config.concurrency }
Expand Down

0 comments on commit 79dcf9d

Please sign in to comment.