Skip to content

fix(sandbox): keep runtime shell state out of rc files - #2741

Merged
ericksoa merged 2 commits into
mainfrom
fix/runtime-env-rc-followup-2227
Apr 30, 2026
Merged

ericksoa merged 2 commits into
mainfrom
fix/runtime-env-rc-followup-2227

Conversation

@ericksoa

@ericksoa ericksoa commented Apr 30, 2026 •

Copy link
Copy Markdown
Member

Summary

  • Follow-up to refactor(sandbox): default to mutable config, make shields opt-in #2227: keep /sandbox/.bashrc and /sandbox/.profile as static source shims instead of rewriting them at startup.
  • Generate gateway token export and the NemoClaw configure guard into /tmp/nemoclaw-proxy-env.sh via the existing trusted emit_sandbox_sourced_file path.
  • Make the base image create shell startup files as root:root mode 444, and update docs/tests for the new default behavior.

Validation

  • npm test -- test/nemoclaw-start.test.ts test/service-env.test.ts test/sandbox-provisioning.test.ts test/sandbox-init.test.ts
  • NEMOCLAW_TEST_IMAGE=nemoclaw-override-test-d7ea5b15 bash test/e2e/test-runtime-overrides.sh

Notes

Summary by CodeRabbit

  • Refactor

    • Hardened sandbox shell startup by making user RC files root-owned and read-only, and moving runtime token/export handling to a generated runtime env script that is sourced via a static shim.
  • Documentation

    • Updated security best-practices to describe the new runtime env script flow and revised default locked-down posture wording.
  • Tests

    • Adjusted and added tests to validate the runtime env emission, shim backfill, and immutable RC file expectations.

@coderabbitai

coderabbitai Bot commented Apr 30, 2026 •

Copy link
Copy Markdown
Contributor
📝 Walkthrough

Walkthrough

Sandbox startup no longer mutates user rc files. Dockerfile.base locks /sandbox/.bashrc and /sandbox/.profile as root:root + chmod 444, and startup now emits /tmp/nemoclaw-proxy-env.sh at runtime (via new functions) which shells are sourced from a shim.

Changes

Cohort / File(s) Summary
Dockerfile Base
Dockerfile.base
Sets ownership of /sandbox/.bashrc and /sandbox/.profile to root:root and chmod 444; adds conditional sourcing of /tmp/nemoclaw-proxy-env.sh.
Startup Script
scripts/nemoclaw-start.sh
Removes rc-marker rewrites and install_configure_guard(); adds write_runtime_shell_env() and ensure_runtime_shell_env_shim() to emit /tmp/nemoclaw-proxy-env.sh (exports token, configure-guard, NODE_OPTIONS, tool-cache redirects) and backfill static shims; calls these before lock_rc_files.
Sandbox Init Docstring
scripts/lib/sandbox-init.sh
Updated comments describing RC locking timing to reference the runtime /tmp/nemoclaw-proxy-env.sh write; no executable changes.
Documentation
docs/security/best-practices.md
Rewords gateway-token and "Locked-Down (Default)" guidance to describe writing /tmp/nemoclaw-proxy-env.sh and static shims rather than embedding marker blocks in rc files.
Tests — startup & service env
test/nemoclaw-start.test.ts, test/service-env.test.ts
Refactors tests to extract and assert write_runtime_shell_env() / ensure_runtime_shell_env_shim() snippets; verify OPENCLAW_GATEWAY_TOKEN export, configure-guard, and proxy env content are written to /tmp/nemoclaw-proxy-env.sh; add shim-backfill assertions.
Tests — provisioning
test/sandbox-provisioning.test.ts
Adds assertions parsing Dockerfile.base: require /tmp/nemoclaw-proxy-env.sh to be sourced twice, assert chown root:root and chmod 444 on /sandbox/.bashrc and /sandbox/.profile, and assert they are not sandbox:sandbox.

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~45 minutes

