diff --git a/app/api/openshell/dashboard/proxy/shared.ts b/app/api/openshell/dashboard/proxy/shared.ts index ed711bd..a78db6d 100644 --- a/app/api/openshell/dashboard/proxy/shared.ts +++ b/app/api/openshell/dashboard/proxy/shared.ts @@ -23,6 +23,7 @@ type ProxyTargetResolution = { instanceId: string | null proxyPrefix: string targetBaseUrl: string + controlUiOrigin: string authorityMode: 'direct-host' | 'bridged-sandbox-map' | 'bridged-instance-override' bridgeActive: boolean } @@ -49,6 +50,7 @@ function resolveProxyTarget(requestUrl: URL): ProxyTargetResolution { instanceId: authority.openclaw.id, proxyPrefix: `/api/openshell/instances/${encodeURIComponent(decodedInstanceId)}/dashboard/proxy`, targetBaseUrl: authority.openclaw.dashboardUrl, + controlUiOrigin: authority.openclaw.controlUiOrigin || new URL(authority.openclaw.dashboardUrl).origin, authorityMode: authority.authorityMode, bridgeActive: authority.bridgeActive, } @@ -63,13 +65,14 @@ function resolveProxyTarget(requestUrl: URL): ProxyTargetResolution { instanceId: authority.bridgeActive ? authority.openclaw.id : null, proxyPrefix: LEGACY_PROXY_PREFIX, targetBaseUrl: authority.openclaw.dashboardUrl, + controlUiOrigin: authority.openclaw.controlUiOrigin || new URL(authority.openclaw.dashboardUrl).origin, authorityMode: authority.authorityMode, bridgeActive: authority.bridgeActive, } } function buildTargetUrl(requestUrl: URL) { - const { proxyPrefix, targetBaseUrl, authorityMode, bridgeActive } = resolveProxyTarget(requestUrl) + const { proxyPrefix, targetBaseUrl, controlUiOrigin, authorityMode, bridgeActive } = resolveProxyTarget(requestUrl) const pathParam = requestUrl.searchParams.get('path') const bootstrapUrl = requestUrl.searchParams.get('bootstrapUrl') const pathname = requestUrl.pathname.startsWith(proxyPrefix) @@ -87,12 +90,11 @@ function buildTargetUrl(requestUrl: URL) { }) } - return { target, proxyPrefix, authorityMode, bridgeActive } + return { target, proxyPrefix, controlUiOrigin, authorityMode, bridgeActive } } -function copyRequestHeaders(request: Request, target: URL) { +function copyRequestHeaders(request: Request, target: URL, controlUiOrigin: string) { const headers = new Headers() - const targetOrigin = target.origin request.headers.forEach((value, key) => { const lowerKey = key.toLowerCase() @@ -107,8 +109,8 @@ function copyRequestHeaders(request: Request, target: URL) { }) headers.set('host', target.host) - headers.set('origin', targetOrigin) - headers.set('referer', `${targetOrigin}/`) + headers.set('origin', controlUiOrigin) + headers.set('referer', `${controlUiOrigin}/`) return headers } @@ -214,9 +216,9 @@ export async function proxyOpenClawDashboard(request: Request) { return bootstrapScriptResponse(resolution.proxyPrefix) } - const { target, proxyPrefix, authorityMode, bridgeActive } = buildTargetUrl(requestUrl) + const { target, proxyPrefix, controlUiOrigin, authorityMode, bridgeActive } = buildTargetUrl(requestUrl) const method = request.method.toUpperCase() - const headers = copyRequestHeaders(request, target) + const headers = copyRequestHeaders(request, target, controlUiOrigin) const shouldSendBody = !['GET', 'HEAD'].includes(method) const upstreamInit: RequestInit & { duplex?: 'half' } = { method, diff --git a/app/lib/openclawInstances.ts b/app/lib/openclawInstances.ts index 72922c9..d462070 100644 --- a/app/lib/openclawInstances.ts +++ b/app/lib/openclawInstances.ts @@ -2,6 +2,7 @@ export type OpenClawInstanceRecord = { id: string label: string dashboardUrl: string + controlUiOrigin?: string | null terminalServerUrl?: string | null loopbackOnly: boolean default?: boolean @@ -52,6 +53,7 @@ function parseRegistryFromEnv(): OpenClawInstanceRecord[] { id, label: typeof entry.label === 'string' && entry.label.trim() ? entry.label.trim() : id, dashboardUrl, + controlUiOrigin: typeof entry.controlUiOrigin === 'string' && entry.controlUiOrigin.trim() ? entry.controlUiOrigin.trim() : null, terminalServerUrl: typeof entry.terminalServerUrl === 'string' && entry.terminalServerUrl.trim() ? entry.terminalServerUrl.trim() : DEFAULT_TERMINAL_SERVER_URL, loopbackOnly: parseBoolean(typeof entry.loopbackOnly === 'string' ? entry.loopbackOnly : undefined, entry.loopbackOnly ?? true), default: Boolean(entry.default), @@ -150,6 +152,7 @@ function resolveSandboxOpenClawInstance(instanceId: string): OpenClawInstanceRec id: instanceId, label: `OpenClaw for ${parsed.sandboxId}`, dashboardUrl: `http://127.0.0.1:${parsed.port}/`, + controlUiOrigin: process.env.OPENCLAW_SANDBOX_CONTROL_UI_ORIGIN || 'http://127.0.0.1:18789', terminalServerUrl: DEFAULT_TERMINAL_SERVER_URL, loopbackOnly: true, default: false, diff --git a/server.mjs b/server.mjs index 5a7cd51..5597c2d 100644 --- a/server.mjs +++ b/server.mjs @@ -69,6 +69,7 @@ function defaultOpenClawInstance() { id: defaultInstanceId, label: 'Default local OpenClaw', dashboardUrl: defaultDashboardUrl, + controlUiOrigin: null, terminalServerUrl: process.env.TERMINAL_SERVER_URL || 'http://127.0.0.1:3011', loopbackOnly: true, default: true, @@ -95,6 +96,7 @@ function parseOpenClawInstanceRegistry() { id, label: typeof entry.label === 'string' && entry.label.trim() ? entry.label.trim() : id, dashboardUrl, + controlUiOrigin: typeof entry.controlUiOrigin === 'string' && entry.controlUiOrigin.trim() ? entry.controlUiOrigin.trim() : null, terminalServerUrl: typeof entry.terminalServerUrl === 'string' && entry.terminalServerUrl.trim() ? entry.terminalServerUrl.trim() : process.env.TERMINAL_SERVER_URL || 'http://127.0.0.1:3011', @@ -182,6 +184,7 @@ function resolveSandboxOpenClawInstance(instanceId) { id: requested, label: `OpenClaw for ${match[2]}`, dashboardUrl: `http://127.0.0.1:${port}/`, + controlUiOrigin: process.env.OPENCLAW_SANDBOX_CONTROL_UI_ORIGIN || 'http://127.0.0.1:18789', terminalServerUrl: process.env.TERMINAL_SERVER_URL || 'http://127.0.0.1:3011', loopbackOnly: true, default: false, @@ -216,6 +219,7 @@ function resolveDashboardUpstream(req) { return { proxyPrefix, upstreamWsUrl, + controlUiOrigin: resolvedInstance.controlUiOrigin || new URL(resolvedInstance.dashboardUrl).origin, instanceId: resolvedInstance.id, } } @@ -241,12 +245,7 @@ const dashboardWsProxyServer = http.createServer((_, res) => { }) const clientWss = new WebSocketServer({ noServer: true }) -function buildRawDashboardUpgradeHeaders(req, upstreamWsUrl) { - const upstreamOrigin = new URL(upstreamWsUrl.toString()) - upstreamOrigin.protocol = upstreamOrigin.protocol === 'wss:' ? 'https:' : 'http:' - upstreamOrigin.pathname = '/' - upstreamOrigin.search = '' - upstreamOrigin.hash = '' +function buildRawDashboardUpgradeHeaders(req, upstreamWsUrl, controlUiOrigin) { const headers = [] const seen = new Set() @@ -258,18 +257,18 @@ function buildRawDashboardUpgradeHeaders(req, upstreamWsUrl) { if (lowerName === 'host') { headers.push(`Host: ${upstreamWsUrl.host}`) } else if (lowerName === 'origin') { - headers.push(`Origin: ${upstreamOrigin.origin}`) + headers.push(`Origin: ${controlUiOrigin}`) } else { headers.push(`${name}: ${value}`) } } if (!seen.has('host')) headers.push(`Host: ${upstreamWsUrl.host}`) - if (!seen.has('origin')) headers.push(`Origin: ${upstreamOrigin.origin}`) + if (!seen.has('origin')) headers.push(`Origin: ${controlUiOrigin}`) return headers } -function tunnelDashboardUpgrade(req, socket, head, upstreamWsUrl, instanceId) { +function tunnelDashboardUpgrade(req, socket, head, upstreamWsUrl, instanceId, controlUiOrigin) { const isSecure = upstreamWsUrl.protocol === 'wss:' const port = Number(upstreamWsUrl.port || (isSecure ? 443 : 80)) const upstreamSocket = isSecure @@ -278,7 +277,7 @@ function tunnelDashboardUpgrade(req, socket, head, upstreamWsUrl, instanceId) { const upstreamPath = `${upstreamWsUrl.pathname || '/'}${upstreamWsUrl.search || ''}` const requestHead = [ `GET ${upstreamPath} HTTP/1.1`, - ...buildRawDashboardUpgradeHeaders(req, upstreamWsUrl), + ...buildRawDashboardUpgradeHeaders(req, upstreamWsUrl, controlUiOrigin), '', '', ].join('\r\n') @@ -470,14 +469,14 @@ server.on('upgrade', (req, socket, head) => { (req.url || '').startsWith(legacyDashboardProxyPrefix) || (req.url || '').startsWith(instancesProxyPrefix) ) { - const { upstreamWsUrl, instanceId } = resolveDashboardUpstream(req) + const { upstreamWsUrl, instanceId, controlUiOrigin } = resolveDashboardUpstream(req) logBridge('dashboard-upgrade-accepted', { path: req.url || '/', upstreamUrl: upstreamWsUrl.toString(), instanceId, remoteAddress: req.socket.remoteAddress || 'unknown', }) - tunnelDashboardUpgrade(req, socket, head, upstreamWsUrl, instanceId) + tunnelDashboardUpgrade(req, socket, head, upstreamWsUrl, instanceId, controlUiOrigin) return } @@ -497,14 +496,14 @@ dashboardWsProxyServer.on('upgrade', (req, socket, head) => { (req.url || '').startsWith(legacyDashboardProxyPrefix) || (req.url || '').startsWith(instancesProxyPrefix) ) { - const { upstreamWsUrl, instanceId } = resolveDashboardUpstream(req) + const { upstreamWsUrl, instanceId, controlUiOrigin } = resolveDashboardUpstream(req) logBridge('dashboard-sidecar-upgrade-accepted', { path: req.url || '/', upstreamUrl: upstreamWsUrl.toString(), instanceId, remoteAddress: req.socket.remoteAddress || 'unknown', }) - tunnelDashboardUpgrade(req, socket, head, upstreamWsUrl, instanceId) + tunnelDashboardUpgrade(req, socket, head, upstreamWsUrl, instanceId, controlUiOrigin) return } diff --git a/tests/openclaw-dashboard-origin-rewrite-check.mjs b/tests/openclaw-dashboard-origin-rewrite-check.mjs index c158e1b..b67a94c 100644 --- a/tests/openclaw-dashboard-origin-rewrite-check.mjs +++ b/tests/openclaw-dashboard-origin-rewrite-check.mjs @@ -9,10 +9,11 @@ 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, /controlUiOrigin: authority\.openclaw\.controlUiOrigin/, 'HTTP proxy must resolve the OpenClaw control UI origin separately from the transport URL') 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') +assert.match(proxySource, /headers\.set\('origin', controlUiOrigin\)/, 'HTTP proxy must rewrite Origin to the gateway control UI origin') +assert.match(proxySource, /headers\.set\('referer', `\$\{controlUiOrigin\}\/`\)/, 'HTTP proxy must rewrite Referer to the gateway control UI origin') +assert.match(serverSource, /controlUiOrigin: process\.env\.OPENCLAW_SANDBOX_CONTROL_UI_ORIGIN \|\| 'http:\/\/127\.0\.0\.1:18789'/, 'sandbox instances must preserve the gateway internal control UI origin') +assert.match(serverSource, /headers\.push\(`Origin: \$\{controlUiOrigin\}`\)/, 'WebSocket tunnel must rewrite Origin to the gateway control UI origin') console.log('openclaw-dashboard-origin-rewrite-check: PASS dashboard proxy origin rewrite assertions')