Open-source platform for 3D AI agents. Turn text or a photo into a rigged, animated GLB avatar, give it an LLM brain, memory and a wallet, and embed it anywhere with one web component. Remote MCP server, x402 payments, Three.js, browser-native.
JavaScript 3d-generationai-agentsanimationavatardigital-humangenerative-aiglbgltfimage-to-3dllmmcpmcp-servermodel-context-protocolsolanatext-to-3dthreejsweb-componentswebglwebxrx402
fix(ssrf): destroy pinned agents instead of draining them so a stalled upstream cannot hold a request for 900 s
nirholasba3e58912641
diff --git a/api/_lib/alert-delivery.js b/api/_lib/alert-delivery.jsindex 29f7b9f90..214f4fefe 100644--- a/api/_lib/alert-delivery.js+++ b/api/_lib/alert-delivery.js@@ -13,7 +13,7 @@import { sql } from './db.js';import { hmacSha256, randomToken } from './crypto.js';-import { validatePublicUrl, resolvePublicHost, pinnedAgent, SsrfError } from './ssrf.js';+import { validatePublicUrl, resolvePublicHost, pinnedAgent, disposeAgent, SsrfError } from './ssrf.js';import { formatAlertSummary } from './pump-alert-eval.js';import { fetchUpstream } from './upstream-fetch.js';@@ -110,7 +110,7 @@ async function deliverWebhook(rule, payload) {return { attempted: true, ok: false, detail: errMsg(e) };} finally {clearTimeout(timer);- await agent.close().catch(() => {});+ await disposeAgent(agent);}}diff --git a/api/_lib/avatar-capabilities.js b/api/_lib/avatar-capabilities.jsindex b6cab5d68..d1877dd04 100644--- a/api/_lib/avatar-capabilities.js+++ b/api/_lib/avatar-capabilities.js@@ -24,7 +24,7 @@// and wonder why nothing changed.import { getObjectRange } from './r2.js';-import { resolvePublicHost, pinnedAgent, validatePublicUrl } from './ssrf.js';+import { disposeAgent, resolvePublicHost, pinnedAgent, validatePublicUrl } from './ssrf.js';import { inspectGlb, glbJsonChunkEnd } from './glb-inspect.js';import { conformanceFromNames, ARKIT_52 } from '../../src/runtime/arkit52.js';import { canonicalizeBoneName, CANONICAL_BONES } from '../../src/glb-canonicalize.js';@@ -107,7 +107,7 @@ async function readUntrustedGlbPrefix(rawUrl, length) {return buf.length > length ? buf.subarray(0, length) : buf;} finally {clearTimeout(timer);- await agent.close().catch(() => {});+ await disposeAgent(agent);}}diff --git a/api/_lib/fetch-model.js b/api/_lib/fetch-model.jsindex 897b1d25a..6ec37c833 100644--- a/api/_lib/fetch-model.js+++ b/api/_lib/fetch-model.js@@ -9,7 +9,7 @@// proxy. This fetcher adds the model-specific concerns on top: a byte size cap// and a total request/streaming timeout.-import { SsrfError, pinnedAgent, resolvePublicHost, validatePublicUrl } from './ssrf.js';+import { SsrfError, disposeAgent, pinnedAgent, resolvePublicHost, validatePublicUrl } from './ssrf.js';const DEFAULT_MAX_BYTES = 50 * 1024 * 1024; // 50 MBconst DEFAULT_TIMEOUT_MS = 20_000;@@ -73,7 +73,7 @@ export async function fetchModel(rawUrl, opts = {}) {// those addresses. Re-done every redirect so a redirect target that// rebinds to a private IP is caught and refused.const addrs = await resolvePublicHost(currentUrl.hostname);- if (agent) await agent.close().catch(() => {});+ await disposeAgent(agent);agent = pinnedAgent(currentUrl.hostname, addrs);const res = await fetch(currentUrl, {@@ -154,6 +154,6 @@ export async function fetchModel(rawUrl, opts = {}) {throw asFetchModelError(err);} finally {clearTimeout(timer);- if (agent) await agent.close().catch(() => {});+ await disposeAgent(agent);}}diff --git a/api/_lib/pay/probe.js b/api/_lib/pay/probe.jsindex dd9619d98..0e5dc478f 100644--- a/api/_lib/pay/probe.js+++ b/api/_lib/pay/probe.js@@ -8,7 +8,7 @@// nothing. Keeping the transport in one place means a hardening fix to one path// cannot leave the other exposed.-import { validatePublicUrl, resolvePublicHost, pinnedAgent, SsrfError } from '../ssrf.js';+import { validatePublicUrl, resolvePublicHost, pinnedAgent, disposeAgent, SsrfError } from '../ssrf.js';/** Canonical USDC mint on Solana mainnet. */export const USDC_SOLANA_MINT = 'EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v';@@ -77,7 +77,7 @@ export async function guardedFetch(rawUrl, {return { status: res.status, ok: res.ok, headers: res.headers, text };} finally {clearTimeout(timer);- await agent.close().catch(() => {});+ await disposeAgent(agent);}}diff --git a/api/_lib/ssrf.js b/api/_lib/ssrf.jsindex 290bece6d..5c67cd84c 100644--- a/api/_lib/ssrf.js+++ b/api/_lib/ssrf.js@@ -99,6 +99,18 @@ export function pinnedAgent(expectedHost, addrs) {});}+// Release a single-use pinned agent. Always destroy(), never close(): close()+// waits for every in-flight request on the agent to finish, and a response whose+// body was never read (a non-2xx we throw on, an oversized content-length, a+// redirect hop) never finishes while the upstream holds its socket open. That is+// how /api/news/image sat at the 900 s Cloud Run limit on publishers that answer+// 403 and then stall. Every caller is done with the agent by the time it+// disposes it, so aborting what is left on it loses nothing.+export async function disposeAgent(agent) {+ if (!agent) return;+ await agent.destroy().catch(() => {});+}+// Parse + scheme-check a URL. https only (http allowed in dev). Returns the URL.export function validatePublicUrl(rawUrl, { allowHttp = IS_DEV } = {}) {let url;@@ -137,7 +149,7 @@ export async function safeFetchJson(try {while (true) {const addrs = await resolvePublicHost(currentUrl.hostname);- if (agent) await agent.close().catch(() => {});+ await disposeAgent(agent);agent = pinnedAgent(currentUrl.hostname, addrs);const res = await fetch(currentUrl, {@@ -183,6 +195,6 @@ export async function safeFetchJson(}} finally {clearTimeout(timer);- if (agent) await agent.close().catch(() => {});+ await disposeAgent(agent);}}diff --git a/api/_lib/webhook-dispatch.js b/api/_lib/webhook-dispatch.jsindex 8451ffd13..402d69be0 100644--- a/api/_lib/webhook-dispatch.js+++ b/api/_lib/webhook-dispatch.js@@ -10,7 +10,7 @@import { sql } from './db.js';import { randomToken, hmacSha256 } from './crypto.js';-import { validatePublicUrl, resolvePublicHost, pinnedAgent, SsrfError } from './ssrf.js';+import { validatePublicUrl, resolvePublicHost, pinnedAgent, disposeAgent, SsrfError } from './ssrf.js';const MAX_ATTEMPTS = 3;const BACKOFF_BASE_MS = 1000;@@ -163,32 +163,30 @@ async function deliver(url, secret, eventId, timestamp, payload) {return { statusCode: null, responseBody: null, error: reason };}+ // The body is read under the same delivery timeout as the request, and the+ // agent is destroyed only once we are done with the response. Closing it+ // before the read (as this used to) waited on a body nobody was reading, with+ // the timeout already cleared, so an endpoint that stalls mid-response hung+ // the dispatch indefinitely.+ const controller = new AbortController();+ const timeout = setTimeout(() => controller.abort(), DELIVERY_TIMEOUT_MS);try {- const controller = new AbortController();- const timeout = setTimeout(() => controller.abort(), DELIVERY_TIMEOUT_MS);-- let res;- try {- res = await fetch(target, {- method: 'POST',- redirect: 'manual',- dispatcher: agent,- headers: {- 'content-type': 'application/json',- 'webhook-id': eventId,- 'webhook-timestamp': String(timestamp),- 'webhook-signature': `v1,${signature}`,- 'user-agent': 'three.ws-webhooks/1.0',- },- body: payload,- signal: controller.signal,- });- } finally {- clearTimeout(timeout);- await agent.close().catch(() => {});- }-- // A redirect is a misconfigured (or hostile) endpoint — record it as a+ const res = await fetch(target, {+ method: 'POST',+ redirect: 'manual',+ dispatcher: agent,+ headers: {+ 'content-type': 'application/json',+ 'webhook-id': eventId,+ 'webhook-timestamp': String(timestamp),+ 'webhook-signature': `v1,${signature}`,+ 'user-agent': 'three.ws-webhooks/1.0',+ },+ body: payload,+ signal: controller.signal,+ });++ // A redirect is a misconfigured (or hostile) endpoint: record it as a// failure instead of following it to a potentially internal target.if (res.status >= 300 && res.status < 400) {return { statusCode: res.status, responseBody: null, error: 'redirect_not_followed' };@@ -205,6 +203,9 @@ async function deliver(url, secret, eventId, timestamp, payload) {return { statusCode: res.status, responseBody, error: null };} catch (err) {return { statusCode: null, responseBody: null, error: err?.message || 'delivery_failed' };+ } finally {+ clearTimeout(timeout);+ await disposeAgent(agent);}}diff --git a/api/x402-pay.js b/api/x402-pay.jsindex f9343f3cd..ee5f57e18 100644--- a/api/x402-pay.js+++ b/api/x402-pay.js@@ -52,7 +52,7 @@ import { assertBearerMaySpend } from './_lib/spend-scope.js';import { recoverSolanaAgentKeypair } from './_lib/agent-wallet.js';import { requireRealFundsAgreement } from './_lib/real-funds-agreement.js';import { SpendLimitError, reserveSpendUsd, updateCustodyEvent, releaseSpendReservation } from './_lib/agent-trade-guards.js';-import { validatePublicUrl, resolvePublicHost, pinnedAgent, SsrfError } from './_lib/ssrf.js';+import { validatePublicUrl, resolvePublicHost, pinnedAgent, disposeAgent, SsrfError } from './_lib/ssrf.js';import { BUILDER_CODE } from './_lib/x402-builder-code.js';const log = logger('x402-pay');@@ -447,7 +447,7 @@ async function guardedFetch(rawUrl, { method = 'GET', headers = {}, body } = {})return { status: res.status, ok: res.ok, headers: res.headers, text };} finally {clearTimeout(timer);- await agent.close().catch(() => {});+ await disposeAgent(agent);}}diff --git a/tests/api/fetch-model-agent-release.test.js b/tests/api/fetch-model-agent-release.test.jsnew file mode 100644index 000000000..548ad8b7d--- /dev/null+++ b/tests/api/fetch-model-agent-release.test.js@@ -0,0 +1,99 @@+// fetchModel must return as soon as it has an answer, even when the upstream+// never finishes its response body.+//+// Production regression (2026-10-08): /api/news/image held requests for the+// full 900 s Cloud Run limit (504) on publishers that answer 403 or a large PDF+// and then keep the socket open. fetchModel threw on the status or the+// content-length without reading the body, and its `finally` awaited+// `agent.close()`, which in undici waits for every in-flight request to finish.+// That request never finished, so the 6 s page timeout (already cleared by then)+// could not rescue it. The pinned agent is single-use, so it must be destroyed,+// not drained.+//+// The DNS/pin layer is swapped for a plain undici Agent so the test can reach a+// real loopback tarpit server; everything else is the production code path.++import http from 'node:http';+import { afterAll, beforeAll, describe, expect, it, vi } from 'vitest';+import { Agent } from 'undici';++vi.mock('../../api/_lib/ssrf.js', async (importOriginal) => {+ const real = await importOriginal();+ return {+ ...real,+ validatePublicUrl: (raw) => new URL(raw),+ resolvePublicHost: async () => [],+ pinnedAgent: () => new Agent(),+ };+});++const { fetchModel, FetchModelError } = await import('../../api/_lib/fetch-model.js');+const { disposeAgent } = await import('../../api/_lib/ssrf.js');++let server;+let base;++beforeAll(async () => {+ server = http.createServer((req, res) => {+ // Every route writes its head and one chunk, then holds the socket open+ // forever, like a WAF tarpit or a slow publisher.+ if (req.url === '/forbidden') {+ res.writeHead(403, { 'content-type': 'text/html' });+ } else if (req.url === '/huge') {+ res.writeHead(200, { 'content-type': 'application/pdf', 'content-length': String(50 * 1024 * 1024) });+ } else if (req.url === '/redirect') {+ res.writeHead(301, { location: '/forbidden', 'content-type': 'text/html' });+ }+ res.write('<html>');+ });+ await new Promise((resolve) => server.listen(0, '127.0.0.1', resolve));+ base = `http://127.0.0.1:${server.address().port}`;+});++afterAll(() => {+ server.closeAllConnections();+ server.close();+});++const QUICK_MS = 2000;++describe('fetchModel releases its connection on early exits', () => {+ it('rejects a non-2xx promptly instead of waiting for the body to end', async () => {+ const started = Date.now();+ await expect(fetchModel(`${base}/forbidden`, { timeoutMs: 6000 })).rejects.toMatchObject({+ code: 'upstream_error',+ });+ expect(Date.now() - started).toBeLessThan(QUICK_MS);+ }, 10_000);++ it('rejects an oversized content-length promptly', async () => {+ const started = Date.now();+ await expect(+ fetchModel(`${base}/huge`, { timeoutMs: 6000, maxBytes: 768 * 1024 }),+ ).rejects.toBeInstanceOf(FetchModelError);+ expect(Date.now() - started).toBeLessThan(QUICK_MS);+ }, 10_000);++ it('follows a redirect whose body never ends', async () => {+ const started = Date.now();+ await expect(fetchModel(`${base}/redirect`, { timeoutMs: 6000 })).rejects.toMatchObject({+ code: 'upstream_error',+ });+ expect(Date.now() - started).toBeLessThan(QUICK_MS);+ }, 10_000);+});++describe('disposeAgent', () => {+ it('resolves while a response body is still open', async () => {+ const agent = new Agent();+ const res = await fetch(`${base}/forbidden`, { dispatcher: agent });+ expect(res.status).toBe(403);+ const started = Date.now();+ await disposeAgent(agent);+ expect(Date.now() - started).toBeLessThan(QUICK_MS);+ }, 10_000);++ it('accepts a missing agent', async () => {+ await expect(disposeAgent(null)).resolves.toBeUndefined();+ });+});diff --git a/tests/pay-probe-endpoint-error.test.js b/tests/pay-probe-endpoint-error.test.jsindex 9b64ab513..e58e0c9da 100644--- a/tests/pay-probe-endpoint-error.test.js+++ b/tests/pay-probe-endpoint-error.test.js@@ -27,7 +27,7 @@ vi.mock('../api/_lib/ssrf.js', async (importOriginal) => {return {...actual,resolvePublicHost: async () => ['203.0.113.10'],- pinnedAgent: () => ({ close: async () => {} }),+ pinnedAgent: () => ({ destroy: async () => {} }),};});