接口 CommandValidator
- 所有已知实现类:
UnixCommandValidator,WindowsCommandValidator
public interface CommandValidator
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 systemsWindowsCommandValidator- For Windows systems
- 另请参阅:
-
嵌套类概要
嵌套类 -
方法概要
修饰符和类型方法说明booleancontainsMultipleCommands(String command) Check if the command contains multiple command separators.extractExecutable(String command) Extract the executable name from a command string.default booleanValidate if a relative path (starting with ./ or .\) stays within the current directory.Validate if a command is allowed to execute.
-
方法详细资料
-
validate
Validate if a command is allowed to execute.Validation checks (in order):
- Extract executable name
- If whitelist is null/empty → allow (backward compatible)
- Check for multiple command separators → reject if found
- Check relative path safety → reject if escapes current directory
- Check whitelist → reject if not in whitelist
- 参数:
command- The command string to validateallowedCommands- Set of allowed command executables (null or empty means allow all)- 返回:
- ValidationResult containing the validation outcome
-
extractExecutable
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)
- 参数:
command- The command string- 返回:
- The executable name, or empty string if extraction fails
-
containsMultipleCommands
Check if the command contains multiple command separators.Uses platform-specific detection:
- Unix:
&,|,;, newline (escape:\) - Windows:
&,|, newline (escape:^)
Separators within quotes are ignored.
- 参数:
command- The command string- 返回:
- true if multiple commands are detected, false otherwise
- Unix:
-
isPathWithinCurrentDirectory
Validate if a relative path (starting with ./ or .\) stays within the current directory.Algorithm: Uses depth-tracking to detect directory traversal:
- Normalize path separators (
\→/) - Remove leading
./ - Split by
/into segments - Track depth:
..decreases depth, normal dirs increase depth - If depth < 0 at any point → path escapes current directory
Examples:
./script.sh→ ✅ allowed (depth: 0→1)./subdir/script.sh→ ✅ allowed (depth: 0→1→2)./a/b/../c/script.sh→ ✅ allowed (depth: 0→1→2→1→2)./../script.sh→ ❌ rejected (depth: 0→-1)./../../script.sh→ ❌ rejected (depth: 0→-1→-2)
Supports both Unix (
/) and Windows (\) path separators.- 参数:
path- The path to validate- 返回:
- true if the path stays within current directory, false if it escapes
- Normalize path separators (
-