From 5bb909063db9e0121b010e34ac2a5a5f8dc31e92 Mon Sep 17 00:00:00 2001 From: Yanyun Liao Date: Thu, 6 Aug 2026 11:58:34 +0800 Subject: [PATCH 01/10] fix(sandbox): reconcile readonly gateway-token trust anchor on source MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The generated /tmp/nemoclaw-proxy-env.sh assigned OPENCLAW_GATEWAY_TOKEN directly. When a sourcing shell had already pinned the variable readonly to a conflicting value, the assignment aborted sourcing with the shell's raw readonly error (exit 2, "OPENCLAW_GATEWAY_TOKEN: is read only") — leaking the failing assignment line and bypassing NemoClaw's controlled diagnostic. Resolve the intended token into a temp variable per URL case, then reconcile it against any pre-existing value: probe writability in a subshell (so a readonly pin can never abort sourcing), advance the anchor when writable, stay silent when it already holds the intended value, and otherwise emit a controlled "Error: conflicting trust anchor" diagnostic (exit 1) that never echoes the trusted token. Scoped to the gateway token, the secret trust anchor named in the report; the non-secret routing vars are left unchanged. Fixes #8428 Signed-off-by: Yanyun Liao --- scripts/nemoclaw-start.sh | 30 ++++- test/proxy-env-gateway-token-conflict.test.ts | 113 ++++++++++++++++++ 2 files changed, 140 insertions(+), 3 deletions(-) create mode 100644 test/proxy-env-gateway-token-conflict.test.ts diff --git a/scripts/nemoclaw-start.sh b/scripts/nemoclaw-start.sh index d8b365fe8c1..20c0252f036 100755 --- a/scripts/nemoclaw-start.sh +++ b/scripts/nemoclaw-start.sh @@ -4068,10 +4068,15 @@ GUARDENVEOF # URL, including loopback, while WhatsApp revalidates its local override # immediately at the specialized exec boundary. printf 'export OPENCLAW_GATEWAY_TOKEN\n' + # Resolve the intended token into a temp var per URL case, then reconcile + # it against any pre-existing value below. A blind assignment would abort + # sourcing with the shell's raw readonly error (exit 2) — and could echo + # the failing assignment line — when the sourcing shell already pinned + # OPENCLAW_GATEWAY_TOKEN readonly to a conflicting value (#8428). cat <<'GATEWAYTOKENENVEOF' case "${OPENCLAW_GATEWAY_URL:-}" in *@*) - OPENCLAW_GATEWAY_TOKEN= + _nemoclaw_intended_gateway_token= ;; '' | ws://127.0.0.1 | ws://127.0.0.1:* | ws://127.0.0.1/* | \ wss://127.0.0.1 | wss://127.0.0.1:* | wss://127.0.0.1/* | \ @@ -4080,12 +4085,31 @@ case "${OPENCLAW_GATEWAY_URL:-}" in "ws://[::1]" | "ws://[::1]:"* | "ws://[::1]/"* | \ "wss://[::1]" | "wss://[::1]:"* | "wss://[::1]/"*) GATEWAYTOKENENVEOF - printf " OPENCLAW_GATEWAY_TOKEN='%s'\n" "$_escaped_gateway_token" + printf " _nemoclaw_intended_gateway_token='%s'\n" "$_escaped_gateway_token" printf ' ;;\n' printf ' *)\n' - printf ' OPENCLAW_GATEWAY_TOKEN=\n' + printf ' _nemoclaw_intended_gateway_token=\n' printf ' ;;\n' printf 'esac\n' + cat <<'GATEWAYTOKENRECONCILEEOF' +# nemoclaw-gateway-token-reconcile start +# The proxy-env file is the trust anchor for OPENCLAW_GATEWAY_TOKEN. Probe +# writability in a subshell so a readonly pin cannot abort sourcing: advance the +# anchor when writable (also the fresh-source and repeated-source paths), stay +# silent when it already holds the intended value, and otherwise emit a +# controlled conflict diagnostic that never echoes the trusted token. +if ( OPENCLAW_GATEWAY_TOKEN="$_nemoclaw_intended_gateway_token" ) 2>/dev/null; then + OPENCLAW_GATEWAY_TOKEN="$_nemoclaw_intended_gateway_token" +elif [ "${OPENCLAW_GATEWAY_TOKEN-}" = "$_nemoclaw_intended_gateway_token" ]; then + : +else + echo "Error: conflicting trust anchor" >&2 + unset _nemoclaw_intended_gateway_token + return 1 2>/dev/null || exit 1 +fi +unset _nemoclaw_intended_gateway_token +# nemoclaw-gateway-token-reconcile end +GATEWAYTOKENRECONCILEEOF fi } | emit_sandbox_sourced_file "$_PROXY_ENV_FILE" } diff --git a/test/proxy-env-gateway-token-conflict.test.ts b/test/proxy-env-gateway-token-conflict.test.ts new file mode 100644 index 00000000000..b67300d7b45 --- /dev/null +++ b/test/proxy-env-gateway-token-conflict.test.ts @@ -0,0 +1,113 @@ +// SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. +// SPDX-License-Identifier: Apache-2.0 +// +// Behavioral contract for the OPENCLAW_GATEWAY_TOKEN trust-anchor reconcile +// block emitted into /tmp/nemoclaw-proxy-env.sh by scripts/nemoclaw-start.sh. +// Exercises the actual emitted shell (extracted from the script) under POSIX +// sh (dash), not a re-implementation. Regression: a blind assignment aborted +// sourcing with the shell's raw readonly error (exit 2) when the sourcing shell +// had already pinned OPENCLAW_GATEWAY_TOKEN readonly to a conflicting value +// (#8428). + +import { spawnSync } from "node:child_process"; +import { mkdtempSync, rmSync, writeFileSync } from "node:fs"; +import { tmpdir } from "node:os"; +import { join } from "node:path"; +import { afterEach, describe, expect, it } from "vitest"; + +import { sliceBlock } from "./helpers/corporate-ca-support"; + +const OPENCLAW_START = join(import.meta.dirname, "../scripts/nemoclaw-start.sh"); +const RECONCILE = sliceBlock( + OPENCLAW_START, + "# nemoclaw-gateway-token-reconcile start", + "# nemoclaw-gateway-token-reconcile end", +); +const REAL_TOKEN = "REAL-GATEWAY-TOKEN-abc123"; + +const tmpRoots: string[] = []; +afterEach(() => { + for (const dir of tmpRoots.splice(0)) { + rmSync(dir, { recursive: true, force: true }); + } +}); + +interface Scenario { + intended: string; + preset?: { value: string; readonly: boolean }; +} + +function runReconcile(scenario: Scenario): { + status: number | null; + stdout: string; + stderr: string; +} { + const dir = mkdtempSync(join(tmpdir(), "nemoclaw-token-reconcile-")); + tmpRoots.push(dir); + const envFile = join(dir, "proxy-env.sh"); + // The reconcile block reads $_nemoclaw_intended_gateway_token (set by the URL + // case above it in the real file) and prints the resolved value on success. + const body = [ + `_nemoclaw_intended_gateway_token='${scenario.intended}'`, + RECONCILE, + "printf 'FINAL_TOKEN=[%s]\\n' \"${OPENCLAW_GATEWAY_TOKEN-}\"", + "", + ].join("\n"); + writeFileSync(envFile, body, { mode: 0o444 }); + const presetPrefix = scenario.preset + ? `${scenario.preset.readonly ? "readonly " : ""}OPENCLAW_GATEWAY_TOKEN='${scenario.preset.value}'; ` + : ""; + // Mirror the reporter's exact invocation shape: `sh -c '; . '`. + const result = spawnSync("sh", ["-c", `${presetPrefix}. '${envFile}'`], { + encoding: "utf-8", + }); + return { status: result.status, stdout: result.stdout, stderr: result.stderr }; +} + +describe("proxy-env OPENCLAW_GATEWAY_TOKEN trust-anchor reconcile (#8428)", () => { + it("emits a controlled conflict diagnostic instead of the raw readonly abort", () => { + const { status, stdout, stderr } = runReconcile({ + intended: REAL_TOKEN, + preset: { value: "SENTINEL_CONFLICT", readonly: true }, + }); + expect(status).toBe(1); + expect(stderr).toContain("Error: conflicting trust anchor"); + expect(stderr).not.toContain("is read only"); + // The trusted token must never be echoed on the conflict path. + expect(`${stdout}\n${stderr}`).not.toContain(REAL_TOKEN); + expect(stdout).not.toContain("FINAL_TOKEN="); + }); + + it("advances the anchor to the intended token on a fresh (unset) source", () => { + const { status, stdout, stderr } = runReconcile({ intended: REAL_TOKEN }); + expect(status).toBe(0); + expect(stdout).toContain(`FINAL_TOKEN=[${REAL_TOKEN}]`); + expect(stderr).not.toContain("conflicting trust anchor"); + }); + + it("is a no-op when the readonly pin already holds the intended value", () => { + const { status, stdout, stderr } = runReconcile({ + intended: REAL_TOKEN, + preset: { value: REAL_TOKEN, readonly: true }, + }); + expect(status).toBe(0); + expect(stdout).toContain(`FINAL_TOKEN=[${REAL_TOKEN}]`); + expect(stderr).not.toContain("conflicting trust anchor"); + expect(stderr).not.toContain("is read only"); + }); + + it("keeps the non-loopback empty-token case exported-empty, not conflicting", () => { + const { status, stdout } = runReconcile({ intended: "" }); + expect(status).toBe(0); + expect(stdout).toContain("FINAL_TOKEN=[]"); + }); + + it("advances a writable pre-existing value (repeated non-readonly source)", () => { + const { status, stdout } = runReconcile({ + intended: REAL_TOKEN, + preset: { value: REAL_TOKEN, readonly: false }, + }); + expect(status).toBe(0); + expect(stdout).toContain(`FINAL_TOKEN=[${REAL_TOKEN}]`); + }); +}); From bda00252a6817a523ddd323575704584001f866b Mon Sep 17 00:00:00 2001 From: Yanyun Liao Date: Thu, 6 Aug 2026 12:19:20 +0800 Subject: [PATCH 02/10] test(sandbox): update proxy-env golden assertions for token temp var The gateway-token trust-anchor reconcile now resolves the token into `_nemoclaw_intended_gateway_token` before assigning OPENCLAW_GATEWAY_TOKEN, so the emitted proxy-env file embeds the token value on that temp variable. Point the existing generator golden-output assertions at the new embedding site; behavioral coverage (sourcing sets OPENCLAW_GATEWAY_TOKEN) is unchanged. Refs #8428 Signed-off-by: Yanyun Liao --- test/nemoclaw-start.test.ts | 24 ++++++++++++++++-------- test/service-env.test.ts | 2 +- 2 files changed, 17 insertions(+), 9 deletions(-) diff --git a/test/nemoclaw-start.test.ts b/test/nemoclaw-start.test.ts index f76e41bf1a5..a1afaa17088 100644 --- a/test/nemoclaw-start.test.ts +++ b/test/nemoclaw-start.test.ts @@ -662,7 +662,7 @@ describe("nemoclaw-start gateway token export (#1114)", () => { expect(result.stderr).toContain("Dashboard auth token redacted from startup logs."); expect(result.stderr).not.toContain("#token="); expect(result.stderr).not.toContain("tok'en"); - expect(envFile).toContain("OPENCLAW_GATEWAY_TOKEN='tok'\\''en'"); + expect(envFile).toContain("_nemoclaw_intended_gateway_token='tok'\\''en'"); expect(envFile).toContain("export OPENCLAW_GATEWAY_TOKEN"); expect(envFile).toContain("nemoclaw-configure-guard begin"); expect(envFile).not.toContain(".bashrc"); @@ -681,7 +681,7 @@ describe("nemoclaw-start gateway token export (#1114)", () => { expect(envFile).toContain("export OPENCLAW_GATEWAY_PORT='18790'"); expect(envFile).toContain("export NEMOCLAW_OPENCLAW_GATEWAY_URL='ws://127.0.0.1:18790'"); expect(envFile).not.toContain("export OPENCLAW_GATEWAY_URL='ws://127.0.0.1:18790'"); - expect(envFile).toContain("OPENCLAW_GATEWAY_TOKEN='token'"); + expect(envFile).toContain("_nemoclaw_intended_gateway_token='token'"); }); it("writes OpenClaw state env for connect-shell pairing approval (#3730)", () => { const { result, envFile } = runGatewayTokenHarness( @@ -694,7 +694,7 @@ describe("nemoclaw-start gateway token export (#1114)", () => { expect(envFile).toContain("export OPENCLAW_CONFIG_PATH='/sandbox/.openclaw/openclaw.json'"); expect(envFile).toContain("export OPENCLAW_OAUTH_DIR='/sandbox/.openclaw/credentials'"); expect(envFile.indexOf("export OPENCLAW_STATE_DIR=")).toBeLessThan( - envFile.indexOf("OPENCLAW_GATEWAY_TOKEN='"), + envFile.indexOf("_nemoclaw_intended_gateway_token='"), ); }); @@ -712,7 +712,9 @@ describe("nemoclaw-start gateway token export (#1114)", () => { expect(envFile).toContain("export OPENCLAW_GATEWAY_PORT='18790'"); expect(envFile).toContain("export NEMOCLAW_OPENCLAW_GATEWAY_URL='ws://127.0.0.1:18790'"); expect(envFile).not.toContain("export OPENCLAW_GATEWAY_URL='ws://127.0.0.1:18790'"); - expect(envFile).toContain(`OPENCLAW_GATEWAY_TOKEN='${configAfter.gateway.auth.token}'`); + expect(envFile).toContain( + `_nemoclaw_intended_gateway_token='${configAfter.gateway.auth.token}'`, + ); expect(envFile).not.toContain("stale-token"); expect(hashAfter).not.toBe("initial-hash\n"); expect(hashAfter).toMatch(/ openclaw\.json\n$/); @@ -730,7 +732,9 @@ describe("nemoclaw-start gateway token export (#1114)", () => { expect(configAfter.gateway.auth.token).toEqual(expect.any(String)); expect(configAfter.gateway.auth.token).not.toBe(""); expect(configAfter.gateway.auth.token).not.toBe(oldToken); - expect(envFile).toContain(`OPENCLAW_GATEWAY_TOKEN='${configAfter.gateway.auth.token}'`); + expect(envFile).toContain( + `_nemoclaw_intended_gateway_token='${configAfter.gateway.auth.token}'`, + ); expect(envFile).not.toContain(oldToken); expect(envFile).not.toContain("stale-token"); expect(hashAfter).not.toBe("initial-hash\n"); @@ -757,7 +761,9 @@ describe("nemoclaw-start gateway token export (#1114)", () => { expect(configAfter.gateway.auth.token).not.toBe(""); expect(configAfter.gateway.auth.token).not.toBe(oldToken); expect(configAfter.model).toBe("nvidia/nemotron-3-super-120b-a12b"); - expect(envFile).toContain(`OPENCLAW_GATEWAY_TOKEN='${configAfter.gateway.auth.token}'`); + expect(envFile).toContain( + `_nemoclaw_intended_gateway_token='${configAfter.gateway.auth.token}'`, + ); expect(envFile).not.toContain(oldToken); expect(envFile).not.toContain("stale-token"); expect(hashAfter).not.toBe("initial-hash\n"); @@ -4775,7 +4781,7 @@ describe("direct-root entrypoint composition under CAP_DAC_OVERRIDE drop", () => expect(fs.existsSync(proxyEnvFile)).toBe(true); const proxyEnv = fs.readFileSync(proxyEnvFile, "utf-8"); - expect(proxyEnv).toMatch(/OPENCLAW_GATEWAY_TOKEN='[A-Za-z0-9_-]{20,}'/); + expect(proxyEnv).toMatch(/_nemoclaw_intended_gateway_token='[A-Za-z0-9_-]{20,}'/); expect(proxyEnv).toContain("export OPENCLAW_GATEWAY_TOKEN"); expect((fs.statSync(bashrcPath).mode & 0o777).toString(8)).toBe("444"); @@ -4783,7 +4789,9 @@ describe("direct-root entrypoint composition under CAP_DAC_OVERRIDE drop", () => const updatedConfig = JSON.parse(fs.readFileSync(configPath, "utf-8")); expect(updatedConfig.gateway?.auth?.token).toMatch(/^[A-Za-z0-9_-]{20,}$/); - expect(proxyEnv).toContain(`OPENCLAW_GATEWAY_TOKEN='${updatedConfig.gateway.auth.token}'`); + expect(proxyEnv).toContain( + `_nemoclaw_intended_gateway_token='${updatedConfig.gateway.auth.token}'`, + ); } finally { fs.rmSync(tmpDir, { recursive: true, force: true }); } diff --git a/test/service-env.test.ts b/test/service-env.test.ts index 2e105547d7b..ee15d4bd1f2 100644 --- a/test/service-env.test.ts +++ b/test/service-env.test.ts @@ -662,7 +662,7 @@ describe("service environment", () => { expect(envFile).toContain("10.200.0.1"); expect(envFile).toContain('export AWS_EC2_METADATA_DISABLED="true"'); expect(envFile).toContain("export OPENCLAW_GATEWAY_TOKEN"); - expect(envFile).toContain("OPENCLAW_GATEWAY_TOKEN='test-token-123'"); + expect(envFile).toContain("_nemoclaw_intended_gateway_token='test-token-123'"); expect(envFile).toContain("nemoclaw-configure-guard begin"); expect(envFile).toContain('/usr/bin/env openclaw "$@"'); // Tool cache redirects should be present (#804) From 46d089ed6af84b70e5ee1d9d5d22ae9bcf9fa2a4 Mon Sep 17 00:00:00 2001 From: Yanyun Liao Date: Thu, 6 Aug 2026 12:40:43 +0800 Subject: [PATCH 03/10] refactor(sandbox): shorten gateway-token temp var name Rename the reconcile temp variable to `_nemoclaw_gateway_token`. The longer prior name pushed the golden-output assertions in test/nemoclaw-start.test.ts past the 100-column width, so Biome wrapped them and grew the file past its legacy test-file-size budget. The shorter name keeps those assertions on one line and the file at its budget. No behavior change. Refs #8428 Signed-off-by: Yanyun Liao --- scripts/nemoclaw-start.sh | 16 ++++++------- test/nemoclaw-start.test.ts | 24 +++++++------------ test/proxy-env-gateway-token-conflict.test.ts | 4 ++-- test/service-env.test.ts | 2 +- 4 files changed, 19 insertions(+), 27 deletions(-) diff --git a/scripts/nemoclaw-start.sh b/scripts/nemoclaw-start.sh index 20c0252f036..cbf2adc442b 100755 --- a/scripts/nemoclaw-start.sh +++ b/scripts/nemoclaw-start.sh @@ -4076,7 +4076,7 @@ GUARDENVEOF cat <<'GATEWAYTOKENENVEOF' case "${OPENCLAW_GATEWAY_URL:-}" in *@*) - _nemoclaw_intended_gateway_token= + _nemoclaw_gateway_token= ;; '' | ws://127.0.0.1 | ws://127.0.0.1:* | ws://127.0.0.1/* | \ wss://127.0.0.1 | wss://127.0.0.1:* | wss://127.0.0.1/* | \ @@ -4085,10 +4085,10 @@ case "${OPENCLAW_GATEWAY_URL:-}" in "ws://[::1]" | "ws://[::1]:"* | "ws://[::1]/"* | \ "wss://[::1]" | "wss://[::1]:"* | "wss://[::1]/"*) GATEWAYTOKENENVEOF - printf " _nemoclaw_intended_gateway_token='%s'\n" "$_escaped_gateway_token" + printf " _nemoclaw_gateway_token='%s'\n" "$_escaped_gateway_token" printf ' ;;\n' printf ' *)\n' - printf ' _nemoclaw_intended_gateway_token=\n' + printf ' _nemoclaw_gateway_token=\n' printf ' ;;\n' printf 'esac\n' cat <<'GATEWAYTOKENRECONCILEEOF' @@ -4098,16 +4098,16 @@ GATEWAYTOKENENVEOF # anchor when writable (also the fresh-source and repeated-source paths), stay # silent when it already holds the intended value, and otherwise emit a # controlled conflict diagnostic that never echoes the trusted token. -if ( OPENCLAW_GATEWAY_TOKEN="$_nemoclaw_intended_gateway_token" ) 2>/dev/null; then - OPENCLAW_GATEWAY_TOKEN="$_nemoclaw_intended_gateway_token" -elif [ "${OPENCLAW_GATEWAY_TOKEN-}" = "$_nemoclaw_intended_gateway_token" ]; then +if ( OPENCLAW_GATEWAY_TOKEN="$_nemoclaw_gateway_token" ) 2>/dev/null; then + OPENCLAW_GATEWAY_TOKEN="$_nemoclaw_gateway_token" +elif [ "${OPENCLAW_GATEWAY_TOKEN-}" = "$_nemoclaw_gateway_token" ]; then : else echo "Error: conflicting trust anchor" >&2 - unset _nemoclaw_intended_gateway_token + unset _nemoclaw_gateway_token return 1 2>/dev/null || exit 1 fi -unset _nemoclaw_intended_gateway_token +unset _nemoclaw_gateway_token # nemoclaw-gateway-token-reconcile end GATEWAYTOKENRECONCILEEOF fi diff --git a/test/nemoclaw-start.test.ts b/test/nemoclaw-start.test.ts index a1afaa17088..a7322685349 100644 --- a/test/nemoclaw-start.test.ts +++ b/test/nemoclaw-start.test.ts @@ -662,7 +662,7 @@ describe("nemoclaw-start gateway token export (#1114)", () => { expect(result.stderr).toContain("Dashboard auth token redacted from startup logs."); expect(result.stderr).not.toContain("#token="); expect(result.stderr).not.toContain("tok'en"); - expect(envFile).toContain("_nemoclaw_intended_gateway_token='tok'\\''en'"); + expect(envFile).toContain("_nemoclaw_gateway_token='tok'\\''en'"); expect(envFile).toContain("export OPENCLAW_GATEWAY_TOKEN"); expect(envFile).toContain("nemoclaw-configure-guard begin"); expect(envFile).not.toContain(".bashrc"); @@ -681,7 +681,7 @@ describe("nemoclaw-start gateway token export (#1114)", () => { expect(envFile).toContain("export OPENCLAW_GATEWAY_PORT='18790'"); expect(envFile).toContain("export NEMOCLAW_OPENCLAW_GATEWAY_URL='ws://127.0.0.1:18790'"); expect(envFile).not.toContain("export OPENCLAW_GATEWAY_URL='ws://127.0.0.1:18790'"); - expect(envFile).toContain("_nemoclaw_intended_gateway_token='token'"); + expect(envFile).toContain("_nemoclaw_gateway_token='token'"); }); it("writes OpenClaw state env for connect-shell pairing approval (#3730)", () => { const { result, envFile } = runGatewayTokenHarness( @@ -694,7 +694,7 @@ describe("nemoclaw-start gateway token export (#1114)", () => { expect(envFile).toContain("export OPENCLAW_CONFIG_PATH='/sandbox/.openclaw/openclaw.json'"); expect(envFile).toContain("export OPENCLAW_OAUTH_DIR='/sandbox/.openclaw/credentials'"); expect(envFile.indexOf("export OPENCLAW_STATE_DIR=")).toBeLessThan( - envFile.indexOf("_nemoclaw_intended_gateway_token='"), + envFile.indexOf("_nemoclaw_gateway_token='"), ); }); @@ -712,9 +712,7 @@ describe("nemoclaw-start gateway token export (#1114)", () => { expect(envFile).toContain("export OPENCLAW_GATEWAY_PORT='18790'"); expect(envFile).toContain("export NEMOCLAW_OPENCLAW_GATEWAY_URL='ws://127.0.0.1:18790'"); expect(envFile).not.toContain("export OPENCLAW_GATEWAY_URL='ws://127.0.0.1:18790'"); - expect(envFile).toContain( - `_nemoclaw_intended_gateway_token='${configAfter.gateway.auth.token}'`, - ); + expect(envFile).toContain(`_nemoclaw_gateway_token='${configAfter.gateway.auth.token}'`); expect(envFile).not.toContain("stale-token"); expect(hashAfter).not.toBe("initial-hash\n"); expect(hashAfter).toMatch(/ openclaw\.json\n$/); @@ -732,9 +730,7 @@ describe("nemoclaw-start gateway token export (#1114)", () => { expect(configAfter.gateway.auth.token).toEqual(expect.any(String)); expect(configAfter.gateway.auth.token).not.toBe(""); expect(configAfter.gateway.auth.token).not.toBe(oldToken); - expect(envFile).toContain( - `_nemoclaw_intended_gateway_token='${configAfter.gateway.auth.token}'`, - ); + expect(envFile).toContain(`_nemoclaw_gateway_token='${configAfter.gateway.auth.token}'`); expect(envFile).not.toContain(oldToken); expect(envFile).not.toContain("stale-token"); expect(hashAfter).not.toBe("initial-hash\n"); @@ -761,9 +757,7 @@ describe("nemoclaw-start gateway token export (#1114)", () => { expect(configAfter.gateway.auth.token).not.toBe(""); expect(configAfter.gateway.auth.token).not.toBe(oldToken); expect(configAfter.model).toBe("nvidia/nemotron-3-super-120b-a12b"); - expect(envFile).toContain( - `_nemoclaw_intended_gateway_token='${configAfter.gateway.auth.token}'`, - ); + expect(envFile).toContain(`_nemoclaw_gateway_token='${configAfter.gateway.auth.token}'`); expect(envFile).not.toContain(oldToken); expect(envFile).not.toContain("stale-token"); expect(hashAfter).not.toBe("initial-hash\n"); @@ -4781,7 +4775,7 @@ describe("direct-root entrypoint composition under CAP_DAC_OVERRIDE drop", () => expect(fs.existsSync(proxyEnvFile)).toBe(true); const proxyEnv = fs.readFileSync(proxyEnvFile, "utf-8"); - expect(proxyEnv).toMatch(/_nemoclaw_intended_gateway_token='[A-Za-z0-9_-]{20,}'/); + expect(proxyEnv).toMatch(/_nemoclaw_gateway_token='[A-Za-z0-9_-]{20,}'/); expect(proxyEnv).toContain("export OPENCLAW_GATEWAY_TOKEN"); expect((fs.statSync(bashrcPath).mode & 0o777).toString(8)).toBe("444"); @@ -4789,9 +4783,7 @@ describe("direct-root entrypoint composition under CAP_DAC_OVERRIDE drop", () => const updatedConfig = JSON.parse(fs.readFileSync(configPath, "utf-8")); expect(updatedConfig.gateway?.auth?.token).toMatch(/^[A-Za-z0-9_-]{20,}$/); - expect(proxyEnv).toContain( - `_nemoclaw_intended_gateway_token='${updatedConfig.gateway.auth.token}'`, - ); + expect(proxyEnv).toContain(`_nemoclaw_gateway_token='${updatedConfig.gateway.auth.token}'`); } finally { fs.rmSync(tmpDir, { recursive: true, force: true }); } diff --git a/test/proxy-env-gateway-token-conflict.test.ts b/test/proxy-env-gateway-token-conflict.test.ts index b67300d7b45..0170da48a49 100644 --- a/test/proxy-env-gateway-token-conflict.test.ts +++ b/test/proxy-env-gateway-token-conflict.test.ts @@ -45,10 +45,10 @@ function runReconcile(scenario: Scenario): { const dir = mkdtempSync(join(tmpdir(), "nemoclaw-token-reconcile-")); tmpRoots.push(dir); const envFile = join(dir, "proxy-env.sh"); - // The reconcile block reads $_nemoclaw_intended_gateway_token (set by the URL + // The reconcile block reads $_nemoclaw_gateway_token (set by the URL // case above it in the real file) and prints the resolved value on success. const body = [ - `_nemoclaw_intended_gateway_token='${scenario.intended}'`, + `_nemoclaw_gateway_token='${scenario.intended}'`, RECONCILE, "printf 'FINAL_TOKEN=[%s]\\n' \"${OPENCLAW_GATEWAY_TOKEN-}\"", "", diff --git a/test/service-env.test.ts b/test/service-env.test.ts index ee15d4bd1f2..cc5ae445d8d 100644 --- a/test/service-env.test.ts +++ b/test/service-env.test.ts @@ -662,7 +662,7 @@ describe("service environment", () => { expect(envFile).toContain("10.200.0.1"); expect(envFile).toContain('export AWS_EC2_METADATA_DISABLED="true"'); expect(envFile).toContain("export OPENCLAW_GATEWAY_TOKEN"); - expect(envFile).toContain("_nemoclaw_intended_gateway_token='test-token-123'"); + expect(envFile).toContain("_nemoclaw_gateway_token='test-token-123'"); expect(envFile).toContain("nemoclaw-configure-guard begin"); expect(envFile).toContain('/usr/bin/env openclaw "$@"'); // Tool cache redirects should be present (#804) From e91c296c4fbf2a1324c81aaf61a1f8568e47df75 Mon Sep 17 00:00:00 2001 From: Apurv Kumaria Date: Thu, 6 Aug 2026 03:55:12 -0700 Subject: [PATCH 04/10] fix(sandbox): harden gateway token reconciliation Signed-off-by: Apurv Kumaria --- ci/test-file-size-budget.json | 2 +- scripts/nemoclaw-start.sh | 64 +++++---- test/nemoclaw-start.test.ts | 19 ++- test/proxy-env-gateway-token-conflict.test.ts | 133 ++++++++++++------ test/service-env.test.ts | 12 +- 5 files changed, 145 insertions(+), 85 deletions(-) diff --git a/ci/test-file-size-budget.json b/ci/test-file-size-budget.json index 61d90b8e3c9..33767efde0f 100644 --- a/ci/test-file-size-budget.json +++ b/ci/test-file-size-budget.json @@ -7,7 +7,7 @@ "src/lib/onboard/preflight.test.ts": 1904, "test/generate-openclaw-config.test.ts": 1941, "test/install-preflight.test.ts": 3921, - "test/nemoclaw-start.test.ts": 4791, + "test/nemoclaw-start.test.ts": 4790, "test/onboard-messaging.test.ts": 2043, "test/onboard-selection.test.ts": 4767 } diff --git a/scripts/nemoclaw-start.sh b/scripts/nemoclaw-start.sh index cbf2adc442b..53771946d7c 100755 --- a/scripts/nemoclaw-start.sh +++ b/scripts/nemoclaw-start.sh @@ -3383,6 +3383,30 @@ _RUNTIME_SHELL_ENV_SHIM="[ -f ${_RUNTIME_SHELL_ENV_FILE} ] && . ${_RUNTIME_SHELL write_runtime_shell_env() { _PROXY_ENV_FILE="/tmp/nemoclaw-proxy-env.sh" + _emit_gateway_token_reconcile() { + local _escaped_intended_gateway_token="$1" + cat <<'GATEWAYTOKENRECONCILESTART' +# nemoclaw-gateway-token-reconcile start +# The proxy-env file is the trust anchor for OPENCLAW_GATEWAY_TOKEN. Probe +# writability in a subshell so a readonly pin cannot abort sourcing: advance the +# anchor when writable (also the fresh-source and repeated-source paths), stay +# silent when it already holds the intended value, and otherwise emit a +# controlled conflict diagnostic that never echoes the trusted token. +GATEWAYTOKENRECONCILESTART + printf "if ( OPENCLAW_GATEWAY_TOKEN='%s' ) 2>/dev/null; then\n" \ + "$_escaped_intended_gateway_token" + printf " OPENCLAW_GATEWAY_TOKEN='%s'\n" "$_escaped_intended_gateway_token" + printf "elif [ \"\${OPENCLAW_GATEWAY_TOKEN-}\" = '%s' ]; then\n" \ + "$_escaped_intended_gateway_token" + cat <<'GATEWAYTOKENRECONCILEEND' + : +else + echo "Error: conflicting trust anchor" >&2 + return 1 2>/dev/null || exit 1 +fi +# nemoclaw-gateway-token-reconcile end +GATEWAYTOKENRECONCILEEND + } { cat </dev/null; then - OPENCLAW_GATEWAY_TOKEN="$_nemoclaw_gateway_token" -elif [ "${OPENCLAW_GATEWAY_TOKEN-}" = "$_nemoclaw_gateway_token" ]; then - : -else - echo "Error: conflicting trust anchor" >&2 - unset _nemoclaw_gateway_token - return 1 2>/dev/null || exit 1 -fi -unset _nemoclaw_gateway_token -# nemoclaw-gateway-token-reconcile end -GATEWAYTOKENRECONCILEEOF fi } | emit_sandbox_sourced_file "$_PROXY_ENV_FILE" } diff --git a/test/nemoclaw-start.test.ts b/test/nemoclaw-start.test.ts index a7322685349..da1629399b3 100644 --- a/test/nemoclaw-start.test.ts +++ b/test/nemoclaw-start.test.ts @@ -8,9 +8,8 @@ import os from "node:os"; import path from "node:path"; import * as ts from "typescript"; import { describe, expect, it } from "vitest"; -import { openClawBootstrapSnippet } from "./support/entrypoint-script-fixture"; - import { extractShellFunctionFromSource } from "./helpers/shell-source"; +import { openClawBootstrapSnippet } from "./support/entrypoint-script-fixture"; const START_SCRIPT = path.join(import.meta.dirname, "..", "scripts", "nemoclaw-start.sh"); const APPROVAL_POLICY_DIR = path.join(import.meta.dirname, "..", "scripts", "lib"); @@ -662,7 +661,7 @@ describe("nemoclaw-start gateway token export (#1114)", () => { expect(result.stderr).toContain("Dashboard auth token redacted from startup logs."); expect(result.stderr).not.toContain("#token="); expect(result.stderr).not.toContain("tok'en"); - expect(envFile).toContain("_nemoclaw_gateway_token='tok'\\''en'"); + expect(envFile).toContain("OPENCLAW_GATEWAY_TOKEN='tok'\\''en'"); expect(envFile).toContain("export OPENCLAW_GATEWAY_TOKEN"); expect(envFile).toContain("nemoclaw-configure-guard begin"); expect(envFile).not.toContain(".bashrc"); @@ -681,7 +680,7 @@ describe("nemoclaw-start gateway token export (#1114)", () => { expect(envFile).toContain("export OPENCLAW_GATEWAY_PORT='18790'"); expect(envFile).toContain("export NEMOCLAW_OPENCLAW_GATEWAY_URL='ws://127.0.0.1:18790'"); expect(envFile).not.toContain("export OPENCLAW_GATEWAY_URL='ws://127.0.0.1:18790'"); - expect(envFile).toContain("_nemoclaw_gateway_token='token'"); + expect(envFile).toContain("OPENCLAW_GATEWAY_TOKEN='token'"); }); it("writes OpenClaw state env for connect-shell pairing approval (#3730)", () => { const { result, envFile } = runGatewayTokenHarness( @@ -694,7 +693,7 @@ describe("nemoclaw-start gateway token export (#1114)", () => { expect(envFile).toContain("export OPENCLAW_CONFIG_PATH='/sandbox/.openclaw/openclaw.json'"); expect(envFile).toContain("export OPENCLAW_OAUTH_DIR='/sandbox/.openclaw/credentials'"); expect(envFile.indexOf("export OPENCLAW_STATE_DIR=")).toBeLessThan( - envFile.indexOf("_nemoclaw_gateway_token='"), + envFile.indexOf("OPENCLAW_GATEWAY_TOKEN='"), ); }); @@ -712,7 +711,7 @@ describe("nemoclaw-start gateway token export (#1114)", () => { expect(envFile).toContain("export OPENCLAW_GATEWAY_PORT='18790'"); expect(envFile).toContain("export NEMOCLAW_OPENCLAW_GATEWAY_URL='ws://127.0.0.1:18790'"); expect(envFile).not.toContain("export OPENCLAW_GATEWAY_URL='ws://127.0.0.1:18790'"); - expect(envFile).toContain(`_nemoclaw_gateway_token='${configAfter.gateway.auth.token}'`); + expect(envFile).toContain(`OPENCLAW_GATEWAY_TOKEN='${configAfter.gateway.auth.token}'`); expect(envFile).not.toContain("stale-token"); expect(hashAfter).not.toBe("initial-hash\n"); expect(hashAfter).toMatch(/ openclaw\.json\n$/); @@ -730,7 +729,7 @@ describe("nemoclaw-start gateway token export (#1114)", () => { expect(configAfter.gateway.auth.token).toEqual(expect.any(String)); expect(configAfter.gateway.auth.token).not.toBe(""); expect(configAfter.gateway.auth.token).not.toBe(oldToken); - expect(envFile).toContain(`_nemoclaw_gateway_token='${configAfter.gateway.auth.token}'`); + expect(envFile).toContain(`OPENCLAW_GATEWAY_TOKEN='${configAfter.gateway.auth.token}'`); expect(envFile).not.toContain(oldToken); expect(envFile).not.toContain("stale-token"); expect(hashAfter).not.toBe("initial-hash\n"); @@ -757,7 +756,7 @@ describe("nemoclaw-start gateway token export (#1114)", () => { expect(configAfter.gateway.auth.token).not.toBe(""); expect(configAfter.gateway.auth.token).not.toBe(oldToken); expect(configAfter.model).toBe("nvidia/nemotron-3-super-120b-a12b"); - expect(envFile).toContain(`_nemoclaw_gateway_token='${configAfter.gateway.auth.token}'`); + expect(envFile).toContain(`OPENCLAW_GATEWAY_TOKEN='${configAfter.gateway.auth.token}'`); expect(envFile).not.toContain(oldToken); expect(envFile).not.toContain("stale-token"); expect(hashAfter).not.toBe("initial-hash\n"); @@ -4775,7 +4774,7 @@ describe("direct-root entrypoint composition under CAP_DAC_OVERRIDE drop", () => expect(fs.existsSync(proxyEnvFile)).toBe(true); const proxyEnv = fs.readFileSync(proxyEnvFile, "utf-8"); - expect(proxyEnv).toMatch(/_nemoclaw_gateway_token='[A-Za-z0-9_-]{20,}'/); + expect(proxyEnv).toMatch(/OPENCLAW_GATEWAY_TOKEN='[A-Za-z0-9_-]{20,}'/); expect(proxyEnv).toContain("export OPENCLAW_GATEWAY_TOKEN"); expect((fs.statSync(bashrcPath).mode & 0o777).toString(8)).toBe("444"); @@ -4783,7 +4782,7 @@ describe("direct-root entrypoint composition under CAP_DAC_OVERRIDE drop", () => const updatedConfig = JSON.parse(fs.readFileSync(configPath, "utf-8")); expect(updatedConfig.gateway?.auth?.token).toMatch(/^[A-Za-z0-9_-]{20,}$/); - expect(proxyEnv).toContain(`_nemoclaw_gateway_token='${updatedConfig.gateway.auth.token}'`); + expect(proxyEnv).toContain(`OPENCLAW_GATEWAY_TOKEN='${updatedConfig.gateway.auth.token}'`); } finally { fs.rmSync(tmpDir, { recursive: true, force: true }); } diff --git a/test/proxy-env-gateway-token-conflict.test.ts b/test/proxy-env-gateway-token-conflict.test.ts index 0170da48a49..a22ae499c11 100644 --- a/test/proxy-env-gateway-token-conflict.test.ts +++ b/test/proxy-env-gateway-token-conflict.test.ts @@ -3,27 +3,26 @@ // // Behavioral contract for the OPENCLAW_GATEWAY_TOKEN trust-anchor reconcile // block emitted into /tmp/nemoclaw-proxy-env.sh by scripts/nemoclaw-start.sh. -// Exercises the actual emitted shell (extracted from the script) under POSIX -// sh (dash), not a re-implementation. Regression: a blind assignment aborted -// sourcing with the shell's raw readonly error (exit 2) when the sourcing shell -// had already pinned OPENCLAW_GATEWAY_TOKEN readonly to a conflicting value -// (#8428). +// Exercises the actual generated file under POSIX sh and Bash. Regression: a +// blind assignment aborted sourcing with the shell's raw readonly error when +// the sourcing shell had already pinned OPENCLAW_GATEWAY_TOKEN readonly to a +// conflicting value (#8428). import { spawnSync } from "node:child_process"; -import { mkdtempSync, rmSync, writeFileSync } from "node:fs"; +import { mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs"; import { tmpdir } from "node:os"; import { join } from "node:path"; import { afterEach, describe, expect, it } from "vitest"; -import { sliceBlock } from "./helpers/corporate-ca-support"; +import { extractShellFunctionFromSource } from "./helpers/shell-source"; const OPENCLAW_START = join(import.meta.dirname, "../scripts/nemoclaw-start.sh"); -const RECONCILE = sliceBlock( - OPENCLAW_START, - "# nemoclaw-gateway-token-reconcile start", - "# nemoclaw-gateway-token-reconcile end", +const WRITE_RUNTIME_SHELL_ENV = extractShellFunctionFromSource( + readFileSync(OPENCLAW_START, "utf-8"), + "write_runtime_shell_env", ); const REAL_TOKEN = "REAL-GATEWAY-TOKEN-abc123"; +const SHELLS = ["sh", "bash"] as const; const tmpRoots: string[] = []; afterEach(() => { @@ -34,7 +33,15 @@ afterEach(() => { interface Scenario { intended: string; + shell?: (typeof SHELLS)[number]; preset?: { value: string; readonly: boolean }; + repeatSources?: boolean; + readonlyPrivateSentinel?: boolean; + sourceUrl?: string; +} + +function shellQuote(value: string): string { + return `'${value.replaceAll("'", "'\\''")}'`; } function runReconcile(scenario: Scenario): { @@ -45,69 +52,105 @@ function runReconcile(scenario: Scenario): { const dir = mkdtempSync(join(tmpdir(), "nemoclaw-token-reconcile-")); tmpRoots.push(dir); const envFile = join(dir, "proxy-env.sh"); - // The reconcile block reads $_nemoclaw_gateway_token (set by the URL - // case above it in the real file) and prints the resolved value on success. - const body = [ - `_nemoclaw_gateway_token='${scenario.intended}'`, - RECONCILE, - "printf 'FINAL_TOKEN=[%s]\\n' \"${OPENCLAW_GATEWAY_TOKEN-}\"", - "", - ].join("\n"); - writeFileSync(envFile, body, { mode: 0o444 }); - const presetPrefix = scenario.preset - ? `${scenario.preset.readonly ? "readonly " : ""}OPENCLAW_GATEWAY_TOKEN='${scenario.preset.value}'; ` - : ""; - // Mirror the reporter's exact invocation shape: `sh -c '; . '`. - const result = spawnSync("sh", ["-c", `${presetPrefix}. '${envFile}'`], { + const generator = join(dir, "generate.sh"); + writeFileSync( + generator, + [ + "#!/usr/bin/env bash", + "set -e", + 'emit_sandbox_sourced_file() { cat > "$1"; }', + WRITE_RUNTIME_SHELL_ENV.replaceAll("/tmp/nemoclaw-proxy-env.sh", envFile), + '_PROXY_URL="http://10.200.0.1:3128"', + '_NO_PROXY_VAL="localhost,127.0.0.1,::1,10.200.0.1"', + '_SANDBOX_SAFETY_NET="/tmp/safety-net.js"', + '_PROXY_FIX_SCRIPT="/tmp/http-proxy-fix.js"', + '_NEMOTRON_FIX_SCRIPT="/tmp/nemotron-fix.js"', + '_CIAO_GUARD_SCRIPT="/tmp/ciao-guard.js"', + "_TOOL_REDIRECTS=()", + `OPENCLAW_GATEWAY_TOKEN=${shellQuote(scenario.intended)}`, + "write_runtime_shell_env", + "", + ].join("\n"), + { mode: 0o700 }, + ); + const generated = spawnSync("bash", [generator], { encoding: "utf-8" }); + if (generated.status !== 0) { + return generated; + } + + const setup = [ + scenario.preset + ? `${scenario.preset.readonly ? "readonly " : ""}OPENCLAW_GATEWAY_TOKEN=${shellQuote(scenario.preset.value)}` + : "", + scenario.readonlyPrivateSentinel ? "readonly _nemoclaw_gateway_token='CALLER-SENTINEL'" : "", + scenario.sourceUrl ? `OPENCLAW_GATEWAY_URL=${shellQuote(scenario.sourceUrl)}` : "", + ].filter(Boolean); + const sourceAndPrint = [ + `. ${shellQuote(envFile)} || exit $?`, + `printf 'TOKEN=[%s] PRIVATE=[%s]\\n' "\${OPENCLAW_GATEWAY_TOKEN-}" "\${_nemoclaw_gateway_token-}"`, + ]; + const commands = scenario.repeatSources + ? [...setup, ...sourceAndPrint, ...sourceAndPrint] + : [...setup, ...sourceAndPrint]; + return spawnSync(scenario.shell ?? "sh", ["-c", commands.join("; ")], { encoding: "utf-8", }); - return { status: result.status, stdout: result.stdout, stderr: result.stderr }; } describe("proxy-env OPENCLAW_GATEWAY_TOKEN trust-anchor reconcile (#8428)", () => { - it("emits a controlled conflict diagnostic instead of the raw readonly abort", () => { + it.each(SHELLS)("emits a controlled conflict diagnostic under %s", (shell) => { const { status, stdout, stderr } = runReconcile({ intended: REAL_TOKEN, + shell, preset: { value: "SENTINEL_CONFLICT", readonly: true }, }); expect(status).toBe(1); expect(stderr).toContain("Error: conflicting trust anchor"); - expect(stderr).not.toContain("is read only"); - // The trusted token must never be echoed on the conflict path. + expect(stderr).not.toContain("read only"); expect(`${stdout}\n${stderr}`).not.toContain(REAL_TOKEN); - expect(stdout).not.toContain("FINAL_TOKEN="); + expect(stdout).not.toContain("TOKEN="); }); - it("advances the anchor to the intended token on a fresh (unset) source", () => { - const { status, stdout, stderr } = runReconcile({ intended: REAL_TOKEN }); + it.each(SHELLS)("accepts an identical readonly trust anchor under %s", (shell) => { + const { status, stdout, stderr } = runReconcile({ + intended: REAL_TOKEN, + shell, + preset: { value: REAL_TOKEN, readonly: true }, + }); expect(status).toBe(0); - expect(stdout).toContain(`FINAL_TOKEN=[${REAL_TOKEN}]`); + expect(stdout).toContain(`TOKEN=[${REAL_TOKEN}]`); expect(stderr).not.toContain("conflicting trust anchor"); + expect(stderr).not.toContain("read only"); }); - it("is a no-op when the readonly pin already holds the intended value", () => { + it("advances a writable value and retains it across repeated sourcing", () => { const { status, stdout, stderr } = runReconcile({ intended: REAL_TOKEN, - preset: { value: REAL_TOKEN, readonly: true }, + preset: { value: "WRITABLE-SENTINEL", readonly: false }, + repeatSources: true, }); expect(status).toBe(0); - expect(stdout).toContain(`FINAL_TOKEN=[${REAL_TOKEN}]`); - expect(stderr).not.toContain("conflicting trust anchor"); - expect(stderr).not.toContain("is read only"); + expect(stdout.match(new RegExp(`TOKEN=\\[${REAL_TOKEN}\\]`, "g"))).toHaveLength(2); + expect(stderr).toBe(""); }); - it("keeps the non-loopback empty-token case exported-empty, not conflicting", () => { - const { status, stdout } = runReconcile({ intended: "" }); + it("does not depend on a caller-controlled readonly temporary variable", () => { + const { status, stdout, stderr } = runReconcile({ + intended: REAL_TOKEN, + readonlyPrivateSentinel: true, + }); expect(status).toBe(0); - expect(stdout).toContain("FINAL_TOKEN=[]"); + expect(stdout).toContain(`TOKEN=[${REAL_TOKEN}] PRIVATE=[CALLER-SENTINEL]`); + expect(stderr).toBe(""); }); - it("advances a writable pre-existing value (repeated non-readonly source)", () => { - const { status, stdout } = runReconcile({ + it("keeps the non-loopback case exported empty", () => { + const { status, stdout, stderr } = runReconcile({ intended: REAL_TOKEN, - preset: { value: REAL_TOKEN, readonly: false }, + sourceUrl: "wss://remote.example.test", }); expect(status).toBe(0); - expect(stdout).toContain(`FINAL_TOKEN=[${REAL_TOKEN}]`); + expect(stdout).toContain("TOKEN=[]"); + expect(stderr).toBe(""); }); }); diff --git a/test/service-env.test.ts b/test/service-env.test.ts index cc5ae445d8d..de4f1f0a178 100644 --- a/test/service-env.test.ts +++ b/test/service-env.test.ts @@ -662,7 +662,17 @@ describe("service environment", () => { expect(envFile).toContain("10.200.0.1"); expect(envFile).toContain('export AWS_EC2_METADATA_DISABLED="true"'); expect(envFile).toContain("export OPENCLAW_GATEWAY_TOKEN"); - expect(envFile).toContain("_nemoclaw_gateway_token='test-token-123'"); + for (const shell of ["sh", "bash"]) { + const sourced = execFileSync( + shell, + [ + "-c", + `unset OPENCLAW_GATEWAY_TOKEN OPENCLAW_GATEWAY_URL _nemoclaw_gateway_token; . '${join(fakeDataDir, "proxy-env.sh")}'; printf 'TOKEN=[%s] TEMP=[%s]\\n' "\${OPENCLAW_GATEWAY_TOKEN-}" "\${_nemoclaw_gateway_token-}"`, + ], + { encoding: "utf-8" }, + ); + expect(sourced).toContain("TOKEN=[test-token-123] TEMP=[]"); + } expect(envFile).toContain("nemoclaw-configure-guard begin"); expect(envFile).toContain('/usr/bin/env openclaw "$@"'); // Tool cache redirects should be present (#804) From 40ff0ef61ce4722c4335f24357c39d4cf1fefb9c Mon Sep 17 00:00:00 2001 From: Apurv Kumaria Date: Thu, 6 Aug 2026 04:04:41 -0700 Subject: [PATCH 05/10] test(sandbox): keep gateway token contract linear Signed-off-by: Apurv Kumaria --- test/proxy-env-gateway-token-conflict.test.ts | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/test/proxy-env-gateway-token-conflict.test.ts b/test/proxy-env-gateway-token-conflict.test.ts index a22ae499c11..6b1d34bd81b 100644 --- a/test/proxy-env-gateway-token-conflict.test.ts +++ b/test/proxy-env-gateway-token-conflict.test.ts @@ -73,10 +73,7 @@ function runReconcile(scenario: Scenario): { ].join("\n"), { mode: 0o700 }, ); - const generated = spawnSync("bash", [generator], { encoding: "utf-8" }); - if (generated.status !== 0) { - return generated; - } + spawnSync("bash", [generator], { encoding: "utf-8" }); const setup = [ scenario.preset From 6d1e19a46d9bf021268edbaf706fc15dd5e12f81 Mon Sep 17 00:00:00 2001 From: Apurv Kumaria Date: Thu, 6 Aug 2026 04:13:40 -0700 Subject: [PATCH 06/10] test(sandbox): cover repeated token sourcing in bash Signed-off-by: Apurv Kumaria --- test/proxy-env-gateway-token-conflict.test.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/test/proxy-env-gateway-token-conflict.test.ts b/test/proxy-env-gateway-token-conflict.test.ts index 6b1d34bd81b..2a867e5d598 100644 --- a/test/proxy-env-gateway-token-conflict.test.ts +++ b/test/proxy-env-gateway-token-conflict.test.ts @@ -120,9 +120,10 @@ describe("proxy-env OPENCLAW_GATEWAY_TOKEN trust-anchor reconcile (#8428)", () = expect(stderr).not.toContain("read only"); }); - it("advances a writable value and retains it across repeated sourcing", () => { + it.each(SHELLS)("advances a writable value across repeated sourcing under %s", (shell) => { const { status, stdout, stderr } = runReconcile({ intended: REAL_TOKEN, + shell, preset: { value: "WRITABLE-SENTINEL", readonly: false }, repeatSources: true, }); From 10e653667f2e7d6cbb9d82d8f45e027a50660710 Mon Sep 17 00:00:00 2001 From: Apurv Kumaria Date: Thu, 6 Aug 2026 08:48:33 -0700 Subject: [PATCH 07/10] fix(sandbox): reject shadowed token comparisons Signed-off-by: Apurv Kumaria --- scripts/nemoclaw-start.sh | 13 +++++++------ test/proxy-env-gateway-token-conflict.test.ts | 16 ++++++++++++++++ 2 files changed, 23 insertions(+), 6 deletions(-) diff --git a/scripts/nemoclaw-start.sh b/scripts/nemoclaw-start.sh index 33cb03ecbec..32e8b5c3273 100755 --- a/scripts/nemoclaw-start.sh +++ b/scripts/nemoclaw-start.sh @@ -3402,13 +3402,14 @@ GATEWAYTOKENRECONCILESTART printf "if ( OPENCLAW_GATEWAY_TOKEN='%s' ) 2>/dev/null; then\n" \ "$_escaped_intended_gateway_token" printf " OPENCLAW_GATEWAY_TOKEN='%s'\n" "$_escaped_intended_gateway_token" - printf "elif [ \"\${OPENCLAW_GATEWAY_TOKEN-}\" = '%s' ]; then\n" \ - "$_escaped_intended_gateway_token" + printf "else\n case \"\${OPENCLAW_GATEWAY_TOKEN-}\" in\n" + printf " '%s') : ;;\n" "$_escaped_intended_gateway_token" cat <<'GATEWAYTOKENRECONCILEEND' - : -else - echo "Error: conflicting trust anchor" >&2 - return 1 2>/dev/null || exit 1 + *) + echo "Error: conflicting trust anchor" >&2 + return 1 2>/dev/null || exit 1 + ;; + esac fi # nemoclaw-gateway-token-reconcile end GATEWAYTOKENRECONCILEEND diff --git a/test/proxy-env-gateway-token-conflict.test.ts b/test/proxy-env-gateway-token-conflict.test.ts index 2a867e5d598..128d49da3aa 100644 --- a/test/proxy-env-gateway-token-conflict.test.ts +++ b/test/proxy-env-gateway-token-conflict.test.ts @@ -37,6 +37,7 @@ interface Scenario { preset?: { value: string; readonly: boolean }; repeatSources?: boolean; readonlyPrivateSentinel?: boolean; + shadowTestCommand?: boolean; sourceUrl?: string; } @@ -80,6 +81,7 @@ function runReconcile(scenario: Scenario): { ? `${scenario.preset.readonly ? "readonly " : ""}OPENCLAW_GATEWAY_TOKEN=${shellQuote(scenario.preset.value)}` : "", scenario.readonlyPrivateSentinel ? "readonly _nemoclaw_gateway_token='CALLER-SENTINEL'" : "", + scenario.shadowTestCommand ? "function [ { return 0; }" : "", scenario.sourceUrl ? `OPENCLAW_GATEWAY_URL=${shellQuote(scenario.sourceUrl)}` : "", ].filter(Boolean); const sourceAndPrint = [ @@ -120,6 +122,20 @@ describe("proxy-env OPENCLAW_GATEWAY_TOKEN trust-anchor reconcile (#8428)", () = expect(stderr).not.toContain("read only"); }); + it("rejects a conflicting readonly value when Bash shadows the test command", () => { + const { status, stdout, stderr } = runReconcile({ + intended: REAL_TOKEN, + shell: "bash", + preset: { value: "SENTINEL_CONFLICT", readonly: true }, + shadowTestCommand: true, + }); + expect(status).toBe(1); + expect(stderr).toContain("Error: conflicting trust anchor"); + expect(stderr).not.toContain("read only"); + expect(`${stdout}\n${stderr}`).not.toContain(REAL_TOKEN); + expect(stdout).not.toContain("TOKEN="); + }); + it.each(SHELLS)("advances a writable value across repeated sourcing under %s", (shell) => { const { status, stdout, stderr } = runReconcile({ intended: REAL_TOKEN, From 1beda0b13e37afb593ed16568e62fbe2457bd30d Mon Sep 17 00:00:00 2001 From: Apurv Kumaria Date: Thu, 6 Aug 2026 23:04:34 -0700 Subject: [PATCH 08/10] fix(sandbox): fail closed on shadowed status commands Signed-off-by: Apurv Kumaria --- scripts/nemoclaw-start.sh | 4 ++-- test/proxy-env-gateway-token-conflict.test.ts | 21 ++++++++++++++++++- 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/scripts/nemoclaw-start.sh b/scripts/nemoclaw-start.sh index 32e8b5c3273..dfc5eeb4bbf 100755 --- a/scripts/nemoclaw-start.sh +++ b/scripts/nemoclaw-start.sh @@ -3406,8 +3406,8 @@ GATEWAYTOKENRECONCILESTART printf " '%s') : ;;\n" "$_escaped_intended_gateway_token" cat <<'GATEWAYTOKENRECONCILEEND' *) - echo "Error: conflicting trust anchor" >&2 - return 1 2>/dev/null || exit 1 + /usr/bin/printf '%s\n' 'Error: conflicting trust anchor' >&2 + /usr/bin/false ;; esac fi diff --git a/test/proxy-env-gateway-token-conflict.test.ts b/test/proxy-env-gateway-token-conflict.test.ts index 128d49da3aa..f5e6b05ea7e 100644 --- a/test/proxy-env-gateway-token-conflict.test.ts +++ b/test/proxy-env-gateway-token-conflict.test.ts @@ -38,6 +38,7 @@ interface Scenario { repeatSources?: boolean; readonlyPrivateSentinel?: boolean; shadowTestCommand?: boolean; + shadowStatusCommands?: boolean; sourceUrl?: string; } @@ -77,15 +78,19 @@ function runReconcile(scenario: Scenario): { spawnSync("bash", [generator], { encoding: "utf-8" }); const setup = [ + "set -e", scenario.preset ? `${scenario.preset.readonly ? "readonly " : ""}OPENCLAW_GATEWAY_TOKEN=${shellQuote(scenario.preset.value)}` : "", scenario.readonlyPrivateSentinel ? "readonly _nemoclaw_gateway_token='CALLER-SENTINEL'" : "", scenario.shadowTestCommand ? "function [ { return 0; }" : "", + scenario.shadowStatusCommands + ? "function return { builtin return 0; }; function exit { builtin return 0; }; function echo { builtin return 0; }" + : "", scenario.sourceUrl ? `OPENCLAW_GATEWAY_URL=${shellQuote(scenario.sourceUrl)}` : "", ].filter(Boolean); const sourceAndPrint = [ - `. ${shellQuote(envFile)} || exit $?`, + `. ${shellQuote(envFile)}`, `printf 'TOKEN=[%s] PRIVATE=[%s]\\n' "\${OPENCLAW_GATEWAY_TOKEN-}" "\${_nemoclaw_gateway_token-}"`, ]; const commands = scenario.repeatSources @@ -136,6 +141,20 @@ describe("proxy-env OPENCLAW_GATEWAY_TOKEN trust-anchor reconcile (#8428)", () = expect(stdout).not.toContain("TOKEN="); }); + it("rejects a conflicting readonly value when Bash shadows status commands", () => { + const { status, stdout, stderr } = runReconcile({ + intended: REAL_TOKEN, + shell: "bash", + preset: { value: "SENTINEL_CONFLICT", readonly: true }, + shadowStatusCommands: true, + }); + expect(status).toBe(1); + expect(stderr).toContain("Error: conflicting trust anchor"); + expect(stderr).not.toContain("read only"); + expect(`${stdout}\n${stderr}`).not.toContain(REAL_TOKEN); + expect(stdout).not.toContain("TOKEN="); + }); + it.each(SHELLS)("advances a writable value across repeated sourcing under %s", (shell) => { const { status, stdout, stderr } = runReconcile({ intended: REAL_TOKEN, From 1ae92140e83606bdc35ebf4624063b841c0cb365 Mon Sep 17 00:00:00 2001 From: Apurv Kumaria Date: Thu, 6 Aug 2026 23:10:46 -0700 Subject: [PATCH 09/10] test(sandbox): assert sourced conflict status directly Signed-off-by: Apurv Kumaria --- test/proxy-env-gateway-token-conflict.test.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/proxy-env-gateway-token-conflict.test.ts b/test/proxy-env-gateway-token-conflict.test.ts index f5e6b05ea7e..4fe371c10cd 100644 --- a/test/proxy-env-gateway-token-conflict.test.ts +++ b/test/proxy-env-gateway-token-conflict.test.ts @@ -78,7 +78,6 @@ function runReconcile(scenario: Scenario): { spawnSync("bash", [generator], { encoding: "utf-8" }); const setup = [ - "set -e", scenario.preset ? `${scenario.preset.readonly ? "readonly " : ""}OPENCLAW_GATEWAY_TOKEN=${shellQuote(scenario.preset.value)}` : "", @@ -91,7 +90,8 @@ function runReconcile(scenario: Scenario): { ].filter(Boolean); const sourceAndPrint = [ `. ${shellQuote(envFile)}`, - `printf 'TOKEN=[%s] PRIVATE=[%s]\\n' "\${OPENCLAW_GATEWAY_TOKEN-}" "\${_nemoclaw_gateway_token-}"`, + "_nemoclaw_test_source_status=$?", + `case "$_nemoclaw_test_source_status" in 0) /usr/bin/printf 'TOKEN=[%s] PRIVATE=[%s]\\n' "\${OPENCLAW_GATEWAY_TOKEN-}" "\${_nemoclaw_gateway_token-}" ;; *) /usr/bin/false ;; esac`, ]; const commands = scenario.repeatSources ? [...setup, ...sourceAndPrint, ...sourceAndPrint] From 94e01bc88a1549213247918f44c339107dc0cf67 Mon Sep 17 00:00:00 2001 From: Apurv Kumaria Date: Fri, 7 Aug 2026 05:40:03 -0700 Subject: [PATCH 10/10] test(sandbox): cover readonly remote token conflicts Signed-off-by: Apurv Kumaria --- test/proxy-env-gateway-token-conflict.test.ts | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/test/proxy-env-gateway-token-conflict.test.ts b/test/proxy-env-gateway-token-conflict.test.ts index 4fe371c10cd..8e64f46388e 100644 --- a/test/proxy-env-gateway-token-conflict.test.ts +++ b/test/proxy-env-gateway-token-conflict.test.ts @@ -23,6 +23,10 @@ const WRITE_RUNTIME_SHELL_ENV = extractShellFunctionFromSource( ); const REAL_TOKEN = "REAL-GATEWAY-TOKEN-abc123"; const SHELLS = ["sh", "bash"] as const; +const EMPTY_TOKEN_URLS = [ + "wss://remote.example.test", + "wss://user:password@remote.example.test", +] as const; const tmpRoots: string[] = []; afterEach(() => { @@ -186,4 +190,21 @@ describe("proxy-env OPENCLAW_GATEWAY_TOKEN trust-anchor reconcile (#8428)", () = expect(stdout).toContain("TOKEN=[]"); expect(stderr).toBe(""); }); + + it.each( + SHELLS.flatMap((shell) => EMPTY_TOKEN_URLS.map((sourceUrl) => ({ shell, sourceUrl }))), + )("rejects a readonly nonempty token for $sourceUrl under $shell", ({ shell, sourceUrl }) => { + const { status, stdout, stderr } = runReconcile({ + intended: REAL_TOKEN, + shell, + preset: { value: "SENTINEL_CONFLICT", readonly: true }, + sourceUrl, + }); + expect(status).toBe(1); + expect(stderr).toContain("Error: conflicting trust anchor"); + expect(stderr).not.toContain("read only"); + expect(`${stdout}\n${stderr}`).not.toContain("SENTINEL_CONFLICT"); + expect(`${stdout}\n${stderr}`).not.toContain(REAL_TOKEN); + expect(stdout).not.toContain("TOKEN="); + }); });