Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CLAUDE.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ context.bot_data["security_validator"]

The `can_use_tool` callback (`src/claude/sdk_integration.py`) validates Claude's own tool calls *before* they execute: file paths for `Read`/`Write`/`Edit`/`MultiEdit`/`NotebookEdit`/`NotebookRead` against `APPROVED_DIRECTORY`, and `Bash` commands against the directory boundary.

The SDK consults this callback only when the Claude CLI sends a `can_use_tool` control request, and the CLI resolves allow rules first -- so a tool named in `CLAUDE_ALLOWED_TOOLS` is pre-approved and never reaches the callback. `ClaudeSDKManager.execute_command` therefore strips those guarded tools (`GUARDED_TOOLS`) from the `allowed_tools` it passes to the SDK, and sets `autoAllowBashIfSandboxed` to `False`, whenever a `SecurityValidator` is wired. Without this the checks are silently inert (issue #219). Removing a guarded tool from `CLAUDE_ALLOWED_TOOLS` does not disable it -- the callback allows anything that passes validation.
The SDK consults this callback only when the Claude CLI sends a `can_use_tool` control request, and the CLI resolves allow rules first -- so a tool named in `CLAUDE_ALLOWED_TOOLS` is pre-approved and never reaches the callback. `ClaudeSDKManager.execute_command` therefore strips those guarded tools (`GUARDED_TOOLS`) from the `allowed_tools` it passes to the SDK -- including scoped rules like `Bash(git:*)`, matched by the tool they name -- and sets `autoAllowBashIfSandboxed` to `False`, whenever a `SecurityValidator` is wired. Without this the checks are silently inert (issue #219). Removing a guarded tool from `CLAUDE_ALLOWED_TOOLS` does not disable it -- the callback allows anything that passes validation.

`DISABLE_TOOL_VALIDATION=true` restores the fully permissive behaviour: no `allowed_tools`/`disallowed_tools` are sent, guarded tools are not routed through the callback, and sandboxed Bash is auto-approved. Trusted environments only.

Expand Down
4 changes: 3 additions & 1 deletion SECURITY.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,9 @@ The callback is reactive: the SDK runs it only when the Claude CLI sends a
`can_use_tool` control request, and the CLI resolves its allow rules before asking.
A tool named in `CLAUDE_ALLOWED_TOOLS` is therefore pre-approved and never reaches
the callback. To keep the checks live, `ClaudeSDKManager.execute_command` removes
the guarded tools from the `allowed_tools` it hands the SDK and disables
the guarded tools from the `allowed_tools` it hands the SDK -- scoped rules such as
`Bash(git:*)` or `Read(//etc/**)` included, since a scoped rule pre-approves the
calls it matches just like a bare tool name -- and disables
`autoAllowBashIfSandboxed` (a second bypass, which auto-approves sandboxed Bash
without a control request). Before this was fixed the checks were wired up but
never consulted on a default configuration
Expand Down
20 changes: 18 additions & 2 deletions src/claude/sdk_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,16 @@ def _to_int(value: Any) -> Optional[int]:
_FILE_PATH_KEYS = ("file_path", "path", "notebook_path")


def _allow_rule_tool(rule: str) -> str:
"""Return the tool an allow rule applies to.

A scoped rule such as ``Bash(git:*)`` or ``Read(//etc/**)`` pre-approves
the calls it matches just as a bare ``Bash`` pre-approves every call, so
both must be stripped when the tool has to reach can_use_tool.
"""
return rule.split("(", 1)[0].strip()


def _make_can_use_tool_callback(
security_validator: SecurityValidator,
working_directory: Path,
Expand Down Expand Up @@ -467,6 +477,8 @@ def _stderr_callback(line: str) -> None:
# allowed_tools is therefore pre-approved and its boundary check
# never runs. Strip them so each call is routed to the callback,
# which allows everything that passes validation. Issue #219.
# Scoped rules such as Bash(git:*) are pre-approvals too and are
# stripped by the tool they name.
boundary_checks_active = (
self.security_validator is not None
and not self.config.disable_tool_validation
Expand All @@ -489,10 +501,14 @@ def _stderr_callback(line: str) -> None:
tools_to_strip = tools_to_strip | GUARDED_TOOLS

if tools_to_strip:
gated = [t for t in sdk_allowed_tools if t in tools_to_strip]
gated = [
rule
for rule in sdk_allowed_tools
if _allow_rule_tool(rule) in tools_to_strip
]
if gated:
sdk_allowed_tools = [
tool for tool in sdk_allowed_tools if tool not in tools_to_strip
rule for rule in sdk_allowed_tools if rule not in gated
]
logger.debug(
"Routing guarded tools through can_use_tool",
Expand Down
37 changes: 37 additions & 0 deletions tests/unit/test_claude/test_sdk_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -1484,6 +1484,43 @@ async def test_custom_allowed_tools_are_filtered_too(self, tmp_path):

assert options.allowed_tools == ["Grep"]

@pytest.mark.parametrize(
"rule",
["Bash(git:*)", "Bash(mkdir:*)", "Read(//etc/**)", "Write(*)", "Edit(src/**)"],
)
async def test_scoped_rules_for_guarded_tools_are_stripped(self, tmp_path, rule):
"""A scoped rule pre-approves the calls it matches, skipping the
boundary check exactly like a bare tool name would."""
manager = ClaudeSDKManager(
self._config(tmp_path, claude_allowed_tools=[rule, "Grep", "Grep(foo)"]),
security_validator=self._validator(tmp_path),
)
options = await self._capture(manager, tmp_path)

assert options.allowed_tools == ["Grep", "Grep(foo)"]

async def test_scoped_rules_for_approval_gated_tools_are_stripped(self, tmp_path):
"""The approval prompt has the same bypass for a scoped rule."""
manager = ClaudeSDKManager(
self._config(
tmp_path,
claude_allowed_tools=["WebFetch(domain:example.com)", "Grep"],
interactive_tool_approval=True,
interactive_tool_approval_tools=["WebFetch"],
)
)
options = await self._capture(manager, tmp_path)

assert options.allowed_tools == ["Grep"]

async def test_scoped_rules_kept_without_security_validator(self, tmp_path):
manager = ClaudeSDKManager(
self._config(tmp_path, claude_allowed_tools=["Bash(git:*)", "Grep"])
)
options = await self._capture(manager, tmp_path)

assert options.allowed_tools == ["Bash(git:*)", "Grep"]

async def test_config_allowed_tools_not_mutated(self, tmp_path):
"""Filtering builds a new list; the Settings value is left intact."""
config = self._config(tmp_path)
Expand Down
Loading