类 WindowsCommandValidator

java.lang.Object
io.agentscope.core.tool.coding.WindowsCommandValidator
所有已实现的接口:
CommandValidator

public class WindowsCommandValidator extends Object implements CommandValidator
Command validator for Windows systems.

Validation Order:

  1. Extract executable from command (remove quotes, handle paths with spaces, remove path, remove extensions, convert to lowercase)
  2. If no whitelist configured → allow (backward compatible)
  3. Check for multiple command separators → reject if found
  4. Check relative path safety (commands starting with .\ or ./) → reject if escapes current directory
  5. Check whitelist (case-insensitive) → reject if not in whitelist

Multiple Command Detection: Detects Windows-specific separators: &, &&, |, ||, newline (ignores separators within quotes or after escape ^)
Note: Semicolon (;) is NOT a separator in Windows cmd.exe

Executable Extraction:

  • Handles quoted commands (double quotes)
  • Handles paths with spaces (e.g., C:\Program Files\app.exe)
  • Extracts command name without path (handles both \ and /)
  • Removes extensions: .exe, .bat, .cmd (case-insensitive)
  • Converts result to lowercase for consistent matching
  • 构造器详细资料

    • WindowsCommandValidator

      public WindowsCommandValidator()
  • 方法详细资料

    • validate

      public CommandValidator.ValidationResult validate(String command, Set<String> allowedCommands)
      从接口复制的说明: CommandValidator
      Validate if a command is allowed to execute.

      Validation checks (in order):

      1. Extract executable name
      2. If whitelist is null/empty → allow (backward compatible)
      3. Check for multiple command separators → reject if found
      4. Check relative path safety → reject if escapes current directory
      5. Check whitelist → reject if not in whitelist
      指定者:
      validate 在接口中 CommandValidator
      参数:
      command - The command string to validate
      allowedCommands - Set of allowed command executables (null or empty means allow all)
      返回:
      ValidationResult containing the validation outcome
    • extractExecutable

      public String extractExecutable(String command)
      从接口复制的说明: CommandValidator
      Extract the executable name from a command string.

      Extraction process:

      • Remove surrounding quotes (if present)
      • Extract first token (before space/tab)
      • Remove directory path (platform-specific)
      • Remove file extensions (platform-specific)
      指定者:
      extractExecutable 在接口中 CommandValidator
      参数:
      command - The command string
      返回:
      The executable name, or empty string if extraction fails
    • containsMultipleCommands

      public boolean containsMultipleCommands(String command)
      Check if the command contains multiple command separators outside of quotes.

      Detects: &, |, newline (ignores separators within quotes or after escape ^)

      Note: Semicolon is NOT a separator in Windows cmd.exe

      指定者:
      containsMultipleCommands 在接口中 CommandValidator
      参数:
      command - The command to check
      返回:
      true if multiple commands are detected, false otherwise