Poem

🐇 I hopped through scripts with nimble paws,

Wrote proxy-env, avoided rc laws.
Root-locked sleeves keep settings tight,
Shells source gently through the night. ✨

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 20.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The PR title 'fix(sandbox): keep runtime shell state out of rc files' directly and clearly summarizes the main change: moving dynamic shell state from rc files to a runtime temp file.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/runtime-env-rc-followup-2227

Review rate limit: 9/10 reviews remaining, refill in 6 minutes.

Comment @coderabbitai help to get the list of available commands and usage tips.

@ericksoa ericksoa self-assigned this Apr 30, 2026

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🧹 Nitpick comments (3)
docs/security/best-practices.md (1)

248-248: ⚡ Quick win

Split this cell into one sentence per line and switch to active voice.

This updated cell now puts two sentences on one source line and uses passive phrasing (is exported, is made available), which conflicts with the docs style guide.

As per coding guidelines: "Active voice required. Flag passive constructions." and "One sentence per line in source (makes diffs readable)."

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@docs/security/best-practices.md` at line 248, Update the single source line
describing the sandbox to use one sentence per line and active voice: break the
combined sentence into separate source lines describing (1) that
`/sandbox/.openclaw` is writable with permissions `700 sandbox:sandbox`, (2)
that `openclaw.json` is `600 sandbox:sandbox`, (3) that the agent can read/write
config, install skills, and manage state directly, and (4) that the gateway
exports the auth token `OPENCLAW_GATEWAY_TOKEN` and places it into
`/tmp/nemoclaw-proxy-env.sh` for interactive sandbox shells; rewrite the passive
phrases (“is exported”, “is made available”) into active constructions (e.g.,
“the gateway exports …” and “the gateway places …”) so each sentence is on its
own source line.
Dockerfile.base (1)

116-131: Please run the full sandbox E2E matrix on a real image build before merge.

These baked root:root / 444 rc shims only get exercised through an actual container boot path; the tests in this PR mostly validate Dockerfile text and startup-script output.

As per coding guidelines Dockerfile.base: "This file affects the sandbox container image. Layer ordering, permissions, and baked config changes are only testable with a real container build."

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@Dockerfile.base` around lines 116 - 131, The PR adds baked root-owned,
read-only shell init files in Dockerfile.base (/sandbox/.bashrc and
/sandbox/.profile with chown root:root and chmod 444) which are only verifiable
by building and running the actual container image; before merging, run the full
sandbox E2E matrix against a real image build (not just lint/text tests), boot
the container, confirm the files exist, have root:root ownership and 444
permissions, and that the entrypoint sourcing of /tmp/nemoclaw-proxy-env.sh
works as expected at interactive login; if any test fails, adjust the
Dockerfile.base changes (the RUN block creating /sandbox/.bashrc and
/sandbox/.profile or the entrypoint behavior) and re-run the full matrix until
all sandbox E2E pass.
test/sandbox-provisioning.test.ts (1)

46-51: ⚡ Quick win

Assert the shim is written to both rc files, not just somewhere in the Dockerfile.

