Build and run agents you can see, understand and trust.
Python agentchatbotlarge-language-modelsllmllm-agentmcpmulti-agentmulti-modalreact-agentrealtime-agentvoice-agentvoice-assistant
fix(tool): judge sed long options that carry an inline value (#2795)
sxh083cbd1975c7
diff --git a/src/agentscope/tool/_builtin/_bash_parser.py b/src/agentscope/tool/_builtin/_bash_parser.pyindex ec094f9fe..52cda546c 100644--- a/src/agentscope/tool/_builtin/_bash_parser.py+++ b/src/agentscope/tool/_builtin/_bash_parser.py@@ -728,16 +728,30 @@ class BashCommandParser:while i < len(args):arg = args[i]+ # A GNU long option may carry its value right after '=', which+ # keeps option name and value in one token. Split them, or the+ # value -- a sed script, a backup suffix -- vanishes from the+ # analysis together with the option that introduced it.+ name, assigned, inline = (+ arg.partition("=") if arg.startswith("--") else (arg, "", "")+ )- # -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):+ # -e/--expression takes a sed script as its value, so it must be+ # matched before the combined short flags below+ if name in ("-e", "--expression"):+ if assigned:+ expressions.append(inline)+ found_first_expr = True+ elif i + 1 < len(args):expressions.append(args[i + 1])found_first_expr = Truei += 1+ # --file is the long spelling of -f, which the flag allowlist+ # below rejects: an expression read from a file is never seen here.+ elif name == "--file":+ flags.append("f")# Handle flags- elif arg.startswith("-") and not arg.startswith("--"):+ elif name.startswith("-") and not name.startswith("--"):# Combined flags like -nEflag_chars = arg[1:]for char in flag_chars:@@ -754,9 +768,11 @@ class BashCommandParser:and "." not in next_arg):i += 1 # Skip backup extension- elif arg == "--in-place":+ elif name == "--in-place":flags.append("i")- if i + 1 < len(args):+ # Written with '=', the backup suffix already came along in+ # this token; only the separated form can take the next one.+ if not assigned and i + 1 < len(args):next_arg = args[i + 1]if (not next_arg.startswith("-")diff --git a/tests/permission_bash_parser_test.py b/tests/permission_bash_parser_test.pyindex 065dfa120..c179408d5 100644--- a/tests/permission_bash_parser_test.py+++ b/tests/permission_bash_parser_test.py@@ -921,6 +921,60 @@ class BashParserSedConstraintsTest(IsolatedAsyncioTestCase):),)+ async def test_long_option_with_inline_value(self) -> None:+ """Test long options that carry their value after an '='.++ ``shlex`` keeps such an option and its value in one token, so a value+ that is a sed script, a backup suffix or a script file has to be split+ off before the option is recognised -- otherwise both disappear from+ the analysis and the command they belong to goes unchecked.+ """+ cases = [+ (+ "sed -n --expression='/bin/sh/e' -e '1p' notes.txt",+ "sed -n -e '1p' -e '/bin/sh/e' notes.txt",+ "sed execute operation (e/E) not allowed",+ ),+ (+ "sed --in-place=.bak 's/x/y/' .env",+ "sed -i 's/x/y/' .env",+ "sed -i modifying dangerous file: .env",+ ),+ (+ "sed --file=script.sed 's/x/y/' notes.txt",+ "sed -f script.sed 's/x/y/' notes.txt",+ "sed flag -f not allowed",+ ),+ (+ "sed --expression='/bin/sh/e' notes.txt",+ "sed --expression '/bin/sh/e' notes.txt",+ "sed execute operation (e/E) not allowed",+ ),+ (+ # An unchecked long option leaves the *filename* to be judged+ # as the expression, so plain line printing is flagged too.+ "sed -n --expression='5p' notes.txt",+ "sed -n '5p' notes.txt",+ None,+ ),+ ]+ for inline, separated, expected in cases:+ with self.subTest(inline=inline):+ self.assertEqual(+ self.parser.check_sed_constraints(+ separated,+ self.dangerous_files,+ ),+ expected,+ )+ self.assertEqual(+ self.parser.check_sed_constraints(+ inline,+ self.dangerous_files,+ ),+ expected,+ )+async def test_denylist_execute_operations(self) -> None:"""Test denylist: execute operations (e/E)."""test_cases = [