类 UnixCommandValidator

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

public class UnixCommandValidator extends Object implements CommandValidator
Command validator for Unix-like systems (Linux, macOS).

Validation Order:

  1. Extract executable from command (remove quotes, extract first token, remove extensions)
  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 ./) → reject if escapes current directory
  5. Check whitelist → reject if not in whitelist

Multiple Command Detection: Detects Unix-specific separators: &, &&, |, ||, ;, newline (ignores separators within quotes or after escape \)

Executable Extraction:

  • Handles quoted commands (single or double quotes)
  • Extracts first token before space/tab
  • Removes script extensions: .sh, .py, .rb, .pl, .bash, .zsh
  • 构造器详细资料

    • UnixCommandValidator

      public UnixCommandValidator()
  • 方法详细资料

    • 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 \)

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