Merge pull request #355 from nwparker/cloud/w3-ports-external-collapsed

fix(w3): hide the sandbox's own services from External ports in cloud workspaces
This commit is contained in:
Neil
2026-11-10 20:29:33 -08:00
committed by GitHub
9 changed files with 193 additions and 6 deletions
File diff suppressed because one or more lines are too long
@@ -178,9 +178,12 @@ ca_env=(NODE_USE_SYSTEM_CA="${NODE_USE_SYSTEM_CA:-1}")
for name in NODE_EXTRA_CA_CERTS SSL_CERT_FILE; do
if [ -n "${!name:-}" ]; then ca_env+=("$name=${!name}"); fi
done
# Ports panel: programs under $ORCA_HOME are the image's own services, and 23456 is Vercel's
# sandbox-init (root, pid invisible to this user); neither is listed as the user's port.
env -i HOME="$HOME" USER="$(id -un)" LANG=C.UTF-8 TERM=xterm-256color "${ca_env[@]}" \
PATH="$ORCA_HOME/tools/bin:$ORCA_HOME/bin:$(dirname "$system_node"):/usr/local/bin:/usr/bin:/bin" \
ORCA_USER_DATA="$HOME/.orcad-data" ORCA_SANDBOX_ID="$ORCA_SANDBOX_ID" "${managed_env[@]}" \
ORCA_HOST_MANAGED_LISTENER_PATHS="$ORCA_HOME" ORCA_HOST_MANAGED_UNOWNED_PORTS=23456 \
NODE_COMPILE_CACHE="$ORCA_HOME/compile-cache" ORCA_ORCAD_STARTUP_QUALIFICATION_DIR="$ORCA_HOME/startup-qualification" \
ORCA_SESSION_ENV_NAMES="${secret_names[*]}" "${agent_env[@]}" \
setsid nohup "$node" "$ORCA_HOME/bin/orcad-boot" --code-cache "$ORCA_HOME/compile-cache/orcad-server.v8" \
@@ -0,0 +1,78 @@
import { describe, expect, it } from 'vitest'
import { hostManagedListenerConfig, isHostManagedListener } from './host-managed-listeners'
const config = (roots: string[] = ['/opt/orca'], unownedPorts: number[] = [23456]) => ({
roots,
unownedPorts: new Set(unownedPorts)
})
describe('host-managed listeners', () => {
it('reads absolute roots and platform ports from the environment', () => {
const parsed = hostManagedListenerConfig({
ORCA_HOST_MANAGED_LISTENER_PATHS: '/opt/orca/:relative:/srv/tools',
ORCA_HOST_MANAGED_UNOWNED_PORTS: '23456, x,70000'
})
expect(parsed.roots).toEqual(['/opt/orca', '/srv/tools'])
expect([...parsed.unownedPorts]).toEqual([23456])
expect(hostManagedListenerConfig({})).toEqual({ roots: [], unownedPorts: new Set() })
})
it('marks the server itself and programs started from a managed root', () => {
expect(isHostManagedListener({ pid: 27, port: 6768 }, config(), 27)).toBe(true)
expect(
isHostManagedListener(
{ pid: 90, port: 2222, commandLine: '/opt/orca/openssh/sbin/sshd -D -f x' },
config(),
27
)
).toBe(true)
expect(
isHostManagedListener(
{
pid: 91,
port: 6080,
commandLine: 'python3 /opt/orca/bin/orca-desktop-proxy --port 6080'
},
config(),
27
)
).toBe(true)
})
it('marks a pid-less listener only on a declared platform port', () => {
expect(isHostManagedListener({ port: 23456 }, config(), 27)).toBe(true)
// A root-run postgres or Docker-published port has no visible pid but is the user's.
expect(isHostManagedListener({ port: 5432 }, config(), 27)).toBe(false)
expect(isHostManagedListener({ port: 23456 }, config(['/opt/orca'], []), 27)).toBe(false)
})
it('leaves user servers alone, matching by program path rather than port', () => {
expect(
isHostManagedListener(
{ pid: 92, port: 6768, commandLine: 'python3 -m http.server 6768' },
config(),
27
)
).toBe(false)
expect(
isHostManagedListener(
{ pid: 92, port: 23456, commandLine: 'python3 -m http.server 23456' },
config(),
27
)
).toBe(false)
expect(
isHostManagedListener(
{ pid: 93, port: 5173, commandLine: 'node vite --config /opt/orca/x.js' },
config(),
27
)
).toBe(false)
expect(
isHostManagedListener({ pid: 94, port: 1, commandLine: '/opt/orcaother/bin/x' }, config(), 27)
).toBe(false)
expect(
isHostManagedListener({ pid: 95, port: 1, commandLine: '/opt/orca/bin/x' }, config([]), 27)
).toBe(false)
})
})
+54
View File
@@ -0,0 +1,54 @@
import type { RawListeningPort } from './local-workspace-port-scan-state'
/** Colon-separated install roots whose binaries are the host's own services (e.g. a sandbox image's tools). */
export const HOST_MANAGED_LISTENER_PATHS_ENV = 'ORCA_HOST_MANAGED_LISTENER_PATHS'
/** Comma-separated ports the host's platform listens on from a process this user cannot see (no pid). */
export const HOST_MANAGED_UNOWNED_PORTS_ENV = 'ORCA_HOST_MANAGED_UNOWNED_PORTS'
export type HostManagedListenerConfig = {
roots: readonly string[]
unownedPorts: ReadonlySet<number>
}
export function hostManagedListenerConfig(
env: Record<string, string | undefined> = process.env
): HostManagedListenerConfig {
const roots = (env[HOST_MANAGED_LISTENER_PATHS_ENV] ?? '')
.split(':')
.map((root) => root.trim().replace(/\/+$/, ''))
.filter((root) => root.startsWith('/') && root.length > 1)
const unownedPorts = new Set(
(env[HOST_MANAGED_UNOWNED_PORTS_ENV] ?? '')
.split(',')
.map((port) => Number(port.trim()))
.filter((port) => Number.isInteger(port) && port > 0 && port < 65536)
)
return { roots, unownedPorts }
}
/**
* Listeners the host runs itself: this server process, a program started from a managed root
* (checked on argv[0] and argv[1], so `python3 /root/bin/tool` counts too), or a declared platform
* port whose owner is invisible. A pid-less listener on any other port (a root-run postgres or
* Docker-published port) is still the user's.
*/
export function isHostManagedListener(
port: Pick<RawListeningPort, 'pid' | 'commandLine' | 'port'>,
config: HostManagedListenerConfig,
serverPid: number = process.pid
): boolean {
if (port.pid === undefined) {
return config.unownedPorts.has(port.port)
}
if (port.pid === serverPid) {
return true
}
if (config.roots.length === 0 || !port.commandLine) {
return false
}
return port.commandLine
.trim()
.split(/\s+/)
.slice(0, 2)
.some((token) => config.roots.some((root) => token === root || token.startsWith(`${root}/`)))
}
+11 -1
View File
@@ -8,6 +8,7 @@ import {
reconcileAdvertisedUrls
} from './local-workspace-port-attribution'
import { scanPlatformListeningPorts } from './local-workspace-platform-port-scanner'
import { hostManagedListenerConfig, isHostManagedListener } from './host-managed-listeners'
import {
getWorkspacePortScanCooldown,
isWorkspacePortScanCommandTimeout,
@@ -40,6 +41,12 @@ export async function scanWorkspacePorts(
worktrees,
options.requireMetadata
)
const hostManaged = hostManagedListenerConfig()
const managed = new Set(
rawPorts
.filter((port) => isHostManagedListener(port, hostManaged))
.map((port) => `${port.host}:${port.port}:${port.pid ?? 'unknown'}`)
)
// Orca's own listeners (runtime socket, hook server) run with a workspace in their command line but are not its servers.
const attributedPorts = attributePortsToWorkspaces(
rawPorts.map((port) =>
@@ -52,7 +59,10 @@ export async function scanWorkspacePorts(
reconcileAdvertisedUrls(attributedPorts, normalizedWorktrees, urlWatcher)
}
const ports = attributedPorts
.map((port) => enrichPort(port, urlWatcher))
.map((port) => {
const enriched = enrichPort(port, urlWatcher)
return managed.has(enriched.id) ? { ...enriched, hostManaged: true as const } : enriched
})
.sort(compareWorkspacePorts)
.slice(0, MAX_PORTS)
return { platform: process.platform, scannedAt: Date.now(), ports }
@@ -0,0 +1,38 @@
import { describe, expect, it } from 'vitest'
import type { WorkspacePort } from '../../../../shared/workspace-ports'
import { getLocalWorkspacePortSections } from './local-workspace-port-sections'
const external = (
port: number,
extra: { pid?: number; hostManaged?: true } = {}
): WorkspacePort => ({
id: `x:${port}`,
kind: 'external',
bindHost: '127.0.0.1',
connectHost: 'localhost',
port,
protocol: 'http',
pid: 'pid' in extra ? extra.pid : 100 + port,
...(extra.hostManaged ? { hostManaged: true } : {})
})
const scan = {
ports: [
external(6768, { hostManaged: true }),
external(23456, { pid: undefined, hostManaged: true }),
external(5432, { pid: undefined }),
external(8123)
]
}
describe('getLocalWorkspacePortSections', () => {
it('hides only host-marked services from External, keeping a pid-less user port', () => {
const sections = getLocalWorkspacePortSections(scan, 'r', 'w', true)
expect(sections.externalPorts.map((port) => port.port)).toEqual([5432, 8123])
})
it('keeps every listener on other hosts', () => {
const sections = getLocalWorkspacePortSections(scan, 'r', 'w')
expect(sections.externalPorts.map((port) => port.port)).toEqual([6768, 23456, 5432, 8123])
})
})
@@ -3,13 +3,15 @@ import type { WorkspacePort } from '../../../../shared/workspace-ports'
export function getLocalWorkspacePortSections(
scan: { ports: WorkspacePort[] } | null | undefined,
activeRepoId: string | null | undefined,
activeWorktreeId: string | null | undefined
activeWorktreeId: string | null | undefined,
/** Orca Cloud: the sandbox's own services (marked host-managed by the host) are not the user's ports. */
hideHostServices = false
): {
activePorts: WorkspacePort[]
otherWorkspacePorts: WorkspacePort[]
externalPorts: WorkspacePort[]
} {
const ports = scan?.ports ?? []
const ports = (scan?.ports ?? []).filter((port) => !hideHostServices || port.hostManaged !== true)
return {
activePorts: ports.filter(
(port) =>
@@ -213,8 +213,8 @@ export function LocalWorkspacePortsPanel({
)
const { activePorts, otherWorkspacePorts, externalPorts } = useMemo(
() => getLocalWorkspacePortSections(displayScan, activeRepoId, activeWorktree?.id),
[activeRepoId, activeWorktree?.id, displayScan]
() => getLocalWorkspacePortSections(displayScan, activeRepoId, activeWorktree?.id, isSandbox),
[activeRepoId, activeWorktree?.id, displayScan, isSandbox]
)
const showPortSections = shouldShowLocalWorkspacePortSections(displayScan, {
+2
View File
@@ -25,6 +25,8 @@ type WorkspacePortBase = {
pid?: number
processName?: string
protocol: 'http' | 'https' | 'unknown'
/** The host's own service (the server process, or a binary under ORCA_HOST_MANAGED_LISTENER_PATHS), not a user's. */
hostManaged?: true
}
export type WorkspacePort =