Harden MCP broker sandbox handoff
This commit is contained in:
@@ -86,6 +86,20 @@ export async function verifySandboxMcpBrokerToken(token: string) {
|
||||
return session
|
||||
}
|
||||
|
||||
export async function revokeSandboxMcpBrokerSession(sandboxId: string) {
|
||||
const store = await readStore()
|
||||
const existing = store.sessions[sandboxId]
|
||||
if (!existing) return null
|
||||
store.sessions[sandboxId] = {
|
||||
...existing,
|
||||
enabled: false,
|
||||
rotatedAt: new Date().toISOString(),
|
||||
expiresAt: new Date().toISOString(),
|
||||
}
|
||||
await writeStore(store)
|
||||
return store.sessions[sandboxId]
|
||||
}
|
||||
|
||||
export async function listAllowedBrokerServers(session: SandboxMcpBrokerSession) {
|
||||
const inventory = await listMcpServers()
|
||||
return inventory.servers.filter((server) => sandboxCanAccessMcpServer(server, session.sandboxId, session.sandboxName))
|
||||
|
||||
@@ -0,0 +1,82 @@
|
||||
import { execFile } from "node:child_process"
|
||||
import { promisify } from "node:util"
|
||||
import { HOST_PATH, OPENSHELL_BIN } from "./hostCommands"
|
||||
import { resolveSandboxRef } from "./openshellHost"
|
||||
|
||||
const execFileAsync = promisify(execFile)
|
||||
|
||||
const LOCAL_HOSTNAMES = new Set(["localhost", "127.0.0.1", "0.0.0.0", "host.docker.internal", "host.openshell.internal"])
|
||||
const FALLBACK_SANDBOX_PROXY_ORIGIN = "http://10.200.0.1:3128"
|
||||
|
||||
async function discoverOpenShellDockerGateway() {
|
||||
const container = process.env.OPEN_SHELL_CONTAINER || "openshell-cluster-nemoclaw"
|
||||
const { stdout } = await execFileAsync("docker", ["inspect", container], {
|
||||
env: { ...process.env, PATH: HOST_PATH },
|
||||
timeout: 5000,
|
||||
maxBuffer: 1024 * 1024,
|
||||
})
|
||||
const [inspect] = JSON.parse(String(stdout)) as Array<{
|
||||
NetworkSettings?: {
|
||||
Networks?: Record<string, { Gateway?: string }>
|
||||
}
|
||||
}>
|
||||
const networks = inspect?.NetworkSettings?.Networks || {}
|
||||
return Object.values(networks).map((network) => network.Gateway).find(Boolean) || null
|
||||
}
|
||||
|
||||
function normalizeProxyOrigin(value: string) {
|
||||
try {
|
||||
const proxy = new URL(value)
|
||||
if (proxy.protocol !== "http:" && proxy.protocol !== "https:") return null
|
||||
return proxy.toString().replace(/\/+$/, "")
|
||||
} catch {
|
||||
return null
|
||||
}
|
||||
}
|
||||
|
||||
async function runOpenShell(args: string[]) {
|
||||
const { stdout } = await execFileAsync(OPENSHELL_BIN, args, {
|
||||
env: {
|
||||
...process.env,
|
||||
PATH: HOST_PATH,
|
||||
OPENSHELL_GATEWAY: process.env.OPENSHELL_GATEWAY || "nemoclaw",
|
||||
NO_COLOR: "1",
|
||||
CLICOLOR: "0",
|
||||
CLICOLOR_FORCE: "0",
|
||||
TERM: "dumb",
|
||||
},
|
||||
timeout: 15000,
|
||||
maxBuffer: 1024 * 1024,
|
||||
})
|
||||
return String(stdout)
|
||||
}
|
||||
|
||||
async function discoverSandboxProxyOrigin(sandboxId: string, sandboxName?: string | null) {
|
||||
const resolvedName = sandboxName || (await resolveSandboxRef(sandboxId)).name
|
||||
const script = "printf '%s\\n' \"${HTTP_PROXY:-${http_proxy:-${HTTPS_PROXY:-${https_proxy:-}}}}\""
|
||||
const output = await runOpenShell(["sandbox", "exec", "-n", resolvedName, "--", "sh", "-lc", script])
|
||||
return normalizeProxyOrigin(output.trim())
|
||||
}
|
||||
|
||||
export async function brokerBaseUrlForSandbox(
|
||||
request: Request,
|
||||
sandbox?: { id: string, name?: string | null },
|
||||
) {
|
||||
if (process.env.OPENSHELL_CONTROL_MCP_BROKER_URL) return process.env.OPENSHELL_CONTROL_MCP_BROKER_URL
|
||||
|
||||
const origin = new URL(request.url).origin
|
||||
const publicOrigin = new URL(origin)
|
||||
if (LOCAL_HOSTNAMES.has(publicOrigin.hostname)) {
|
||||
const gateway = await discoverOpenShellDockerGateway().catch(() => null)
|
||||
publicOrigin.hostname = gateway || "host.docker.internal"
|
||||
}
|
||||
|
||||
const hostBrokerUrl = `${publicOrigin.toString().replace(/\/+$/, "")}/api/mcp/broker`
|
||||
const proxyOrigin = sandbox
|
||||
? await discoverSandboxProxyOrigin(sandbox.id, sandbox.name).catch(() => null)
|
||||
: null
|
||||
|
||||
if (proxyOrigin) return `${proxyOrigin}/${hostBrokerUrl}`
|
||||
if (LOCAL_HOSTNAMES.has(new URL(origin).hostname)) return `${FALLBACK_SANDBOX_PROXY_ORIGIN}/${hostBrokerUrl}`
|
||||
return hostBrokerUrl
|
||||
}
|
||||
@@ -85,6 +85,7 @@ export const MCP_SERVER_CATALOG: McpCatalogEntry[] = [
|
||||
tags: ["web", "utility"],
|
||||
},
|
||||
]
|
||||
const BASELINE_MCP_SERVER_IDS = ["memory"]
|
||||
|
||||
function emptyStore(): StoreShape {
|
||||
return { servers: {} }
|
||||
@@ -164,6 +165,23 @@ async function writeStore(store: StoreShape) {
|
||||
await writeFile(STORE_PATH, JSON.stringify(store, null, 2), { mode: 0o600 })
|
||||
}
|
||||
|
||||
async function ensureBaselineMcpServers(store: StoreShape) {
|
||||
let changed = false
|
||||
for (const id of BASELINE_MCP_SERVER_IDS) {
|
||||
const entry = MCP_SERVER_CATALOG.find((candidate) => candidate.id === id)
|
||||
if (!entry || store.servers[id]) continue
|
||||
store.servers[id] = normalizeInstall({
|
||||
...entry,
|
||||
source: "catalog",
|
||||
enabled: true,
|
||||
accessMode: "disabled",
|
||||
allowedSandboxIds: [],
|
||||
})
|
||||
changed = true
|
||||
}
|
||||
if (changed) await writeStore(store)
|
||||
}
|
||||
|
||||
export function buildMcpClientConfig(servers: McpServerInstall[]) {
|
||||
return {
|
||||
mcpServers: Object.fromEntries(
|
||||
@@ -195,6 +213,7 @@ export function sandboxCanAccessMcpServer(server: McpServerInstall, sandboxId: s
|
||||
|
||||
export async function listMcpServers() {
|
||||
const store = await readStore()
|
||||
await ensureBaselineMcpServers(store)
|
||||
const servers = Object.values(store.servers).sort((a, b) => a.name.localeCompare(b.name))
|
||||
return {
|
||||
catalog: MCP_SERVER_CATALOG,
|
||||
|
||||
@@ -21,6 +21,16 @@ function sanitizeArchiveStem(value: string) {
|
||||
return value.trim().replace(/[^\w.@:+-]/g, "-").replace(/-+/g, "-").replace(/^-|-$/g, "") || "sandbox"
|
||||
}
|
||||
|
||||
function normalizeRelativeArchivePath(input: string, fallbackFileName: string) {
|
||||
const raw = input.trim() || fallbackFileName
|
||||
if (!raw || raw.includes("\0")) throw new Error("relative file path contains unsupported characters")
|
||||
const normalized = path.posix.normalize(raw).replace(/^\/+/, "")
|
||||
if (!normalized || normalized === "." || normalized.startsWith("../") || normalized.includes("/../")) {
|
||||
throw new Error("relative file path must stay inside the uploaded directory")
|
||||
}
|
||||
return normalized
|
||||
}
|
||||
|
||||
export function normalizeSandboxPath(input: string, fallbackFileName?: string) {
|
||||
const raw = input.trim()
|
||||
if (!raw) throw new Error("sandbox path is required")
|
||||
@@ -106,6 +116,31 @@ export async function uploadSandboxFile(sandboxId: string, destinationPath: stri
|
||||
}
|
||||
}
|
||||
|
||||
export async function uploadSandboxFiles(
|
||||
sandboxId: string,
|
||||
destinationDirectory: string,
|
||||
files: Array<{ fileName: string; relativePath: string; payload: Buffer }>
|
||||
) {
|
||||
if (files.length === 0) throw new Error("at least one file is required")
|
||||
const totalBytes = files.reduce((sum, file) => sum + file.payload.byteLength, 0)
|
||||
if (totalBytes > MAX_FILE_BYTES) {
|
||||
throw new Error(`directory upload is too large; max transfer size is ${Math.floor(MAX_FILE_BYTES / 1024 / 1024)} MiB`)
|
||||
}
|
||||
|
||||
const uploaded = []
|
||||
for (const file of files) {
|
||||
const relativePath = normalizeRelativeArchivePath(file.relativePath, file.fileName)
|
||||
const targetPath = path.posix.join(destinationDirectory, relativePath)
|
||||
uploaded.push(await uploadSandboxFile(sandboxId, targetPath, file.fileName, file.payload))
|
||||
}
|
||||
|
||||
return {
|
||||
sandboxName: uploaded[0]?.sandboxName || await resolveSandboxName(sandboxId),
|
||||
files: uploaded,
|
||||
bytes: totalBytes,
|
||||
}
|
||||
}
|
||||
|
||||
export async function downloadSandboxFile(sandboxId: string, sourcePath: string) {
|
||||
const sandboxName = await resolveSandboxName(sandboxId)
|
||||
const targetPath = normalizeSandboxPath(sourcePath)
|
||||
@@ -128,6 +163,36 @@ export async function downloadSandboxFile(sandboxId: string, sourcePath: string)
|
||||
}
|
||||
}
|
||||
|
||||
export async function downloadSandboxPath(sandboxId: string, sourcePath: string) {
|
||||
const sandboxName = await resolveSandboxName(sandboxId)
|
||||
const targetPath = normalizeSandboxPath(sourcePath)
|
||||
const typeScript = `if [ -f ${shellQuote(targetPath)} ]; then echo file; elif [ -d ${shellQuote(targetPath)} ]; then echo directory; else exit 1; fi`
|
||||
const typeResult = await runSandboxExec(sandboxName, typeScript, undefined, 30000)
|
||||
if (typeResult.code !== 0) throw new Error("sandbox path does not exist or is not readable")
|
||||
const type = typeResult.stdout.toString("utf8").trim()
|
||||
|
||||
if (type === "file") {
|
||||
return { ...(await downloadSandboxFile(sandboxId, targetPath)), archive: false }
|
||||
}
|
||||
|
||||
const parent = path.posix.dirname(targetPath)
|
||||
const base = path.posix.basename(targetPath) || "sandbox"
|
||||
const archiveName = `${sanitizeArchiveStem(base)}.tar.gz`
|
||||
const result = await runSandboxExec(sandboxName, `tar -C ${shellQuote(parent)} -czf - ${shellQuote(base)}`, undefined, 120000)
|
||||
if (result.code !== 0) throw new Error(result.stderr || "failed to archive sandbox directory")
|
||||
if (result.stdout.byteLength > MAX_FILE_BYTES) {
|
||||
throw new Error(`directory archive is too large; max transfer size is ${Math.floor(MAX_FILE_BYTES / 1024 / 1024)} MiB`)
|
||||
}
|
||||
|
||||
return {
|
||||
sandboxName,
|
||||
path: targetPath,
|
||||
fileName: archiveName,
|
||||
bytes: result.stdout,
|
||||
archive: true,
|
||||
}
|
||||
}
|
||||
|
||||
export type SandboxFileListEntry = {
|
||||
name: string
|
||||
path: string
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { rotateSandboxMcpBrokerSession } from "./mcpBrokerStore"
|
||||
import { revokeSandboxMcpBrokerSession, rotateSandboxMcpBrokerSession } from "./mcpBrokerStore"
|
||||
import { uploadSandboxFile } from "./sandboxFiles"
|
||||
|
||||
export const SANDBOX_MCP_MANIFEST_PATH = "/sandbox/openshell_control_mcp.md"
|
||||
@@ -87,3 +87,40 @@ export async function syncSandboxMcpManifest(
|
||||
markdown: handoff.markdown,
|
||||
}
|
||||
}
|
||||
|
||||
export async function revokeSandboxMcpManifest(sandbox: SandboxRef) {
|
||||
const session = await revokeSandboxMcpBrokerSession(sandbox.id)
|
||||
const now = new Date().toISOString()
|
||||
const markdown = [
|
||||
"# OpenShell Control MCP Broker",
|
||||
"",
|
||||
`Generated: ${now}`,
|
||||
`Sandbox: ${sandbox.name || sandbox.id}`,
|
||||
`Sandbox ID: ${sandbox.id}`,
|
||||
"",
|
||||
"MCP access is currently disabled for this sandbox.",
|
||||
"",
|
||||
"The previous broker token has been revoked. Ask the operator to enable an MCP server and issue a new broker config if MCP access is needed.",
|
||||
"",
|
||||
].join("\n")
|
||||
const uploaded = await uploadSandboxFile(
|
||||
sandbox.id,
|
||||
SANDBOX_MCP_MANIFEST_PATH,
|
||||
"openshell_control_mcp.md",
|
||||
Buffer.from(markdown, "utf8"),
|
||||
)
|
||||
|
||||
return {
|
||||
path: uploaded.path,
|
||||
sandboxName: uploaded.sandboxName,
|
||||
bytes: uploaded.bytes,
|
||||
brokerSession: session ? {
|
||||
sandboxId: session.sandboxId,
|
||||
sandboxName: session.sandboxName,
|
||||
enabled: session.enabled,
|
||||
rotatedAt: session.rotatedAt,
|
||||
expiresAt: session.expiresAt,
|
||||
} : null,
|
||||
markdown,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -16,6 +16,13 @@ export type SandboxNetworkRule = {
|
||||
binaries: string[]
|
||||
}
|
||||
|
||||
type BrokerNetworkAction = {
|
||||
action: "approve" | "reject"
|
||||
chunkId: string
|
||||
endpoint: string
|
||||
status: string
|
||||
}
|
||||
|
||||
function runOpenShell(args: string[], timeout = 60000) {
|
||||
return execFileAsync(OPENSHELL_BIN, args, {
|
||||
env: {
|
||||
@@ -73,6 +80,21 @@ function parseNetworkRules(output: string): SandboxNetworkRule[] {
|
||||
.filter((rule) => rule.chunkId)
|
||||
}
|
||||
|
||||
function brokerEndpointCandidates(brokerBaseUrl: string) {
|
||||
const parsed = new URL(brokerBaseUrl)
|
||||
const port = parsed.port || (parsed.protocol === "https:" ? "443" : "80")
|
||||
const hostPort = `${parsed.hostname}:${port}`
|
||||
const candidates = new Set([hostPort])
|
||||
if (parsed.hostname === "host.docker.internal") candidates.add(`0.0.0.0:${port}`)
|
||||
return candidates
|
||||
}
|
||||
|
||||
function ruleMatchesEndpoints(rule: SandboxNetworkRule, candidates: Set<string>) {
|
||||
return rule.endpoints.some((endpoint) => candidates.has(endpoint))
|
||||
|| Array.from(candidates).some((endpoint) => rule.rule.includes(endpoint.replace(/[^a-zA-Z0-9]/g, "_")))
|
||||
|| Array.from(candidates).some((endpoint) => rule.rationale.includes(endpoint))
|
||||
}
|
||||
|
||||
async function listRulesForStatus(sandboxName: string, status: "pending" | "approved" | "rejected") {
|
||||
const { stdout } = await runOpenShell(["rule", "get", "--status", status, sandboxName], 30000)
|
||||
return parseNetworkRules(String(stdout))
|
||||
@@ -121,3 +143,69 @@ export async function resolveSandboxNetworkRule(sandboxId: string, action: strin
|
||||
feed,
|
||||
}
|
||||
}
|
||||
|
||||
async function probeBrokerEndpoint(sandboxName: string, brokerBaseUrl: string) {
|
||||
const capabilitiesUrl = `${brokerBaseUrl.replace(/\/+$/, "")}/capabilities`
|
||||
const script = `curl --noproxy '*' -sS --max-time 5 ${JSON.stringify(capabilitiesUrl)} >/dev/null || true`
|
||||
await runOpenShell(["sandbox", "exec", "-n", sandboxName, "--", "sh", "-lc", script], 15000).catch(() => null)
|
||||
}
|
||||
|
||||
export async function syncBrokerNetworkAccess(sandboxId: string, brokerBaseUrl: string) {
|
||||
const resolved = await resolveSandboxRef(sandboxId)
|
||||
const candidates = brokerEndpointCandidates(brokerBaseUrl)
|
||||
await probeBrokerEndpoint(resolved.name, brokerBaseUrl)
|
||||
const pending = await listRulesForStatus(resolved.name, "pending").catch(() => [] as SandboxNetworkRule[])
|
||||
const matchingPending = pending.filter((rule) => ruleMatchesEndpoints(rule, candidates))
|
||||
const approved: BrokerNetworkAction[] = []
|
||||
|
||||
for (const rule of matchingPending) {
|
||||
await resolveSandboxNetworkRule(resolved.name, "approve", rule.chunkId)
|
||||
approved.push({
|
||||
action: "approve",
|
||||
chunkId: rule.chunkId,
|
||||
endpoint: rule.endpoints.find((endpoint) => candidates.has(endpoint)) || Array.from(candidates)[0] || "",
|
||||
status: rule.status,
|
||||
})
|
||||
}
|
||||
|
||||
const currentlyApproved = await listRulesForStatus(resolved.name, "approved").catch(() => [] as SandboxNetworkRule[])
|
||||
return {
|
||||
sandboxId,
|
||||
sandboxName: resolved.name,
|
||||
brokerBaseUrl,
|
||||
approved,
|
||||
alreadyApproved: currentlyApproved.filter((rule) => ruleMatchesEndpoints(rule, candidates)).map((rule) => ({
|
||||
chunkId: rule.chunkId,
|
||||
endpoints: rule.endpoints,
|
||||
status: rule.status,
|
||||
})),
|
||||
}
|
||||
}
|
||||
|
||||
export async function revokeBrokerNetworkAccess(sandboxId: string, brokerBaseUrl: string) {
|
||||
const resolved = await resolveSandboxRef(sandboxId)
|
||||
const candidates = brokerEndpointCandidates(brokerBaseUrl)
|
||||
const [pending, approved] = await Promise.all([
|
||||
listRulesForStatus(resolved.name, "pending").catch(() => [] as SandboxNetworkRule[]),
|
||||
listRulesForStatus(resolved.name, "approved").catch(() => [] as SandboxNetworkRule[]),
|
||||
])
|
||||
const matching = [...pending, ...approved].filter((rule) => ruleMatchesEndpoints(rule, candidates))
|
||||
const rejected: BrokerNetworkAction[] = []
|
||||
|
||||
for (const rule of matching) {
|
||||
await resolveSandboxNetworkRule(resolved.name, "reject", rule.chunkId, "MCP broker access revoked")
|
||||
rejected.push({
|
||||
action: "reject",
|
||||
chunkId: rule.chunkId,
|
||||
endpoint: rule.endpoints.find((endpoint) => candidates.has(endpoint)) || Array.from(candidates)[0] || "",
|
||||
status: rule.status,
|
||||
})
|
||||
}
|
||||
|
||||
return {
|
||||
sandboxId,
|
||||
sandboxName: resolved.name,
|
||||
brokerBaseUrl,
|
||||
rejected,
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user