Skip to content

JiuwenSwarm is an intelligent AI Agent built on openJiuwen. It extends the powerful capabilities of large language models directly to your fingertips through various communication apps you use daily.

Python

fix: 修复bash工具执行复合命令权限校验问题

ZhibinHand8bc36646f52
diff --git a/jiuwenclaw/agentserver/permissions/core.py b/jiuwenclaw/agentserver/permissions/core.py
index 33c0f1857d..cec18477ce 100644
--- a/jiuwenclaw/agentserver/permissions/core.py
+++ b/jiuwenclaw/agentserver/permissions/core.py
@@ -24,6 +24,7 @@ from jiuwenclaw.agentserver.permissions.models import (
)
from jiuwenclaw.agentserver.permissions.tiered_policy import (
evaluate_tiered_policy,
+ matched_rule_allows_legacy_shell_operator_escalation,
matched_rule_uses_approval_override,
maybe_escalate_shell_operators,
permissions_schema_is_tiered_policy,
@@ -104,7 +105,10 @@ class PermissionEngine:
if matched_rule == "tiered_policy:fallback(no_config)":
permission = None
matched_rule = None
- elif not matched_rule_uses_approval_override(matched_rule):
+ elif (
+ not matched_rule_uses_approval_override(matched_rule)
+ and matched_rule_allows_legacy_shell_operator_escalation(matched_rule)
+ ):
permission = maybe_escalate_shell_operators(tool_name, tool_args, permission)
else:
permission, matched_rule = self._tool_checker.check_tool(tool_name, tool_args, channel_id)
diff --git a/jiuwenclaw/agentserver/permissions/tiered_policy.py b/jiuwenclaw/agentserver/permissions/tiered_policy.py
index 219878e1b5..474e696093 100644
--- a/jiuwenclaw/agentserver/permissions/tiered_policy.py
+++ b/jiuwenclaw/agentserver/permissions/tiered_policy.py
@@ -49,6 +49,7 @@ _BUILTIN_RULES_CACHE: tuple[str, float, list[dict[str, Any]]] | None = None
_MR = "tiered_policy"
_APPROVAL_OVERRIDES_PREFIX = f"{_MR}:approval_overrides"
+_SHELL_SUBCOMMANDS_PREFIX = f"{_MR}:shell_subcommands"
@dataclass(frozen=True)
@@ -632,6 +633,15 @@ def maybe_escalate_shell_operators(
return permission
+def matched_rule_allows_legacy_shell_operator_escalation(matched_rule: str | None) -> bool:
+ """当前结果是否仍允许走旧版 shell operator escalation 兼容逻辑。"""
+ if not isinstance(matched_rule, str):
+ return True
+ if matched_rule_uses_approval_override(matched_rule):
+ return False
+ return not matched_rule.startswith(_SHELL_SUBCOMMANDS_PREFIX)
+
+
def matched_rule_uses_approval_override(matched_rule: str | None) -> bool:
"""当前结果是否来自 approval_overrides。"""
if not isinstance(matched_rule, str):
diff --git a/tests/unit_tests/agentserver/test_tiered_policy_permissions.py b/tests/unit_tests/agentserver/test_tiered_policy_permissions.py
index d6c110d613..7f1ecb96ae 100644
--- a/tests/unit_tests/agentserver/test_tiered_policy_permissions.py
+++ b/tests/unit_tests/agentserver/test_tiered_policy_permissions.py
@@ -24,6 +24,7 @@ from jiuwenclaw.agentserver.permissions.suggestions import build_shell_permissio
from jiuwenclaw.agentserver.permissions.tiered_policy import (
collect_builtin_permission_rail_tool_names,
evaluate_tiered_policy,
+ matched_rule_allows_legacy_shell_operator_escalation,
maybe_escalate_shell_operators,
permissions_schema_is_tiered_policy,
severity_to_decision,
@@ -171,6 +172,12 @@ def test_create_terminal_operator_chain_escalates_allow_to_ask():
) == PermissionLevel.ASK
+def test_shell_subcommand_matched_rule_disables_legacy_operator_escalation():
+ assert not matched_rule_allows_legacy_shell_operator_escalation(
+ "tiered_policy:shell_subcommands:ls=>tiered_policy:rules:rules[allow_ls]"
+ )
+
+
def test_shell_ast_fallback_keeps_simple_command(monkeypatch):
shell_ast_module = importlib.import_module("jiuwenclaw.agentserver.permissions.shell_ast")
monkeypatch.setattr(shell_ast_module, "_TREE_SITTER_BASH_READY", False)
@@ -567,6 +574,53 @@ def test_evaluate_global_policy_directly_uses_tiered_policy_even_when_disabled()
assert "rules" in mr
+def test_evaluate_global_policy_directly_keeps_allow_for_ast_simple_subcommands(monkeypatch):
+ tiered_policy_module = importlib.import_module("jiuwenclaw.agentserver.permissions.tiered_policy")
+ monkeypatch.setattr(
+ tiered_policy_module,
+ "parse_shell_for_permission",
+ lambda _command: ShellAstParseResult(
+ kind="simple",
+ subcommands=(
+ ShellSubcommand(text="ls"),
+ ShellSubcommand(text="echo hello"),
+ ),
+ flags=ShellStructureFlags(has_compound_operators=True, has_actual_operator_nodes=True, operators=("||",)),
+ backend="test",
+ ),
+ )
+
+ engine = PermissionEngine({
+ "schema": "tiered_policy",
+ "permission_mode": "normal",
+ "defaults": {"*": "ask"},
+ "tools": {"bash": "ask"},
+ "rules": [
+ {
+ "id": "allow_ls",
+ "tools": ["bash"],
+ "pattern": "ls",
+ "severity": "LOW",
+ },
+ {
+ "id": "allow_echo",
+ "tools": ["bash"],
+ "pattern": "echo hello",
+ "severity": "LOW",
+ },
+ ],
+ })
+
+ perm, mr = engine.evaluate_global_policy_directly(
+ "bash",
+ {"command": "ls || echo hello"},
+ include_external_directory=False,
+ )
+
+ assert perm == PermissionLevel.ALLOW
+ assert mr.startswith("tiered_policy:shell_subcommands:")
+
+
def test_owner_scope_global_level_sees_approval_overrides(tmp_path, monkeypatch):
monkeypatch.setenv("HOME", str(tmp_path))
monkeypatch.setenv("JIUWENCLAW_CONFIG_DIR", str(tmp_path))