String values silently disable tool restrictions and expand add_dirs in ClaudeAgentOptions
TITLE: String values silently disable tool restrictions and expand add_dirs in ClaudeAgentOptions
BODY:
Thanks for the SDK. While testing how ClaudeAgentOptions values reach the CLI, I found that list-typed options accept a plain str and turn it into a different effective configuration without any error or warning. For restriction-style options this weakens the restriction the caller meant to set. skills already rejects non-list values (_reject_non_list_skills), but the options below have no such guard.
This is an input-validation / hardening report, not a claim of a security vulnerability: it requires the caller to pass the wrong type.
Tested with claude-agent-sdk 0.2.185 (bundled CLI 2.1.328), Python 3.13, Linux. The tool call comes from a scripted local Messages API (no real model, no real API key).
Mechanisms (they differ per option)
disallowed_tools="Bash"is iterated as characters and joined asB,a,s,h.add_dirs="/tmp/allowed"is iterated as characters, so/(and single letters) become--add-direntries.tools="Read"is not iterated. Any non-list value takes the "preset object" branch, which emits--tools default, i.e. every default tool.allowed_tools="Read"becomesR,e,a,d. I only checked the generated arguments here; I do not claim an access change (it is a pre-approval list, not an exclusive tool list). Listed for completeness of the validation.
Command-level (no API needed):
from claude_agent_sdk import ClaudeAgentOptions
from claude_agent_sdk._internal.transport.subprocess_cli import SubprocessCLITransport as T
def cmd(**kw):
t = T(prompt="x", options=ClaudeAgentOptions(**kw))
t._cli_path = "/bin/claude"
return t._build_command()
cmd(disallowed_tools="Bash") # ... '--disallowedTools', 'B,a,s,h'
cmd(add_dirs="/tmp/allowed") # ... '--add-dir','/', '--add-dir','t', '--add-dir','m', ...
cmd(tools="Read") # ... '--tools', 'default'
cmd(allowed_tools="Read") # ... '--allowedTools', 'R,e,a,d'
End to end (observed effect)
The scripted model asks for one tool call. Each pair differs only in list vs. string.
| Option | Input | Result |
|---|---|---|
| disallowed_tools | ["Bash"] | Bash blocked ("No such tool available: Bash") |
| disallowed_tools | "Bash" | Bash executed (marker file written) |
| tools | ["Read"] | Bash blocked |
| tools | "Read" | Bash executed |
| add_dirs | ["/tmp/oa/allowed"] | Write to /tmp/oa/forbidden/pwn.txt refused (permission) |
| add_dirs | "/tmp/oa/allowed" | Write to /tmp/oa/forbidden/pwn.txt succeeded |
The first four rows used permission_mode="bypassPermissions" (deny rules still apply there, as the list-form control shows). The add_dirs rows used permission_mode="acceptEdits". For add_dirs I only claim that the effective directory scope changes in that mode; I did not test the default mode or an OS-level sandbox.
Suggested fix
Reuse the skills approach: raise TypeError at connect time when tools, allowed_tools, disallowed_tools or add_dirs receive a str, with the same "Did you mean [...]?" hint (tools must still accept the preset dict). Valid lists and presets should keep working unchanged.
Related but different: #1374 (delimiters inside list entries) and #1542 (dict hook entries).
Happy to send tests or a PR if useful.
Prepared with AI assistance; every output above comes from commands I ran against the versions listed.
I'd like to work on this — please assign it to me.