toContain(...) only proves the source line exists once. If a future edit drops it from either .bashrc or .profile, this test still passes. Matching both printf blocks or asserting two occurrences would make the regression guard line up with the runtime contract.

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@test/sandbox-provisioning.test.ts` around lines 46 - 51, The test
"Dockerfile.base keeps shell startup files static and trusted" currently only
uses expect(src).toContain(...) which only proves the shim line exists
somewhere; update the assertion to verify the shim is written into both shell rc
files by either checking that the specific string "[ -f
/tmp/nemoclaw-proxy-env.sh ] && . /tmp/nemoclaw-proxy-env.sh" appears twice in
src (e.g., count occurrences) or by asserting it appears once in the .bashrc
block and once in the .profile block (e.g., match the surrounding printf blocks
or use regex anchors). Keep the existing chown/chmod assertions for
.bashrc/.profile and adjust only the shim assertion on the src variable in the
same test.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@scripts/nemoclaw-start.sh`:
- Around line 2031-2032: The startup sequence currently calls
write_runtime_shell_env and then lock_rc_files assuming the base image rc files
already contain the /tmp/nemoclaw-proxy-env.sh shim; update this flow so
lock_rc_files only runs after verifying the shim is present (or after
backfilling it) into the target rc files: have write_runtime_shell_env create
the temp shim, then check the rc files for the source line for
/tmp/nemoclaw-proxy-env.sh (and the OPENCLAW_GATEWAY_TOKEN/configure guard
markers) and insert it if missing before calling lock_rc_files, ensuring
openshell sandbox connect will pick up the proxy env on older base images.

---

Nitpick comments:
In `@Dockerfile.base`:
- Around line 116-131: The PR adds baked root-owned, read-only shell init files
in Dockerfile.base (/sandbox/.bashrc and /sandbox/.profile with chown root:root
and chmod 444) which are only verifiable by building and running the actual
container image; before merging, run the full sandbox E2E matrix against a real
image build (not just lint/text tests), boot the container, confirm the files
exist, have root:root ownership and 444 permissions, and that the entrypoint
sourcing of /tmp/nemoclaw-proxy-env.sh works as expected at interactive login;
if any test fails, adjust the Dockerfile.base changes (the RUN block creating
/sandbox/.bashrc and /sandbox/.profile or the entrypoint behavior) and re-run
the full matrix until all sandbox E2E pass.

In `@docs/security/best-practices.md`:
- Line 248: Update the single source line describing the sandbox to use one
sentence per line and active voice: break the combined sentence into separate
source lines describing (1) that `/sandbox/.openclaw` is writable with
permissions `700 sandbox:sandbox`, (2) that `openclaw.json` is `600
sandbox:sandbox`, (3) that the agent can read/write config, install skills, and
manage state directly, and (4) that the gateway exports the auth token
`OPENCLAW_GATEWAY_TOKEN` and places it into `/tmp/nemoclaw-proxy-env.sh` for
interactive sandbox shells; rewrite the passive phrases (“is exported”, “is made
available”) into active constructions (e.g., “the gateway exports …” and “the
gateway places …”) so each sentence is on its own source line.

In `@test/sandbox-provisioning.test.ts`:
- Around line 46-51: The test "Dockerfile.base keeps shell startup files static
and trusted" currently only uses expect(src).toContain(...) which only proves
the shim line exists somewhere; update the assertion to verify the shim is
written into both shell rc files by either checking that the specific string "[
-f /tmp/nemoclaw-proxy-env.sh ] && . /tmp/nemoclaw-proxy-env.sh" appears twice
in src (e.g., count occurrences) or by asserting it appears once in the .bashrc
block and once in the .profile block (e.g., match the surrounding printf blocks
or use regex anchors). Keep the existing chown/chmod assertions for
.bashrc/.profile and adjust only the shim assertion on the src variable in the
same test.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Enterprise

Run ID: abc6fb6f-a114-4f2e-8004-711fc5704139

📥 Commits

Reviewing files that changed from the base of the PR and between 15fde69 and d7ea5b1.

📒 Files selected for processing (7)
  • Dockerfile.base
  • docs/security/best-practices.md
  • scripts/lib/sandbox-init.sh
  • scripts/nemoclaw-start.sh
  • test/nemoclaw-start.test.ts
  • test/sandbox-provisioning.test.ts
  • test/service-env.test.ts

Comment thread scripts/nemoclaw-start.sh Resolved
@github-actions

Copy link
Copy Markdown
Contributor

Selective E2E Results — ✅ All requested jobs passed

Run: 25148321123
Branch: fix/runtime-env-rc-followup-2227
Requested jobs: all (no filter)
Summary: 19 passed, 0 failed, 1 skipped

