claude-code - 💡(How to fix) Fix Expose sandbox state to hook scripts [1 participants]

Official PRs (…)
ON THIS PAGE

Recommended Tools

×6

Utilities matched from this issue’s tags and category — try them while you read without losing context.

GitHub issue graph ai analysis

Paste a GitHub issue URL. We fetch that issue, discover linked issues from bodies/comments/timeline, collect linked pull requests, and produce a structured English report.

The report is written in English Markdown for sharing and archival.

Helpful · Quick feedback

Loading…
GitHub stats
anthropics/claude-code#46210Fetched 2026-04-11 06:26:15
View on GitHub
Comments
0
Participants
1
Timeline
3
Reactions
0
Author
Participants
Timeline (top)
labeled ×3

Root Cause

Checking sandbox.enabled in settings.json doesn't work because the user can toggle sandbox on/off at runtime via /sandbox, and the file may not reflect the current session state.

Code Example

# Option 1: check stdin JSON
if echo "$input" | jq -e '.is_sandboxed == true' >/dev/null 2>&1; then
  exit 0
fi

# Option 2: check env var
if [[ "${CLAUDE_SANDBOX_ENABLED:-}" == "1" ]]; then
  exit 0
fi
RAW_BUFFERClick to expand / collapse

Problem

PreToolUse hook scripts have no way to detect whether sandbox mode is currently active at runtime. This makes it impossible to conditionally skip hook logic that is redundant when the sandbox is enforcing constraints.

Use case

I have PreToolUse hooks that escalate potentially dangerous rg and find commands (e.g., rg --pre, find -exec) to "permissionDecision": "ask". When sandbox mode is enabled, these guards are redundant — the sandbox already constrains what commands can do. I'd like the hooks to exit early (do nothing) when sandbox is active, avoiding unnecessary permission prompts.

Investigation

I tested both the environment variables and the stdin JSON passed to hooks with sandbox enabled vs disabled. They are identical — no field or variable indicates sandbox state:

  • Env vars: No CLAUDE_SANDBOX, CLAUDE_SANDBOX_ENABLED, or similar variable
  • Hook stdin JSON: Contains cwd, hook_event_name, permission_mode, session_id, tool_input, tool_name, tool_use_id, transcript_path — no sandbox field

Checking sandbox.enabled in settings.json doesn't work because the user can toggle sandbox on/off at runtime via /sandbox, and the file may not reflect the current session state.

Proposed solution

Either (or both):

  1. Add an is_sandboxed boolean field to the common hook input JSON
  2. Set a CLAUDE_SANDBOX_ENABLED environment variable during hook execution

This would allow hooks to trivially detect sandbox state:

# Option 1: check stdin JSON
if echo "$input" | jq -e '.is_sandboxed == true' >/dev/null 2>&1; then
  exit 0
fi

# Option 2: check env var
if [[ "${CLAUDE_SANDBOX_ENABLED:-}" == "1" ]]; then
  exit 0
fi

extent analysis

TL;DR

To detect sandbox mode in PreToolUse hook scripts, add an is_sandboxed boolean field to the hook input JSON or set a CLAUDE_SANDBOX_ENABLED environment variable.

Guidance

  • Consider adding an is_sandboxed field to the common hook input JSON to allow hooks to detect sandbox state.
  • Alternatively, setting a CLAUDE_SANDBOX_ENABLED environment variable during hook execution could also solve the issue.
  • To verify the solution, test the hook scripts with sandbox mode enabled and disabled, checking that the is_sandboxed field or CLAUDE_SANDBOX_ENABLED variable correctly indicates the sandbox state.
  • If implementing the proposed solution is not feasible, consider modifying the hook scripts to check for other indicators of sandbox mode, although this may not be reliable.

Example

# Check stdin JSON for is_sandboxed field
if echo "$input" | jq -e '.is_sandboxed == true' >/dev/null 2>&1; then
  exit 0
fi

Notes

The proposed solution requires changes to the hook input JSON or environment variables, which may require updates to the underlying system or framework. The issue does not provide information on how to modify these, so further investigation or documentation review may be necessary.

Recommendation

Apply workaround by adding an is_sandboxed boolean field to the hook input JSON, as this seems to be a more straightforward and reliable solution than relying on environment variables.

Vote matrix · Quick signals

Works
Did the solution work? Tap to confirm.
Easy Fix
Was it a quick fix?
Time Saver
Did it save you time?
Blocking
Was it severely blocking?
Common Issue
Are others likely hitting this too?
Flaky / Intermittent
Is it intermittent?
Verified / Reproducible
Can you reproduce it reliably?
Loading…

Still need to ship something?

×6

Another batch ranked right after the header list — different links, same matching logic.

Back to top recommendations

TRENDING