Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(webhook): return 400 (not 500) when a webhook URL's DNS lookup rejects (#457)

Softronice27bc21ba083
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 0739cb79e..46d77a698 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -15,6 +15,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Fixed
- Sessions (Baileys): when a session is logged out — unlinked from the phone or via the API — the now-invalid on-disk auth state is cleared, so re-linking shows a fresh QR instead of getting stuck silently reloading the dead credentials. (#453 — thanks @ulises2k)
+- Webhooks: registering a webhook (`POST /sessions/:id/webhooks`) to a host whose DNS lookup *rejects* (NXDOMAIN, or a transient `EAI_AGAIN`/`ESERVFAIL` under resolver pressure) now returns `400 Could not resolve host: <host> (<code>)` instead of a generic `500 Internal server error`. The SSRF guard's DNS deadline already mapped resolution *timeouts* and empty results to a 4xx; a rejected lookup leaked the raw DNS error, which surfaced as an intermittent 500 during back-to-back session-create → webhook-register flows.
## [0.7.1] - 2026-06-24
diff --git a/src/common/security/ssrf-guard.spec.ts b/src/common/security/ssrf-guard.spec.ts
index 8354b6565..772c97bc9 100644
--- a/src/common/security/ssrf-guard.spec.ts
+++ b/src/common/security/ssrf-guard.spec.ts
@@ -199,9 +199,14 @@ describe('resolveSafeFetchTarget', () => {
await expect(resolveSafeFetchTarget('https://rebind.example/hook')).rejects.toThrow(SsrfBlockedError);
});
- it('propagates a DNS lookup failure (rejection) rather than hanging', async () => {
- (dnsPromises.lookup as jest.Mock).mockRejectedValueOnce(new Error('ENOTFOUND'));
- await expect(resolveSafeFetchTarget('https://nxdomain.example/hook')).rejects.toThrow(/ENOTFOUND/);
+ it('maps a DNS lookup failure (rejection) to SsrfBlockedError instead of leaking a raw error', async () => {
+ // A rejected lookup (NXDOMAIN, or a transient EAI_AGAIN under resolver pressure) must become a
+ // typed SsrfBlockedError so callers map it to a 4xx. A raw error here leaks as a generic 500 —
+ // the intermittent failure seen at webhook registration (POST /sessions/:id/webhooks).
+ (dnsPromises.lookup as jest.Mock).mockRejectedValueOnce(
+ Object.assign(new Error('getaddrinfo ENOTFOUND nxdomain.example'), { code: 'ENOTFOUND' }),
+ );
+ await expect(resolveSafeFetchTarget('https://nxdomain.example/hook')).rejects.toThrow(SsrfBlockedError);
});
it('rejects when DNS resolution exceeds the deadline (a hanging resolver cannot pin a worker)', async () => {
diff --git a/src/common/security/ssrf-guard.ts b/src/common/security/ssrf-guard.ts
index 1928279a1..25dd816ec 100644
--- a/src/common/security/ssrf-guard.ts
+++ b/src/common/security/ssrf-guard.ts
@@ -182,8 +182,10 @@ function resolveDnsTimeoutMs(): number {
/**
* Resolve a host with `{ all: true }`, bounded by a deadline so a hanging/slow DNS resolver cannot
* pin a worker indefinitely (the lookup is otherwise unbounded). The default deadline is generous
- * and overridable via SSRF_DNS_TIMEOUT_MS. On expiry it throws SsrfBlockedError; the in-flight lookup
- * is left to settle with its late result swallowed (no unhandledRejection).
+ * and overridable via SSRF_DNS_TIMEOUT_MS. On expiry — or on a rejected lookup (NXDOMAIN, transient
+ * EAI_AGAIN, ESERVFAIL, …) — it throws SsrfBlockedError; the in-flight lookup is left to settle with
+ * its late result swallowed (no unhandledRejection). Wrapping the rejection keeps every resolution
+ * failure typed, so callers map it to a 4xx instead of leaking a raw DNS error as a generic 500.
*/
async function lookupWithDeadline(host: string): Promise<LookupAddress[]> {
const lookupPromise = lookup(host, { all: true });
@@ -194,6 +196,10 @@ async function lookupWithDeadline(host: string): Promise<LookupAddress[]> {
});
try {
return await Promise.race([lookupPromise, deadline]);
+ } catch (err) {
+ if (err instanceof SsrfBlockedError) throw err; // deadline already produced a typed error
+ const code = (err as NodeJS.ErrnoException)?.code;
+ throw new SsrfBlockedError(`Could not resolve host: ${host}${code ? ` (${code})` : ''}`);
} finally {
if (timer) clearTimeout(timer);
}