AS AgentScopeAPI 手册 / API Reference

CommandValidator

public interface CommandValidator
源码Source io/agentscope/core/tool/coding/CommandValidator.java:42–185

定义命令校验器契约的接口。位于 io.agentscope.core.tool.coding 包。核心成员包括 validate、extractExecutable、containsMultipleCommands、isPathWithinCurrentDirectory。

Interface for validating shell commands before execution.

Validation Flow:

  • Extract Executable: Parse command to extract the executable name

  • Whitelist Check: If whitelist is empty/null, allow all (backward compatible)

  • Multi-Command Detection: Reject if command contains multiple command separators

  • Relative Path Safety: For commands starting with ./ or .\, verify path doesn't escape current directory

  • Whitelist Validation: Reject if executable not in whitelist

Built-in implementations:

  • UnixCommandValidator - For Unix/Linux/macOS systems

  • WindowsCommandValidator - For Windows systems

嵌套类摘要Nested Class Summary

类Class类型Kind说明Description
ValidationResult类class
命令校验的结果。
Result of command validation.

方法摘要Method Summary

方法Method说明Description
public ValidationResult validate(String command, Set<String> allowedCommands)
校验 if 命令是已允许到执行。
Validate if a command is allowed to execute.
public String extractExecutable(String command)
设置该参数。
Extract the executable name from a command string.
public boolean containsMultipleCommands(String command)
设置该参数。
Check if the command contains multiple command separators.
default public boolean isPathWithinCurrentDirectory(String path)
default
判断是否 `isPathWithinCurrentDirectory`。
Validate if a relative path (starting with ./ or .\) stays within the current directory.

方法详情Method Detail

validate

public ValidationResult validate(String command, Set<String> allowedCommands)

校验 if 命令是已允许到执行。

Validate if a command is allowed to execute.

参数Parameters
名称Name类型Type说明Description
commandString命令(类型 `String`)。The command string to validate
allowedCommandsSet<String>已允许 Commands(类型 `Set`)。Set of allowed command executables (null or empty means allow all)
返回Returns ValidationResult
返回ValidationResult。ValidationResult containing the validation outcome
源码Source io/agentscope/core/tool/coding/CommandValidator.java:60

extractExecutable

public String extractExecutable(String command)

设置该参数。

Extract the executable name from a command string.

参数Parameters
名称Name类型Type说明Description
commandString命令(类型 `String`)。The command string
返回Returns String
返回字符串结果。The executable name, or empty string if extraction fails
源码Source io/agentscope/core/tool/coding/CommandValidator.java:76

containsMultipleCommands

public boolean containsMultipleCommands(String command)

设置该参数。

Check if the command contains multiple command separators.

参数Parameters
名称Name类型Type说明Description
commandString命令(类型 `String`)。The command string
返回Returns boolean
返回布尔值,表示判断结果。true if multiple commands are detected, false otherwise
源码Source io/agentscope/core/tool/coding/CommandValidator.java:91

isPathWithinCurrentDirectory

default public boolean isPathWithinCurrentDirectory(String path)
default

判断是否 isPathWithinCurrentDirectory。

Validate if a relative path (starting with ./ or .) stays within the current directory.

参数Parameters
名称Name类型Type说明Description
pathString路径到校验。The path to validate
返回Returns boolean
返回布尔值,表示判断结果。true if the path stays within current directory, false if it escapes
源码Source io/agentscope/core/tool/coding/CommandValidator.java:119–149

嵌套类详情Nested Class Detail

ValidationResult

class ValidationResult

命令校验的结果。位于 io.agentscope.core.tool.coding 包。核心成员包括 isAllowed、getReason、getExecutable、allowed、rejected、ValidationResult。

Result of command validation.

基于官方固定版本源码静态生成Generated from pinned official sources · Python 2.0.11 · Java 2.0.4
签名与文档为静态抽取;如有疑问请以源码为准。Signatures and docs are extracted statically; verify against the source when in doubt.