Find secrets with Gitleaks š
āāāāāāā
ā āā² ā
ā ā ā ā
ā ā ā ā
āāāāāāā[!WARNING] Gitleaks is feature complete. I'm not merging new features into Gitleaks. Future releases will be security patches only. I'm shifting my focus to Betterleaks
Gitleaks is a tool for detecting secrets like passwords, API keys, and tokens in git repos, files, and whatever else you wanna throw at it via stdin. If you wanna learn more about how the detection engine works check out this blog: Regex is (almost) all you need.
ā ~/code(master) gitleaks git -v
ā
āā²
ā ā
ā ā
ā gitleaks
Finding: "export BUNDLE_ENTERPRISE__CONTRIBSYS__COM=cafebabe:deadbeef",
Secret: cafebabe:deadbeef
RuleID: sidekiq-secret
Entropy: 2.609850
File: cmd/generate/config/rules/sidekiq.go
Line: 23
Commit: cd5226711335c68be1e720b318b7bc3135a30eb2
Author: John
Email: [email protected]
Date: 2022-08-03T12:31:40Z
Fingerprint: cd5226711335c68be1e720b318b7bc3135a30eb2:cmd/generate/config/rules/sidekiq.go:sidekiq-secret:23Gitleaks can be installed using Homebrew, Docker, or Go. Gitleaks is also available in binary form for many popular platforms and OS types on the releases page. In addition, Gitleaks can be implemented as a pre-commit hook directly in your repo or as a GitHub action using Gitleaks-Action.
# MacOS
brew install gitleaks
# Docker (DockerHub)
docker pull zricethezav/gitleaks:latest
docker run -v ${path_to_host_folder_to_scan}:/path zricethezav/gitleaks:latest [COMMAND] [OPTIONS] [SOURCE_PATH]
# Docker (ghcr.io)
docker pull ghcr.io/gitleaks/gitleaks:latest
docker run -v ${path_to_host_folder_to_scan}:/path ghcr.io/gitleaks/gitleaks:latest [COMMAND] [OPTIONS] [SOURCE_PATH]
# From Source (make sure `go` is installed)
git clone https://github.com/gitleaks/gitleaks.git
cd gitleaks
make buildInstall pre-commit from https://pre-commit.com/#install
Create a .pre-commit-config.yaml file at the root of your repository with the following content:
repos:
- repo: https://github.com/gitleaks/gitleaks
rev: v8.24.2
hooks:
- id: gitleaksfor a native execution of gitleaks or use the gitleaks-docker pre-commit ID for executing gitleaks using the official Docker images
Auto-update the config to the latest repos' versions by executing pre-commit autoupdate
Install with pre-commit install
Now you're all set!
ā git commit -m "this commit contains a secret"
Detect hardcoded secrets.................................................FailedNote: to disable the gitleaks pre-commit hook you can prepend SKIP=gitleaks to the commit command
and it will skip running gitleaks
ā SKIP=gitleaks git commit -m "skip gitleaks check"
Detect hardcoded secrets................................................Skippedā¦ā ļø v8.19.0 introduced a change that deprecated detect and protect. Those commands are still available but
are hidden in the --help menu. Take a look at this gist for easy command translations.
If you find v8.19.0 broke an existing command (detect/protect), please open an issue.
There are three scanning modes: git, dir, and stdin.
The git command lets you scan local git repos. Under the hood, gitleaks uses the git log -p command to scan patches.
You can configure the behavior of git log -p with the log-opts option.
For example, if you wanted to run gitleaks on a range of commits you could use the following
command: gitleaks git -v --log-opts="--all commitA..commitB" path_to_repo. See the git log documentation for more information.
If there is no target specified as a positional argument, then gitleaks will attempt to scan the current working directory as a git repo.
The dir (aliases include files, directory) command lets you scan directories and files. Example: gitleaks dir -v path_to_directory_or_file.
If there is no target specified as a positional argument, then gitleaks will scan the current working directory.
You can also stream data to gitleaks with the stdin command. Example: cat some_file | gitleaks -v stdin
When scanning large repositories or repositories with a long history, it can be convenient to use a baseline. When using a baseline,
gitleaks will ignore any old findings that are present in the baseline. A baseline can be any gitleaks report. To create a gitleaks report, run gitleaks with the --report-path parameter.
gitleaks git --report-path gitleaks-report.json # This will save the report in a file called gitleaks-report.jsonOnce as baseline is created it can be applied when running the detect command again:
gitleaks git --baseline-path gitleaks-report.json --report-path findings.jsonAfter running the detect command with the --baseline-path parameter, report output (findings.json) will only contain new issues.
You can run Gitleaks as a pre-commit hook by copying the example pre-commit.py script into
your .git/hooks/ directory.
The order of precedence is:
--config/-c option:gitleaks git --config /home/dev/customgitleaks.toml .GITLEAKS_CONFIG with the file path:export GITLEAKS_CONFIG="/home/dev/customgitleaks.toml"
gitleaks git .GITLEAKS_CONFIG_TOML with the file content:export GITLEAKS_CONFIG_TOML=`cat customgitleaks.toml`
gitleaks git ..gitleaks.toml file within the target path:gitleaks git .If none of the four options are used, then gitleaks will use the default config.
Gitleaks offers a configuration format you can follow to write your own secret detection rules:
ā¦Refer to the default gitleaks config for examples or follow the contributing guidelines if you would like to contribute to the default configuration. Additionally, you can check out this gitleaks blog post which covers advanced configuration setups.
required Rules)In v8.28.0 Gitleaks introduced composite rules, which are made up of a single "primary" rule and one or more auxiliary or required rules. To create a composite rule, add a [[rules.required]] table to the primary rule specifying an id and optionally withinLines and/or withinColumns proximity constraints. A fragment is a chunk of content that Gitleaks processes at once (typically a file, part of a file, or git diff), and proximity matching instructs the primary rule to only report a finding if the auxiliary required rules also find matches within the specified area of the fragment.
Proximity matching: Using the withinLines and withinColumns fields instructs the primary rule to only report a finding if the auxiliary required rules also find matches within the specified proximity. You can set:
withinLines: N - required findings must be within N lines (vertically)withinColumns: N - required findings must be within N characters (horizontally)Here are diagrams illustrating each proximity behavior:
ā¦If you are knowingly committing a test secret that gitleaks will catch you can add a gitleaks:allow comment to that line which will instruct gitleaks
to ignore that secret. Ex:
class CustomClass:
discord_client_secret = '8dyfuiRyq=vVc3RRr_edRk-fK__JItpZ' #gitleaks:allow
You can ignore specific findings by creating a .gitleaksignore file at the root of your repo. In release v8.10.0 Gitleaks added a Fingerprint value to the Gitleaks report. Each leak, or finding, has a Fingerprint that uniquely identifies a secret. Add this fingerprint to the .gitleaksignore file to ignore that specific secret. See Gitleaks' .gitleaksignore for an example. Note: this feature is experimental and is subject to change in the future.
Sometimes secrets are encoded in a way that can make them difficult to find
with just regex. Now you can tell gitleaks to automatically find and decode
encoded text. The flag --max-decode-depth enables this feature (the default
value "0" means the feature is disabled by default).
Recursive decoding is supported since decoded text can also contain encoded
text. The flag --max-decode-depth sets the recursion limit. Recursion stops
when there are no new segments of encoded text to decode, so setting a really
high max depth doesn't mean it will make that many passes. It will only make as
many as it needs to decode the text. Overall, decoding only minimally increases
scan times.
The findings for encoded text differ from normal findings in the following ways:
decoded:<encoding> and decode-depth:<depth>Currently supported encodings:
.gitleaksignore fingerprint (file:rule:line) matches by position, not secret content
Private key regex is greedy
gitleaks git --pre-commit --staged does not detect secrets in staged files
Create rule for SQL Server connection strings
Add detection rules for Supabase secret API keys and personal access tokens
Rule request: detect database connection strings with inline credentials (postgres/mysql/mongodb)
Rule request: detect Supabase `service_role` JWTs (distinct from safe `anon` keys)
Add Composio API key rules (ak_, oak_, uak_)
`gitleaks git` silently reports "no leaks found" with exit 0 when `git log` writes anything to stderr (git-crypt repos affected by default)
perf: detectRule() calls r.Regex.FindAllStringIndex twice per match cycle, doubling regex work on every scan