test: consolidate repetitive cases across the suite

This commit is contained in:
chengyongru authored and chengyongru committed 2026-09-25 14:28:21 +08:00
1 parent c406024b59
commit 6eccc2859a
31 files changed
+745 -1315

No files matched your search

+76 -85
View File
@@ -2,6 +2,8 @@
from __future__ import annotations
import pytest
from nanobot.agent.tools.shell import ExecTool
@@ -13,19 +15,84 @@ def test_deny_patterns_block_rm_rf():
assert "deny pattern filter" in result.lower()
def test_allow_patterns_bypass_deny():
"""allow_patterns take priority: matching command skips deny check."""
tool = ExecTool(allow_patterns=[r"rm\s+-rf\s+/tmp/.*"])
result = tool._guard_command("rm -rf /tmp/build", "/tmp")
@pytest.mark.parametrize(
"command, allow_pattern",
[
pytest.param("rm -rf /tmp/build", "rm\\s+-rf\\s+/tmp/.*", id="allow_patterns_bypass_deny"),
pytest.param(
"echo allowlisted 2>&1",
"echo\\s+allowlisted\\s+2>&1",
id="guard_allow_patterns_keep_fd_redirection_ampersand",
),
pytest.param(
"rm -rf /tmp/build",
"rm\\s+-rf\\s+/tmp/build",
id="allow_patterns_fullmatch_allows_exact_command",
),
pytest.param(
"echo allowlisted",
"\\becho\\s+allowlisted\\b",
id="guard_allow_patterns_allow_single_matching_segment",
),
pytest.param(
"echo allowlisted",
"^echo\\s+allowlisted$",
id="guard_allow_patterns_keep_fullmatch_style_compatibility",
),
],
)
def test_allow_patterns_accept_matching_commands(command, allow_pattern):
tool = ExecTool(allow_patterns=[allow_pattern])
result = tool._guard_command(command, "/tmp")
assert result is None
def test_allow_patterns_must_match_to_bypass():
"""Non-matching allow_patterns do NOT bypass deny."""
tool = ExecTool(allow_patterns=[r"rm\s+-rf\s+/opt/"])
result = tool._guard_command("rm -rf /tmp/build", "/tmp")
@pytest.mark.parametrize(
"command, reason, allow_pattern",
[
pytest.param(
"rm -rf /tmp/build",
"deny pattern filter",
"rm\\s+-rf\\s+/opt/",
id="allow_patterns_must_match_to_bypass",
),
pytest.param(
"echo allowlisted && touch /tmp/evil",
"allowlist",
"\\becho\\s+allowlisted\\b",
id="guard_allow_patterns_block_non_matching_chained_segment",
),
pytest.param(
"echo allowlisted & touch /tmp/evil",
"allowlist",
"echo\\s+allowlisted.*",
id="guard_allow_patterns_block_single_ampersand_chained_segment",
),
pytest.param(
"echo allowlisted\ntouch /tmp/evil",
"allowlist",
"echo\\s+allowlisted\\s*.*",
id="guard_allow_patterns_block_newline_chained_segment",
),
pytest.param(
"echo allowlisted\nrm -rf /",
"deny pattern filter",
"echo\\s+allowlisted\\s*.*",
id="guard_newline_chained_segment_still_hits_deny_patterns",
),
pytest.param(
"echo allowlisted &",
"allowlist",
"echo\\s+allowlisted",
id="guard_allow_patterns_preserve_trailing_background_operator",
),
],
)
def test_allow_patterns_do_not_bypass_rejected_commands(command, reason, allow_pattern):
tool = ExecTool(allow_patterns=[allow_pattern])
result = tool._guard_command(command, "/tmp")
assert result is not None
assert "deny pattern filter" in result.lower()
assert reason in result.lower()
def test_extra_deny_patterns_from_config():
@@ -58,42 +125,6 @@ def test_allow_patterns_is_whitelist_only():
assert "allowlist" in result.lower()
def test_guard_allow_patterns_block_non_matching_chained_segment():
"""Every top-level shell segment must match an allow pattern."""
tool = ExecTool(allow_patterns=[r"\becho\s+allowlisted\b"])
result = tool._guard_command("echo allowlisted && touch /tmp/evil", "/tmp")
assert result is not None
assert "allowlist" in result.lower()
def test_guard_allow_patterns_block_single_ampersand_chained_segment():
"""A backgrounded command is also a top-level shell segment."""
tool = ExecTool(allow_patterns=[r"echo\s+allowlisted.*"])
result = tool._guard_command("echo allowlisted & touch /tmp/evil", "/tmp")
assert result is not None
assert "allowlist" in result.lower()
def test_guard_allow_patterns_block_newline_chained_segment():
"""A newline separates commands, so each line must match on its own."""
tool = ExecTool(allow_patterns=[r"echo\s+allowlisted\s*.*"])
result = tool._guard_command("echo allowlisted\ntouch /tmp/evil", "/tmp")
assert result is not None
assert "allowlist" in result.lower()
def test_guard_newline_chained_segment_still_hits_deny_patterns():
"""An allowlisted first line does not exempt a denied later line."""
tool = ExecTool(allow_patterns=[r"echo\s+allowlisted\s*.*"])
result = tool._guard_command("echo allowlisted\nrm -rf /", "/tmp")
assert result is not None
assert "deny pattern filter" in result.lower()
def test_split_shell_segments_keep_line_continuation_intact():
"""A backslash-escaped newline continues one command, not a new segment."""
assert ExecTool._split_shell_segments("echo allowlisted \\\nextra") == [
@@ -101,23 +132,6 @@ def test_split_shell_segments_keep_line_continuation_intact():
]
def test_guard_allow_patterns_preserve_trailing_background_operator():
tool = ExecTool(allow_patterns=[r"echo\s+allowlisted"])
result = tool._guard_command("echo allowlisted &", "/tmp")
assert result is not None
assert "allowlist" in result.lower()
def test_guard_allow_patterns_keep_fd_redirection_ampersand():
tool = ExecTool(allow_patterns=[r"echo\s+allowlisted\s+2>&1"])
result = tool._guard_command("echo allowlisted 2>&1", "/tmp")
assert result is None
def test_deny_patterns_search_original_command_with_quoted_hash():
"""Deny checks must still inspect text after a quoted hash."""
tool = ExecTool(deny_patterns=[r"\brm\s+-rf\s+/"])
@@ -126,21 +140,6 @@ def test_deny_patterns_search_original_command_with_quoted_hash():
assert "deny pattern filter" in result.lower()
def test_allow_patterns_fullmatch_allows_exact_command():
"""A full-command allow pattern can still exempt an exact denied command."""
tool = ExecTool(allow_patterns=[r"rm\s+-rf\s+/tmp/build"])
result = tool._guard_command("rm -rf /tmp/build", "/tmp")
assert result is None
def test_guard_allow_patterns_allow_single_matching_segment():
tool = ExecTool(allow_patterns=[r"\becho\s+allowlisted\b"])
result = tool._guard_command("echo allowlisted", "/tmp")
assert result is None
def test_guard_allow_patterns_allow_multiple_matching_segments():
tool = ExecTool(
allow_patterns=[
@@ -152,11 +151,3 @@ def test_guard_allow_patterns_allow_multiple_matching_segments():
result = tool._guard_command("echo allowlisted && echo also_allowed", "/tmp")
assert result is None
def test_guard_allow_patterns_keep_fullmatch_style_compatibility():
tool = ExecTool(allow_patterns=[r"^echo\s+allowlisted$"])
result = tool._guard_command("echo allowlisted", "/tmp")
assert result is None