Skip to content

fix(db): pin safeFetch to vetted IPs to close a DNS-rebinding window - #273

Open
shaikn6 wants to merge 1 commit into
trycompai:mainfrom
shaikn6:fix/safe-fetch-dns-pin
Open

shaikn6 wants to merge 1 commit into
trycompai:mainfrom
shaikn6:fix/safe-fetch-dns-pin

Conversation

@shaikn6

@shaikn6 shaikn6 commented Nov 14, 2026 •

Copy link
Copy Markdown

Use case

I run the CRM self-hosted. The agent and tracking fetch external URLs through safeFetch. I want safeFetch to stop an SSRF reaching internal or cloud-metadata hosts, including when DNS answers change between the check and the connect.

Problem

safeFetch resolves the host in resolvesToPublicHost, checks every address is public, then calls fetch. fetch resolves the host again at connect time. A name that passes the public check resolves to a private or metadata address on the second lookup, and safeFetch connects to the internal host. This is a DNS-rebinding TOCTOU. Severity: Medium. Three callers take an attacker-controlled URL (tracked site in apps/api/src/tracking/tracking.service.ts, MCP OAuth metadata in packages/auth/src/mcp-oauth.ts, company domain via favicon/blob), http: is allowed, and favicon/blob persist the bytes.

Fix

safeFetch resolves the host once and issues the request through node:http/node:https with a pinned lookup that returns only those vetted addresses. fetch is no longer used, so there is no second resolution. For https it sets servername to the hostname, so SNI and certificate validation still run against the hostname while the socket goes to a vetted IP. The public-address check runs again on every redirect hop. A latent bug is fixed too: the lookup honors options.all.

No new dependency. An earlier revision used an undici dispatcher, but Bun's fetch ignores the dispatcher, so the dispatcher approach was inert in this repo's runtime. node:http/node:https with a pinned lookup works on both.

Validation

  • cd packages/db && bun test test/safe-fetch.spec.ts test/favicon.spec.ts -> 9 pass, 0 fail.
  • tsc --noEmit on the file is clean.
  • Runtime check on both bun 1.3.14 and node v26: safeFetch("https://example.com/") returns 200 and a 577-byte body over a pinned IP; safeFetch to a loopback literal returns null; a non-http scheme returns null.
  • node:https.request with a pinned lookup + servername was confirmed to connect to the pinned IP and still validate the certificate against the hostname on both runtimes.

Issues

  1. RISK — Each call makes a new connection and does not reuse a keep-alive pool. Favicon and tracking volume is low, so this is acceptable.

@vercel

vercel Bot commented Nov 14, 2026

Copy link
Copy Markdown

@shaikn6 is attempting to deploy a commit to the Comp AI - PoC Team on Vercel.

A member of the Team first needs to authorize it.

@shaikn6
shaikn6 force-pushed the fix/safe-fetch-dns-pin branch from 8e14111 to a40484c Compare November 15, 2026 17:51
@shaikn6
shaikn6 marked this pull request as draft November 15, 2026 17:52
resolvesToPublicHost resolved the hostname and checked the addresses, then
fetch resolved the hostname again at connect time. A name that passed the
public check could resolve to a private or metadata address on the second
lookup, so safeFetch could still connect to an internal host.

safeFetch now resolves the host once and issues the request through
node:http/node:https with a pinned lookup that returns only those vetted
addresses, and a servername so TLS still validates against the hostname.
The check runs again on every redirect hop. This works on both Node and
Bun (Bun's fetch ignores a dispatcher, so no undici dependency is used).
Adds a test for the public-host guard and the safeFetch refusals.
@shaikn6
shaikn6 force-pushed the fix/safe-fetch-dns-pin branch from a40484c to c636f8b Compare November 15, 2026 18:23
@shaikn6
shaikn6 marked this pull request as ready for review November 15, 2026 18:24
@comp-review

comp-review Bot commented Nov 19, 2026 •

Copy link
Copy Markdown

Comp Review · c636f8bd7e99

Human approval · 0 of 1 received.

Codex review · Pending.

Why approval is needed

safeFetch is rewritten from fetch to node:http/https with a pinned lookup to close a DNS-rebinding SSRF. This reworks a security boundary guarding internal hosts and cloud metadata, so a mistake could reopen the bypass or weaken TLS/address checks.

What to verify

  • Confirm the pinned lookup returns only vetted addresses and that each redirect hop re-runs the public-address check before connecting.
  • Confirm https requests set servername and still validate the certificate against the hostname, and that blocked IPv4/IPv6 literals remain rejected.

@comp-review comp-review Bot added the needs-1-approval Comp Review: one person must approve label Nov 19, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

needs-1-approval Comp Review: one person must approve

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant