CommandValidator
public interface CommandValidator
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.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
command | String | 命令(类型 `String`)。The command string to validate |
allowedCommands | Set<String> | 已允许 Commands(类型 `Set`)。Set of allowed command executables (null or empty means allow all) |
ValidationResultio/agentscope/core/tool/coding/CommandValidator.java:60extractExecutable
public String extractExecutable(String command)
设置该参数。
Extract the executable name from a command string.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
command | String | 命令(类型 `String`)。The command string |
Stringio/agentscope/core/tool/coding/CommandValidator.java:76containsMultipleCommands
public boolean containsMultipleCommands(String command)
设置该参数。
Check if the command contains multiple command separators.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
command | String | 命令(类型 `String`)。The command string |
booleanio/agentscope/core/tool/coding/CommandValidator.java:91isPathWithinCurrentDirectory
default public boolean isPathWithinCurrentDirectory(String path)
判断是否 isPathWithinCurrentDirectory。
Validate if a relative path (starting with ./ or .) stays within the current directory.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
path | String | 路径到校验。The path to validate |
booleanio/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.