sast-gosec

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

SAST 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

操作步骤

  1. Identify the target — Determine the Go package(s) or directory to scan.
  2. 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 ./...
  3. Parse the results — Read JSON output and present findings:
| # | Severity | Confidence | Rule ID | File:Line | Finding | Remediation |
|---|----------|------------|---------|-----------|---------|-------------|
  1. Summarize — Provide total issues by severity, critical findings with code context, and fixes.
  1. 确定扫描目标 — 明确要扫描的Go包或目录。
  2. 运行扫描:
    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 ./...
  3. 解析扫描结果 — 读取JSON格式的输出并呈现发现的问题:
| 序号 | 严重程度 | 可信度 | 规则ID | 文件:行号 | 问题描述 | 修复建议 |
|---|----------|------------|---------|-----------|---------|-------------|
  1. 结果总结 — 按严重程度统计问题总数,列出带有代码上下文的严重问题及修复方案。

Key gosec Rules

gosec核心规则

RuleDescription
G101Hardcoded credentials
G102Bind to all interfaces
G104Errors not checked
G107URL provided to HTTP request as taint input
G108Profiling endpoint exposed
G201SQL query construction via string concatenation
G202SQL query construction via string formatting
G301Insecure file permissions on directory creation
G304File path provided as taint input (path traversal)
G401Insecure hash (MD5/SHA1)
G402TLS InsecureSkipVerify enabled
G501Importing insecure crypto packages
规则ID规则描述
G101硬编码凭证
G102绑定到所有网络接口
G104未检查错误
G107HTTP请求使用受污染的URL输入
G108暴露性能分析端点
G201通过字符串拼接构造SQL查询
G202通过字符串格式化构造SQL查询
G301创建目录时设置不安全的文件权限
G304文件路径使用受污染的输入(路径遍历风险)
G401使用不安全的哈希算法(MD5/SHA1)
G402TLS启用InsecureSkipVerify(不安全的跳过验证)
G501导入不安全的加密包