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