From 2404397edca47b4a6abbc9c17dbb151ecd11245f Mon Sep 17 00:00:00 2001 From: mmckeen-nv <108093260+mmckeen-nv@users.noreply.github.com> Date: Fri, 21 Aug 2026 23:07:51 -0700 Subject: [PATCH] fix: fail closed on removed gateway start command (#42) Co-authored-by: mmckeen-nv --- app/api/openshell/gateway/repair/route.ts | 73 +++-------------------- tests/sandbox-lifecycle-check.mjs | 9 ++- 2 files changed, 13 insertions(+), 69 deletions(-) diff --git a/app/api/openshell/gateway/repair/route.ts b/app/api/openshell/gateway/repair/route.ts index e304fcb..f0ff6b6 100644 --- a/app/api/openshell/gateway/repair/route.ts +++ b/app/api/openshell/gateway/repair/route.ts @@ -1,68 +1,9 @@ import { NextResponse } from "next/server" -import { execFile } from "node:child_process" -import { cp, mkdir, readFile } from "node:fs/promises" -import path from "node:path" -import { promisify } from "node:util" -import { OPENSHELL_BIN, OPENSHELL_XDG_CONFIG_HOME, hostCommandEnv } from "@/app/lib/hostCommands" - -const execFileAsync = promisify(execFile) -const OPENSHELL_GATEWAY = process.env.OPENSHELL_GATEWAY || "nemoclaw" - -function sanitizeGatewayName(value: string) { - const name = value.trim() - if (!/^[a-zA-Z0-9][a-zA-Z0-9._-]{0,62}$/.test(name)) throw new Error("invalid gateway name") - return name -} - -async function activeGatewayName() { - try { - const raw = await readFile(path.join(OPENSHELL_XDG_CONFIG_HOME, "openshell/active_gateway"), "utf8") - return sanitizeGatewayName(raw) - } catch { - return sanitizeGatewayName(OPENSHELL_GATEWAY) - } -} - -async function runOpenShell(args: string[], timeout = 90000) { - const { stdout, stderr } = await execFileAsync(OPENSHELL_BIN, args, { - env: hostCommandEnv({ - OPENSHELL_GATEWAY, - }), - timeout, - maxBuffer: 10 * 1024 * 1024, - }) - return { stdout: String(stdout).trim(), stderr: String(stderr).trim() } -} - -async function backupOpenShellConfig() { - const source = path.join(OPENSHELL_XDG_CONFIG_HOME, "openshell") - const backupRoot = path.join(OPENSHELL_XDG_CONFIG_HOME, "openshell-dashboard-backups") - const backupPath = path.join(backupRoot, `openshell-${Date.now().toString(36)}`) - await mkdir(backupRoot, { recursive: true, mode: 0o700 }) - await cp(source, backupPath, { recursive: true, force: false }) - return backupPath -} - -export async function POST(request: Request) { - try { - const body = await request.json().catch(() => ({})) - const gateway = sanitizeGatewayName(typeof body?.gateway === "string" && body.gateway.trim() ? body.gateway : await activeGatewayName()) - const backupPath = await backupOpenShellConfig() - const start = await runOpenShell(["gateway", "start", "--name", gateway]) - const select = await runOpenShell(["gateway", "select", gateway], 30000) - const verify = await runOpenShell(["sandbox", "list"], 30000) - - return NextResponse.json({ - ok: true, - gateway, - backupPath, - warning: "OpenShell gateway trust repair backed up local OpenShell config, restarted the selected gateway, reselected it, and verified sandbox inventory. Use this only when gateway TLS trust is out of sync.", - start, - select, - verify, - }) - } catch (error) { - const message = error instanceof Error ? error.message : "Failed to repair OpenShell gateway trust" - return NextResponse.json({ ok: false, error: message }, { status: /invalid|required/.test(message) ? 400 : 500 }) - } +export async function POST() { + return NextResponse.json({ + ok: false, + code: "GATEWAY_LIFECYCLE_EXTERNAL", + error: "Automatic gateway trust repair is unavailable because current OpenShell releases do not expose `openshell gateway start`.", + guidance: "For a NemoClaw-owned gateway, run `nemoclaw onboard` to recover its managed lifecycle. For an externally managed gateway, start it with the owning deployment and then run `openshell gateway select `.", + }, { status: 409 }) } diff --git a/tests/sandbox-lifecycle-check.mjs b/tests/sandbox-lifecycle-check.mjs index daffc57..aead39b 100644 --- a/tests/sandbox-lifecycle-check.mjs +++ b/tests/sandbox-lifecycle-check.mjs @@ -309,9 +309,12 @@ assert.match(sandboxRestartRouteSource, /restartMode: "openclaw-runtime"/, 'rest assert.match(sandboxRestartRouteSource, /waitForSandboxReady\(sandboxName, 15000, 1000\)/, 'restart route must verify OpenShell reports the sandbox ready before runtime restart') assert.match(sandboxListSource, /Restart Sandbox/, 'sandbox details must expose the Monday-era sandbox restart button') assert.match(sandboxListSource, /\/api\/sandbox\/\$\{encodeURIComponent\(selectedSandbox\.id\)\}\/restart/, 'restart button must call the sandbox restart endpoint') -assert.match(gatewayRepairRouteSource, /gateway", "start", "--name", gateway/, 'gateway repair route must restart the selected OpenShell gateway') -assert.match(gatewayRepairRouteSource, /backupOpenShellConfig/, 'gateway repair route must back up OpenShell config before repair') -assert.match(gatewayRepairRouteSource, /\["sandbox", "list"\]/, 'gateway repair route must verify sandbox inventory after repair') +assert.match(gatewayRepairRouteSource, /GATEWAY_LIFECYCLE_EXTERNAL/, 'obsolete gateway repair route must fail closed with a stable compatibility code') +assert.match(gatewayRepairRouteSource, /current OpenShell releases do not expose `openshell gateway start`/, 'gateway repair route must explain the removed OpenShell command') +assert.match(gatewayRepairRouteSource, /nemoclaw onboard/, 'gateway repair route must direct NemoClaw-owned gateways to the supported lifecycle owner') +assert.match(gatewayRepairRouteSource, /openshell gateway select /, 'gateway repair route must retain safe external-gateway selection guidance') +assert.match(gatewayRepairRouteSource, /status: 409/, 'obsolete gateway repair must return a conflict instead of reporting a false repair') +assert.doesNotMatch(gatewayRepairRouteSource, /backupOpenShellConfig|gateway", "start"|\["sandbox", "list"\]/, 'obsolete gateway repair must not mutate config or invoke removed lifecycle commands') assert.doesNotMatch(pageSource, /Repair Gateway Trust|api\/openshell\/gateway\/repair|It does not destroy sandboxes/, 'dashboard page must not include the later gateway trust repair UI after rollback') assert.match(sandboxListSource, /setInterval\(loadPermissionFeeds/, 'sandbox list must match the Monday-era permission feed polling behavior') assert.match(sandboxListSource, /