Skip to content

Build and run agents you can see, understand and trust.

fix(tool): check every sed -e expression against the denylist (#2747)

lihongyuan9928fea7fd3ccf
diff --git a/src/agentscope/tool/_builtin/_bash_parser.py b/src/agentscope/tool/_builtin/_bash_parser.py
index 95fc65e0a..ec094f9fe 100644
--- a/src/agentscope/tool/_builtin/_bash_parser.py
+++ b/src/agentscope/tool/_builtin/_bash_parser.py
@@ -729,8 +729,15 @@ class BashCommandParser:
while i < len(args):
arg = args[i]
+ # -e/--expression takes the next token as an expression, so it
+ # must be matched before the combined short flags below
+ if arg in ("-e", "--expression"):
+ if i + 1 < len(args):
+ expressions.append(args[i + 1])
+ found_first_expr = True
+ i += 1
# Handle flags
- if arg.startswith("-") and not arg.startswith("--"):
+ elif arg.startswith("-") and not arg.startswith("--"):
# Combined flags like -nE
flag_chars = arg[1:]
for char in flag_chars:
@@ -757,10 +764,6 @@ class BashCommandParser:
and "." not in next_arg
):
i += 1
- elif arg in ["-e", "--expression"]:
- if i + 1 < len(args):
- expressions.append(args[i + 1])
- i += 1
elif not arg.startswith("-"):
# First non-flag, non-option arg is expression (if no -e used)
if not found_first_expr:
diff --git a/tests/permission_bash_parser_test.py b/tests/permission_bash_parser_test.py
index 3899af451..065dfa120 100644
--- a/tests/permission_bash_parser_test.py
+++ b/tests/permission_bash_parser_test.py
@@ -898,6 +898,29 @@ class BashParserSedConstraintsTest(IsolatedAsyncioTestCase):
self.assertIsNotNone(result)
self.assertIn(expected_substring, result)
+ async def test_denylist_every_e_expression(self) -> None:
+ """Test denylist: every -e expression is checked, not only the 1st."""
+ self.assertEqual(
+ self.parser.check_sed_constraints(
+ "sed -e 's/a/b/' -e '/x/w /tmp/out' f",
+ self.dangerous_files,
+ ),
+ "sed write operation (w/W) not allowed",
+ )
+ self.assertEqual(
+ self.parser.check_sed_constraints(
+ "sed -e 's/a/b/' -e '1e id' f",
+ self.dangerous_files,
+ ),
+ "sed expression '1e id' not in allowlist",
+ )
+ self.assertIsNone(
+ self.parser.check_sed_constraints(
+ "sed -e 's/a/b/' -e 's/x/y/g' f",
+ self.dangerous_files,
+ ),
+ )
+
async def test_denylist_execute_operations(self) -> None:
"""Test denylist: execute operations (e/E)."""
test_cases = [