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(threews-avatar-mcp): clamp name/camera-orbit args before they reach modelViewerHtml
nirholas2a5caaec9a4f
diff --git a/packages/threews-avatar-mcp/src/threews.js b/packages/threews-avatar-mcp/src/threews.jsindex 161b7a690..a18664b1e 100644--- a/packages/threews-avatar-mcp/src/threews.js+++ b/packages/threews-avatar-mcp/src/threews.js@@ -22,13 +22,19 @@ export function baseUrl(env = process.env) {return (env.THREEWS_BASE_URL?.trim() || DEFAULT_BASE).replace(/\/$/, '');}+// A sane upper bound for any single tool argument that reaches HTML: long+// enough for a real GLB url or display name, short enough that a caller+// cannot use the tool result to balloon the rendered page or chat payload.+const MAX_ARG_LENGTH = 2048;+// Only https model URLs are safe to hand to <model-viewer src> / embeds.// A raw `model` arg flows verbatim into HTML and into the browser, so a-// `javascript:` / `data:` / `blob:` / `file:` scheme is an XSS/SSRF surface.+// `javascript:` / `data:` / `blob:` / `file:` scheme is an XSS/SSRF surface,+// and an unbounded length is a payload-size surface even once escaped.// http is allowed only for localhost (dev convenience). Anything else is// rejected so it can never reach an attribute or a fetch.export function isSafeModelUrl(url) {- if (typeof url !== 'string' || url.trim() === '') return false;+ if (typeof url !== 'string' || url.trim() === '' || url.length > MAX_ARG_LENGTH) return false;let parsed;try {parsed = new URL(url);@@ -43,6 +49,13 @@ export function isSafeModelUrl(url) {return false;}+// Clamp any other untrusted string arg (name, camera-orbit) before it+// reaches modelViewerHtml's output, the same way isSafeModelUrl clamps urls.+function clampArg(value, max = 200) {+ const s = String(value ?? '');+ return s.length > max ? s.slice(0, max) : s;+}+function assertSafeModelUrl(url) {if (!isSafeModelUrl(url)) {throw new ThreewsError(@@ -193,11 +206,13 @@ export function iframeSnippet(embed, { width = '100%', height = 480 } = {}) {// rotatable 3D avatar inline.export function modelViewerHtml({ model_url, name = 'Avatar', background = 'transparent', height = 480, cameraOrbit = '0deg 80deg 2m', autoRotate = true }) {assertSafeModelUrl(model_url);+ const safeName = clampArg(name);+ const safeCameraOrbit = clampArg(cameraOrbit, 64);const bg = background === 'transparent' ? 'transparent' : background;return `<!doctype html><html lang="en"><head><meta charset="utf-8" /><meta name="viewport" content="width=device-width, initial-scale=1" />-<title>${escapeHtml(name)}</title>+<title>${escapeHtml(safeName)}</title><script type="module" src="https://cdn.jsdelivr.net/npm/@google/[email protected]/dist/model-viewer.min.js"></script><style>html,body { margin:0; height:100%; background:${bg}; }@@ -206,7 +221,7 @@ export function modelViewerHtml({ model_url, name = 'Avatar', background = 'tran</head><body><model-viewersrc="${escapeHtml(model_url)}"- alt="${escapeHtml(name)}"+ alt="${escapeHtml(safeName)}"camera-controls${autoRotate ? 'auto-rotate' : ''}shadow-intensity="1.0"@@ -214,7 +229,7 @@ export function modelViewerHtml({ model_url, name = 'Avatar', background = 'tranexposure="1.5"tone-mapping="aces"environment-image="neutral"- camera-orbit="${escapeHtml(cameraOrbit)}"+ camera-orbit="${escapeHtml(safeCameraOrbit)}"ar ar-modes="webxr scene-viewer quick-look"></model-viewer></body></html>`;diff --git a/packages/threews-avatar-mcp/test/threews.test.mjs b/packages/threews-avatar-mcp/test/threews.test.mjsnew file mode 100644index 000000000..f8ab59ab6--- /dev/null+++ b/packages/threews-avatar-mcp/test/threews.test.mjs@@ -0,0 +1,32 @@+// A raw `model` url is the one tool argument that flows straight from the+// caller into HTML (escaped, so not an XSS vector, but unbounded in length).+// An oversized or control-character-laden url should be rejected before it+// ever reaches modelViewerHtml's output, not merely HTML-escaped.++import test from 'node:test';+import assert from 'node:assert/strict';++import { isSafeModelUrl, modelViewerHtml, ThreewsError } from '../src/threews.js';++test('isSafeModelUrl rejects an oversized url', () => {+ const huge = `https://example.com/${'a'.repeat(10_000)}.glb`;+ assert.equal(isSafeModelUrl(huge), false);+});++test('modelViewerHtml throws ThreewsError for an oversized model url instead of passing it through', () => {+ const huge = `https://example.com/${'a'.repeat(10_000)}.glb`;+ assert.throws(() => modelViewerHtml({ model_url: huge, name: 'Avatar' }), ThreewsError);+});++test('isSafeModelUrl still accepts a normal https GLB url', () => {+ assert.equal(isSafeModelUrl('https://three.ws/models/avatar.glb'), true);+});++test('modelViewerHtml clamps an overlong name instead of emitting it verbatim', () => {+ const longName = 'A'.repeat(5000);+ const html = modelViewerHtml({ model_url: 'https://three.ws/models/avatar.glb', name: longName });+ assert.ok(!html.includes(longName), 'overlong name must not reach the HTML output unclamped');+ const titleMatch = html.match(/<title>([^<]*)<\/title>/);+ assert.ok(titleMatch, 'title tag must still be present');+ assert.ok(titleMatch[1].length <= 200, `clamped title should be short, got ${titleMatch[1].length} chars`);+});