| name | go-vuln-ssrf-requestforgery |
| description | Use when auditing Go code involving HTTP client requests, webhook callbacks, URL handling, HTML template rendering in Go web frameworks, or CSRF protection. Covers CWE-918/352/79. Keywords: SSRF, server-side request forgery, XSS, cross-site scripting, CSRF, http.Get, http.Client, template.HTML, Gin, Echo, Fiber, webhook, Kyverno, DNS rebinding |
Go SSRF / XSS / CSRF Vulnerability Patterns (CWE-918/352/79)
当审计 Go 代码中涉及 HTTP 客户端请求、webhook 回调、URL 处理、HTML 模板渲染、CSRF 防护时加载此 Skill。
Detection Strategy
SSRF (CWE-918)
Sources(攻击入口):
- 用户提供的 URL(webhook callback URL、image URL、import URL)
- Kyverno policy 中的
apiCall URL
- Redirect URL 参数
- Git 仓库 URL
- Proxy 目标 URL
Sinks(HTTP 请求发起点):
http.Get(userURL) / http.Post(userURL, ...)
http.Client{}.Do(req) 其中 req.URL 来自用户
net.Dial(userHost + ":" + port)
grpc.Dial(userAddr)
url.Parse(userURL) -> http.NewRequest("GET", parsedURL, nil)
Sanitization(SSRF 防护):
- URL allowlist(白名单匹配协议 + 主机)
- IP blocklist(禁止
127.0.0.1, 10.0.0.0/8, 169.254.169.254, ::1)
- DNS resolution 后再检查 IP(防止 DNS rebinding)
- 禁用 HTTP redirect following(
CheckRedirect 返回错误)
ssrf 防护库(如 github.com/trufflesecurity/of-ssrf)
http.Client{Timeout: 10 * time.Second} -- 设置请求超时防止 hang(默认无超时)
XSS (CWE-79)
Sources(攻击入口):
- 用户输入通过 Go API 传递到前端
- 用户输入在 Go 模板中渲染
Sinks(XSS 注入点):
template.HTML(userInput) -- 类型转换绕过 html/template 自动转义
template.JS(userInput) / template.CSS(userInput) -- 同上
c.HTML(200, template) (Gin) + text/template 而非 html/template
c.String(200, userInput) with Content-Type: text/html
w.Write([]byte(userInput)) with text/html Content-Type
Sanitization:
html/template 自动转义(但 template.HTML() 类型转换会绕过)
bluemonday HTML sanitizer
html.EscapeString(userInput)
- 正确的
Content-Type header(application/json 而非 text/html)
CSRF (CWE-352)
Sources(攻击入口):
Sinks(状态修改端点):
- 无 CSRF token 的 POST handler
- Cookie-based auth 的 API 端点
Sanitization:
gorilla/csrf 中间件
SameSite=Strict / SameSite=Lax cookie 属性
- 自定义 CSRF token(
X-CSRF-Token header)
- Double-submit cookie pattern
检测路径:
grep -rn "http.Get\|http.Post\|http.Client\|http.NewRequest" --include="*.go"
grep -rn "url.Parse\|url.PathEscape\|net.Dial\|grpc.Dial" --include="*.go"
grep -rn "template.HTML\|template.JS\|template.CSS" --include="*.go"
grep -rn '"text/template"' --include="*.go"
grep -rn "csrf\|gorilla/csrf\|SameSite\|csrfToken" --include="*.go"
grep -rn "webhook\|callback.*url\|notify.*url" --include="*.go"
grep -rn "net.LookupIP\|net.ResolveIPAddr\|IsPrivate\|IsLoopback" --include="*.go"
- SSRF: 搜索 HTTP 客户端调用,追踪 URL 来源是否来自用户输入,验证是否有 URL 白名单或 IP 黑名单
- XSS: 搜索
template.HTML() 类型转换和 text/template 使用,确认用户输入是否绕过了 auto-escaping
- CSRF: 检查状态修改端点是否有 CSRF 保护中间件,cookie 是否设置了
SameSite 属性
Detection Checklist
False Positive Exclusion Guide
以下模式不是此类漏洞:
- SSRF: HTTP 客户端访问硬编码 URL -- 如
http.Get("https://api.github.com/...") 无用户控制
- SSRF: 服务间内部通信 -- 如 gRPC 调用已知的内部服务地址
- XSS:
template.HTML 用于静态内容 -- 如硬编码的 HTML 片段
- CSRF: 纯 API token 认证 -- 不使用 cookie 认证的 API 不受 CSRF 影响
以下模式需要深入检查:
- SSRF: URL 来自数据库配置 -- 配置值是否可被低权限用户修改?
- SSRF:
http.Client 默认跟随重定向 -- 第一跳是合法 URL,但重定向到内网
- XSS:
html/template + 自定义 FuncMap -- FuncMap 中的函数是否返回 template.HTML 类型?
- CSRF: SPA 前端 + Cookie auth -- 即使 SPA 不使用表单提交,cookie 仍会被浏览器自动发送
Real-World Cases
详见 references/cases.md(7 个真实案例,需要时加载)。