bug:shell_command 传递含特殊字符/Unicode路径/嵌套命令时字符串被破坏,导致命令执行失败
- Dominant language
- TypeScript
- Stars
- 2.7k
- Forks
- 395
- Avg merge
- 21h 48m
- Merged PRs (30d)
- 776
Description
**提交人**: 匿名
**客户端版本**: 0.1.22
---
## 现象
`shell_command` 工具在传递包含特殊字符(引号、`$`、`@`、`<`、`>`)、Unicode 路径及嵌套命令的命令字符串时,会在传递给底层解释器(PowerShell / Node 等)之前被破坏或截断,导致命令无法执行或抛出语法错误。
常见错误表现:
- `The string is missing the terminator`:引号配对被破坏
- `Missing expression after ','`:参数被截断或拆分
- `Unexpected token 'tap'''`:特殊字符被误解析为 PowerShell 操作符
- 完整命令被逐词拆分(例如 `const fs = require(fs)` 被拆分为独立命令)
- 错误信息中出现乱码/解码错误的中文字符(如"鍟嗗搧鍥劇墖"等),表明编码/解码链存在不一致
## 复现步骤
在 Windows 环境下执行以下测试命令,可复现不同程度的失败:
| 测试类型 | 命令示例 | 结果 |
|----------|----------|------|
| 纯 ASCII 无特殊字符 | `powershell -Command "Write-Host 'hello'"` | 执行成功 |
| 简单单双引号混合 | `powershell -Command "'double' and 'single'"` | 执行失败(引号被提前消耗) |
| Unicode 路径 + 简单脚本 | `powershell -Command "Set-Location 'C:\Users\<用户名>\中文目录'; Get-Content test.txt"`(路径实际存在) | 执行失败(提示找不到路径) |
| 含 Vue/HTML 模板特殊字符 | `powershell -Command "-replace '', ''"` | 执行失败(`<`被当作保留操作符,`@`被当作 splatting 操作符) |
| 多层嵌套引号 | `powershell -Command "'string ''inner'' double'"` | 执行失败(引号配对完全崩溃) |
**关键观察:**
1. 纯 Unicode 路径本身可正常工作,问题出现在「Unicode 路径 + 内层含特殊字符的脚本」组合场景下
2. 外层 shell wrapper 对命令字符串做了不透明、不完整的预处理,破坏了原始的引用结构和转义规则
## 实际行为
所有涉及以下特征的场景均无法正常工作:
1. Windows 路径包含非 ASCII 字符(中文、日文、韩文等)
2. 脚本中含有 HTML/Vue/JS 模板指令(`@`、`$`、`{{`、`}}`、`v-if` 等)
3. 需要精确控制多层引号嵌套
4. 需要传递多行脚本命令
已尝试所有常规绕过方案均无效:Base64编码传递、单引号全包裹、PowerShell here-string、拆分为多条简单命令、`--%`停止解析符号、写脚本文件再执行、换用Node/Python等其他语言——所有方案都会在命令传递层遭遇相同的预处理破坏,唯一可行的绕过方式是手动编辑文件。
## 期望行为
1. 原始命令字符串能够无损传递给底层解释器,不破坏引号配对、转义规则和特殊字符
2. 完整支持含非ASCII字符(中文/日文/韩文等)的Windows路径
3. 正确处理HTML/Vue/JS等模板代码中的特殊字符(`$`/`@`/`<`/`>`/`{{}}`等)
4. 支持多层嵌套引号和多行脚本的传递执行
### 建议修复方向
- **短期**:文档明确标注`shell_command`的当前限制场景,引导用户在受限场景下使用其他方式操作
- **中期**:在工具内部增加完整的转义和引号保护机制,或改为通过STDIN传递命令而非命令行参数,避免shell命令行解析问题
- **长期**:考虑新增专门的文件编辑/文件系统API接口,绕过shell命令行传递的问题;对Windows Unicode路径的编码转换做专项测试加固
---
**版本区域**: CN
**OS**: win32 x64 (10.0.26200)
**界面语言**: zh-CN
Contributor guide
Research direction
Start at the shell_command entry point and trace the Windows wrapper before it invokes PowerShell or Node, focusing on argument, quoting, and encoding handling. Reproduce the listed ASCII, nested-quote, Unicode-path, template-character, and multiline cases. Done means the original command reaches the underlying interpreter intact across those cases.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- node.js, powershell, typescript
- Domain
- tooling
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Quiet
- Clarity
- Mostly clear
- Newbie friendliness
- 48/100