متقدم
البحث فيأدلة «كيف»
طريقة البحث
  • اكتب المهمة بكلماتك (find port، open firewall، disk full) أو الصق رسالة الخطأ بين علامتي تنصيص.
  • اختر «الإجابة السريعة» لترى الأوامر فقط، أو مجالًا لتبقى في مجال واحد.
  • "كلمتان" تجد العبارة بالضبط.
  • الكلمات العادية تجد الأسماء والنصوص والأمثلة.
  • ↑ ↓ للتنقل · Enter للفتح · Esc للإغلاق · / للبحث
جرّب:

كيف 121Security · Git

How to find secrets in a repository with Gitleaks

Passwords, API keys and private keys end up in Git all the time — in a .env, a test file, an old commit. Gitleaks scans the whole history (and new commits) for them. This how-to scans a repository, reads and triages the findings, blocks new secrets with a pre-commit hook and in CI, and allows the false positives in a controlled way.

  • 5 الدروس
  • 3 الأوامر

هذا الجزء لم يُترجم بعد. أنت ترى النص الإنجليزي.

$ مستخدم عادي# بصلاحية root (أو مع sudo)

121.1

The quick answer

gitleaks git -v --redact
gitleaks dir . -v --redact
gitleaks git --report-format sarif --report-path gitleaks.sarif

git = all commits of the repo; dir = only the files as they are now. --redact hides the secret in the output.

121.2

Step by step

  1. Install — a single binary (see the full guide), or run it in a container:
docker run --rm -v "$PWD":/repo zricethezav/gitleaks:v8.28.0 git /repo -v --redact
  1. Scan the whole history (quick answer) and read a finding:
Finding:     AWS_SECRET_ACCESS_KEY=REDACTED
RuleID:      aws-access-token
File:        deploy/old.env
Commit:      3f9c2e1…
Author:      alice
Date:        2025-03-11T10:22:41Z
  1. Triage every finding:
It is…Do
A real secret, still validRotate it now, then remove it from history.
A real secret, already revokedRemove it from history (or document it as revoked).
A test value / exampleAllow it (step 5).
  1. Block new secrets before they are committed — pre-commit framework (for the whole team):
.pre-commit-config.yaml
repos:
  - repo: https://github.com/gitleaks/gitleaks
    rev: v8.28.0
    hooks:
      - id: gitleaks
pre-commit install

And in CI (so nobody can skip the hook):

gitleaks git --log-opts="origin/main..HEAD" -v --redact --exit-code 1
  1. Allow false positives — a config with an allowlist (narrow: paths or exact values), or an inline comment:
.gitleaks.toml
[extend]
useDefault = true
 
[allowlist]
description = "Test fixtures"
paths = ['''tests/fixtures/.*''']
example line in code
api_key = "test-key-not-real"  # gitleaks:allow
  1. Already pushed? Rotate first, then clean the history (see the next how-to), and turn on GitHub push protection.

121.3

Check that it worked

gitleaks git -v --redact ends with no leaks found, a test commit with a fake key is blocked by the hook, and the CI job fails on a new secret.

الأوامر في هذا الدرس

121.4

Common problems

ProblemCauseFix
Hundreds of findings in an old repoYears of history, test keys.Triage by rule and file; rotate real ones; allowlist fixtures.
CI finds nothing newShallow clone (fetch-depth: 1).Fetch the history the scan needs (fetch-depth: 0).
unknown command "detect"Newer Gitleaks uses git / dir subcommands.gitleaks git instead of gitleaks detect.
The hook is skipped (--no-verify)Local hooks are optional.Run Gitleaks in CI as a required check.
A real secret is in the allowlistToo wide allowlist (.*).Allow exact paths/values only; review the config in PRs.
Secrets in Docker images or logsGitleaks only scans Git/files.Scan images with Trivy (--scanners secret), keep secrets out of logs.

121.5

Go further

1What is the first thing to do when Gitleaks finds a real, valid key?

Rotate (revoke) the key — then clean the history.

2Why run Gitleaks in CI when there is already a pre-commit hook?

Local hooks can be skipped or not installed; CI checks every push.

كل الأوامر في هذا الفصل