Skip to content

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

fix(tool): treat a separated dotted sed backup suffix as a suffix (#3031)

ffffff55c3249be863a6
diff --git a/src/agentscope/tool/_builtin/_bash_parser.py b/src/agentscope/tool/_builtin/_bash_parser.py
index 30635e939..23c41be9a 100644
--- a/src/agentscope/tool/_builtin/_bash_parser.py
+++ b/src/agentscope/tool/_builtin/_bash_parser.py
@@ -788,10 +788,11 @@ class BashCommandParser:
# consumed as the remainder above.
if flag_chars.endswith("i") and i + 1 < len(args):
next_arg = args[i + 1]
+ # A sed script never starts with a dot, e.g. BSD ".bak"
if (
not next_arg.startswith("-")
and not next_arg.startswith("s")
- and "." not in next_arg
+ and (next_arg.startswith(".") or "." not in next_arg)
):
i += 1
elif name == "--in-place":
@@ -803,7 +804,7 @@ class BashCommandParser:
if (
not next_arg.startswith("-")
and not next_arg.startswith("s")
- and "." not in next_arg
+ and (next_arg.startswith(".") or "." not in next_arg)
):
i += 1
elif not arg.startswith("-"):
diff --git a/tests/permission_bash_parser_test.py b/tests/permission_bash_parser_test.py
index 458102251..3c493a74d 100644
--- a/tests/permission_bash_parser_test.py
+++ b/tests/permission_bash_parser_test.py
@@ -1045,6 +1045,45 @@ class BashParserSedConstraintsTest(IsolatedAsyncioTestCase):
expected,
)
+ async def test_separated_backup_suffix(self) -> None:
+ """A separated dotted -i suffix is a backup suffix, not a script."""
+ cases = [
+ (
+ "sed -i .bak 's/x/y/' notes.txt",
+ None,
+ ),
+ (
+ "sed -i .orig 's/x/y/' notes.txt",
+ None,
+ ),
+ (
+ "sed --in-place .bak 's/x/y/' notes.txt",
+ None,
+ ),
+ (
+ "sed -i .bak 's/x/y/' .env",
+ "sed -i modifying dangerous file: .env",
+ ),
+ (
+ "sed -i bak 's/x/y/' notes.txt",
+ None,
+ ),
+ (
+ # A dot inside a script keeps the token unconsumed.
+ "sed -i '/example.com/d' notes.txt",
+ "sed expression '/example.com/d' not in allowlist",
+ ),
+ ]
+ for cmd, expected in cases:
+ with self.subTest(cmd=cmd):
+ self.assertEqual(
+ self.parser.check_sed_constraints(
+ cmd,
+ self.dangerous_files,
+ ),
+ expected,
+ )
+
async def test_denylist_execute_operations(self) -> None:
"""Test denylist: execute operations (e/E)."""
test_cases = [