Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 65 additions & 0 deletions src/lib/actions/sandbox/connect-flow-dcode-probe-preamble.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
// SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
// SPDX-License-Identifier: Apache-2.0

import { afterEach, beforeEach, describe, expect, it, type MockInstance, vi } from "vitest";

import {
connectModulePath,
createConnectHarness,
requireDist,
} from "../../../../test/support/connect-flow-test-harness";

describe("connectSandbox DCode probe preamble boundary", () => {
let exitSpy: MockInstance;
const originalStdoutIsTty = process.stdout.isTTY;

beforeEach(() => {
process.env.NEMOCLAW_TEST_NO_SLEEP = "1";
Object.defineProperty(process.stdout, "isTTY", { configurable: true, value: true });
exitSpy = vi.spyOn(process, "exit").mockImplementation(((code?: number | string | null) => {
throw new Error(`process.exit(${code ?? 0})`);
}) as never);
});

afterEach(() => {
vi.restoreAllMocks();
Object.defineProperty(process.stdout, "isTTY", {
configurable: true,
value: originalStdoutIsTty,
});
delete process.env.NEMOCLAW_TEST_NO_SLEEP;
delete require.cache[requireDist.resolve(connectModulePath)];
});

it.each([
"OK 200\nBROKEN 000",
"BROKEN 503\nOK 200",
])("rejects login-shell preamble evidence without repair or SSH (%s) (#6192)", async (output) => {
const harness = createConnectHarness({
agentName: "langchain-deepagents-code",
registryEntry: {
provider: "nvidia-prod",
model: "nvidia/nemotron-3-super-120b-a12b",
},
inferenceGetOutput:
"Gateway inference:\n Provider: nvidia-prod\n Model: nvidia/nemotron-3-super-120b-a12b\n",
inferenceProbeResponses: [output],
sessionAgent: { name: "langchain-deepagents-code" },
});

await expect(harness.connectSandbox("alpha")).rejects.toThrow("process.exit(1)");

expect(harness.applyVmDnsMonkeypatchSpy).not.toHaveBeenCalled();
expect(harness.runSetupDnsProxySpy).not.toHaveBeenCalled();
expect(harness.runOpenshellSpy).not.toHaveBeenCalled();
expect(harness.spawnSyncSpy).not.toHaveBeenCalledWith(
"openshell",
["sandbox", "connect", "alpha"],
expect.any(Object),
);
expect(harness.errorSpy.mock.calls.flat().join("\n")).toContain(
"did not return a trusted result",
);
expect(exitSpy).toHaveBeenCalledWith(1);
});
});
6 changes: 2 additions & 4 deletions src/lib/actions/sandbox/connect-flow.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ describe("connectSandbox flow", () => {
expect(exitSpy).toHaveBeenCalledWith(0);
});

it("runs the dcode inference route probe through its login-shell proxy contract (#6191)", async () => {
it("runs the dcode inference route probe through its immutable proxy contract (#6191)", async () => {
const harness = createConnectHarness({
agentName: "langchain-deepagents-code",
sessionAgent: {
Expand Down Expand Up @@ -282,9 +282,7 @@ describe("connectSandbox flow", () => {
"--",
"sh",
"-c",
expect.stringContaining('bash -lc "$1" "$CA_BUNDLE"'),
"nemoclaw-ca-capture",
expect.stringContaining("/usr/bin/curl"),
expect.stringContaining("/usr/local/share/nemoclaw/dcode-proxy-host"),
],
expect.objectContaining({ ignoreError: true }),
);
Expand Down
79 changes: 73 additions & 6 deletions src/lib/actions/sandbox/connect-inference-route-probe.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
// SPDX-License-Identifier: Apache-2.0

import { spawnSync } from "node:child_process";
import fs from "node:fs";
import os from "node:os";
import path from "node:path";
import { describe, expect, it } from "vitest";

import {
Expand All @@ -12,17 +15,21 @@ import {
} from "./connect-inference-route-probe";

describe("sandbox connect inference route probe argv", () => {
it("uses the dcode login-shell proxy contract without inherited proxy variables (#6191)", () => {
it("uses root-owned DCode proxy files without login-shell startup code (#6191)", () => {
const args = buildSandboxInferenceRouteProbeArgs("deep-code", {
name: "langchain-deepagents-code",
});

expect(args.slice(0, 7)).toEqual(["sandbox", "exec", "--name", "deep-code", "--", "sh", "-c"]);
expect(args.at(-3)).toContain('bash -lc "$1" "$CA_BUNDLE"');
expect(args.at(-3)).toContain("-u HTTPS_PROXY");
expect(args.at(-2)).toBe("nemoclaw-ca-capture");
expect(args.at(-1)).toContain('CA_BUNDLE="$0"');
expect(args).toHaveLength(8);
expect(args.at(-1)).toContain("/usr/local/share/nemoclaw/dcode-proxy-host");
expect(args.at(-1)).toContain("/usr/local/share/nemoclaw/dcode-proxy-port");
expect(args.at(-1)).toContain("0:444");
expect(args.at(-1)).toContain('HTTPS_PROXY="$PROXY_URL"');
expect(args.at(-1)).toContain("https://inference.local/v1/models");
expect(args.at(-1)).not.toContain("bash -lc");
expect(args.at(-1)).not.toContain("3>&1");
expect(args.at(-1)).not.toContain("/tmp/nemoclaw-proxy-env.sh");
expect(args.every((arg) => !/[\r\n]/.test(arg))).toBe(true);
});

Expand All @@ -47,7 +54,7 @@ describe("sandbox connect inference route probe argv", () => {
const args = buildSandboxInferenceRouteProbeArgs("alpha", { name: "openclaw" });
const script = args.at(-1) ?? "";

expect(script).toContain("/usr/bin/curl -s -o /dev/null");
expect(script).toContain("/usr/bin/curl -q -s -o /dev/null");
expect(script).toContain('CA_BUNDLE="${CURL_CA_BUNDLE:-${SSL_CERT_FILE:-}}"');
expect(script).toContain('--cacert "$CA_BUNDLE"');
expect(script).toContain("printf 'UNAVAILABLE OpenShell CA bundle missing or unreadable'");
Expand All @@ -74,6 +81,53 @@ describe("sandbox connect inference route probe argv", () => {
parseSandboxInferenceRouteProbeResult({ status: result.status, output: result.stdout }),
).toMatchObject({ healthy: false, broken: false, httpStatus: 0 });
});

it.each([
"OK 200",
"BROKEN 503",
])("does not execute DCode login-shell startup code containing a %s spoof (#6192)", (spoof) => {
const home = fs.mkdtempSync(path.join(os.tmpdir(), "nemoclaw-dcode-probe-"));
try {
const caBundle = path.join(home, "openshell-ca.pem");
const hostFile = path.join(home, "dcode-proxy-host");
const portFile = path.join(home, "dcode-proxy-port");
const profileMarker = path.join(home, "profile-ran");
const curlConfigMarker = path.join(home, "curl-config-ran");
fs.writeFileSync(caBundle, "test CA boundary", "utf8");
fs.writeFileSync(hostFile, "127.0.0.1\n", { mode: 0o444 });
fs.writeFileSync(portFile, "9\n", { mode: 0o444 });
fs.chmodSync(hostFile, 0o444);
fs.chmodSync(portFile, 0o444);
fs.writeFileSync(
path.join(home, ".bash_profile"),
`printf '%s\\n' ${JSON.stringify(spoof)} >&3; printf ran > ${JSON.stringify(profileMarker)}`,
);
fs.writeFileSync(
path.join(home, ".curlrc"),
`trace-ascii = ${JSON.stringify(curlConfigMarker)}\n`,
);
const args = buildSandboxInferenceRouteProbeArgs("deep-code", {
name: "langchain-deepagents-code",
});
const script = String(args.at(-1))
.replace("/usr/local/share/nemoclaw/dcode-proxy-host", hostFile)
.replace("/usr/local/share/nemoclaw/dcode-proxy-port", portFile)
.replaceAll("0:444", `${process.getuid?.() ?? 0}:444`);

const result = spawnSync("sh", ["-c", script], {
encoding: "utf8",
env: { ...process.env, HOME: home, CURL_CA_BUNDLE: caBundle, SSL_CERT_FILE: "" },
});

expect(result.status).toBe(0);
expect(result.stdout).toBe("BROKEN 000");
expect(result.stdout).not.toContain(spoof);
expect(fs.existsSync(profileMarker)).toBe(false);
expect(fs.existsSync(curlConfigMarker)).toBe(false);
} finally {
fs.rmSync(home, { force: true, recursive: true });
}
});
});

describe("sandbox inference route probe result", () => {
Expand Down Expand Up @@ -125,6 +179,19 @@ describe("sandbox inference route probe result", () => {
).toMatchObject({ healthy: false, broken: false, httpStatus: 0 });
});

it.each([
"OK 200\nBROKEN 000",
"BROKEN 503\nOK 200",
"[stdout] OK 200\nBROKEN 000",
"[stdout] BROKEN 503\nOK 200",
])("does not trust login-shell preamble output (%s) (#6192)", (output) => {
expect(parseSandboxInferenceRouteProbeResult({ status: 0, output })).toMatchObject({
healthy: false,
broken: false,
httpStatus: 0,
});
});

it("fails closed when malformed output claims an unhealthy status is OK (#6192)", () => {
expect(parseSandboxInferenceRouteProbeResult({ status: 0, output: "OK 503" })).toMatchObject({
healthy: false,
Expand Down
55 changes: 26 additions & 29 deletions src/lib/actions/sandbox/connect-inference-route-probe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,36 +27,34 @@ const INFERENCE_ROUTE_CA_FROM_ENV = 'CA_BUNDLE="${CURL_CA_BUNDLE:-${SSL_CERT_FIL
const INFERENCE_ROUTE_CA_VALIDATION =
'[ -n "$CA_BUNDLE" ] && [ -f "$CA_BUNDLE" ] && [ -r "$CA_BUNDLE" ] || { printf \'UNAVAILABLE OpenShell CA bundle missing or unreadable\'; exit 1; }';
const INFERENCE_ROUTE_PROBE_CORE_SCRIPT = [
"HTTP_CODE=$(/usr/bin/curl -s -o /dev/null -w '%{http_code}' --cacert \"$CA_BUNDLE\" --connect-timeout 3 --max-time 8 https://inference.local/v1/models 2>/dev/null) || HTTP_CODE=000",
"HTTP_CODE=$(/usr/bin/curl -q -s -o /dev/null -w '%{http_code}' --cacert \"$CA_BUNDLE\" --connect-timeout 3 --max-time 8 https://inference.local/v1/models 2>/dev/null) || HTTP_CODE=000",
'case "$HTTP_CODE" in [2-4][0-9][0-9]) printf \'OK %s\' "$HTTP_CODE" ;; *) printf \'BROKEN %s\' "$HTTP_CODE" ;; esac',
].join("; ");
export const INFERENCE_ROUTE_PROBE_SCRIPT = [
INFERENCE_ROUTE_CA_FROM_ENV,
INFERENCE_ROUTE_CA_VALIDATION,
INFERENCE_ROUTE_PROBE_CORE_SCRIPT,
].join("; ");
const INFERENCE_ROUTE_PROBE_FROM_ARG0_SCRIPT = [
'CA_BUNDLE="$0"',
INFERENCE_ROUTE_PROBE_CORE_SCRIPT,
].join("; ");

const PROXY_ENV_KEYS = [
"HTTP_PROXY",
"HTTPS_PROXY",
"http_proxy",
"https_proxy",
"NO_PROXY",
"no_proxy",
"ALL_PROXY",
"all_proxy",
] as const;

const DCODE_INFERENCE_ROUTE_PROBE_WRAPPER = [
const DCODE_PROXY_HOST_FILE = "/usr/local/share/nemoclaw/dcode-proxy-host";
const DCODE_PROXY_PORT_FILE = "/usr/local/share/nemoclaw/dcode-proxy-port";
const DCODE_PROXY_UNAVAILABLE =
"UNAVAILABLE managed DCode proxy files are missing, unsafe, or invalid";
const DCODE_INFERENCE_ROUTE_PROBE_SCRIPT = [
INFERENCE_ROUTE_CA_FROM_ENV,
INFERENCE_ROUTE_CA_VALIDATION,
// bash -lc receives CA_BUNDLE as argv[0], so the inner script reads the
// exact OpenShell-injected CA path from $0 after the login shell loads.
`exec env ${PROXY_ENV_KEYS.map((key) => `-u ${key}`).join(" ")} HOME=/sandbox bash -lc "$1" "$CA_BUNDLE"`,
`PROXY_HOST_FILE="${DCODE_PROXY_HOST_FILE}"`,
`PROXY_PORT_FILE="${DCODE_PROXY_PORT_FILE}"`,
`[ -f "$PROXY_HOST_FILE" ] && [ ! -L "$PROXY_HOST_FILE" ] && [ "$(/usr/bin/stat -c '%u:%a' "$PROXY_HOST_FILE" 2>/dev/null)" = "0:444" ] && [ -f "$PROXY_PORT_FILE" ] && [ ! -L "$PROXY_PORT_FILE" ] && [ "$(/usr/bin/stat -c '%u:%a' "$PROXY_PORT_FILE" 2>/dev/null)" = "0:444" ] || { printf '${DCODE_PROXY_UNAVAILABLE}'; exit 1; }`,
`PROXY_HOST=$(/usr/bin/cat "$PROXY_HOST_FILE") || { printf '${DCODE_PROXY_UNAVAILABLE}'; exit 1; }`,
`PROXY_PORT=$(/usr/bin/cat "$PROXY_PORT_FILE") || { printf '${DCODE_PROXY_UNAVAILABLE}'; exit 1; }`,
`case "$PROXY_HOST" in ""|*[!A-Za-z0-9._-]*) printf '${DCODE_PROXY_UNAVAILABLE}'; exit 1 ;; esac`,
`case "$PROXY_PORT" in ""|*[!0-9]*) printf '${DCODE_PROXY_UNAVAILABLE}'; exit 1 ;; esac`,
`[ "$PROXY_PORT" -ge 1 ] 2>/dev/null && [ "$PROXY_PORT" -le 65535 ] 2>/dev/null || { printf '${DCODE_PROXY_UNAVAILABLE}'; exit 1; }`,
'PROXY_URL="http://${PROXY_HOST}:${PROXY_PORT}"',
'export HTTP_PROXY="$PROXY_URL" HTTPS_PROXY="$PROXY_URL" http_proxy="$PROXY_URL" https_proxy="$PROXY_URL"',
'export NO_PROXY="localhost,127.0.0.1,::1,${PROXY_HOST}" no_proxy="localhost,127.0.0.1,::1,${PROXY_HOST}"',
"unset ALL_PROXY all_proxy OPENAI_PROXY",
INFERENCE_ROUTE_PROBE_CORE_SCRIPT,
].join("; ");

/**
Expand All @@ -75,15 +73,12 @@ export function buildSandboxInferenceRouteProbeArgs(
const command =
agent?.name === "langchain-deepagents-code"
? [
// Capture OpenShell's trusted CA before the login shell sources the
// DCode runtime environment. The login shell still reconstructs the
// proxy contract from /tmp/nemoclaw-proxy-env.sh after inherited
// proxy variables are cleared.
// Do not run a login shell: sandbox-writable startup files can write
// to every inherited descriptor. Reconstruct only the required proxy
// route from immutable image files before running the fixed probe.
"sh",
"-c",
DCODE_INFERENCE_ROUTE_PROBE_WRAPPER,
"nemoclaw-ca-capture",
INFERENCE_ROUTE_PROBE_FROM_ARG0_SCRIPT,
DCODE_INFERENCE_ROUTE_PROBE_SCRIPT,
]
: ["sh", "-c", INFERENCE_ROUTE_PROBE_SCRIPT];

Expand All @@ -98,7 +93,9 @@ export function parseSandboxInferenceRouteProbeResult(
// Some OpenShell releases frame child stdout for humans. Normalize only the
// two known frame prefixes at the beginning of the captured output.
const detail = rawDetail.replace(/^(?:\[stdout\]|stdout:)\s*/i, "");
const match = /^(OK|BROKEN)\s+([0-9]{3})\b/.exec(detail);
// A trusted probe emits one result line. Reject preambles or extra lines so
// shell startup output can never be mistaken for the authoritative result.
const match = /^(OK|BROKEN)\s+([0-9]{3})\b[^\r\n]*$/.exec(detail);
const httpStatus = match ? Number.parseInt(match[2], 10) : 0;
const isReachableHttpStatus = httpStatus >= 200 && httpStatus < 500;
const commandSucceeded = result.status === 0;
Expand Down
Loading