Repository navigation
Description
Describe the bug
ShellCommandTool validates the executable against a whitelist, but then passes the entire command string to a shell. The validators do not detect several shell constructs that can execute additional commands or redirect output:
UnixCommandValidator.validatechecks the executable and callscontainsMultipleCommands, which detects&,|,;, and newlines. It does not detect command substitution ($(...), backticks), variable expansion, or redirection. For example, with onlyechowhitelisted,echo "$(printf marker)"is allowed even though the shell executesprintffirst. Double quotes do not prevent this substitution.WindowsCommandValidatorsimilarly allows variable expansion and redirection. Its quote/escape handling also misses cases wherecmd.exe /cremoves outer quotes and exposes command separators.ShellCommandTool.executeShellCommandinvokes the approval callback only when validation rejects the command. These inputs pass validation and reachsh -corcmd.exe /cwithout approval.
Additionally, both validators allow every command when the whitelist is null or empty. This contradicts the tool description: “No whitelist configured - all commands require approval.”
Impact
Applications using the whitelist to restrict agent-generated commands can execute additional commands or write files without invoking their approval callback. The resulting operations run with the permissions of the process hosting the agent.
Reachability depends on the application exposing this tool and accepting commands influenced by untrusted input.
To Reproduce
- Use the unpatched source at commit
3c1c29c0. - Run the following code with
agentscope-coreon the classpath:
import io.agentscope.core.tool.coding.UnixCommandValidator;
import io.agentscope.core.tool.coding.WindowsCommandValidator;
import java.util.Set;
public class ShellWhitelistReproduction {
public static void main(String[] args) {
UnixCommandValidator unix = new UnixCommandValidator();
WindowsCommandValidator windows = new WindowsCommandValidator();
Set<String> allowedCommands = Set.of("echo");
for (String command : new String[] {
"echo hello",
"echo $(printf marker)",
"echo \"$(printf marker)\"",
"echo `printf marker`",
"echo marker > marker.txt"
}) {
System.out.println(
command + " -> "
+ unix.validate(command, allowedCommands).isAllowed());
}
System.out.println(
"Windows redirection -> "
+ windows.validate(
"echo marker > marker.txt", allowedCommands).isAllowed());
System.out.println(
"Empty whitelist -> "
+ unix.validate("echo marker", Set.of()).isAllowed());
}
}- Observe that every check returns
true, including the substitution, redirection, and empty-whitelist examples.
This reproduction only invokes validation; it does not execute commands or create files. When passed to ShellCommandTool, the allowed results bypass approval and are interpreted by the shell.
Expected behavior
- Ordinary whitelisted commands execute directly.
- Command substitution, variable expansion, redirection, and command chaining require explicit approval.
- Quoted and escaped literal arguments remain supported where safe.
- Rejected commands do not execute without an approving callback.
- Empty-whitelist behavior matches the tool description.
Environment
- AgentScope-Java Version:
2.0.4-SNAPSHOT - Commit: 3c1c29c0
- Java Version: OpenJDK 21
- OS: Windows 11
- Both validators were tested directly; native shell execution was tested on Windows.
Additional context
Happy to submit a PR — a local patch is prepared to tighten the existing validators, preserve the approval callback, and add regression tests without introducing dependencies.
The proposed empty-whitelist change requires migration: applications using new ShellCommandTool() would need to configure a whitelist or an approval callback. The patch updates the English and Chinese tool documentation; README files remain unchanged.
An executable whitelist remains distinct from a sandbox: allowed programs can themselves execute code or access files.
Activity
oss-maintainer commentedon Sep 24, 2026
Thanks for the detailed report and reproduction — this was reviewed against
main@3c1c29c0(static code review, no runtime reproduction).Verdict: confirmed. The whitelist is a lexical check on the first token plus a separator scan, while the full command string is later handed to an interactive shell, so shell features that are not separators are not covered.
UnixCommandValidator.java:205-209—containsMultipleCommandsonly flags&,|,;, newline outside quotes;UnixCommandValidator.java:60,82— executable check passes forecho "$(printf marker)"%VAR%expansion and redirectionWindowsCommandValidator.java:236-243— separator scan covers&,|, newline only;%,<,>are never inspectedShellCommandTool.java:502-520—requestUserApprovalis called inside the!isAllowed()branch; everything that passes validation goes straight toProcessBuilder("sh","-c",command)/("cmd.exe","/c",command)atShellCommandTool.java:559-561UnixCommandValidator.java:55-57,WindowsCommandValidator.java:58-60— short-circuitValidationResult.allowed(...)before any check, so with an empty whitelist the approval callback is never invoked at allPoint 4 is a documentation/behaviour contradiction rather than a plain bug:
CommandValidator.java:26,50-57and theShellCommandToolconstructor javadocs document "null or empty means allow all (backward compatible)", whileShellCommandTool.getDescription()(ShellCommandTool.java:378) advertises "No whitelist configured - all commands require approval". One of the two has to change; making the empty whitelist mean "always ask" is the safer semantics but is a behavioural change for existingnew ShellCommandTool()users and needs a release note.Suggested fix shape (consistent with the current abstraction, no new dependencies):
$(...), backticks), unquoted$/%...%expansion, and</>redirection, so a configured callback still gets a chance on things likegrep foo > out.txt.getDescription()(or the reverse), and add tests: there are currently no unit tests forUnixCommandValidator/WindowsCommandValidator/ShellCommandToolapproval flow, soagentscope-core/src/test/java/io/agentscope/core/tool/coding/needs new coverage either way.Not verified: the sub-claim that
cmd.exe /couter-quote stripping exposes additional separators — the mechanism is plausible but no concrete whitelist bypass was constructed statically. Exploitation also depends on an application exposing this tool with commands influenced by untrusted input.A community PR (#3286) is already open against this issue, so no separate fix PR will be generated here; it will go through the regular review rotation instead.
ci(tools): cover shell command validator edge cases
ci(tools): test and document shell approval behavior
aiyili commentedon Oct 23, 2026
Hi, I'd like to work on this. The whitelist bypass via shell metacharacters is a real security gap. Plan: enforce an allowlist character class / token validation for the whitelisted command's arguments (rejecting shell metacharacters rather than trying to escape), keep the whitelist lookup on the resolved base command, and add bypass-attempt regression tests. PR coming shortly — will confirm the exact validation shape against how the whitelisted commands consume arguments.
fix(tools): prevent shell command whitelist bypass (agentscope-ai#3286)