Job Result
cloud-e2e ✅ success
cloud-inference-e2e ✅ success
cloud-onboard-e2e ✅ success
deployment-services-e2e ✅ success
diagnostics-e2e ✅ success
docs-validation-e2e ✅ success
gpu-e2e ⏭️ skipped
hermes-e2e ✅ success
inference-routing-e2e ✅ success
messaging-providers-e2e ✅ success
network-policy-e2e ✅ success
overlayfs-autofix-e2e ✅ success
rebuild-hermes-e2e ✅ success
rebuild-openclaw-e2e ✅ success
sandbox-operations-e2e ✅ success
sandbox-survival-e2e ✅ success
shields-config-e2e ✅ success
skill-agent-e2e ✅ success
snapshot-commands-e2e ✅ success
token-rotation-e2e ⚠️ cancelled
upgrade-stale-sandbox-e2e ✅ success

@ericksoa
ericksoa enabled auto-merge (squash) April 30, 2026 05:33
@ericksoa
ericksoa disabled auto-merge April 30, 2026 05:33
@ericksoa
ericksoa merged commit 3555308 into main Apr 30, 2026
49 of 51 checks passed

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@scripts/nemoclaw-start.sh`:
- Around line 1801-1826: The current flow keeps and then locks existing mutable
rc files (rc_file) which can contain untrusted agent content; instead, when
rc_file pre-exists and is not already a verified canonical shim, replace its
contents with the trusted shim (write _RUNTIME_SHELL_ENV_SHIM and a minimal safe
header) before changing ownership/permissions and before any later lock_rc_files
call; update the branch that handles "[ -f \"$rc_file\" ]" to detect
pre-existing files and either overwrite them with the canonical shim (using >)
or, if you must preserve legacy content, sanitize/replace it with a known-safe
template variable (e.g., LEGACY_SAFE_RC_TEMPLATE) prior to chown/chmod and
locking.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Enterprise

Run ID: d291eb21-8d4f-4b94-811a-e61e8c61a64c

📥 Commits

Reviewing files that changed from the base of the PR and between d7ea5b1 and 92c166a.

📒 Files selected for processing (5)
  • docs/security/best-practices.md
  • scripts/nemoclaw-start.sh
  • test/nemoclaw-start.test.ts
  • test/sandbox-provisioning.test.ts
  • test/service-env.test.ts
🚧 Files skipped from review as they are similar to previous changes (2)
  • test/nemoclaw-start.test.ts
  • test/service-env.test.ts

Comment thread scripts/nemoclaw-start.sh
Comment on lines +1801 to +1826
if [ "$(id -u)" -eq 0 ] && [ -f "$rc_file" ]; then
if ! chown root:root "$rc_file" 2>/dev/null; then
echo "[SECURITY] could not take ownership of $rc_file before shim backfill" >&2
failed=1
continue
fi
if ! chmod 644 "$rc_file" 2>/dev/null; then
echo "[SECURITY] could not make $rc_file writable before shim backfill" >&2
failed=1
continue
fi
elif [ -f "$rc_file" ]; then
chmod u+w "$rc_file" 2>/dev/null || true
fi

if [ -e "$rc_file" ]; then
if ! printf '\n%s\n%s\n' '# Source runtime proxy config' "$_RUNTIME_SHELL_ENV_SHIM" >>"$rc_file"; then
echo "[SECURITY] could not backfill runtime env shim into $rc_file" >&2
failed=1
continue
fi
elif ! printf '%s\n%s\n' '# Source runtime proxy config' "$_RUNTIME_SHELL_ENV_SHIM" >"$rc_file"; then
echo "[SECURITY] could not create $rc_file with runtime env shim" >&2
failed=1
continue
fi

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟠 Major | ⚡ Quick win

Do not lock pre-existing mutable rc contents into place.

On stale images, .bashrc and .profile may still contain sandbox-writable content. This path takes ownership of the existing file, appends the trusted shim, and then the later lock_rc_files call freezes the whole file as root:root + read-only. That preserves any agent-written commands as trusted startup code for future connect shells.

Replace stale rc files with the canonical shim content, or only preserve a known-safe legacy template before locking.

🔒 Suggested hardening
-    if [ -e "$rc_file" ]; then
-      if ! printf '\n%s\n%s\n' '# Source runtime proxy config' "$_RUNTIME_SHELL_ENV_SHIM" >>"$rc_file"; then
-        echo "[SECURITY] could not backfill runtime env shim into $rc_file" >&2
-        failed=1
-        continue
-      fi
-    elif ! printf '%s\n%s\n' '# Source runtime proxy config' "$_RUNTIME_SHELL_ENV_SHIM" >"$rc_file"; then
+    if ! printf '%s\n%s\n' '# Source runtime proxy config' "$_RUNTIME_SHELL_ENV_SHIM" >"$rc_file"; then
       echo "[SECURITY] could not create $rc_file with runtime env shim" >&2
       failed=1
       continue
-    fi
+    fi
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
if [ "$(id -u)" -eq 0 ] && [ -f "$rc_file" ]; then
if ! chown root:root "$rc_file" 2>/dev/null; then
echo "[SECURITY] could not take ownership of $rc_file before shim backfill" >&2
failed=1
continue
fi
if ! chmod 644 "$rc_file" 2>/dev/null; then
echo "[SECURITY] could not make $rc_file writable before shim backfill" >&2
failed=1
continue
fi
elif [ -f "$rc_file" ]; then
chmod u+w "$rc_file" 2>/dev/null || true
fi
if [ -e "$rc_file" ]; then
if ! printf '\n%s\n%s\n' '# Source runtime proxy config' "$_RUNTIME_SHELL_ENV_SHIM" >>"$rc_file"; then
echo "[SECURITY] could not backfill runtime env shim into $rc_file" >&2
failed=1
continue
fi
elif ! printf '%s\n%s\n' '# Source runtime proxy config' "$_RUNTIME_SHELL_ENV_SHIM" >"$rc_file"; then
echo "[SECURITY] could not create $rc_file with runtime env shim" >&2
failed=1
continue
fi
if [ "$(id -u)" -eq 0 ] && [ -f "$rc_file" ]; then
if ! chown root:root "$rc_file" 2>/dev/null; then
echo "[SECURITY] could not take ownership of $rc_file before shim backfill" >&2
failed=1
continue
fi
if ! chmod 644 "$rc_file" 2>/dev/null; then
echo "[SECURITY] could not make $rc_file writable before shim backfill" >&2
failed=1
continue
fi
elif [ -f "$rc_file" ]; then
chmod u+w "$rc_file" 2>/dev/null || true
fi
if ! printf '%s\n%s\n' '# Source runtime proxy config' "$_RUNTIME_SHELL_ENV_SHIM" >"$rc_file"; then
echo "[SECURITY] could not create $rc_file with runtime env shim" >&2
failed=1
continue
fi
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@scripts/nemoclaw-start.sh` around lines 1801 - 1826, The current flow keeps
and then locks existing mutable rc files (rc_file) which can contain untrusted
agent content; instead, when rc_file pre-exists and is not already a verified
canonical shim, replace its contents with the trusted shim (write
_RUNTIME_SHELL_ENV_SHIM and a minimal safe header) before changing
ownership/permissions and before any later lock_rc_files call; update the branch
that handles "[ -f \"$rc_file\" ]" to detect pre-existing files and either
overwrite them with the canonical shim (using >) or, if you must preserve legacy
content, sanitize/replace it with a known-safe template variable (e.g.,
LEGACY_SAFE_RC_TEMPLATE) prior to chown/chmod and locking.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug-fix PR fixes a bug or regression

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants