From 8b705e546f7d562b59de2e6f6959c27e19b60e97 Mon Sep 17 00:00:00 2001 From: Codex Date: Fri, 24 Apr 2026 16:13:33 +0000 Subject: [PATCH] Rewrite dashboard proxy origins --- app/api/openshell/dashboard/proxy/shared.ts | 11 ++++++++++- ...openclaw-dashboard-origin-rewrite-check.mjs | 18 ++++++++++++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) create mode 100644 tests/openclaw-dashboard-origin-rewrite-check.mjs diff --git a/app/api/openshell/dashboard/proxy/shared.ts b/app/api/openshell/dashboard/proxy/shared.ts index c2a5ded..ed711bd 100644 --- a/app/api/openshell/dashboard/proxy/shared.ts +++ b/app/api/openshell/dashboard/proxy/shared.ts @@ -92,14 +92,23 @@ function buildTargetUrl(requestUrl: URL) { function copyRequestHeaders(request: Request, target: URL) { const headers = new Headers() + const targetOrigin = target.origin request.headers.forEach((value, key) => { - if (!HOP_BY_HOP_HEADERS.has(key.toLowerCase()) && key.toLowerCase() !== 'host') { + const lowerKey = key.toLowerCase() + if ( + !HOP_BY_HOP_HEADERS.has(lowerKey) && + lowerKey !== 'host' && + lowerKey !== 'origin' && + lowerKey !== 'referer' + ) { headers.set(key, value) } }) headers.set('host', target.host) + headers.set('origin', targetOrigin) + headers.set('referer', `${targetOrigin}/`) return headers } diff --git a/tests/openclaw-dashboard-origin-rewrite-check.mjs b/tests/openclaw-dashboard-origin-rewrite-check.mjs new file mode 100644 index 0000000..c158e1b --- /dev/null +++ b/tests/openclaw-dashboard-origin-rewrite-check.mjs @@ -0,0 +1,18 @@ +import assert from 'node:assert/strict' +import { readFile } from 'node:fs/promises' +import path from 'node:path' + +const root = process.cwd() +const proxyPath = path.join(root, 'app/api/openshell/dashboard/proxy/shared.ts') +const serverPath = path.join(root, 'server.mjs') + +const proxySource = await readFile(proxyPath, 'utf8') +const serverSource = await readFile(serverPath, 'utf8') + +assert.match(proxySource, /const targetOrigin = target\.origin/, 'HTTP proxy must derive an upstream-safe target origin') +assert.match(proxySource, /lowerKey !== 'origin'/, 'HTTP proxy must not forward browser Origin to OpenClaw') +assert.match(proxySource, /headers\.set\('origin', targetOrigin\)/, 'HTTP proxy must rewrite Origin to the loopback/tunnel upstream origin') +assert.match(proxySource, /headers\.set\('referer', `\$\{targetOrigin\}\/`\)/, 'HTTP proxy must rewrite Referer to the upstream origin') +assert.match(serverSource, /headers\.push\(`Origin: \$\{upstreamOrigin\.origin\}`\)/, 'WebSocket tunnel must continue rewriting Origin for gateway upgrades') + +console.log('openclaw-dashboard-origin-rewrite-check: PASS dashboard proxy origin rewrite assertions')