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.

Implement safe navigation functions and tests to prevent open redirects

nirholaseaf18198dd4f
diff --git a/.gitignore b/.gitignore
index 92db21b0c..8631cbe50 100644
--- a/.gitignore
+++ b/.gitignore
@@ -396,3 +396,4 @@ tests/openai-comarketing-language.test.js
# local research datasets (large, regenerable; see datasets/*/README.md)
/datasets/
+SECURITY-AUDIT.md
diff --git a/public/login.html b/public/login.html
index ccaae1877..89129d0b3 100644
--- a/public/login.html
+++ b/public/login.html
@@ -962,8 +962,23 @@
}
// ── Auth redirect ──
- const next =
- params.get('next') || sessionStorage.getItem('login_redirect') || '/dashboard';
+ // safeNext: inline copy of src/safe-next.js (tests/safe-next.test.js drift-guards it).
+function safeNext(raw, fallback = '/dashboard') {
+ if (typeof raw !== 'string' || raw.length === 0) return fallback;
+ // Require a single leading '/': '//evil.com' is protocol-relative, and a
+ // backslash anywhere lets '/\evil.com' normalize to '//evil.com' in the
+ // URL parser.
+ if (!raw.startsWith('/') || raw.startsWith('//')) return fallback;
+ if (raw.includes('\\')) return fallback;
+ // Control characters can smuggle a scheme past naive prefix checks.
+ // eslint-disable-next-line no-control-regex
+ if (/[ -]/.test(raw)) return fallback;
+ return raw;
+}
+ const next = safeNext(
+ params.get('next') || sessionStorage.getItem('login_redirect'),
+ '/dashboard',
+ );
sessionStorage.removeItem('login_redirect');
window.__loginNext = next;
const navType = performance.getEntriesByType('navigation')[0]?.type;
diff --git a/public/paywall.js b/public/paywall.js
index 92af0224f..f70448129 100644
--- a/public/paywall.js
+++ b/public/paywall.js
@@ -32,6 +32,26 @@
}
}
+ // safeNavUrl: inline copy of src/safe-next.js (tests/safe-next.test.js drift-guards it).
+function safeNavUrl(raw, fallback = '/') {
+ if (typeof raw !== 'string' || raw.length === 0) return fallback;
+ // eslint-disable-next-line no-control-regex
+ if (raw.includes('\\') || /[ -]/.test(raw)) return fallback;
+ // Same-origin relative path.
+ if (raw.startsWith('/') && !raw.startsWith('//')) return raw;
+ // Absolute URL: http(s) only, so javascript:/data:/vbscript: can never
+ // reach an href or location sink. The WHATWG parser strips leading
+ // whitespace and C0 controls before scheme parsing, and lowercases the
+ // scheme, so ' javascript:...' and 'JaVaScRiPt:...' both fail here.
+ try {
+ const u = new URL(raw);
+ if (u.protocol === 'https:' || u.protocol === 'http:') return u.href;
+ } catch {
+ // Not a parseable absolute URL: fall through to the fallback.
+ }
+ return fallback;
+}
+
function getReturnUrl() {
var params = getParams();
var ret = params.get('return');
@@ -92,9 +112,11 @@
function render(requirements) {
var returnUrl = getReturnUrl();
- // Wire the close button
+ // Wire the close button. The return URL comes from the query string, so
+ // it must pass the scheme guard: a crafted link could otherwise turn the
+ // close button into a javascript: execution sink in this origin.
var closeBtn = document.getElementById('close-btn');
- if (closeBtn) closeBtn.href = returnUrl;
+ if (closeBtn) closeBtn.href = safeNavUrl(returnUrl);
if (!requirements || !requirements.length) {
// No requirements decoded — show generic message
diff --git a/public/register.html b/public/register.html
index 502fa2411..73e48d472 100644
--- a/public/register.html
+++ b/public/register.html
@@ -819,7 +819,23 @@
// Registration form submit
const params = new URLSearchParams(location.search);
- const next = params.get('next') || sessionStorage.getItem('login_redirect') || '/create';
+ // safeNext: inline copy of src/safe-next.js (tests/safe-next.test.js drift-guards it).
+function safeNext(raw, fallback = '/dashboard') {
+ if (typeof raw !== 'string' || raw.length === 0) return fallback;
+ // Require a single leading '/': '//evil.com' is protocol-relative, and a
+ // backslash anywhere lets '/\evil.com' normalize to '//evil.com' in the
+ // URL parser.
+ if (!raw.startsWith('/') || raw.startsWith('//')) return fallback;
+ if (raw.includes('\\')) return fallback;
+ // Control characters can smuggle a scheme past naive prefix checks.
+ // eslint-disable-next-line no-control-regex
+ if (/[ -]/.test(raw)) return fallback;
+ return raw;
+}
+ const next = safeNext(
+ params.get('next') || sessionStorage.getItem('login_redirect'),
+ '/create',
+ );
sessionStorage.removeItem('login_redirect');
window.__loginNext = next;
const form = document.getElementById('f');
diff --git a/public/wallet-connect-demo.html b/public/wallet-connect-demo.html
index 315981f86..e679b9bd7 100644
--- a/public/wallet-connect-demo.html
+++ b/public/wallet-connect-demo.html
@@ -89,7 +89,20 @@
const { createConnectWalletButton } = await import(/* @vite-ignore */ '/wallet/connect-button.js');
const params = new URLSearchParams(location.search);
- const next = params.get('next') || '/dashboard/';
+ // safeNext: inline copy of src/safe-next.js (tests/safe-next.test.js drift-guards it).
+function safeNext(raw, fallback = '/dashboard') {
+ if (typeof raw !== 'string' || raw.length === 0) return fallback;
+ // Require a single leading '/': '//evil.com' is protocol-relative, and a
+ // backslash anywhere lets '/\evil.com' normalize to '//evil.com' in the
+ // URL parser.
+ if (!raw.startsWith('/') || raw.startsWith('//')) return fallback;
+ if (raw.includes('\\')) return fallback;
+ // Control characters can smuggle a scheme past naive prefix checks.
+ // eslint-disable-next-line no-control-regex
+ if (/[ -]/.test(raw)) return fallback;
+ return raw;
+}
+ const next = safeNext(params.get('next'), '/dashboard/');
function wire(mountId, logId, opts) {
const mountEl = document.getElementById(mountId);
diff --git a/public/wallet-login.js b/public/wallet-login.js
index e7a202afe..42c20ad10 100644
--- a/public/wallet-login.js
+++ b/public/wallet-login.js
@@ -31,7 +31,20 @@ try {
}
const params = new URLSearchParams(location.search);
-const next = window.__loginNext || params.get('next') || sessionStorage.getItem('login_redirect') || '/dashboard';
+// safeNext: inline copy of src/safe-next.js (tests/safe-next.test.js drift-guards it).
+function safeNext(raw, fallback = '/dashboard') {
+ if (typeof raw !== 'string' || raw.length === 0) return fallback;
+ // Require a single leading '/': '//evil.com' is protocol-relative, and a
+ // backslash anywhere lets '/\evil.com' normalize to '//evil.com' in the
+ // URL parser.
+ if (!raw.startsWith('/') || raw.startsWith('//')) return fallback;
+ if (raw.includes('\\')) return fallback;
+ // Control characters can smuggle a scheme past naive prefix checks.
+ // eslint-disable-next-line no-control-regex
+ if (/[ -]/.test(raw)) return fallback;
+ return raw;
+}
+const next = safeNext(window.__loginNext || params.get('next') || sessionStorage.getItem('login_redirect'), '/dashboard');
sessionStorage.removeItem('login_redirect');
function setErr(m) {
diff --git a/src/safe-next.js b/src/safe-next.js
new file mode 100644
index 000000000..a1b731f49
--- /dev/null
+++ b/src/safe-next.js
@@ -0,0 +1,45 @@
+// Same-origin redirect guards for navigation params (?next=, ?return=).
+//
+// safeNext is the strict guard for post-auth redirects: only same-origin
+// relative paths survive. safeNavUrl is the looser guard for links that may
+// legitimately leave the origin (e.g. the paywall return link): it allows
+// absolute http(s) URLs but blocks script-capable schemes (javascript:,
+// data:, vbscript:) and protocol-relative/backslash tricks.
+//
+// These functions are inlined into public pages that cannot import /src at
+// runtime (public/login.html, public/register.html, public/wallet-login.js,
+// public/wallet-connect-demo.html, public/paywall.js). The inline copies omit
+// the `export` keyword but are otherwise byte-identical;
+// tests/safe-next.test.js fails if any copy drifts. Update all together.
+
+export function safeNext(raw, fallback = '/dashboard') {
+ if (typeof raw !== 'string' || raw.length === 0) return fallback;
+ // Require a single leading '/': '//evil.com' is protocol-relative, and a
+ // backslash anywhere lets '/\evil.com' normalize to '//evil.com' in the
+ // URL parser.
+ if (!raw.startsWith('/') || raw.startsWith('//')) return fallback;
+ if (raw.includes('\\')) return fallback;
+ // Control characters can smuggle a scheme past naive prefix checks.
+ // eslint-disable-next-line no-control-regex
+ if (/[\u0000-\u001f]/.test(raw)) return fallback;
+ return raw;
+}
+
+export function safeNavUrl(raw, fallback = '/') {
+ if (typeof raw !== 'string' || raw.length === 0) return fallback;
+ // eslint-disable-next-line no-control-regex
+ if (raw.includes('\\') || /[\u0000-\u001f]/.test(raw)) return fallback;
+ // Same-origin relative path.
+ if (raw.startsWith('/') && !raw.startsWith('//')) return raw;
+ // Absolute URL: http(s) only, so javascript:/data:/vbscript: can never
+ // reach an href or location sink. The WHATWG parser strips leading
+ // whitespace and C0 controls before scheme parsing, and lowercases the
+ // scheme, so ' javascript:...' and 'JaVaScRiPt:...' both fail here.
+ try {
+ const u = new URL(raw);
+ if (u.protocol === 'https:' || u.protocol === 'http:') return u.href;
+ } catch {
+ // Not a parseable absolute URL: fall through to the fallback.
+ }
+ return fallback;
+}
diff --git a/tests/safe-next.test.js b/tests/safe-next.test.js
new file mode 100644
index 000000000..9ec4353e9
--- /dev/null
+++ b/tests/safe-next.test.js
@@ -0,0 +1,120 @@
+// Regression tests for the open-redirect guards (security audit 2026-07-23:
+// unvalidated ?next= / ?return= params on the auth and paywall pages allowed
+// attacker-controlled post-login redirects and javascript: hrefs).
+//
+// Two layers:
+// 1. Behavior tests for the canonical guards in src/safe-next.js.
+// 2. Drift tests asserting the inline copies shipped in the public pages
+// (which cannot import /src at runtime) stay byte-identical to the
+// canonical functions.
+
+import { describe, it, expect } from 'vitest';
+import { readFileSync } from 'node:fs';
+import { safeNext, safeNavUrl } from '../src/safe-next.js';
+
+describe('safeNext', () => {
+ it('allows same-origin relative paths', () => {
+ expect(safeNext('/dashboard')).toBe('/dashboard');
+ expect(safeNext('/create')).toBe('/create');
+ expect(safeNext('/dashboard/embed-policy?agent=abc#top')).toBe(
+ '/dashboard/embed-policy?agent=abc#top',
+ );
+ expect(safeNext('/extension/auth-callback')).toBe('/extension/auth-callback');
+ expect(safeNext('/%2f%2fevil.com')).toBe('/%2f%2fevil.com'); // stays a path
+ });
+
+ it('rejects absolute and protocol-relative URLs', () => {
+ expect(safeNext('https://evil.com')).toBe('/dashboard');
+ expect(safeNext('http://evil.com')).toBe('/dashboard');
+ expect(safeNext('//evil.com')).toBe('/dashboard');
+ expect(safeNext('///evil.com')).toBe('/dashboard');
+ expect(safeNext('javascript:alert(1)')).toBe('/dashboard');
+ expect(safeNext('data:text/html,x')).toBe('/dashboard');
+ });
+
+ it('rejects backslash normalization tricks', () => {
+ expect(safeNext('/\\evil.com')).toBe('/dashboard');
+ expect(safeNext('/\\/evil.com')).toBe('/dashboard');
+ expect(safeNext('\\evil.com')).toBe('/dashboard');
+ });
+
+ it('rejects control characters', () => {
+ expect(safeNext('/\tlogin')).toBe('/dashboard');
+ expect(safeNext('/dash\nboard')).toBe('/dashboard');
+ expect(safeNext('/dashboard')).toBe('/dashboard');
+ });
+
+ it('rejects empty and non-string input', () => {
+ expect(safeNext('')).toBe('/dashboard');
+ expect(safeNext(null)).toBe('/dashboard');
+ expect(safeNext(undefined)).toBe('/dashboard');
+ expect(safeNext(42)).toBe('/dashboard');
+ });
+
+ it('honors a custom fallback', () => {
+ expect(safeNext('https://evil.com', '/create')).toBe('/create');
+ expect(safeNext(null, '/dashboard/')).toBe('/dashboard/');
+ });
+});
+
+describe('safeNavUrl', () => {
+ it('allows same-origin relative paths', () => {
+ expect(safeNavUrl('/')).toBe('/');
+ expect(safeNavUrl('/pay/receipt?id=1')).toBe('/pay/receipt?id=1');
+ });
+
+ it('allows absolute http(s) URLs', () => {
+ expect(safeNavUrl('https://example.com/resource')).toBe('https://example.com/resource');
+ expect(safeNavUrl('http://example.com')).toBe('http://example.com/');
+ });
+
+ it('blocks script-capable and opaque schemes', () => {
+ expect(safeNavUrl('javascript:alert(1)')).toBe('/');
+ expect(safeNavUrl('JaVaScRiPt:alert(1)')).toBe('/');
+ expect(safeNavUrl(' javascript:alert(1)')).toBe('/');
+ expect(safeNavUrl('data:text/html,<script>alert(1)</script>')).toBe('/');
+ expect(safeNavUrl('vbscript:msgbox(1)')).toBe('/');
+ expect(safeNavUrl('file:///etc/passwd')).toBe('/');
+ });
+
+ it('blocks protocol-relative and backslash tricks', () => {
+ expect(safeNavUrl('//evil.com')).toBe('/');
+ expect(safeNavUrl('/\\evil.com')).toBe('/');
+ expect(safeNavUrl('https:\\evil.com')).toBe('/');
+ });
+
+ it('falls back on empty, non-string, and unparseable input', () => {
+ expect(safeNavUrl('')).toBe('/');
+ expect(safeNavUrl(null)).toBe('/');
+ expect(safeNavUrl('not a url')).toBe('/');
+ });
+});
+
+// ── Inline-copy drift guard ──────────────────────────────────────────────────
+
+const canonical = readFileSync(new URL('../src/safe-next.js', import.meta.url), 'utf8');
+
+function canonicalSource(exportName) {
+ const match = canonical.match(new RegExp(`export function ${exportName}[\\s\\S]*?\\n}`));
+ if (!match) throw new Error(`canonical source for ${exportName} not found`);
+ return match[0].replace('export function', 'function');
+}
+
+const copies = [
+ ['public/login.html', ['safeNext']],
+ ['public/register.html', ['safeNext']],
+ ['public/wallet-login.js', ['safeNext']],
+ ['public/wallet-connect-demo.html', ['safeNext']],
+ ['public/paywall.js', ['safeNavUrl']],
+];
+
+describe('inline copies stay in sync with src/safe-next.js', () => {
+ for (const [file, fns] of copies) {
+ for (const fn of fns) {
+ it(`${file} carries the canonical ${fn}`, () => {
+ const text = readFileSync(new URL(`../${file}`, import.meta.url), 'utf8');
+ expect(text.includes(canonicalSource(fn))).toBe(true);
+ });
+ }
+ }
+});