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(safe-url): reject relative paths a browser would normalize into a protocol-relative URL

nirholas228074f5f8c8
diff --git a/src/safe-url.js b/src/safe-url.js
index 3b0306b63..00f0721a1 100644
--- a/src/safe-url.js
+++ b/src/safe-url.js
@@ -4,7 +4,8 @@
* collapses to a safe placeholder, neutralising script-scheme injection in
* `href`, `src`, and CSS `url(...)` sinks.
*
- * Allowed: site-relative paths (`/` but not protocol-relative `//`), `./`,
+ * Allowed: site-relative paths (`/` but not protocol-relative `//`, and with
+ * no backslash or control character that a browser would normalize into one), `./`,
* `../`, in-page anchors (`#`), or http(s) URLs (`^https?:`, case-insensitive,
* after trimming surrounding whitespace).
*
@@ -16,7 +17,14 @@ export function safeUrl(url, fallback = '#') {
if (typeof url !== 'string') return fallback;
const trimmed = url.trim();
if (!trimmed) return fallback;
- if (/^(?:\/(?!\/)|\.\/|\.\.\/|#)/.test(trimmed)) return url;
+ if (/^(?:\/(?!\/)|\.\/|\.\.\/|#)/.test(trimmed)) {
+ // A backslash or a tab/newline inside a "relative" path is how one turns
+ // protocol-relative: browsers read `/\evil.example` as `//evil.example`
+ // and drop tab/CR/LF entirely, so `/\t/evil.example` lands off-site too.
+ // eslint-disable-next-line no-control-regex
+ if (/[\\\u0000-\u001f]/.test(trimmed)) return fallback;
+ return url;
+ }
if (/^https?:/i.test(trimmed)) return url;
return fallback;
}
diff --git a/tests/safe-url.test.js b/tests/safe-url.test.js
index 3e25d4f24..c7c34d817 100644
--- a/tests/safe-url.test.js
+++ b/tests/safe-url.test.js
@@ -16,6 +16,12 @@ describe('safeUrl', () => {
}
});
+ it('rejects relative paths a browser would normalize into a protocol-relative URL', () => {
+ for (const bad of ['/\\evil.example/x', '/\\/evil.example', '/\t/evil.example', '/\n/evil.example', './\\evil']) {
+ expect(safeUrl(bad)).toBe('#');
+ }
+ });
+
it('returns the caller fallback so a rejected link can be omitted entirely', () => {
expect(safeUrl('javascript:alert(1)', '')).toBe('');
expect(safeUrl(null, '')).toBe('');