Repository navigation
fix(sandbox): keep runtime shell state out of rc files - #2741
Conversation
📝 WalkthroughWalkthroughSandbox startup no longer mutates user rc files. Dockerfile.base locks Changes
Estimated code review effort🎯 4 (Complex) | ⏱️ ~45 minutes Poem
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Review rate limit: 9/10 reviews remaining, refill in 6 minutes. Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
🧹 Nitpick comments (3)
docs/security/best-practices.md (1)
248-248: ⚡ Quick winSplit 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/444rc 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 winAssert 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.bashrcor.profile, this test still passes. Matching bothprintfblocks 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
📒 Files selected for processing (7)
Dockerfile.basedocs/security/best-practices.mdscripts/lib/sandbox-init.shscripts/nemoclaw-start.shtest/nemoclaw-start.test.tstest/sandbox-provisioning.test.tstest/service-env.test.ts
Selective E2E Results — ✅ All requested jobs passedRun: 25148321123
|
There was a problem hiding this comment.
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
📒 Files selected for processing (5)
docs/security/best-practices.mdscripts/nemoclaw-start.shtest/nemoclaw-start.test.tstest/sandbox-provisioning.test.tstest/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
| 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 |
There was a problem hiding this comment.
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.
| 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.
Summary
/sandbox/.bashrcand/sandbox/.profileas static source shims instead of rewriting them at startup./tmp/nemoclaw-proxy-env.shvia the existing trustedemit_sandbox_sourced_filepath.root:rootmode444, 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.tsNEMOCLAW_TEST_IMAGE=nemoclaw-override-test-d7ea5b15 bash test/e2e/test-runtime-overrides.shNotes
docker runruntime-overrides failure where root had already droppedCAP_DAC_OVERRIDEand could no longer create/sandbox/.bashrctemp files.$HOME/.openclawmutable state behavior from refactor(sandbox): default to mutable config, make shields opt-in #2227 remains unchanged.Summary by CodeRabbit
Refactor
Documentation
Tests