Repository navigation
fix(tools): prevent shell command whitelist bypass - #3286
Conversation
Require approval for shell operators, substitutions, and variable expansion before executing command strings. Correct platform quoting and escaping, reject ambiguous executable prefixes, and require relative executables to match the whitelist. Add regression tests and update the English and Chinese tool guides. Validation: focused core verify passed with 75 tests passing and one Unix-only test skipped, including Spotless, packaging, and Javadoc. The full core suite ran 2532 tests with no assertion failures; two existing symlink tests errored due to Windows permissions, and nine tests were skipped. BREAKING CHANGE: Empty command whitelists now require explicit approval. Applications using the default ShellCommandTool constructor must configure an allow-list or supply an approval callback to enable execution. Fixes agentscope-ai#3285
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
Exercise separator detection, quoted executable boundaries, and incomplete shell syntax. Cover all changed validator lines and branches with focused regression tests. Validation: 94 tests passed; one Unix-only test skipped on Windows. Maven verify, Spotless, and git diff --check passed. Refs agentscope-ai#3285
oss-maintainer
left a comment
There was a problem hiding this comment.
Summary
Security fix for ShellCommandTool: shell operators/expansions and an empty whitelist no longer slip past the whitelist as auto-executable, both validators are rewritten around a shared containsUnsafeShellSyntax(command, checkExpansions) helper, and the reject path is routed through the approval callback (fail-closed when no callback is set). Verified against main@3c1c29c0: requestUserApproval() already maps a null callback / null / false / thrown exception to a SecurityError, so every newly rejected shape stops executing rather than falling through — the design is sound and the 76 added cases are unusually thorough for a first contribution.
The findings are about pinning down two undocumented parser assumptions (cmd quote semantics at WindowsCommandValidator:261, intentional asymmetry at :232) and closing the false-positive/migration-doc gap; none of them asks you to weaken a check.
Findings
- [Warning]
WindowsCommandValidator.java:261—< > ( )(and& |) are only flagged outside double quotes; the cmd quote-suppression assumption should be commented and pinned by a Windows-gated test. - [Warning]
WindowsCommandValidator.java:226— any%/!at any position requires approval (echo 50% donetoo); consider matching expansion shapes or documenting it. - [Warning]
WindowsCommandValidator.java:232— the conservative quoted-executable rule also rejects"C:\Program Files (x86)\app\tool.exe"; comment + test to lock the intent. - [Warning]
docs/v2/en/docs/building-blocks/tool.md:486—docs/v2/{en,zh}/docs/change-log.md:258still recommendsnew ShellCommandTool(), which now executes nothing; plus explicit notes for cleared whitelists and relative-path executables. - [Info]
CommandValidator.java:50— wording of the ordered flow, and a@deprecated/@sincehint on the now reject-all no-arg constructor.
Suggestions
The highest-value follow-ups are documentation-side rather than code-side: this changes what an existing new ShellCommandTool() does at runtime, and it also tightens CommandValidator#containsMultipleCommands behaviour on trailing escapes / unbalanced quotes for anyone who implemented the interface with the documented default semantics. Both belong in the v2 change log next to the snippets, so an upgrading application sees why its commands started returning SecurityError.
Automated review by github-manager-bot
Add Windows regression tests for quoted operators, literal expansion markers, and parentheses in quoted executable paths. Explain the conservative checks and clarify validator and constructor Javadocs. Update English and Chinese tool guides and migration examples for empty and cleared whitelists, relative executables, and custom validators. Validation: 102 focused tests passed; one Unix-only test skipped on Windows. Changed validator lines and branches remain fully covered. Maven verify, Spotless, 12 documentation tests, repository link checks, and Mintlify build validation passed. Mintlify broken-links also fails against the unmodified baseline on Windows. Refs agentscope-ai#3285
|
Review feedback addressed in cc857be0:
Kept as-is on purpose: the conservative security checks and the non-deprecated default constructor. Local tooling caveat: standalone |
oss-maintainer
left a comment
There was a problem hiding this comment.
Summary
Re-review after cc857be. All five items from my previous review are addressed, and this is ready from my side — approving.
What changed since the last review
WindowsCommandValidator— the three parser assumptions are now stated where they live: quote-suppression of< > ( ) & |at the!inDoubleQuotecheck, the intentional "any literal%/!requires approval" rule, and the conservative quoted-executable rule that also coversProgram Files (x86)paths. Assumptions that were previously implicit are now pinned by tests instead of by review memory.- Tests —
windowsQuotedOperatorsRemainLiteral(Windows-gated, runsecho "<arg>"end to end and asserts both returncode 0 and the absence of a straymarker.txt) andwindowsRequiresApprovalForParenthesesInQuotedExecutablePathslock the two behaviors down; theCommandValidatorTestparameterizations add the50% done/done!literals. - Docs —
change-log.md(en + zh) no longer advertisesnew ShellCommandTool()as the recommended snippet, carries an explicit breaking-behavior bullet, andbuilding-blocks/tool.md(en + zh) now documents whitelist clearing, relative-executable rules, custom-validator policy, and the Windows false-positive shapes. CommandValidatorJavadoc — the ordered list now matches the implementation, so a safe relative path no longer reads as if it short-circuits the whitelist.
Notes, no action required
- The no-arg constructor got an explanatory "Since 2.0.4" Javadoc rather than
@deprecated. That is a defensible choice — the constructor is still useful when the whitelist is populated viaaddAllowedCommand— just flagging that the IDE will not surface the change on its own. - The new Windows-gated assertions are only exercised on the
windows-latestmatrix leg, which was still running when this review was submitted. Worth confirming it goes green before merge, since it is the test that backs the quote-suppression assumption. - Behavior is fail-closed by design: approved commands still run through
sh -c/cmd.exe /c, so an approval authorizes the whole original command string. The PR body and the tool description both say this, which is the right way to leave it.
Automated review by github-manager-bot
juruoO-o
left a comment
There was a problem hiding this comment.
Reviewed against latest main: empty whitelist now fails closed to the approval callback (SecurityError without one), shell operators/expansions and unbalanced quoting require approval on both platforms, safe relative paths no longer bypass the whitelist, quoted-prefix executable extraction hardened. Merge-from-main commits c72030a/515fb6cda have empty combined diffs (no manual conflict resolutions), so the approved change set is unchanged. Remaining warnings are documented fail-closed tradeoffs (Windows %/!, quoted-executable operators); change-log examples updated. CI green.
AgentScope-Java Version
2.0.4-SNAPSHOT (contribution based on main at
3c1c29c0)Description
Fixes #3285.
Fixes cases where
ShellCommandToolaccepts a whitelisted executable but allows shell syntax in its arguments to trigger additional execution or file writes without approval. It also makes empty-whitelist behavior consistent with the tool description.Before
With
echowhitelisted:After
Implementation
cmd.exe /c.Compatibility and behavior boundaries
new ShellCommandTool()rejects commands until its whitelist is configured. Applications needing callback-based execution can usenew ShellCommandTool(allowedCommands, approvalCallback).sh -corcmd.exe /c. Approval therefore authorizes the complete original command.Tests
CommandValidatorTest: 66 cases covering Unix and Windows shell syntax, literal arguments, quote/escape handling, empty whitelists, executable boundaries, relative paths, and Windows outer-quote stripping.ShellCommandToolTest: 10 cases covering default rejection, ordinary whitelisted execution, approval acceptance/denial/failure, empty and cleared whitelists, and prevention of unapproved file writes.Verification
Environment: Windows 11, OpenJDK 21.
mvn -B -pl agentscope-core -am spotless:apply "-DspotlessFiles=.*coding.*[.]java"— passed.mvn -B -pl agentscope-core -am verify "-Dtest=CommandValidatorTest,ShellCommandToolTest"— passed, including Spotless checks, packaging, and Javadoc generation.mvn -B -pl agentscope-core -am verify— 2,532 tests, 0 assertion failures, 2 errors, 9 skipped. The errors occurred in existingDangerousPathBypassTestsymlink tests because the Windows account lacks symbolic-link creation privileges.git diff --check— passed.The full repository reactor and native Linux execution have not been verified locally.
Checklist
mvn spotless:applymvn test) — full core verification encountered the two Windows permission errors described above