Skip to content

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

fix(tool): parse attached sed short-option values (#2824)

wangtao5e6587b36fc8
diff --git a/src/agentscope/tool/_builtin/_bash_parser.py b/src/agentscope/tool/_builtin/_bash_parser.py
index 52cda546c..8f40526ab 100644
--- a/src/agentscope/tool/_builtin/_bash_parser.py
+++ b/src/agentscope/tool/_builtin/_bash_parser.py
@@ -752,22 +752,39 @@ class BashCommandParser:
flags.append("f")
# Handle flags
elif name.startswith("-") and not name.startswith("--"):
- # Combined flags like -nE
- flag_chars = arg[1:]
- for char in flag_chars:
+ # Parse combined flags while respecting options whose value
+ # may be attached to the same token, e.g. -ne'5p',
+ # -e's/a/b/' and -i.bak.
+ flag_chars = name[1:]
+ for index, char in enumerate(flag_chars):
flags.append(char)
- # -i flag may have optional backup extension argument
- # But only skip if next arg doesn't look like an expression
- if "i" in flag_chars and i + 1 < len(args):
+ remainder = flag_chars[index + 1 :]
+ if char == "e":
+ if remainder:
+ expressions.append(remainder)
+ found_first_expr = True
+ elif i + 1 < len(args):
+ expressions.append(args[i + 1])
+ found_first_expr = True
+ i += 1
+ break
+ if char in ("f", "i"):
+ # -f consumes a script filename and -i consumes an
+ # optional backup suffix. Neither remainder contains
+ # more flags.
+ break
+
+ # A separated -i backup suffix retains the existing
+ # platform-compatible handling. Attached suffixes were
+ # consumed as the remainder above.
+ if flag_chars.endswith("i") and i + 1 < len(args):
next_arg = args[i + 1]
- # Skip backup extension only if it's not an expression
- # or file
if (
not next_arg.startswith("-")
and not next_arg.startswith("s")
and "." not in next_arg
):
- i += 1 # Skip backup extension
+ i += 1
elif name == "--in-place":
flags.append("i")
# Written with '=', the backup suffix already came along in
diff --git a/tests/permission_bash_parser_test.py b/tests/permission_bash_parser_test.py
index c179408d5..002b23b82 100644
--- a/tests/permission_bash_parser_test.py
+++ b/tests/permission_bash_parser_test.py
@@ -975,6 +975,52 @@ class BashParserSedConstraintsTest(IsolatedAsyncioTestCase):
expected,
)
+ async def test_short_option_with_attached_value(self) -> None:
+ """Attached short-option values match their separated forms."""
+ cases = [
+ (
+ "sed -e's/a/b/' notes.txt",
+ "sed -e 's/a/b/' notes.txt",
+ None,
+ ),
+ (
+ "sed -ne'5p' notes.txt",
+ "sed -n -e '5p' notes.txt",
+ None,
+ ),
+ (
+ "sed -e'/bin/sh/e' notes.txt",
+ "sed -e '/bin/sh/e' notes.txt",
+ "sed execute operation (e/E) not allowed",
+ ),
+ (
+ "sed -i.bak 's/x/y/' .env",
+ "sed --in-place=.bak 's/x/y/' .env",
+ "sed -i modifying dangerous file: .env",
+ ),
+ (
+ "sed -fscript.sed 's/x/y/' notes.txt",
+ "sed -f script.sed 's/x/y/' notes.txt",
+ "sed flag -f not allowed",
+ ),
+ ]
+ for attached, separated, expected in cases:
+ with self.subTest(attached=attached):
+ self.assertEqual(
+ self.parser.check_sed_constraints(
+ separated,
+ self.dangerous_files,
+ ),
+ expected,
+ )
+ self.assertEqual(
+ self.parser.check_sed_constraints(
+ attached,
+ self.dangerous_files,
+ ),
+ expected,
+ )
+
async def test_denylist_execute_operations(self) -> None:
"""Test denylist: execute operations (e/E)."""
test_cases = [