Skip to content

fix(tools): prevent shell command whitelist bypass - #3286

Merged
juruoO-o merged 5 commits into
agentscope-ai:mainfrom
25344xiaoning:fix/3285-shell-command-whitelist
Nov 11, 2026
Merged

juruoO-o merged 5 commits into
agentscope-ai:mainfrom
25344xiaoning:fix/3285-shell-command-whitelist

Conversation

@25344xiaoning

Copy link
Copy Markdown
Contributor

AgentScope-Java Version

2.0.4-SNAPSHOT (contribution based on main at 3c1c29c0)

Description

Fixes #3285.

Fixes cases where ShellCommandTool accepts 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 echo whitelisted:

echo "$(printf marker)"
  -> executable matches whitelist
  -> validation passes
  -> sh evaluates the substitution without approval

echo marker > marker.txt
  -> executable matches whitelist
  -> validation passes
  -> shell writes the file without approval

Empty whitelist
  -> validation allows every command
  -> approval callback is bypassed

After

Ordinary whitelisted command
  -> validation passes
  -> execute

Command requiring approval
  -> validation rejects automatic execution
  -> approval callback
       approved                 -> execute the original command
       denied, unavailable,
       null result, or exception -> return SecurityError

Empty whitelist
  -> require approval through the same callback path

Implementation

  • Detect Unix command substitution, variable expansion, redirection, and grouping syntax before automatic execution.
  • Correct Unix quote handling: backslashes are literal inside single quotes, while substitutions remain active inside double quotes.
  • Detect Windows variable expansion, redirection, grouping, and line separators; account for caret handling and outer-quote stripping by cmd.exe /c.
  • Reject unbalanced quoting and ambiguous quoted executable prefixes.
  • Require relative executables to match the whitelist after passing the existing path-safety check.
  • Route null or empty whitelists through the existing approval mechanism.
  • Update API/class Javadocs, the tool description, and English and Chinese tool documentation.
  • Add regression tests without introducing dependencies.

Compatibility and behavior boundaries

  • Breaking behavior change: new ShellCommandTool() rejects commands until its whitelist is configured. Applications needing callback-based execution can use new ShellCommandTool(allowedCommands, approvalCallback).
  • Clearing the whitelist now requires subsequent commands to receive approval.
  • Relative executables must match the whitelist; remaining within the current directory alone no longer grants permission.
  • Commands using the newly detected shell syntax require approval even when their executable is whitelisted.
  • Approved commands still execute through sh -c or cmd.exe /c. Approval therefore authorizes the complete original command.
  • These changes apply to the default validators. Custom validators remain responsible for their own validation policy.
  • A command whitelist is not a sandbox: allowed programs may themselves execute code or access files.

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.
  • Focused tests: 76 total, 75 passed, 0 failures, 0 errors, 1 skipped. The skipped test requires Unix shell execution; both platform validators were tested directly.
  • mvn -B -pl agentscope-core -am verify — 2,532 tests, 0 assertion failures, 2 errors, 9 skipped. The errors occurred in existing DangerousPathBypassTest symlink 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

  • Code has been formatted with mvn spotless:apply
  • All tests are passing (mvn test) — full core verification encountered the two Windows permission errors described above
  • Javadoc comments are complete and follow project conventions
  • Related documentation has been updated
  • Code is ready for review

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
@CLAassistant

CLAassistant commented Sep 24, 2026 •

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@codecov

codecov Bot commented Sep 24, 2026 •

Copy link
Copy Markdown

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 oss-maintainer left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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% done too); 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:258 still recommends new 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/@since hint 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

Comment thread docs/v2/en/docs/building-blocks/tool.md Resolved
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
@25344xiaoning

Copy link
Copy Markdown
Contributor Author

Review feedback addressed in cc857be0:

  • Windows quoting assumptions pinned down: added explanatory comments and Windows-only execution tests confirming that quoted operators remain literal and create no redirected file. Added a validator test requiring approval for quoted executable paths containing Program Files (x86).
  • Conservative % / ! handling documented: both language versions of the tool guide now explain that literal or unmatched markers also require approval, with regression tests covering these cases.
  • Migration guidance completed: updated the English and Chinese changelog examples to configure a whitelist. Documented empty/cleared whitelists, relative executable requirements, and custom validator behavior. Clarified the validation-flow Javadocs and added a 2.0.4 behavior-change note to the default constructor.
  • Verification: 103 focused tests, 0 failures, 0 errors, 1 Unix-only test skipped on Windows. Local JaCoCo reports 100% coverage of changed validator lines and branches. Spotless, Javadoc generation, documentation tests, repository link/anchor checks, and Mintlify build validation passed.

Kept as-is on purpose: the conservative security checks and the non-deprecated default constructor. containsMultipleCommands() retains its separator-only contract; stricter checks for incomplete quoting/escaping belong to validate(), with tests covering that distinction.

Local tooling caveat: standalone mint broken-links also fails against the unchanged baseline on Windows. The newly added links pass the repository’s link/anchor checker.

@oss-maintainer oss-maintainer left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 !inDoubleQuote check, the intentional "any literal %/! requires approval" rule, and the conservative quoted-executable rule that also covers Program Files (x86) paths. Assumptions that were previously implicit are now pinned by tests instead of by review memory.
  • Tests — windowsQuotedOperatorsRemainLiteral (Windows-gated, runs echo "<arg>" end to end and asserts both returncode 0 and the absence of a stray marker.txt) and windowsRequiresApprovalForParenthesesInQuotedExecutablePaths lock the two behaviors down; the CommandValidatorTest parameterizations add the 50% done / done! literals.
  • Docs — change-log.md (en + zh) no longer advertises new ShellCommandTool() as the recommended snippet, carries an explicit breaking-behavior bullet, and building-blocks/tool.md (en + zh) now documents whitelist clearing, relative-executable rules, custom-validator policy, and the Windows false-positive shapes.
  • CommandValidator Javadoc — 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 via addAllowedCommand — just flagging that the IDE will not surface the change on its own.
  • The new Windows-gated assertions are only exercised on the windows-latest matrix 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

@jujn jujn added the P0 重要紧急 label Nov 4, 2026

@juruoO-o juruoO-o left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@juruoO-o
juruoO-o merged commit dee7a21 into agentscope-ai:main Nov 11, 2026
6 checks passed
chickenlj added a commit that referenced this pull request Nov 13, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

P0 重要紧急

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: ShellCommandTool whitelist bypass allows command execution without approval

5 participants