fix(transport): default an enabled sandbox to failIfUnavailable - #1209
Open
chintan-diwakar wants to merge 1 commit into
Open
fix(transport): default an enabled sandbox to failIfUnavailable#1209chintan-diwakar wants to merge 1 commit into
chintan-diwakar wants to merge 1 commit into
Conversation
The CLI reads sandbox.failIfUnavailable as `?? false`, so when a caller asks for a sandbox and it cannot start (missing dependencies, unsupported platform, unprivileged container), the CLI prints a warning and then runs every bash command unsandboxed. The Python transport passed options.sandbox through verbatim, so callers silently lost the isolation they asked for. The TypeScript SDK closes this: its settings merge injects failIfUnavailable: true whenever sandbox.enabled is true and the caller did not set it. Identical options therefore fail closed in TypeScript and fail open in Python. Default failIfUnavailable to true for an enabled sandbox. An explicit value is always preserved, a sandbox that is not enabled is untouched, and the caller's dict is copied rather than mutated. Add failIfUnavailable to the SandboxSettings TypedDict so an explicit override type-checks. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
_build_settings_value(_internal/transport/subprocess_cli.py:513) passesoptions.sandboxto the CLI verbatim. The CLI readssandbox.failIfUnavailableas?? false, so when a caller asks for a sandbox and it cannot start (missing dependencies, unsupported platform, unprivileged container) the CLI prints a warning and then runs every bash command unsandboxed. The caller silently loses the isolation they asked for.The TypeScript SDK closes this. Its settings merge injects
failIfUnavailable: truewheneversandbox.enabledis true and the caller did not set it, insdk.mjsof@anthropic-ai/claude-agent-sdk@0.3.233(the release paired with bundled CLI 2.1.233):So identical options fail closed in TypeScript and fail open in Python. This makes Python match.
The CLI does not apply this default itself when it parses
--settings, so the SDK is the only place it can be applied.Behavior change
This is deliberate and worth calling out. If you set
sandbox={"enabled": True}on a host where the sandbox cannot start, the run previously warned and continued unsandboxed, and now fails instead. The CLI's error names the opt-out:Setting
failIfUnavailable: Falseexplicitly restores the old warn-and-continue behavior. TypeScript SDK users already live under the fail-closed default today.Fix
src/claude_agent_sdk/_internal/transport/subprocess_cli.py: defaultfailIfUnavailabletoTruewhensandbox.enabledisTrueand the caller did not set it. An explicit value is preserved, a sandbox that is not enabled is untouched, and the caller's dict is copied rather than mutated. Theenabled is Truecheck mirrors the TypeScript=== true, so a truthy non-Truevalue is passed through unchanged.src/claude_agent_sdk/types.py: addfailIfUnavailable: boolto theSandboxSettingsTypedDict so an explicit override type-checks.total=False, so this is non-breaking.tests/test_transport.py: regression tests, plus an update totest_build_command_sandbox_minimal, whose pinned assertion encodes the old fail-open payload.Sandboxes enabled only through a
settingsJSON string, with nosandboxoption, get no injection. That matches the TypeScript merge, which returns early when the sandbox option is absent.Tests
test_sandbox_enabled_defaults_to_fail_closedfails onmainand passes with the fix, along with the updatedtest_build_command_sandbox_minimal.test_sandbox_explicit_fail_if_unavailable_is_preserved,test_sandbox_not_enabled_gets_no_fail_if_unavailableandtest_sandbox_options_dict_is_not_mutatedpin the boundaries so the default cannot over-inject.python -m pytest tests/: 1370 passed, 5 skipped, same baseline asmainplus the new tests.python -m ruff check src/ tests/,ruff format --checkandpython -m mypy src/: clean.Used AI assistance; reviewed and tested by me.