Install
openclaw skills install yuyonghao-powershell-sandbox在受限的 PowerShell 环境中安全执行脚本,支持命令白名单、超时控制、输出限制和文件路径隔离。
openclaw skills install yuyonghao-powershell-sandbox版本: 0.1.0
作者: 小蒲萄 (Clawd)
创建日期: 2026-03-18
平台: Windows PowerShell 5.1+ / PowerShell 7+
在受限的 PowerShell 环境中安全执行用户脚本,提供:
# 执行脚本(默认 30 秒超时)
.\src\sandbox.ps1 -ScriptPath "C:\path\to\script.ps1"
# 自定义超时时间
.\src\sandbox.ps1 -ScriptPath "script.ps1" -TimeoutSeconds 60
# 指定工作目录
.\src\sandbox.ps1 -ScriptPath "script.ps1" -WorkingDirectory "C:\workspace"
# 允许网络访问(谨慎使用!)
.\src\sandbox.ps1 -ScriptPath "script.ps1" -AllowNetwork
# 通过 exec 调用
exec(
command: '.\skills\powershell-sandbox\src\sandbox.ps1 -ScriptPath "C:\Users\99236\.openclaw\workspace\scripts\user-script.ps1"',
timeout: 60
)
| 参数 | 类型 | 默认值 | 说明 |
|---|---|---|---|
-ScriptPath | string | 必填 | 要执行的脚本路径 |
-TimeoutSeconds | int | 30 | 执行超时时间(秒) |
-MaxOutputLines | int | 1000 | 最大输出行数 |
-MaxOutputChars | int | 50000 | 最大输出字符数 |
-WorkingDirectory | string | 脚本目录 | 工作目录 |
-AllowNetwork | switch | $false | 允许网络访问(⚠️ 高风险) |
-AllowedCommands | string[] | 内置白名单 | 自定义允许的命令 |
允许的命令(部分):
Write-Host, Write-Output, Write-WarningGet-Content, Set-Content, Get-ChildItem, Test-PathJoin-Path, Split-Path, Resolve-PathSelect-Object, Where-Object, ForEach-Object, Sort-ObjectConvertTo-Json, ConvertFrom-Json, ConvertTo-CsvGet-Date, Get-TimeSpan, Start-Sleep禁止的命令:
Invoke-WebRequest, Invoke-RestMethod, Start-BitsTransferStart-Process, Invoke-Expression, Get-ProcessEnter-PSSession, Invoke-Command, New-PSSessionGet-ItemProperty -Path HKLM:*Get-Service, Start-Service, Stop-ServiceGet-ScheduledTask, Register-ScheduledTaskGet-WmiObject, Get-CimInstanceInvoke-Expression, Add-Type, IEX允许的类型:
System.String, System.Int32, System.DateTimeSystem.IO.Path, System.IO.File, System.IO.DirectorySystem.Math, System.Convert, System.GuidSystem.Text.StringBuilder, System.Text.RegularExpressions.Regex禁止的类型:
System.Net.*(网络)System.Diagnostics.Process(进程)System.Reflection.*(反射)System.Management.Automation.*(PowerShell 内部)System.ServiceProcess.*(服务)System.Threading.*(线程)沙箱会在执行前扫描脚本内容,检测:
IEX 缩写(Invoke-Expression)发现违规会立即拒绝执行并报告具体问题。
使用 PowerShell Job 机制,超时后自动终止:
$job = Start-Job -ScriptBlock $script
$completed = Wait-Job -Job $job -Timeout $TimeoutSeconds
if (-not $completed) {
Stop-Job -Job $job
Write-Error "执行超时!"
}
脚本只能访问工作目录内的文件,防止遍历到系统目录。
| 代码 | 含义 |
|---|---|
| 0 | 执行成功 |
| 1 | 脚本文件不存在 |
| 2 | 安全检查失败 |
| 3 | 脚本执行错误 |
| 4 | 执行超时 |
# test-safe.ps1
Write-Host "Hello from sandbox!"
$numbers = 1..10
$sum = ($numbers | Measure-Object -Sum).Sum
Write-Host "Sum of 1-10: $sum"
执行:
.\src\sandbox.ps1 -ScriptPath "test-safe.ps1"
预期输出:
[2026-03-18 09:15:00] [INFO] 开始沙箱执行
[2026-03-18 09:15:00] [INFO] 安全检查通过 ✓
[2026-03-18 09:15:01] [INFO] 执行完成
========== 执行输出 ==========
Hello from sandbox!
Sum of 1-10: 55
========== 执行统计 ==========
状态:成功
行数:2
字符数:38
# test-dangerous.ps1
Invoke-WebRequest -Uri "https://example.com"
执行:
.\src\sandbox.ps1 -ScriptPath "test-dangerous.ps1"
预期输出:
[2026-03-18 09:15:00] [INFO] 执行安全检查...
[2026-03-18 09:15:00] [ERROR] 安全检查失败!
[2026-03-18 09:15:00] [ERROR] - 包含禁止命令:Invoke-WebRequest
# test-timeout.ps1
Write-Host "Starting..."
Start-Sleep -Seconds 60
Write-Host "Done!"
执行(30 秒超时):
.\src\sandbox.ps1 -ScriptPath "test-timeout.ps1" -TimeoutSeconds 30
预期输出:
[2026-03-18 09:15:00] [INFO] 等待脚本执行(超时:30 秒)...
[2026-03-18 09:15:30] [ERROR] 超时!强制终止执行...
# test-file.ps1
$content = Get-Content "data.txt"
Write-Host "File content: $content"
Set-Content "output.txt" "Processed: $content"
执行:
.\src\sandbox.ps1 -ScriptPath "test-file.ps1" -WorkingDirectory "C:\workspace"
skills/powershell-sandbox/
├── SKILL.md # 技能文档(本文件)
├── src/
│ ├── sandbox.ps1 # 沙箱执行器(核心)
│ ├── test-safe.ps1 # 测试用例 - 安全脚本
│ ├── test-dangerous.ps1 # 测试用例 - 危险脚本
│ └── test-timeout.ps1 # 测试用例 - 超时脚本
└── README.md # 使用说明(可选)
编辑 sandbox.ps1 中的 $DEFAULT_ALLOWED_COMMANDS 数组:
$DEFAULT_ALLOWED_COMMANDS = @(
# ... 现有命令 ...
"Your-Safe-Cmdlet" # 添加新命令
)
更严格(推荐用于不受信任的脚本):
.\src\sandbox.ps1 -ScriptPath "script.ps1" `
-TimeoutSeconds 10 `
-MaxOutputLines 100 `
-MaxOutputChars 5000
更宽松(仅用于可信脚本):
.\src\sandbox.ps1 -ScriptPath "script.ps1" `
-TimeoutSeconds 300 `
-AllowNetwork
-AllowNetwork 需谨慎).learnings/sandbox-log.md欢迎提交 Issue 和 PR 来改进沙箱的安全性和功能!
安全报告:如发现安全漏洞,请优先通过私密渠道报告。
MIT License
最后更新:2026-03-18