Skip to content

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.

fix(tty): fetch remote models through the pinned per-hop SSRF fetcher and stop leaking errors

nirholasc67769be5e99
diff --git a/api/tty.js b/api/tty.js
index c6a75bf36..b2fd377c5 100644
--- a/api/tty.js
+++ b/api/tty.js
@@ -21,6 +21,7 @@ import { createRenderer, loadModel } from '../packages/tty-3d/src/index.js';
import { ansi, ColorMode } from '../packages/tty-3d/src/term.js';
import { getAvatar } from './_lib/avatars.js';
import { error, rateLimited, redirect, wantsHtmlNavigation, wrap } from './_lib/http.js';
+import { fetchModel } from './_lib/fetch-model.js';
import { clientIp, limits } from './_lib/rate-limit.js';
import { assertSafePublicUrl, SsrfBlockedError } from './_lib/ssrf-guard.js';
@@ -44,6 +45,7 @@ export function defaultModelPath(cwd = process.cwd()) {
return null;
}
+const MAX_MODEL_BYTES = 64 * 1024 * 1024;
const MAX_FRAMES = 600;
const MAX_SECONDS = 45;
const MIN_COLS = 20;
@@ -99,6 +101,17 @@ export async function resolveSource(query) {
return { url: local, label: 'default agent' };
}
+// Remote models are fetched by the pinned, per-hop validated fetcher (redirects
+// handled manually, every hop resolved and connection-pinned), never by loadModel's
+// own redirect-following fetch. Applies to src and to a stored avatar model_url.
+async function loadSource(url) {
+ if (/^https?:\/\//i.test(url)) {
+ const { bytes } = await fetchModel(url, { maxBytes: MAX_MODEL_BYTES });
+ return loadModel(bytes);
+ }
+ return loadModel(url);
+}
+
function banner(label, info, cols) {
const left = `three.ws ${label}`;
const right = `${info.triangles.toLocaleString('en-US')} tris${info.skinned ? ' rigged' : ''}`;
@@ -141,9 +154,11 @@ export default wrap(async function handler(req, res) {
let model;
try {
- model = await loadModel(source.url);
+ model = await loadSource(source.url);
} catch (err) {
- return error(res, 502, 'model_unavailable', `could not read that model: ${err.message}`);
+ // Upstream detail (status, resolver verdicts) stays out of the response:
+ // it would turn this endpoint into an internal-network probe.
+ return error(res, 502, 'model_unavailable', 'could not read that model');
}
const renderer = createRenderer(model, {
diff --git a/tests/api-tty-ssrf.test.js b/tests/api-tty-ssrf.test.js
new file mode 100644
index 000000000..2d9a01e32
--- /dev/null
+++ b/tests/api-tty-ssrf.test.js
@@ -0,0 +1,54 @@
+// /api/tty must fetch remote models through the pinned, per-hop validated
+// fetcher. The avatar branch hands the handler a stored model_url that never
+// passed the SSRF guard, so a loopback model_url must be refused without the
+// server ever connecting to it, and the response must not echo internals.
+
+import { describe, it, expect, vi, afterEach } from 'vitest';
+import http from 'node:http';
+
+vi.mock('../api/_lib/avatars.js', () => ({ getAvatar: vi.fn() }));
+vi.mock('../api/_lib/rate-limit.js', () => ({
+ clientIp: () => '203.0.113.9',
+ limits: { ttyIp: async () => ({ success: true, limit: 10 }) },
+}));
+
+import { getAvatar } from '../api/_lib/avatars.js';
+import handler from '../api/tty.js';
+
+let server;
+afterEach(async () => {
+ if (server) await new Promise((r) => server.close(r));
+ server = null;
+});
+
+function fakeRes() {
+ const res = {
+ statusCode: 200,
+ headers: {},
+ body: '',
+ setHeader(k, v) { this.headers[k.toLowerCase()] = v; },
+ getHeader(k) { return this.headers[k.toLowerCase()]; },
+ write(c) { this.body += c; return true; },
+ end(c) { if (c) this.body += c; this.done = true; },
+ on() {},
+ };
+ return res;
+}
+
+describe('/api/tty avatar model_url SSRF', () => {
+ it('never connects to a loopback model_url and does not leak the error detail', async () => {
+ let hits = 0;
+ server = http.createServer((_req, res) => { hits += 1; res.end('x'); });
+ await new Promise((r) => server.listen(0, '127.0.0.1', r));
+ const { port } = server.address();
+ getAvatar.mockResolvedValue({ name: 'a', model_url: `http://127.0.0.1:${port}/m.glb` });
+
+ const req = { url: '/api/tty?avatar=11111111-1111-1111-1111-111111111111', headers: {}, method: 'GET', on() {} };
+ const res = fakeRes();
+ await handler(req, res);
+
+ expect(hits).toBe(0);
+ expect(res.statusCode).toBeGreaterThanOrEqual(400);
+ expect(res.body).not.toMatch(/127\.0\.0\.1|private|fetch/i);
+ });
+});