fix: simplify dns to use .lookup instead of .resolve4 and .resolve6, automatically allow requests to own hostname

This commit is contained in:
Julian Lam
2025-05-22 15:36:22 -04:00
parent 9d3b8c3abc
commit df36021628

View File

@@ -90,6 +90,11 @@ async function call(url, method, { body, timeout, jar, ...config } = {}) {
// Checks url to ensure it is not in reserved IP range (private, etc.) // Checks url to ensure it is not in reserved IP range (private, etc.)
async function check(url) { async function check(url) {
const { host } = new URL(url);
if (host === nconf.get('url_parsed').host) {
return true;
}
const cached = checkCache.get(url); const cached = checkCache.get(url);
if (cached) { if (cached) {
return cached; return cached;
@@ -99,16 +104,9 @@ async function check(url) {
if (ipaddr.isValid(url)) { if (ipaddr.isValid(url)) {
addresses.add(url); addresses.add(url);
} else { } else {
const { host } = new URL(url); const lookup = await dns.lookup(host, { all: true });
const [v4, v6] = await Promise.all([ lookup.forEach(({ address }) => {
dns.resolve4(host), addresses.add(address);
dns.resolve6(host),
]);
v4.forEach((ip) => {
addresses.add(ip);
});
v6.forEach((ip) => {
addresses.add(ip);
}); });
} }
@@ -118,7 +116,7 @@ async function check(url) {
return parsed.range() === 'unicast'; return parsed.range() === 'unicast';
}); });
checkCache.set(url, ok); checkCache.set(host, ok);
return ok; return ok;
} }