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(security): refuse CSS/HTML breakout characters in isSafeImageURL, not just dangerous schemes

nirholas05201f1ffdd0
diff --git a/src/ipfs.js b/src/ipfs.js
index 29334f5b0..3f2e807bc 100644
--- a/src/ipfs.js
+++ b/src/ipfs.js
@@ -93,10 +93,19 @@ const SAFE_IMAGE_SCHEMES = new Set(['http', 'https', 'ipfs', 'ar', 'blob']);
// us issue an oversized upstream request, and the proxy would reject it anyway.
const MAX_SOURCE_URL = 2048;
+// Characters that never appear unencoded in a real image path, and that are
+// exactly the ones a crafted value uses to break out of the syntax it lands in:
+// quotes and parentheses close a CSS url(), a semicolon starts a new declaration,
+// angle brackets open a tag, whitespace and backslash split the token. A
+// scheme-less value carrying any of them is not art someone forgot to encode, it
+// is a payload, and handing it to an image sink costs a doomed request and a
+// console error even when the escaping downstream holds.
+const CSS_HTML_UNSAFE = /["'()<>;\\\s]/;
+
/**
* True when a URL is safe to hand to an <img>/TextureLoader as an image source.
- * Relative and site-absolute paths (no scheme) always qualify; `data:` only when
- * it declares an image media type.
+ * Relative and site-absolute paths qualify as long as they still look like paths;
+ * `data:` only when it declares an image media type.
*
* @param {string} url
* @returns {boolean}
@@ -109,9 +118,12 @@ export function isSafeImageURL(url) {
// ("java\tscript:alert(1)" is a live URL in some engines). Refuse outright.
if (/[\u0000-\u001f\u007f]/.test(s)) return false;
const scheme = s.match(/^([a-z][a-z0-9+.\-]*):/i)?.[1]?.toLowerCase();
- if (!scheme) return true; // relative or site-absolute path, no scheme to abuse
+ if (!scheme) return !CSS_HTML_UNSAFE.test(s); // relative or site-absolute path
if (scheme === 'data') return /^data:image\/[a-z0-9.+\-]+[,;]/i.test(s);
- return SAFE_IMAGE_SCHEMES.has(scheme);
+ if (!SAFE_IMAGE_SCHEMES.has(scheme)) return false;
+ // blob: is an opaque single token; the rest are real URLs whose path is subject
+ // to the same no-breakout-characters rule as a relative path.
+ return scheme === 'blob' || !CSS_HTML_UNSAFE.test(s);
}
/**
diff --git a/tests/ipfs-image-url-safety.test.js b/tests/ipfs-image-url-safety.test.js
index 50e5559f3..d7f7dd0e9 100644
--- a/tests/ipfs-image-url-safety.test.js
+++ b/tests/ipfs-image-url-safety.test.js
@@ -62,6 +62,39 @@ describe('isSafeImageURL', () => {
expect(isSafeImageURL('java\u0000script:window.x=1')).toBe(false);
});
+ it('refuses a scheme-less value carrying CSS/HTML breakout characters', () => {
+ // The /play CSS-breakout payload. cssBgImage percent-encodes these before
+ // they reach a style attribute, so nothing escapes the declaration, but the
+ // value is still not art: passing it through cost a doomed relative-path
+ // request and a 404 in the console on a surface whose bar is zero console
+ // output. It is rejected at the source instead.
+ for (const url of [
+ 'x");position:fixed;inset:0;z-index:2147483647;background:red;--x:url("y',
+ 'art.png";background:red',
+ "art.png');background:red",
+ 'art<img>.png',
+ 'art .png',
+ 'art\\..png',
+ 'https://example.com/a");background:red;--x:url("b.png',
+ ]) {
+ expect(isSafeImageURL(url), url).toBe(false);
+ }
+ });
+
+ it('still accepts the ordinary paths and query strings real art uses', () => {
+ // The tightening above must not cost a legitimate source. Query strings,
+ // percent-encoding, dashes, dots and nested paths all stay valid.
+ for (const url of [
+ '/api/img?url=https%3A%2F%2Fipfs.io%2Fipfs%2Fbafy&seed=FeMbDoX7R1Psc4GEcvJdsbNbZA3bfztcyDCatJVJpump',
+ '/avatars/default.glb',
+ 'art/token-01.png',
+ 'https://cdn.example.com/a/b/c_d-e.f.png?v=2&x=1',
+ 'blob:https://three.ws/6d9a-4c2f',
+ ]) {
+ expect(isSafeImageURL(url), url).toBe(true);
+ }
+ });
+
it('refuses non-strings and empties rather than throwing on them', () => {
for (const v of [null, undefined, 42, {}, [], '', ' ']) {
expect(isSafeImageURL(v)).toBe(false);