mirror of
https://github.com/bytedance/deer-flow.git
synced 2026-06-18 13:46:02 +00:00
fix(sandbox): stop flagging string-literal path fragments as unsafe absolute paths (#3623)
* fix(sandbox): stop flagging string-literal path fragments as unsafe paths
The host-bash absolute-path guard scans the raw command string, so /segment
sequences inside string literals, f-strings, and templates were treated as
absolute path arguments and rejected — e.g. python -c "print(f'/端口{port}')"
or a REST template /devices/{id}/port. Whether a fragment tripped the guard
depended on the character right before the slash (a word char suppressed the
match), so the same literal could pass or fail unpredictably, pushing the model
into retry loops that bloat context and wall-clock time.
Exempt matches carrying non-ASCII characters or format braces: real host paths
a command would open contain neither, so these are text, not paths. The guard
is best-effort (not a security boundary), and plain ASCII host paths like
/etc/passwd — including ones written inside a code string such as
open('/etc/passwd') — stay rejected.
* fix(sandbox): only exempt identifier-template braces, not bash brace expansion
The literal-fragment exemption exempted any path fragment containing { or },
which let bash brace expansion (cat /etc/{passwd,shadow}) and ${VAR} expansion
reconstitute real host paths past validate_local_bash_command_paths. Tighten
the brace branch to only exempt fragments where every {...} block is a single
identifier-like placeholder (/devices/{id}/port, f-string /{port}); reject
${VAR} shell-variable expansion. Add parametrized regression tests for the
brace-expansion and shell-var bypasses.
This commit is contained in:
@@ -445,6 +445,54 @@ def test_validate_local_bash_command_paths_allows_http_url_dotdot_segments() ->
|
||||
)
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"command",
|
||||
[
|
||||
# f-string / string-literal fragments with CJK text or template braces are
|
||||
# NOT path arguments and must not be flagged as unsafe absolute paths.
|
||||
"python3 -c \"print(f'/端口{port}')\"",
|
||||
"echo '健康检查 /端口 状态'",
|
||||
"python3 -c \"x = f'/{port}'\"",
|
||||
"python3 -c \"print('/devices/{id}/port')\"",
|
||||
],
|
||||
)
|
||||
def test_validate_local_bash_command_paths_allows_non_path_string_literals(command: str) -> None:
|
||||
validate_local_bash_command_paths(command, _THREAD_DATA)
|
||||
|
||||
|
||||
def test_validate_local_bash_command_paths_still_blocks_ascii_host_path_in_code() -> None:
|
||||
"""The literal exemption is shape-based (non-ASCII / identifier-template
|
||||
braces); a plain ASCII host path stays blocked even when written inside a
|
||||
code string, so the guard keeps nudging the model toward virtual paths."""
|
||||
with pytest.raises(PermissionError, match="Unsafe absolute paths"):
|
||||
validate_local_bash_command_paths("python3 -c \"open('/etc/passwd').read()\"", _THREAD_DATA)
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"command",
|
||||
[
|
||||
# Bash brace expansion reconstitutes plain host paths at runtime
|
||||
# (`cat /etc/{passwd,shadow}` -> `cat /etc/passwd /etc/shadow`), so the
|
||||
# brace exemption must NOT fire on these — only single identifier-like
|
||||
# template placeholders such as `/devices/{id}/port` are text.
|
||||
"cat /etc/{passwd,shadow}",
|
||||
"cat /etc/passwd{,.bak}",
|
||||
"cat /{etc,var}/passwd",
|
||||
'bash -c "cat /etc/{passwd,shadow}"',
|
||||
# ``${VAR}`` shell variable expansion is the same bypass class: bash
|
||||
# substitutes a real host path at runtime even though `USER` is
|
||||
# identifier-shaped, so it must stay blocked too.
|
||||
"cat /home/${USER}/.ssh/id_rsa",
|
||||
],
|
||||
)
|
||||
def test_validate_local_bash_command_paths_blocks_brace_expansion_host_paths(command: str) -> None:
|
||||
"""Regression for the brace-expansion bypass: a `{...}` block that is not a
|
||||
single identifier placeholder (commas, dots, leading separators) must keep
|
||||
the host path blocked rather than be exempted as a literal."""
|
||||
with pytest.raises(PermissionError, match="Unsafe absolute paths"):
|
||||
validate_local_bash_command_paths(command, _THREAD_DATA)
|
||||
|
||||
|
||||
def test_bash_tool_rejects_host_bash_when_local_sandbox_default(monkeypatch) -> None:
|
||||
runtime = SimpleNamespace(
|
||||
state={"sandbox": {"sandbox_id": "local"}, "thread_data": _THREAD_DATA.copy()},
|
||||
|
||||
Reference in New Issue
Block a user