Skip to content

Commit

Permalink
fix: fixed hostname lookup causing uncaught exception
Browse files Browse the repository at this point in the history
  • Loading branch information
titanism committed Aug 21, 2024
1 parent 4573ca7 commit 8af531b
Showing 1 changed file with 21 additions and 12 deletions.
33 changes: 21 additions & 12 deletions helpers/is-allowlisted.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ const { isEmail } = require('validator');

const config = require('#config');
const env = require('#config/env');
const logger = require('#helpers/logger');
const parseHostFromDomainOrAddress = require('#helpers/parse-host-from-domain-or-address');
const parseRootDomain = require('#helpers/parse-root-domain');

Expand Down Expand Up @@ -75,18 +76,26 @@ async function isAllowlisted(val, client, resolver) {
const root = parseRootDomain(val);
if (root === env.WEB_HOST) return true;
} else if (isIP(val)) {
// reverse lookup IP and if it was allowlisted then return early
const [clientHostname] = await resolver.reverse(val);
if (isFQDN(clientHostname)) {
// check domain
if (await isAllowlisted(clientHostname, client, resolver)) return true;
// check root domain (if differed)
const root = parseRootDomain(clientHostname);
if (
clientHostname !== root &&
(await isAllowlisted(root, client, resolver))
)
return true;
try {
// reverse lookup IP and if it was allowlisted then return early
const [clientHostname] = await resolver.reverse(val);
if (isFQDN(clientHostname)) {
// check domain
if (await isAllowlisted(clientHostname, client, resolver)) return true;
// check root domain (if differed)
const root = parseRootDomain(clientHostname);
if (
clientHostname !== root &&
(await isAllowlisted(root, client, resolver))
)
return true;
}
} catch (err) {
//
// NOTE: the native Node.js DNS module would throw an error previously
// <https://github.com/nodejs/node/issues/3112#issuecomment-1452548779>
//
if (env.NODE_ENV !== 'production') logger.debug(err);
}
}

Expand Down

0 comments on commit 8af531b

Please sign in to comment.