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(og): refuse an image content type that could break out of the SVG card

nirholasc7c3465f11b9
diff --git a/api/_lib/og-avatar.js b/api/_lib/og-avatar.js
index e1c2b1c6e..1af873312 100644
--- a/api/_lib/og-avatar.js
+++ b/api/_lib/og-avatar.js
@@ -24,6 +24,9 @@ export const MAX_OG_IMAGE_BYTES = 1_500_000;
const DEFAULT_TIMEOUT_MS = 3000;
+/** A bare `image/<subtype>` token with nothing that can close a quoted attribute. */
+const IMAGE_MEDIA_TYPE = /^image\/[a-z0-9][a-z0-9.+-]{0,63}$/;
+
/**
* @param {string|null|undefined} url remote image URL (user-supplied)
* @param {{ timeoutMs?: number, maxBytes?: number }} [opts]
@@ -39,8 +42,12 @@ export async function fetchOgImage(url, opts = {}) {
const resp = await fetch(url, { signal: AbortSignal.timeout(timeoutMs) });
if (!resp.ok) return null;
- const ct = resp.headers.get('content-type') || '';
- if (!/^image\//i.test(ct)) return null;
+ // The type lands inside an SVG `href="data:<ct>;base64,..."` attribute,
+ // so it must be a bare media-type token: a hostile host answering
+ // `image/png"/><script>...` would otherwise break out of the attribute
+ // and run script in the same-origin SVG card.
+ const ct = (resp.headers.get('content-type') || '').split(';')[0].trim().toLowerCase();
+ if (!IMAGE_MEDIA_TYPE.test(ct)) return null;
const declared = Number(resp.headers.get('content-length') || 0);
if (declared > maxBytes) return null;
@@ -48,7 +55,7 @@ export async function fetchOgImage(url, opts = {}) {
const bytes = await readCapped(resp, maxBytes);
if (!bytes) return null;
- return { ct: ct.split(';')[0].trim(), b64: Buffer.from(bytes).toString('base64') };
+ return { ct, b64: Buffer.from(bytes).toString('base64') };
} catch {
// A slow host, a DNS failure, or an aborted read is never worth a broken
// unfurl. The caller draws its monogram instead.
diff --git a/tests/og-avatar.test.js b/tests/og-avatar.test.js
index 76ba1ef0c..c81bdb6e6 100644
--- a/tests/og-avatar.test.js
+++ b/tests/og-avatar.test.js
@@ -66,6 +66,15 @@ describe('og-avatar: fetchOgImage failure paths', () => {
expect(spy).toHaveBeenCalledOnce();
});
+ it('rejects a content type that would break out of the SVG data-URI attribute', async () => {
+ mockFetch(
+ imageResponse(Buffer.alloc(16, 1), {
+ ct: 'image/png"/><script>alert(document.domain)</script><x a="',
+ }),
+ );
+ expect(await fetchOgImage('https://cdn.example.test/evil.png')).toBeNull();
+ });
+
it('rejects a non-image response, so an HTML error page never lands in the card', async () => {
mockFetch(imageResponse(Buffer.from('<!doctype html>'), { ct: 'text/html' }));
expect(await fetchOgImage('https://cdn.example.test/oops')).toBeNull();