gitlink-cli/skills/gitlink-compliance/references/check-secrets.md

3.6 KiB

敏感信息扫描

扫描仓库中可能存在的硬编码凭据、密钥、Token 等敏感信息。

安全原则:扫描时只报告文件路径和行号,禁止输出匹配到的原始内容。

扫描规则

S-001: Token 作为 URL 查询参数

# 搜索 access_token 等作为 URL 参数传递的代码
git grep -n 'access_token\|private_token' -- '*.go' '*.js' '*.ts' '*.py' '*.sh' '*.yaml' '*.yml'

S-002: 硬编码密码

# 搜索硬编码的 password= 或 passwd=
git grep -n -E 'password\s*[:=]\s*"[^"]{1,}"' -- '*.go' '*.js' '*.ts' '*.py' '*.yaml' '*.yml' '*.json'
git grep -n -E 'passwd\s*[:=]\s*"[^"]{1,}"' -- '*.go' '*.js' '*.ts' '*.py'

S-003: 硬编码 API Key

git grep -n -iE 'api[_-]?key\s*[:=]\s*"[a-zA-Z0-9_-]{8,}"' -- '*.go' '*.js' '*.ts' '*.py' '*.yaml' '*.yml'

S-004: 私钥文件

# 搜索私钥内容
git grep -n 'BEGIN.*PRIVATE KEY' -- ':/' 2>/dev/null || echo "未发现私钥"

# 搜索私钥文件
find . -type f \( -name "*.pem" -o -name "*.key" -o -name "*.p12" -o -name "*.pfx" \) \
  -not -path "./vendor/*" -not -path "./node_modules/*" 2>/dev/null

S-005: 硬编码 JWT / 长 Token

git grep -n -E 'token\s*[:=]\s*"eyJ[A-Za-z0-9_-]{20,}"' -- '*.go' '*.js' '*.ts' '*.py' '*.yaml' '*.yml' 2>/dev/null
git grep -n -E 'token\s*[:=]\s*"[A-Za-z0-9+/=_-]{32,}"' -- '*.go' '*.js' '*.ts' '*.py' 2>/dev/null

S-006: 硬编码 Secret

git grep -n -iE 'secret\s*[:=]\s*"[^"]{8,}"' -- '*.go' '*.js' '*.ts' '*.py' '*.yaml' '*.yml' 2>/dev/null

S-007: 凭据配置文件

# 搜索可能包含凭据的配置文件
find . -type f \( -name ".env" -o -name "credentials" -o -name "*.pem" \) \
  -not -path "./vendor/*" -not -path "./node_modules/*" -not -path "./.git/*" 2>/dev/null

S-008: Debug 输出中的 Token 泄露

git grep -n -E '(fmt|log)\.(Print|Debug|Info).*[Tt]oken' -- '*.go' 2>/dev/null
git grep -n -E 'console\.log.*[Tt]oken' -- '*.js' '*.ts' 2>/dev/null

S-009: CI/CD 明文 Fallback

# 检查 CI/CD 配置中的密钥是否有明文默认值
grep -n -E 'secrets\.[A-Z_]+\s*\|\|' .github/workflows/*.yml .devops/*.yml 2>/dev/null

S-010: 数据库连接串

git grep -n -E '(mongodb|mysql|postgres|postgresql|redis|jdbc)://[^@]*@' -- '*.go' '*.js' '*.ts' '*.yaml' '*.yml' '*.json' 2>/dev/null

执行完整扫描

# 汇总执行所有扫描规则
echo "=== S-001: URL Token 参数 ===" && git grep -n 'access_token\|private_token' -- '*.go' '*.js' '*.ts' '*.py' '*.sh' '*.yaml' '*.yml' 2>/dev/null
echo "=== S-004: 私钥 ===" && git grep -n 'BEGIN.*PRIVATE KEY' -- ':/' 2>/dev/null
echo "=== S-007: 凭据文件 ===" && find . -type f \( -name ".env" -o -name "credentials" -o -name "*.pem" \) -not -path "./vendor/*" -not -path "./node_modules/*" -not -path "./.git/*" 2>/dev/null
echo "=== S-008: Debug Token ===" && git grep -n -E '(fmt|log)\.(Print|Debug|Info).*[Tt]oken' -- '*.go' 2>/dev/null
echo "=== S-010: 数据库连接串 ===" && git grep -n -E '(mongodb|mysql|postgres|postgresql|redis)://[^@]*@' -- ':/' 2>/dev/null

排除列表

以下匹配不被视为安全问题:

模式 原因 示例
${{ secrets.XXX }} CI/CD 变量引用 GitHub Actions / DevOps Pipeline
os.Getenv("XXX") 环境变量读取 os.Getenv("GITLINK_TOKEN")
keyring.Get("xxx") OS Keychain 调用 keyring.Get("gitlink-cli", "token")
--secret flag 定义 CLI flag 参数定义 cmd.Flags().String("secret", "", "secret")
文档中的 example.com 示例域名 https://example.com/webhook