Skills2026年4月13日·1 分钟阅读

Gitleaks — Find Secrets in Git Repos and Code

Gitleaks is a fast SAST tool for detecting hardcoded secrets like passwords, API keys, and tokens in Git repositories. It scans commit history and source code using regex patterns, preventing secret leaks before they reach production.

Agent 就绪

这个资产会安全暂存

这个资产会先安全暂存。复制的指令会要求 Agent 读取暂存文件,并在激活脚本、MCP 配置或全局配置前先确认。

Stage only · 29/100策略:需暂存
Agent 入口
任意 MCP/CLI Agent
类型
Skill
安装
Stage only
信任
信任等级:Established
入口
step-1.md
安全暂存命令
npx -y tokrepo@latest install 40b108c4-372b-11f1-9bc6-00163e2b0d79 --target codex

先暂存文件;激活前需要读取暂存 README 和安装计划。

TL;DR
Gitleaks scans Git repos and code for hardcoded secrets like API keys, passwords, and tokens using regex patterns.
§01

What it is

Gitleaks is a fast static application security testing (SAST) tool for detecting hardcoded secrets in Git repositories. It scans commit history and source code using regex patterns to find AWS keys, database passwords, API tokens, private keys, and other credentials that should never be committed.

Gitleaks is designed for security engineers and developers who want to prevent secret leaks before they reach production, integrated into CI/CD pipelines and pre-commit hooks.

§02

How it saves time or tokens

Gitleaks scans entire Git histories in seconds, catching secrets that were committed and later deleted but still exist in history. Running it as a pre-commit hook prevents secrets from being committed in the first place. This avoids the costly process of rotating compromised credentials after a leak.

§03

How to use

  1. Install Gitleaks:
brew install gitleaks
# Or: go install github.com/gitleaks/gitleaks/v8@latest
  1. Scan your repository:
# Scan all commits
gitleaks detect

# Scan only staged changes (pre-commit)
gitleaks protect --staged

# Scan a directory without Git
gitleaks dir -s ./src
  1. Add as a pre-commit hook for continuous protection
§04

Example

# Output results as JSON for CI integration
gitleaks detect --report-format json --report-path results.json

# Custom config for additional patterns
cat > .gitleaks.toml << 'EOF'
[[rules]]
id = 'custom-api-key'
description = 'Custom API Key'
regex = '''MYAPP_API_KEY=[a-zA-Z0-9]{32}'''
[rules.allowlist]
paths = ['test/', 'docs/']
EOF

gitleaks detect -c .gitleaks.toml
§05

Related on TokRepo

§06

Common pitfalls

  • Not scanning Git history (use detect not just dir), which misses secrets in deleted commits
  • Generating too many false positives without configuring allowlists for test data
  • Running Gitleaks only in CI without a pre-commit hook, allowing secrets to enter the repo first

常见问题

How does Gitleaks differ from git-secrets?+

Gitleaks scans the entire Git history by default and comes with a comprehensive built-in ruleset for common secret patterns. git-secrets by AWS focuses on preventing commits and requires manual rule configuration. Gitleaks is faster and has broader pattern coverage.

Can I use Gitleaks in CI/CD?+

Yes. Gitleaks provides a GitHub Action, and works in any CI system. Run gitleaks detect in your pipeline and fail the build on findings. JSON output integrates with security dashboards.

How do I handle false positives?+

Add a .gitleaks.toml config with allowlists for specific paths, commits, or patterns. You can also add inline comments with gitleaks:allow to suppress specific findings.

Does Gitleaks scan non-Git directories?+

Yes. Use gitleaks dir -s ./path to scan any directory without Git history. This is useful for scanning build artifacts, config files, or code before it enters version control.

What types of secrets does Gitleaks detect?+

Gitleaks detects AWS keys, GCP credentials, Azure tokens, GitHub tokens, private keys, database connection strings, API keys for major services, JWTs, and many more through its built-in regex rules.

引用来源 (3)

讨论

登录后参与讨论。
还没有评论,来写第一条吧。

相关资产