Skip to content

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

Closed

Description

@25344xiaoning

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:

  1. UnixCommandValidator.validate checks the executable and calls containsMultipleCommands, which detects &, |, ;, and newlines. It does not detect command substitution ($(...), backticks), variable expansion, or redirection. For example, with only echo whitelisted, echo "$(printf marker)" is allowed even though the shell executes printf first. Double quotes do not prevent this substitution.
  2. WindowsCommandValidator similarly allows variable expansion and redirection. Its quote/escape handling also misses cases where cmd.exe /c removes outer quotes and exposes command separators.
  3. ShellCommandTool.executeShellCommand invokes the approval callback only when validation rejects the command. These inputs pass validation and reach sh -c or cmd.exe /c without 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

  1. Use the unpatched source at commit 3c1c29c0.
  2. Run the following code with agentscope-core on 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());
    }
}
  1. 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

  1. added
    bug
    on Sep 24, 2026
  2. oss-maintainer commented on Sep 24, 2026

    @oss-maintainer
    Collaborator

    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.

    # Claim Result Evidence
    1 Unix validator misses command substitution / variable expansion / redirection Confirmed UnixCommandValidator.java:205-209 — containsMultipleCommands only flags &, |, ;, newline outside quotes; UnixCommandValidator.java:60,82 — executable check passes for echo "$(printf marker)"
    2 Windows validator allows %VAR% expansion and redirection Confirmed WindowsCommandValidator.java:236-243 — separator scan covers &, |, newline only; %, <, > are never inspected
    3 Approval callback fires only on validation rejection Confirmed ShellCommandTool.java:502-520 — requestUserApproval is called inside the !isAllowed() branch; everything that passes validation goes straight to ProcessBuilder("sh","-c",command) / ("cmd.exe","/c",command) at ShellCommandTool.java:559-561
    4 Null/empty whitelist allows every command Confirmed UnixCommandValidator.java:55-57, WindowsCommandValidator.java:58-60 — short-circuit ValidationResult.allowed(...) before any check, so with an empty whitelist the approval callback is never invoked at all

    Point 4 is a documentation/behaviour contradiction rather than a plain bug: CommandValidator.java:26,50-57 and the ShellCommandTool constructor javadocs document "null or empty means allow all (backward compatible)", while ShellCommandTool.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 existing new ShellCommandTool() users and needs a release note.

    Suggested fix shape (consistent with the current abstraction, no new dependencies):

    1. In both validators, reject (require-approval, not hard-block) command substitution ($(...), backticks), unquoted $ / %...% expansion, and < / > redirection, so a configured callback still gets a chance on things like grep foo > out.txt.
    2. Keep the approval callback wired to "anything not positively proven safe by the whitelist", not just to rejection — i.e. route the result of validation through the callback for constructs the whitelist cannot reason about.
    3. Align the empty-whitelist behaviour with getDescription() (or the reverse), and add tests: there are currently no unit tests for UnixCommandValidator / WindowsCommandValidator / ShellCommandTool approval flow, so agentscope-core/src/test/java/io/agentscope/core/tool/coding/ needs new coverage either way.

    Not verified: the sub-claim that cmd.exe /c outer-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.

  3. added
    area/core/tool
    on Sep 24, 2026
  4. aiyili commented on Oct 23, 2026

    @aiyili
    Contributor

    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.

  5. added a commit that references this issue on Nov 11, 2026
    dee7a21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area/core/toolbug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions