| name | go-vuln-injection |
| description | Use when auditing Go code involving OS command execution, SQL queries, template rendering, or child command invocation. Covers CWE-78/89/77/94/88. Keywords: command injection, SQL injection, exec.Command, os/exec, database/sql, text/template, html/template, argument injection, shell injection, Gogs, Grafana, MCP stdio |
Go Injection Vulnerability Patterns (CWE-78/89/77/94/88)
当审计 Go 代码中涉及命令执行、SQL 查询、模板渲染、外部进程调用时加载此 Skill。
Detection Strategy
Sources(攻击入口):
- HTTP 请求参数(query params, form values, JSON body)
- gRPC 请求字段
- Git 仓库内容(commit message, branch name, file content)
- MCP tool 输入参数
- 用户提交的配置值(Helm values, Kustomize patches)
- AI/LLM 生成的 SQL 查询
Sinks(危险操作):
exec.Command("sh", "-c", userInput) -- OS 命令注入
exec.Command("bash", "-c", userInput) -- 同上
exec.Command(binary, "--flag=" + userInput) -- 参数注入 (CWE-88)
exec.CommandContext(ctx, "git", "--upload-pack=evil", ...) -- Git argument injection
db.Query("SELECT * FROM t WHERE id=" + userInput) -- SQL 注入
db.Exec(fmt.Sprintf("INSERT INTO t VALUES ('%s')", userInput)) -- SQL 注入
template.New("").Parse(userInput) (text/template) -- 模板注入
template.HTML(userInput) -- XSS(绕过 html/template 自动转义)
Sanitization(安全屏障):
exec.Command(binary, arg1, arg2) -- 不经过 shell,每个参数独立传递
db.Query(sql, args...) / db.Exec(sql, args...) -- 参数化查询
html/template(非 text/template)-- 自动 HTML 转义
shellescape / shlex 包 -- shell 参数转义
- 输入白名单验证(正则匹配允许的字符)
检测路径:
grep -rn "exec.Command\|exec.CommandContext\|os.StartProcess" --include="*.go"
grep -rn '"sh".*"-c"\|"bash".*"-c"\|"cmd".*"/c"' --include="*.go"
grep -rn 'fmt.Sprintf.*SELECT\|fmt.Sprintf.*INSERT\|fmt.Sprintf.*UPDATE\|fmt.Sprintf.*DELETE' --include="*.go"
grep -rn 'Sprintf.*WHERE\|"+.*WHERE\|`.*%s.*FROM' --include="*.go"
grep -rn "text/template\|template.New\|template.Must" --include="*.go"
grep -rn "db.Query.*,\|db.Exec.*,\|db.QueryRow.*," --include="*.go"
grep -rn '"git".*"--upload-pack\|"git".*"--exec-path\|"git".*"--config"' --include="*.go"
- 定位命令执行/SQL 查询/模板渲染的 Sink 函数
- 回溯参数来源,确认是否包含用户输入
- 验证是否有安全屏障:
exec.Command 是否通过 shell(sh -c)执行?直接传参不经过 shell 通常安全
- SQL 是否使用参数化查询(
? 占位符)?fmt.Sprintf 拼接 SQL 是危险信号
- 模板是否使用
html/template(安全)而非 text/template(不安全)?
- Git 命令是否允许用户控制
--upload-pack、--config 等可执行的参数?
- 若无安全屏障或屏障可被绕过 -> 标记为候选漏洞
Detection Checklist
False Positive Exclusion Guide
以下模式不是此类漏洞:
exec.Command("git", "--version") -- 无用户输入的硬编码命令
exec.Command(binary, fixedArgs...) -- 参数完全硬编码,无用户输入
db.Query("SELECT * FROM t WHERE id = ?", userID) -- 参数化查询是安全的
html/template 渲染用户输入 -- 自动转义会处理 XSS(除非使用 template.HTML() 类型转换)
fmt.Sprintf 用于日志而非 SQL -- 拼接字符串用于 log 而非数据库查询
以下模式需要深入检查:
exec.Command("git", userProvidedRepoURL) -- URL 中可能包含 --upload-pack 参数
db.Exec("CREATE TABLE " + tableName) -- DDL 语句中标识符不能用 ? 参数化
text/template 用于非 HTML 输出 -- 如生成 YAML/JSON,可能导致结构注入
strings.Replace(input, "'", "''", -1) -- 手工 SQL 转义极易遗漏边缘情况
Real-World Cases
详见 references/cases.md(7 个真实案例,需要时加载)。