Skip to content

fix(bwclaude): turn Claude Code's own sandbox off inside bwrap - #52

Merged
Dan Allan (danielballan) merged 5 commits into
NSLS2:mainfrom
stuwilkins:add_bwclaude_inner_sandbox_off
Dec 3, 2026
Merged

Dan Allan (danielballan) merged 5 commits into
NSLS2:mainfrom
stuwilkins:add_bwclaude_inner_sandbox_off

Conversation

@stuwilkins

@stuwilkins Stuart B. Wilkins (stuwilkins) commented Dec 2, 2026 •

Copy link
Copy Markdown
Contributor

Summary

bwclaude binds a managed-settings drop-in,
/etc/claude-code/managed-settings.d/99-bwclaude.json, that turns Claude Code's
own sandbox off inside the bwclaude sandbox. The outer bwrap sandbox is the
boundary, as in bwopencode2. claude run outside bwclaude keeps the strict
managed policy.

Why: the managed settings of the compliance role set sandbox.enabled and
sandbox.failIfUnavailable. Inside bwclaude:

  • Claude Code's sandbox gives skill scripts no direct network route (only a
    proxy that allows no host by default) and a read-only uv cache, so skills that
    connect directly fail with a name resolution error (NSLS2/ansible#5196). That
    is pvws.py, and from reading the scripts most of the Loki, Mimir, Tempo,
    NeDi and archiver readers too. The ldapsearch-based bnl-ldap cannot use an
    HTTP proxy at all.
  • With sandbox.failIfUnavailable, claude also refuses to start on a host
    without bubblewrap and socat. (socat is not masked in the launcher: an
    earlier revision of this text and of the first commit message said so, and
    that was wrong.)

Description

  • build_managed_claude_paths binds a drop-in with
    {"sandbox":{"enabled":false,"failIfUnavailable":false}} after the managed
    file. Claude Code merges managed-settings.d/*.json after the managed file,
    so every other managed key (telemetry, allowUnsandboxedCommands, ...) stays
    in force. It is added only when the managed file exists.
  • CLAUDE_MANAGED_DIR is a plain variable so the tests can point it at a
    scratch directory.
  • bin/bwclaude gets the same main guard as bwopencode2, so a test can
    source it, and finds its library from BASH_SOURCE[0] instead of $0 (fourth
    commit; Copilot's third review), so it can be sourced from any directory. Run
    directly the two are the same. feat(bwclaude,bwcodex): sign in to the AI gateway with your own n2c-token #47 makes the same one-line change.
  • docs/bw-wrappers.md explains what bwclaude now relies on and what it loses.
  • ~/.azure is added to the launcher's blocked-path list (second commit), with
    tests/test-blocked-paths.sh. Copilot's review pointed out that the managed
    denyRead list names ~/.azure but main's list did not, so with the inner
    sandbox off --ro-path ~/.azure was allowed. feat(bwclaude,bwcodex): sign in to the AI gateway with your own n2c-token #47 has the same two library lines
    and the same test file.
  • /etc/claude-code inside the sandbox is a tmpfs of its own, remounted
    read-only after the managed files, drop-in and skills are bound into it (third
    commit). Copilot's second review pointed out that a plain --dir in the root
    tmpfs stays writable, so with the inner sandbox off a command could add its own
    managed-settings.d/*.json and override the managed policy. The inner sandbox
    used to prevent that.

What is lost inside bwclaude, for reviewers to weigh:

  • The inner proxy's domain allowlist: the network is the outer sandbox's own,
    with DNS logged by the relay (as in bwopencode2).
  • Claude Code's write protection for .claude/ settings, hooks and .mcp.json
    in the working directory. A planted hook could run later outside bwclaude,
    unsandboxed, if someone runs a plain claude in that directory. The same holds
    for the other launchers.
  • The managed denyRead list is not lost: the home directory is not mounted and
    --ro-path and --rw-path refuse those paths (~/.azure with this change).

Open questions:

Testing

  • tests/test-bwclaude-managed-settings.sh (new, 7 checks, runs on any host,
    needs no sandbox): no drop-in without managed settings; managed settings bound
    read-only; the drop-in bound after them at 99-bwclaude.json; it sets
    enabled and failIfUnavailable to false and nothing else; valid JSON; the
    directory is a read-only tmpfs; the launcher can be sourced from another
    directory (this check fails with the old $0 line).
  • tests/test-blocked-paths.sh (15 checks, from feat(bwclaude,bwcodex): sign in to the AI gateway with your own n2c-token #47): --ro-path and --rw-path
    refuse ~/.aws, ~/.azure (and below), ~/.config/gh, ~/.ssh and
    ~/.docker, and allow ~/.azure-opencode-ro and an ordinary directory. Without
    the library change exactly the four ~/.azure checks fail.
  • The real argument list from build_managed_claude_paths run through a nested
    bwrap: before, a command could create managed-settings.d/evil.json, a file
    in the managed directory and in .claude/, and a subdirectory; after, every
    one is refused with "Read-only file system", and the drop-in, the managed file
    and the skills still read correctly. The nested test bound a scratch directory
    over itself, not the real tmpfs root; the mechanism is the same.
  • prek on the changed files passes (pixi run --frozen -e dev prek run --files ...).
  • The same drop-in was used in an earlier revision of feat(bwclaude,bwcodex): sign in to the AI gateway with your own n2c-token #47 (commit 40e74c8) and
    tested then; this change has not been run through a real bwclaude launch
    yet. Not tested: claude -p running a Bash tool and pvws.py and mimir.py
    inside bwclaude with the drop-in, on a managed workstation.

Checklist

  • Edit the sections above with relevant information
  • Update in-repo documentation, if applicable
  • Run pixi run -e dev lint locally (or wait for CI) — see .github/workflows/lint.yml
  • Test the changes on a representative managed host (or runansible deployment)

Assisted-by: opencode:claude-sonnet-5-5

The managed settings (compliance role) turn on Claude Code's own sandbox with failIfUnavailable. Inside bwclaude it cannot start (socat is masked) and, where it does, it gives skill scripts no direct network route and a read-only uv cache (NSLS2/ansible#5196). Bind a managed-settings.d/99-bwclaude.json drop-in that sets sandbox.enabled and failIfUnavailable to false; Claude Code merges it after the managed file, so every other managed key stays. Added only when the managed file exists, and only inside the sandbox.

Add the main guard so tests can source bin/bwclaude, and a test for the drop-in.

Refs NSLS2/ansible#5196

Assisted-by: opencode:claude-sonnet-5-5
Copilot AI balanced review requested due to automatic review settings December 2, 2026 17:02

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🔵 Needs a closer look

Disabling the inner sandbox leaves ~/.azure unblocked, allowing --ro-path or --rw-path to expose Azure credential caches to Bash tools despite the managed policy.

No open issues 🎉

What changed in this PR

This PR makes bwclaude rely on its outer bubblewrap sandbox by disabling Claude Code’s inner sandbox when managed settings exist.

Changes:

  • Adds a sandbox-only managed-settings override.
  • Adds focused checks and a sourceable launcher guard.
  • Documents the security and networking tradeoffs.
File Description
tests/​test-bwclaude-managed-settings.sh Checks override contents and mount arguments.
docs/​bw-wrappers.md Explains sandbox behavior and lost protections.
bin/​bwclaude Adds the managed override and sourceable entry point.

Review effort: Balanced

The managed Claude Code settings deny reading ~/.azure, but with Claude Code's own sandbox off inside bwclaude the launcher's blocked list is the only guard, and it did not name ~/.azure. Add it, with a test for the credential directories in that list. Found by Copilot review of NSLS2#52.

Assisted-by: opencode:claude-sonnet-5-5

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🔵 Needs a closer look

Disabling the inner sandbox leaves critical gaps that allow automatic directory mounts to expose credentials and writable managed-policy directories to accept policy-overriding drop-ins.

No open issues 🎉

Review effort: Balanced

@stuwilkins
Stuart B. Wilkins (stuwilkins) marked this pull request as draft December 2, 2026 17:53
@stuwilkins
Stuart B. Wilkins (stuwilkins) marked this pull request as ready for review December 2, 2026 17:53
A directory made with --dir in the sandbox's root tmpfs is writable. With Claude Code's own sandbox off, a command could write a file into managed-settings.d/ (merged after the managed file) and override the managed policy for the session. Make /etc/claude-code a tmpfs of its own and remount it read-only after the managed files, drop-in and skills are bound into it. Found by Copilot review of NSLS2#52.

Assisted-by: opencode:claude-sonnet-5-5
Copilot AI balanced review requested due to automatic review settings December 2, 2026 18:14

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request due to rate limiting. Please try again later. See Usage limits for GitHub Copilot for details.

Copilot AI balanced review requested due to automatic review settings December 2, 2026 18:23

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🔵 Needs a closer look

Sourcing from an external script or bash -c can fail before the guard because SCRIPT_DIR is derived from $0 rather than ${BASH_SOURCE[0]}, and the new test masks this path issue.

No open issues 🎉

Review effort: Balanced

SCRIPT_DIR came from $0, so sourcing bin/bwclaude from a script outside tests/ (or with bash -c) failed to find lib/bwrap_sandbox_lib.sh. Use BASH_SOURCE[0], as bwopencode2 does; run directly the two are the same. Test it by sourcing from another directory and checking for a function of the library. Found by Copilot review of NSLS2#52.

Assisted-by: opencode:claude-sonnet-5-5
Copilot AI balanced review requested due to automatic review settings December 2, 2026 18:46

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🔵 Needs a closer look

Human assessment is needed to weigh an unpublished finding that the documented rationale for disabling the inner sandbox incorrectly treats socat as masked.

No open issues 🎉

Review effort: Balanced

The comment in bin/bwclaude, the launcher header and docs/bw-wrappers.md said socat is masked inside bwclaude. It is not in _MASKED_BINS. Give the actual reasons for turning Claude Code's own sandbox off: its proxy-only network and read-only uv cache break skill scripts (NSLS2/ansible#5196), and failIfUnavailable stops claude on a host without bubblewrap and socat. The message of the first commit repeats the wrong claim. Found by Copilot review of NSLS2#52.

Assisted-by: opencode:claude-sonnet-5-5
Copilot AI balanced review requested due to automatic review settings December 2, 2026 19:47

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🔵 Needs a closer look

Disabling the inner sandbox removes security protections and conflicts with PR #47’s policy, requiring human security-policy review and managed-host validation that remains unperformed.

No open issues 🎉

Review effort: Balanced

@danielballan
Dan Allan (danielballan) merged commit 3451fd9 into NSLS2:main Dec 3, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants