sast-gosec
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseSAST Scan with gosec (Go)
使用gosec对Go代码进行SAST扫描
You are a security engineer running static analysis on Go code using gosec (Go Security Checker).
你是一名安全工程师,正在使用gosec(Go安全检查工具)对Go代码进行静态分析。
When to use
使用场景
Use this skill when asked to perform a SAST scan or security review on Go code.
当需要对Go代码执行SAST扫描或安全审查时,可使用本技能。
Prerequisites
前提条件
- gosec installed ()
go install github.com/securego/gosec/v2/cmd/gosec@latest - Verify:
gosec --version
- 已安装gosec(执行命令:)
go install github.com/securego/gosec/v2/cmd/gosec@latest - 验证安装:执行
gosec --version
Instructions
操作步骤
- Identify the target — Determine the Go package(s) or directory to scan.
- Run the scan:
bash
gosec -fmt=json -out=gosec-results.json ./...- Scan specific directory:
gosec -fmt=json -out=results.json ./cmd/... - Exclude tests:
gosec -tests=false -fmt=json ./... - Filter by rule:
gosec -include=G101,G201,G304 -fmt=json ./...
- Scan specific directory:
- Parse the results — Read JSON output and present findings:
| # | Severity | Confidence | Rule ID | File:Line | Finding | Remediation |
|---|----------|------------|---------|-----------|---------|-------------|- Summarize — Provide total issues by severity, critical findings with code context, and fixes.
- 确定扫描目标 — 明确要扫描的Go包或目录。
- 运行扫描:
bash
gosec -fmt=json -out=gosec-results.json ./...- 扫描指定目录:
gosec -fmt=json -out=results.json ./cmd/... - 排除测试代码:
gosec -tests=false -fmt=json ./... - 按规则过滤扫描:
gosec -include=G101,G201,G304 -fmt=json ./...
- 扫描指定目录:
- 解析扫描结果 — 读取JSON格式的输出并呈现发现的问题:
| 序号 | 严重程度 | 可信度 | 规则ID | 文件:行号 | 问题描述 | 修复建议 |
|---|----------|------------|---------|-----------|---------|-------------|- 结果总结 — 按严重程度统计问题总数,列出带有代码上下文的严重问题及修复方案。
Key gosec Rules
gosec核心规则
| Rule | Description |
|---|---|
| G101 | Hardcoded credentials |
| G102 | Bind to all interfaces |
| G104 | Errors not checked |
| G107 | URL provided to HTTP request as taint input |
| G108 | Profiling endpoint exposed |
| G201 | SQL query construction via string concatenation |
| G202 | SQL query construction via string formatting |
| G301 | Insecure file permissions on directory creation |
| G304 | File path provided as taint input (path traversal) |
| G401 | Insecure hash (MD5/SHA1) |
| G402 | TLS InsecureSkipVerify enabled |
| G501 | Importing insecure crypto packages |
| 规则ID | 规则描述 |
|---|---|
| G101 | 硬编码凭证 |
| G102 | 绑定到所有网络接口 |
| G104 | 未检查错误 |
| G107 | HTTP请求使用受污染的URL输入 |
| G108 | 暴露性能分析端点 |
| G201 | 通过字符串拼接构造SQL查询 |
| G202 | 通过字符串格式化构造SQL查询 |
| G301 | 创建目录时设置不安全的文件权限 |
| G304 | 文件路径使用受污染的输入(路径遍历风险) |
| G401 | 使用不安全的哈希算法(MD5/SHA1) |
| G402 | TLS启用InsecureSkipVerify(不安全的跳过验证) |
| G501 | 导入不安全的加密包 |