Skip to content

Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS

address review (codex): fail closed when SSRF validation lookup fails, not fail-open (#1859)

[._.]/ Adam Eivy478837383210
diff --git a/server/lib/safeUrlFetch.js b/server/lib/safeUrlFetch.js
index 75ca5b1bb8..95bc58c336 100644
--- a/server/lib/safeUrlFetch.js
+++ b/server/lib/safeUrlFetch.js
@@ -34,10 +34,12 @@ const DEFAULT_MAX_BYTES = 12 * 1024 * 1024; // 12 MB — generous for a single i
* TOCTOU where a re-resolution at connect could land on a private address the
* validation lookup never saw. Never throws.
*
- * `address` is null when there is nothing to pin: an IP-literal target (the
- * connect goes straight to the literal — no name to re-resolve, so no TOCTOU)
- * or a lookup that failed (the fetch falls back to its own resolution, exactly
- * as before, preserving prior fail-open-on-resolve behavior).
+ * `address` is null only for an IP-literal target (the connect goes straight to
+ * the literal — there's no name to re-resolve, so no TOCTOU and nothing to pin).
+ * A hostname that can't be resolved fails CLOSED ({ safe: false }) rather than
+ * falling through to an unpinned fetch: an unpinned connect would re-resolve at
+ * connect time, exactly the rebinding window this guard exists to close (an
+ * attacker can fail the validation lookup, then answer a private IP at connect).
*
* @returns {Promise<{ safe: boolean, address?: string|null, family?: number }>}
*/
@@ -49,9 +51,9 @@ async function resolvePublicHttpUrl(target) {
const isIpLiteral = /^[\d.]+$/.test(hostname) || hostname.includes(':');
if (isIpLiteral) return { safe: true, address: null };
const resolved = await dns.lookup(hostname).catch(() => null);
- if (resolved?.address && isBlockedIngestHost(resolved.address)) return { safe: false };
- // resolved null (lookup failed) → safe but unpinned (matches prior behavior).
- return { safe: true, address: resolved?.address ?? null, family: resolved?.family };
+ if (!resolved?.address) return { safe: false }; // can't vet → can't safely pin → fail closed
+ if (isBlockedIngestHost(resolved.address)) return { safe: false };
+ return { safe: true, address: resolved.address, family: resolved.family };
}
/**
diff --git a/server/lib/safeUrlFetch.test.js b/server/lib/safeUrlFetch.test.js
index 21574f2217..ba93bb1451 100644
--- a/server/lib/safeUrlFetch.test.js
+++ b/server/lib/safeUrlFetch.test.js
@@ -117,11 +117,14 @@ describe('connect-time IP pinning (DNS-rebinding TOCTOU, #1859)', () => {
expect(fetchMock.mock.calls[0][1].dispatcher).toBeDefined();
});
- it('skips pinning (no dispatcher) when the lookup fails — preserves fail-open', async () => {
+ it('fails closed when the validation lookup fails (no unpinned connect-time re-resolve)', async () => {
lookupMock.mockRejectedValue(new Error('ENOTFOUND'));
- fetchMock.mockResolvedValue(res({ text: 'ok' }));
- await fetchPublicText('https://transient-dns.example.com/x');
- expect(fetchMock.mock.calls[0][1].dispatcher).toBeUndefined();
+ // A hostname we can't vet must not fall through to an unpinned fetch — the
+ // first hop throws like any other unsafe URL and never reaches the network.
+ await expect(fetchPublicText('https://unresolvable.example.com/x'))
+ .rejects.toMatchObject({ status: 400, code: 'UNSAFE_URL' });
+ expect(fetchMock).not.toHaveBeenCalled();
+ expect(await isPublicHttpUrlSafe('https://unresolvable.example.com/x')).toBe(false);
});
// End-to-end proof the mechanism actually defeats rebinding: a hostname that