mirror of
https://github.com/gitleaks/gitleaks.git
synced 2026-11-27 03:34:22 +01:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
5bd59b0d8c | ||
|
|
464e152783 | ||
|
|
91406b0c4c | ||
|
|
04e81270cb | ||
|
|
69c6695765 | ||
|
|
9e0e050f92 | ||
|
|
b2efcf36c8 | ||
|
|
ba39abfb19 | ||
|
|
f47e1f71ee | ||
|
|
6d980b4eab | ||
|
|
ca18e8e2f6 | ||
|
|
ec436d7b33 | ||
|
|
38a511f6ba | ||
|
|
15ac1ffdd1 | ||
|
|
eb977ba237 | ||
|
|
9a6b1c3bbb | ||
|
|
95443e742c | ||
|
|
0c94db2d6b | ||
|
|
9808551eed | ||
|
|
b6fe5b08d3 | ||
|
|
5309811a09 | ||
|
|
3b6c198715 | ||
|
|
57bfa41b33 | ||
|
|
474266c0b5 | ||
|
|
c891470344 | ||
|
|
e6004da801 | ||
|
|
e18b1a884a | ||
|
|
faeb0914b2 | ||
|
|
d6578df998 | ||
|
|
f010513d17 | ||
|
|
d4be9602e3 | ||
|
|
7676690697 | ||
|
|
f4b9b6d0b3 | ||
|
|
7a3d155675 | ||
|
|
68b6388ef7 | ||
|
|
547e9768ec | ||
|
|
8608cf5b48 | ||
|
|
a08ddedb88 | ||
|
|
4bc7c13310 | ||
|
|
771055fd59 | ||
|
|
75c70bf804 |
No files matched your search
@@ -10,3 +10,4 @@
|
||||
*.out
|
||||
|
||||
examples
|
||||
src/gitleaksConfig
|
||||
@@ -1,5 +1,16 @@
|
||||
CHANGELOG
|
||||
=========
|
||||
2.1.0
|
||||
- `--commit` does not iterate on commit history. Now it directly accesses the commit object. https://github.com/zricethezav/gitleaks/pull/236
|
||||
- Fixing logging local repo path https://github.com/zricethezav/gitleaks/pull/204
|
||||
- Better README examples https://github.com/zricethezav/gitleaks/pull/202
|
||||
- Better logging on Windows https://github.com/zricethezav/gitleaks/pull/230
|
||||
- Better error messaging
|
||||
- Bunch new default rules https://github.com/zricethezav/gitleaks/pull/231 https://github.com/zricethezav/gitleaks/pull/241
|
||||
- `--repo-config` works on orgs and users now https://github.com/zricethezav/gitleaks/pull/239
|
||||
- Proper exit codes https://github.com/zricethezav/gitleaks/pull/244
|
||||
- Gitlab Token support https://github.com/zricethezav/gitleaks/pull/184
|
||||
|
||||
2.0.0
|
||||
----
|
||||
- rules introduced in the gitleaks configurationn
|
||||
|
||||
@@ -1,62 +1,59 @@
|
||||
Gitleaks
|
||||
--------
|
||||
|
||||
<p align="left">
|
||||
<a href="https://travis-ci.org/zricethezav/gitleaks"><img alt="Travis" src="https://img.shields.io/travis/zricethezav/gitleaks/master.svg?style=flat-square"></a>
|
||||
</p>
|
||||
Audit git repos for secrets. Gitleaks provides a way for you to find unencrypted secrets and other unwanted data types in git source code repositories. As part of it's core functionality, it provides;
|
||||
</p>
|
||||
|
||||
Audit git repos for secrets. Gitleaks provides a way for you to find unencrypted secrets and other unwanted data types in git source code repositories. As part of it's core functionality, it provides:
|
||||
|
||||
* Github and Gitlab support including support for bulk organization and repository owner (user) repository scans, as well as pull request scanning for use in common CI workflows.
|
||||
* Support for private repository scans, and repositories that require key based authentication
|
||||
* Output in CSV and JSON formats for consumption in other reporting tools and frameworks
|
||||
* Externalised configuration for environment specific customisation including regex rules
|
||||
* Customizable repository name, file type, commit ID, branchname and regex whitelisting to reduce false positives
|
||||
* Customizable repository name, file type, commit ID, branch name and regex whitelisting to reduce false positives
|
||||
* High performance through the use of src-d's [go-git](https://github.com/src-d/go-git) framework
|
||||
|
||||
It has been successfully used in a number of different scenarios, including:
|
||||
|
||||
It has been sucessfully used in a number of different scenarios, including;
|
||||
* Adhoc scans of local and remote repositories by filesystem path or clone URL
|
||||
* Automated scans of github users and organizations (Both public and enterprise platforms)
|
||||
* As part of a CICD workflow to identify secrets before they make it deeper into your codebase
|
||||
* As part of a wider secrets auditing automation capability for git data in large environments
|
||||
|
||||
|
||||
### Example execution
|
||||
## Example execution
|
||||
|
||||
|
||||
<p align="left">
|
||||
<img src="https://cdn.rawgit.com/zricethezav/5bf8259b7fea0170becffc06b8588edb/raw/f762769fe20ef3669bff34612b1bede6457631e6/termtosvg_je8bp82s.svg">
|
||||
</p>
|
||||
|
||||
#### Installation
|
||||
## Installation
|
||||
|
||||
Written in Go, gitleaks is available in binary form for many popular platforms and OS types from the [releases page](https://github.com/zricethezav/gitleaks/releases). Alternatively, executed via Docker or it can be installed using Go directly, as per the below;
|
||||
|
||||
##### MacOS
|
||||
#### MacOS
|
||||
|
||||
```
|
||||
brew install gitleaks
|
||||
```
|
||||
|
||||
##### Docker
|
||||
#### Docker
|
||||
|
||||
```bash
|
||||
# Run gitleaks against a public repository
|
||||
docker run --rm --name=gitleaks zricethezav/gitleaks -v -r https://github.com/zricethezav/gitleaks.git
|
||||
|
||||
# Run gitleaks against a local repository already cloned into /tmp/
|
||||
docker run --rm --name=gitleaks -v /tmp/:/code/ zricethezav/gitleaks -v --repo-path=/code/gitleaks
|
||||
|
||||
# Run gitleaks against a specific Github Pull request
|
||||
docker run --rm --name=gitleaks -e GITHUB_TOKEN={your token} zricethezav/gitleaks --github-pr=https://github.com/owner/repo/pull/9000
|
||||
docker pull zricethezav/gitleaks
|
||||
```
|
||||
|
||||
##### Go
|
||||
#### Go
|
||||
|
||||
```bash
|
||||
go get -u github.com/zricethezav/gitleaks
|
||||
```
|
||||
|
||||
#### Usage and Options
|
||||
gitleaks has a wide range of configuration options that can be adjusted at runtime or via a configuration file based on your specific requirements.
|
||||
## Usage and Options
|
||||
|
||||
gitleaks has a wide range of configuration options that can be adjusted at runtime or via a configuration file based on your specific requirements.
|
||||
|
||||
```
|
||||
Usage:
|
||||
@@ -93,7 +90,76 @@ Help Options:
|
||||
-h, --help Show this help message
|
||||
```
|
||||
|
||||
#### Exit Codes
|
||||
### Docker usage examples
|
||||
|
||||
Run gitleaks against:
|
||||
|
||||
###### Public repository
|
||||
|
||||
```bash
|
||||
docker run --rm --name=gitleaks zricethezav/gitleaks -v -r https://github.com/zricethezav/gitleaks.git
|
||||
```
|
||||
|
||||
###### Local repository already cloned into /tmp/
|
||||
|
||||
```bash
|
||||
docker run --rm --name=gitleaks -v /tmp/:/code/ zricethezav/gitleaks -v --repo-path=/code/gitleaks
|
||||
```
|
||||
|
||||
###### Specific Github Pull request
|
||||
|
||||
You need GitHub token with `repo` access. [How create token](https://help.github.com/en/articles/creating-a-personal-access-token-for-the-command-line).
|
||||
|
||||
```bash
|
||||
export GITHUB_TOKEN=""
|
||||
|
||||
docker run --rm --name=gitleaks \
|
||||
-e GITHUB_TOKEN=${GITHUB_TOKEN} \
|
||||
zricethezav/gitleaks \
|
||||
--github-pr=https://github.com/owner/repo/pull/9000
|
||||
```
|
||||
|
||||
###### Private repository
|
||||
|
||||
You need private SSH key associated with user which have pull access to private repo.
|
||||
|
||||
```bash
|
||||
export SSH_KEY_DIR=$(echo $HOME)/.ssh
|
||||
export SSH_KEY_NAME=id_rsa
|
||||
export REPO="git@github.com:zricethezav/gitleaks.git"
|
||||
|
||||
docker run --rm --name=gitleaks \
|
||||
--mount type=bind,src=${SSH_KEY_DIR},dst=/root/.ssh,readonly \
|
||||
zricethezav/gitleaks \
|
||||
--ssh-key=/root/.ssh/${SSH_KEY_NAME} \
|
||||
--verbose \
|
||||
--repo=${REPO}
|
||||
```
|
||||
|
||||
###### Specific Github organization with private repos
|
||||
|
||||
You need GitHub token with `repo` access. [How create token](https://help.github.com/en/articles/creating-a-personal-access-token-for-the-command-line).
|
||||
|
||||
```bash
|
||||
export GITHUB_TOKEN=""
|
||||
export ORG="github_org_name" # "git" in "https://github.com/git/", for instance.
|
||||
|
||||
docker run --rm --name=gitleaks \
|
||||
-e GITHUB_TOKEN=${GITHUB_TOKEN} \
|
||||
zricethezav/gitleaks \
|
||||
--verbose \
|
||||
--disk \
|
||||
--threads=$(($(nproc --all) - 1)) \
|
||||
--github-org=${ORG}
|
||||
```
|
||||
|
||||
By default repos cloned to memory. Using `--disk` for clone to disk or you can quickly out of memory.
|
||||
|
||||
For speed up analyze operation using `--threads` parameter, which set to `ALL - 1` treads at your instance CPU.
|
||||
|
||||
|
||||
## Exit Codes
|
||||
|
||||
Gitleaks provides consistent exist codes to assist in automation workflows such as CICD platforms and bulk scanning.
|
||||
|
||||
These can be effectively used in conjunction with the report output file to detect and return meaningful data back to the user or external system about if leaks have been detected, and where they reside.
|
||||
@@ -106,7 +172,8 @@ The code return codes are:
|
||||
2: error encountered
|
||||
```
|
||||
|
||||
### Additional information
|
||||
## Additional information
|
||||
|
||||
* Additional documentation about how gitleaks functions can be found on the [wiki page](https://github.com/zricethezav/gitleaks/wiki)
|
||||
* The below links detail the various approaches to remediating unwanted data in git repos
|
||||
* [Removing sensitive data from a repository (github.com)](https://help.github.com/articles/removing-sensitive-data-from-a-repository/)
|
||||
@@ -119,7 +186,8 @@ The code return codes are:
|
||||
* How does gitleaks differ to Github token scanning?
|
||||
* [Github recently announced](https://blog.github.com/2018-10-16-future-of-software/#github-token-scanning-for-public-repositories-public-beta) a new capability to their cloud platform that detects exposed credentials for a number of common services and platforms and automatically notifies the provider for revocation or similar action. Gitleaks provides a similar detection capability for non-Github cloud users, in which repositories can be easily audited and results provided in a number of formats.
|
||||
|
||||
### Give Thanks
|
||||
## Give Thanks
|
||||
|
||||
If using gitleaks has made you job easier consider donating to an organization, C-U at Home, that does vital work for those who most need it in the community of Champaign-Urbana, IL (my home).
|
||||
|
||||
Donate: https://www.cuathome.us/give/
|
||||
|
||||
+134
-7
@@ -4,11 +4,23 @@ title = "sample gitleaks config"
|
||||
# The output you are seeing here is the default gitleaks config. If GITLEAKS_CONFIG environment variable
|
||||
# is set, gitleaks will load configurations from that path. If option --config-path is set, gitleaks will load
|
||||
# configurations from that path. Gitleaks does not whitelist anything by default.
|
||||
# - https://www.ndss-symposium.org/wp-content/uploads/2019/02/ndss2019_04B-3_Meli_paper.pdf
|
||||
# - https://github.com/dxa4481/truffleHogRegexes/blob/master/truffleHogRegexes/regexes.json
|
||||
[[rules]]
|
||||
description = "AWS Key"
|
||||
regex = '''AKIA[0-9A-Z]{16}'''
|
||||
description = "AWS Client ID"
|
||||
regex = '''(A3T[A-Z0-9]|AKIA|AGPA|AIDA|AROA|AIPA|ANPA|ANVA|ASIA)[A-Z0-9]{16}'''
|
||||
tags = ["key", "AWS"]
|
||||
|
||||
[[rules]]
|
||||
description = "AWS Secret Key"
|
||||
regex = '''(?i)aws(.{0,20})?(?-i)['\"][0-9a-zA-Z\/+]{40}['\"]'''
|
||||
tags = ["key", "AWS"]
|
||||
|
||||
[[rules]]
|
||||
description = "AWS MWS key"
|
||||
regex = '''amzn\.mws\.[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}'''
|
||||
tags = ["key", "AWS", "MWS"]
|
||||
|
||||
[[rules]]
|
||||
description = "PKCS8"
|
||||
regex = '''-----BEGIN PRIVATE KEY-----'''
|
||||
@@ -30,25 +42,140 @@ regex = '''-----BEGIN PGP PRIVATE KEY BLOCK-----'''
|
||||
tags = ["key", "PGP"]
|
||||
|
||||
[[rules]]
|
||||
description = "Facebook"
|
||||
regex = '''(?i)facebook(.{0,4})?['\"][0-9a-f]{32}['\"]'''
|
||||
description = "Facebook Secret Key"
|
||||
regex = '''(?i)(facebook|fb)(.{0,20})?(?-i)['\"][0-9a-f]{32}['\"]'''
|
||||
tags = ["key", "Facebook"]
|
||||
|
||||
[[rules]]
|
||||
description = "Twitter"
|
||||
regex = '''(?i)twitter(.{0,4})?['\"][0-9a-zA-Z]{35,44}['\"]'''
|
||||
description = "Facebook Client ID"
|
||||
regex = '''(?i)(facebook|fb)(.{0,20})?['\"][0-9]{13,17}['\"]'''
|
||||
tags = ["key", "Facebook"]
|
||||
|
||||
[[rules]]
|
||||
description = "Facebook access token"
|
||||
regex = '''EAACEdEose0cBA[0-9A-Za-z]+'''
|
||||
tags = ["key", "Facebook"]
|
||||
|
||||
[[rules]]
|
||||
description = "Twitter Secret Key"
|
||||
regex = '''(?i)twitter(.{0,20})?['\"][0-9a-z]{35,44}['\"]'''
|
||||
tags = ["key", "Twitter"]
|
||||
|
||||
[[rules]]
|
||||
description = "Twitter Client ID"
|
||||
regex = '''(?i)twitter(.{0,20})?['\"][0-9a-z]{18,25}['\"]'''
|
||||
tags = ["client", "Twitter"]
|
||||
|
||||
[[rules]]
|
||||
description = "Github"
|
||||
regex = '''(?i)github(.{0,4})?['\"][0-9a-zA-Z]{35,40}['\"]'''
|
||||
regex = '''(?i)github(.{0,20})?(?-i)['\"][0-9a-zA-Z]{35,40}['\"]'''
|
||||
tags = ["key", "Github"]
|
||||
|
||||
[[rules]]
|
||||
description = "LinkedIn Client ID"
|
||||
regex = '''(?i)linkedin(.{0,20})?(?-i)['\"][0-9a-z]{12}['\"]'''
|
||||
tags = ["client", "Twitter"]
|
||||
|
||||
[[rules]]
|
||||
description = "LinkedIn Secret Key"
|
||||
regex = '''(?i)linkedin(.{0,20})?['\"][0-9a-z]{16}['\"]'''
|
||||
tags = ["secret", "Twitter"]
|
||||
|
||||
[[rules]]
|
||||
description = "Slack"
|
||||
regex = '''xox[baprs]-([0-9a-zA-Z]{10,48})?'''
|
||||
tags = ["key", "Slack"]
|
||||
|
||||
[[rules]]
|
||||
description = "EC"
|
||||
regex = '''-----BEGIN EC PRIVATE KEY-----'''
|
||||
tags = ["key", "EC"]
|
||||
|
||||
[[rules]]
|
||||
description = "Generic API key"
|
||||
regex = '''(?i)api_key(.{0,20})?['|"][0-9a-zA-Z]{32,45}['|"]'''
|
||||
tags = ["key", "API", "generic"]
|
||||
|
||||
[[rules]]
|
||||
description = "Generic Secret"
|
||||
regex = '''(?i)secret(.{0,20})?['|"][0-9a-zA-Z]{32,45}['|"]'''
|
||||
tags = ["key", "Secret", "generic"]
|
||||
|
||||
[[rules]]
|
||||
description = "Google API key"
|
||||
regex = '''AIza[0-9A-Za-z\\-_]{35}'''
|
||||
tags = ["key", "Google"]
|
||||
|
||||
[[rules]]
|
||||
description = "Google Cloud Platform API key"
|
||||
regex = '''(?i)(google|gcp|youtube|drive|yt)(.{0,20})?['\"][AIza[0-9a-z\\-_]{35}]['\"]'''
|
||||
tags = ["key", "Google", "GCP"]
|
||||
|
||||
[[rules]]
|
||||
description = "Google OAuth"
|
||||
regex = '''(?i)(google|gcp|auth)(.{0,20})?['"][0-9]+-[0-9a-z_]{32}\.apps\.googleusercontent\.com['"]'''
|
||||
tags = ["key", "Google", "OAuth"]
|
||||
|
||||
[[rules]]
|
||||
description = "Google OAuth access token"
|
||||
regex = '''ya29\.[0-9A-Za-z\-_]+'''
|
||||
tags = ["key", "Google", "OAuth"]
|
||||
|
||||
[[rules]]
|
||||
description = "Heroku API key"
|
||||
regex = '''(?i)heroku(.{0,20})?['"][0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}['"]'''
|
||||
tags = ["key", "Heroku"]
|
||||
|
||||
[[rules]]
|
||||
description = "MailChimp API key"
|
||||
regex = '''(?i)(mailchimp|mc)(.{0,20})?['"][0-9a-f]{32}-us[0-9]{1,2}['"]'''
|
||||
tags = ["key", "Mailchimp"]
|
||||
|
||||
[[rules]]
|
||||
description = "Mailgun API key"
|
||||
regex = '''(?i)(mailgun|mg)(.{0,20})?['"][0-9a-z]{32}['"]'''
|
||||
tags = ["key", "Mailgun"]
|
||||
|
||||
[[rules]]
|
||||
description = "Password in URL"
|
||||
regex = '''[a-zA-Z]{3,10}:\/\/[^\/\s:@]{3,20}:[^\/\s:@]{3,20}@.{1,100}\/?.?'''
|
||||
tags = ["key", "URL", "generic"]
|
||||
|
||||
[[rules]]
|
||||
description = "PayPal Braintree access token"
|
||||
regex = '''access_token\$production\$[0-9a-z]{16}\$[0-9a-f]{32}'''
|
||||
tags = ["key", "Paypal"]
|
||||
|
||||
[[rules]]
|
||||
description = "Picatic API key"
|
||||
regex = '''sk_live_[0-9a-z]{32}'''
|
||||
tags = ["key", "Picatic"]
|
||||
|
||||
[[rules]]
|
||||
description = "Slack Webhook"
|
||||
regex = '''https://hooks.slack.com/services/T[a-zA-Z0-9_]{8}/B[a-zA-Z0-9_]{8}/[a-zA-Z0-9_]{24}'''
|
||||
tags = ["key", "slack"]
|
||||
|
||||
[[rules]]
|
||||
description = "Stripe API key"
|
||||
regex = '''(?i)stripe(.{0,20})?['\"][sk|rk]_live_[0-9a-zA-Z]{24}'''
|
||||
tags = ["key", "Stripe"]
|
||||
|
||||
[[rules]]
|
||||
description = "Square access token"
|
||||
regex = '''sq0atp-[0-9A-Za-z\-_]{22}'''
|
||||
tags = ["key", "square"]
|
||||
|
||||
[[rules]]
|
||||
description = "Square OAuth secret"
|
||||
regex = '''sq0csp-[0-9A-Za-z\\-_]{43}'''
|
||||
tags = ["key", "square"]
|
||||
|
||||
[[rules]]
|
||||
description = "Twilio API key"
|
||||
regex = '''(?i)twilio(.{0,20})?['\"][0-9a-f]{32}['\"]'''
|
||||
tags = ["key", "twilio"]
|
||||
|
||||
[whitelist]
|
||||
files = [
|
||||
"(.*?)(jpg|gif|doc|pdf|bin)$"
|
||||
|
||||
@@ -9,15 +9,14 @@ require (
|
||||
github.com/hako/durafmt v0.0.0-20180520121703-7b7ae1e72ead
|
||||
github.com/ipfs/go-ipfs v0.4.19 // indirect
|
||||
github.com/jessevdk/go-flags v1.4.0
|
||||
github.com/mattn/go-colorable v0.1.2
|
||||
github.com/onsi/ginkgo v1.8.0 // indirect
|
||||
github.com/onsi/gomega v1.5.0 // indirect
|
||||
github.com/sirupsen/logrus v1.0.6
|
||||
github.com/xanzy/go-gitlab v0.11.3
|
||||
golang.org/x/crypto v0.0.0-20180910181607-0e37d006457b // indirect
|
||||
golang.org/x/net v0.0.0-20180925072008-f04abc6bdfa7 // indirect
|
||||
golang.org/x/lint v0.0.0-20190409202823-959b441ac422 // indirect
|
||||
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be
|
||||
golang.org/x/sync v0.0.0-20190227155943-e225da77a7e6 // indirect
|
||||
golang.org/x/sys v0.0.0-20180925112736-b09afc3d579e // indirect
|
||||
google.golang.org/appengine v1.2.0 // indirect
|
||||
gopkg.in/airbrake/gobrake.v2 v2.0.9 // indirect
|
||||
gopkg.in/gemnasium/logrus-airbrake-hook.v2 v2.1.2 // indirect
|
||||
|
||||
@@ -42,6 +42,10 @@ github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORN
|
||||
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
|
||||
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
|
||||
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
|
||||
github.com/mattn/go-colorable v0.1.2 h1:/bC9yWikZXAL9uJdulbSfyVNIR3n3trXl+v8+1sx8mU=
|
||||
github.com/mattn/go-colorable v0.1.2/go.mod h1:U0ppj6V5qS13XJ6of8GYAs25YV2eR4EVcfRqFIhoBtE=
|
||||
github.com/mattn/go-isatty v0.0.8 h1:HLtExJ+uU2HOZ+wI0Tt5DtUDrx8yhUqDcp7fYERX4CE=
|
||||
github.com/mattn/go-isatty v0.0.8/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s=
|
||||
github.com/mitchellh/go-homedir v1.0.0 h1:vKb8ShqSby24Yrqr/yDYkuFz8d0WUjys40rvnGC8aR0=
|
||||
github.com/mitchellh/go-homedir v1.0.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0=
|
||||
github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
|
||||
@@ -70,10 +74,16 @@ github.com/xanzy/ssh-agent v0.2.0/go.mod h1:0NyE30eGUDliuLEHJgYte/zncp2zdTStcOnW
|
||||
golang.org/x/crypto v0.0.0-20180904163835-0709b304e793/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
|
||||
golang.org/x/crypto v0.0.0-20180910181607-0e37d006457b h1:2b9XGzhjiYsYPnKXoEfL7klWZQIt8IfyRCz62gCqqlQ=
|
||||
golang.org/x/crypto v0.0.0-20180910181607-0e37d006457b/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
|
||||
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2 h1:VklqNMn3ovrHsnt90PveolxSbWFaJdECFbxSq0Mqo2M=
|
||||
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
|
||||
golang.org/x/lint v0.0.0-20190409202823-959b441ac422 h1:QzoH/1pFpZguR8NrRHLcO6jKqfv2zpuSqZLgdm7ZmjI=
|
||||
golang.org/x/lint v0.0.0-20190409202823-959b441ac422/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc=
|
||||
golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
|
||||
golang.org/x/net v0.0.0-20180906233101-161cd47e91fd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
|
||||
golang.org/x/net v0.0.0-20180925072008-f04abc6bdfa7 h1:zKzVgSQ8WOSHzD7I4k8LQjrHUUCNOlBsgc0PcYLVNnY=
|
||||
golang.org/x/net v0.0.0-20180925072008-f04abc6bdfa7/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
|
||||
golang.org/x/net v0.0.0-20190311183353-d8887717615a h1:oWX7TPOiFAMXLq8o0ikBYfCJVlRHBcsciT5bXOrH628=
|
||||
golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
|
||||
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be h1:vEDujvNQGv4jgYKudGeI/+DAX4Jffq6hpD55MmoEvKs=
|
||||
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
|
||||
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
@@ -83,8 +93,13 @@ golang.org/x/sys v0.0.0-20180903190138-2b024373dcd9/go.mod h1:STP8DvDyc/dI5b8T5h
|
||||
golang.org/x/sys v0.0.0-20180909124046-d0be0721c37e/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
||||
golang.org/x/sys v0.0.0-20180925112736-b09afc3d579e h1:LSlw/Dbj0MkNvPYAAkGinYmGliq+aqS7eKPYlE4oWC4=
|
||||
golang.org/x/sys v0.0.0-20180925112736-b09afc3d579e/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
||||
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
||||
golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223 h1:DH4skfRX4EBpamg7iV4ZlCpblAHI6s6TDM39bFZumv8=
|
||||
golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
||||
golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg=
|
||||
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
||||
golang.org/x/tools v0.0.0-20190311212946-11955173bddd h1:/e+gpKk9r3dJobndpTytxS2gOy6m5uvpg+ISQoEcusQ=
|
||||
golang.org/x/tools v0.0.0-20190311212946-11955173bddd/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs=
|
||||
google.golang.org/appengine v1.2.0 h1:S0iUepdCWODXRvtE+gcRDd15L+k+k1AiHlMiMjefH24=
|
||||
google.golang.org/appengine v1.2.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4=
|
||||
gopkg.in/airbrake/gobrake.v2 v2.0.9 h1:7z2uVWwn7oVeeugY1DtlPAy5H+KYgB1KeKTnqjNatLo=
|
||||
|
||||
@@ -2,22 +2,26 @@ package main
|
||||
|
||||
import (
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
log "github.com/sirupsen/logrus"
|
||||
"github.com/zricethezav/gitleaks/src"
|
||||
)
|
||||
|
||||
func main() {
|
||||
report, err := gitleaks.Run(gitleaks.ParseOpts())
|
||||
leakCount, err := gitleaks.Run(gitleaks.ParseOpts())
|
||||
if err != nil {
|
||||
if strings.Contains(err.Error(), "whitelisted") {
|
||||
log.Info(err.Error())
|
||||
os.Exit(0)
|
||||
}
|
||||
log.Error(err)
|
||||
os.Exit(gitleaks.ErrExit)
|
||||
}
|
||||
|
||||
if len(report.Leaks) != 0 {
|
||||
log.Warnf("%d leaks detected. %d commits inspected in %s", len(report.Leaks), report.Commits, report.Duration)
|
||||
os.Exit(gitleaks.LeakExit)
|
||||
if leakCount == 0 {
|
||||
os.Exit(gitleaks.NoLeaks)
|
||||
} else {
|
||||
log.Infof("%d leaks detected. %d commits inspected in %s", len(report.Leaks), report.Commits, report.Duration)
|
||||
os.Exit(gitleaks.LeakExit)
|
||||
}
|
||||
}
|
||||
+5
-7
@@ -187,23 +187,21 @@ func getEntropyRanges(entropyLimitStr []string) ([]*entropyRange, error) {
|
||||
|
||||
// externalConfig will attempt to load a pinned ".gitleaks.toml" configuration file
|
||||
// from a remote or local repo. Use the --repo-config option to trigger this.
|
||||
func (config *Config) updateFromRepo(repo *RepoInfo) error {
|
||||
func (config *Config) updateFromRepo(repo *Repo) error {
|
||||
var tomlConfig TomlConfig
|
||||
wt, err := repo.repository.Worktree()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
f, err := wt.Filesystem.Open(".gitleaks.toml")
|
||||
defer f.Close()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if _, err := toml.DecodeReader(f, &config); err != nil {
|
||||
return fmt.Errorf("problem loading config: %v", err)
|
||||
}
|
||||
f.Close()
|
||||
if err != nil {
|
||||
return err
|
||||
if _, err := toml.DecodeReader(f, &tomlConfig); err != nil {
|
||||
return fmt.Errorf("problem loading config: %v", err)
|
||||
}
|
||||
|
||||
return config.update(tomlConfig)
|
||||
}
|
||||
|
||||
|
||||
+137
-9
@@ -1,6 +1,8 @@
|
||||
package gitleaks
|
||||
|
||||
const version = "2.0.0"
|
||||
const version = "2.1.0"
|
||||
|
||||
const NoLeaks = 0
|
||||
|
||||
const defaultGithubURL = "https://api.github.com/"
|
||||
const defaultThreadNum = 1
|
||||
@@ -16,13 +18,24 @@ const defaultConfig = `
|
||||
# The output you are seeing here is the default gitleaks config. If GITLEAKS_CONFIG environment variable
|
||||
# is set, gitleaks will load configurations from that path. If option --config is set, gitleaks will load
|
||||
# configurations from that path. Gitleaks does not whitelist anything by default.
|
||||
# - https://www.ndss-symposium.org/wp-content/uploads/2019/02/ndss2019_04B-3_Meli_paper.pdf
|
||||
# - https://github.com/dxa4481/truffleHogRegexes/blob/master/truffleHogRegexes/regexes.json
|
||||
|
||||
title = "gitleaks config"
|
||||
[[rules]]
|
||||
description = "AWS Key"
|
||||
regex = '''AKIA[0-9A-Z]{16}'''
|
||||
description = "AWS Client ID"
|
||||
regex = '''(A3T[A-Z0-9]|AKIA|AGPA|AIDA|AROA|AIPA|ANPA|ANVA|ASIA)[A-Z0-9]{16}'''
|
||||
tags = ["key", "AWS"]
|
||||
severity = "high"
|
||||
|
||||
[[rules]]
|
||||
description = "AWS Secret Key"
|
||||
regex = '''(?i)aws(.{0,20})?(?-i)['\"][0-9a-zA-Z\/+]{40}['\"]'''
|
||||
tags = ["key", "AWS"]
|
||||
|
||||
[[rules]]
|
||||
description = "AWS MWS key"
|
||||
regex = '''amzn\.mws\.[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}'''
|
||||
tags = ["key", "AWS", "MWS"]
|
||||
|
||||
[[rules]]
|
||||
description = "PKCS8"
|
||||
@@ -45,25 +58,140 @@ regex = '''-----BEGIN PGP PRIVATE KEY BLOCK-----'''
|
||||
tags = ["key", "PGP"]
|
||||
|
||||
[[rules]]
|
||||
description = "Facebook"
|
||||
regex = '''(?i)facebook(.{0,4})?['\"][0-9a-f]{32}['\"]'''
|
||||
description = "Facebook Secret Key"
|
||||
regex = '''(?i)(facebook|fb)(.{0,20})?(?-i)['\"][0-9a-f]{32}['\"]'''
|
||||
tags = ["key", "Facebook"]
|
||||
|
||||
[[rules]]
|
||||
description = "Twitter"
|
||||
regex = '''(?i)twitter(.{0,4})?['\"][0-9a-zA-Z]{35,44}['\"]'''
|
||||
description = "Facebook Client ID"
|
||||
regex = '''(?i)(facebook|fb)(.{0,20})?['\"][0-9]{13,17}['\"]'''
|
||||
tags = ["key", "Facebook"]
|
||||
|
||||
[[rules]]
|
||||
description = "Facebook access token"
|
||||
regex = '''EAACEdEose0cBA[0-9A-Za-z]+'''
|
||||
tags = ["key", "Facebook"]
|
||||
|
||||
[[rules]]
|
||||
description = "Twitter Secret Key"
|
||||
regex = '''(?i)twitter(.{0,20})?['\"][0-9a-z]{35,44}['\"]'''
|
||||
tags = ["key", "Twitter"]
|
||||
|
||||
[[rules]]
|
||||
description = "Twitter Client ID"
|
||||
regex = '''(?i)twitter(.{0,20})?['\"][0-9a-z]{18,25}['\"]'''
|
||||
tags = ["client", "Twitter"]
|
||||
|
||||
[[rules]]
|
||||
description = "Github"
|
||||
regex = '''(?i)github(.{0,4})?['\"][0-9a-zA-Z]{35,40}['\"]'''
|
||||
regex = '''(?i)github(.{0,20})?(?-i)['\"][0-9a-zA-Z]{35,40}['\"]'''
|
||||
tags = ["key", "Github"]
|
||||
|
||||
[[rules]]
|
||||
description = "LinkedIn Client ID"
|
||||
regex = '''(?i)linkedin(.{0,20})?(?-i)['\"][0-9a-z]{12}['\"]'''
|
||||
tags = ["client", "Twitter"]
|
||||
|
||||
[[rules]]
|
||||
description = "LinkedIn Secret Key"
|
||||
regex = '''(?i)linkedin(.{0,20})?['\"][0-9a-z]{16}['\"]'''
|
||||
tags = ["secret", "Twitter"]
|
||||
|
||||
[[rules]]
|
||||
description = "Slack"
|
||||
regex = '''xox[baprs]-([0-9a-zA-Z]{10,48})?'''
|
||||
tags = ["key", "Slack"]
|
||||
|
||||
[[rules]]
|
||||
description = "EC"
|
||||
regex = '''-----BEGIN EC PRIVATE KEY-----'''
|
||||
tags = ["key", "EC"]
|
||||
|
||||
[[rules]]
|
||||
description = "Generic API key"
|
||||
regex = '''(?i)(api_key|apikey)(.{0,20})?['|"][0-9a-zA-Z]{32,45}['|"]'''
|
||||
tags = ["key", "API", "generic"]
|
||||
|
||||
[[rules]]
|
||||
description = "Generic Secret"
|
||||
regex = '''(?i)secret(.{0,20})?['|"][0-9a-zA-Z]{32,45}['|"]'''
|
||||
tags = ["key", "Secret", "generic"]
|
||||
|
||||
[[rules]]
|
||||
description = "Google API key"
|
||||
regex = '''AIza[0-9A-Za-z\\-_]{35}'''
|
||||
tags = ["key", "Google"]
|
||||
|
||||
[[rules]]
|
||||
description = "Google Cloud Platform API key"
|
||||
regex = '''(?i)(google|gcp|youtube|drive|yt)(.{0,20})?['\"][AIza[0-9a-z\\-_]{35}]['\"]'''
|
||||
tags = ["key", "Google", "GCP"]
|
||||
|
||||
[[rules]]
|
||||
description = "Google OAuth"
|
||||
regex = '''(?i)(google|gcp|auth)(.{0,20})?['"][0-9]+-[0-9a-z_]{32}\.apps\.googleusercontent\.com['"]'''
|
||||
tags = ["key", "Google", "OAuth"]
|
||||
|
||||
[[rules]]
|
||||
description = "Google OAuth access token"
|
||||
regex = '''ya29\.[0-9A-Za-z\-_]+'''
|
||||
tags = ["key", "Google", "OAuth"]
|
||||
|
||||
[[rules]]
|
||||
description = "Heroku API key"
|
||||
regex = '''(?i)heroku(.{0,20})?['"][0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}['"]'''
|
||||
tags = ["key", "Heroku"]
|
||||
|
||||
[[rules]]
|
||||
description = "MailChimp API key"
|
||||
regex = '''(?i)(mailchimp|mc)(.{0,20})?['"][0-9a-f]{32}-us[0-9]{1,2}['"]'''
|
||||
tags = ["key", "Mailchimp"]
|
||||
|
||||
[[rules]]
|
||||
description = "Mailgun API key"
|
||||
regex = '''(?i)(mailgun|mg)(.{0,20})?['"][0-9a-z]{32}['"]'''
|
||||
tags = ["key", "Mailgun"]
|
||||
|
||||
[[rules]]
|
||||
description = "Password in URL"
|
||||
regex = '''[a-zA-Z]{3,10}:\/\/[^\/\s:@]{3,20}:[^\/\s:@]{3,20}@.{1,100}\/?.?'''
|
||||
tags = ["key", "URL", "generic"]
|
||||
|
||||
[[rules]]
|
||||
description = "PayPal Braintree access token"
|
||||
regex = '''access_token\$production\$[0-9a-z]{16}\$[0-9a-f]{32}'''
|
||||
tags = ["key", "Paypal"]
|
||||
|
||||
[[rules]]
|
||||
description = "Picatic API key"
|
||||
regex = '''sk_live_[0-9a-z]{32}'''
|
||||
tags = ["key", "Picatic"]
|
||||
|
||||
[[rules]]
|
||||
description = "Slack Webhook"
|
||||
regex = '''https://hooks.slack.com/services/T[a-zA-Z0-9_]{8}/B[a-zA-Z0-9_]{8}/[a-zA-Z0-9_]{24}'''
|
||||
tags = ["key", "slack"]
|
||||
|
||||
[[rules]]
|
||||
description = "Stripe API key"
|
||||
regex = '''(?i)stripe(.{0,20})?['\"][sk|rk]_live_[0-9a-zA-Z]{24}'''
|
||||
tags = ["key", "Stripe"]
|
||||
|
||||
[[rules]]
|
||||
description = "Square access token"
|
||||
regex = '''sq0atp-[0-9A-Za-z\-_]{22}'''
|
||||
tags = ["key", "square"]
|
||||
|
||||
[[rules]]
|
||||
description = "Square OAuth secret"
|
||||
regex = '''sq0csp-[0-9A-Za-z\\-_]{43}'''
|
||||
tags = ["key", "square"]
|
||||
|
||||
[[rules]]
|
||||
description = "Twilio API key"
|
||||
regex = '''(?i)twilio(.{0,20})?['\"][0-9a-f]{32}['\"]'''
|
||||
tags = ["key", "twilio"]
|
||||
|
||||
[whitelist]
|
||||
files = [
|
||||
"(.*?)(jpg|gif|doc|pdf|bin)$"
|
||||
|
||||
+35
-44
@@ -1,14 +1,10 @@
|
||||
package gitleaks
|
||||
|
||||
import (
|
||||
log "github.com/sirupsen/logrus"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/hako/durafmt"
|
||||
log "github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
var (
|
||||
@@ -17,8 +13,6 @@ var (
|
||||
dir string
|
||||
threads int
|
||||
totalCommits int64
|
||||
commitMap = make(map[string]bool)
|
||||
auditDone bool
|
||||
mutex = &sync.Mutex{}
|
||||
)
|
||||
|
||||
@@ -36,17 +30,16 @@ type Report struct {
|
||||
}
|
||||
|
||||
// Run is the entry point for gitleaks
|
||||
func Run(optsL *Options) (*Report, error) {
|
||||
func Run(optsL *Options) (int, error) {
|
||||
var (
|
||||
leaks []Leak
|
||||
err error
|
||||
leaks []Leak
|
||||
)
|
||||
|
||||
now := time.Now()
|
||||
opts = optsL
|
||||
config, err = newConfig()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return NoLeaks, err
|
||||
}
|
||||
|
||||
if opts.Disk {
|
||||
@@ -54,63 +47,61 @@ func Run(optsL *Options) (*Report, error) {
|
||||
dir, err = ioutil.TempDir("", "gitleaks")
|
||||
defer os.RemoveAll(dir)
|
||||
if err != nil {
|
||||
goto postAudit
|
||||
return NoLeaks, err
|
||||
}
|
||||
}
|
||||
|
||||
// start audits
|
||||
if opts.Repo != "" || opts.RepoPath != "" {
|
||||
repoInfo, err := newRepoInfo()
|
||||
var repo *Repo
|
||||
repo, err = newRepo()
|
||||
if err != nil {
|
||||
goto postAudit
|
||||
return NoLeaks, err
|
||||
}
|
||||
err = repoInfo.clone()
|
||||
err = repo.clone()
|
||||
if err != nil {
|
||||
goto postAudit
|
||||
return NoLeaks, err
|
||||
}
|
||||
leaks, err = repoInfo.audit()
|
||||
err = repo.audit()
|
||||
if err != nil {
|
||||
return NoLeaks, err
|
||||
}
|
||||
repo.report()
|
||||
leaks = repo.leaks
|
||||
} else if opts.OwnerPath != "" {
|
||||
repoDs, err := discoverRepos(opts.OwnerPath)
|
||||
var repos []*Repo
|
||||
repos, err = discoverRepos(opts.OwnerPath)
|
||||
if err != nil {
|
||||
goto postAudit
|
||||
return NoLeaks, err
|
||||
}
|
||||
for _, repoInfo := range repoDs {
|
||||
err = repoInfo.clone()
|
||||
for _, repo := range repos {
|
||||
err = repo.clone()
|
||||
if err != nil {
|
||||
log.Warnf("error occurred cloning repo: %s, continuing to next repo", repo.name)
|
||||
continue
|
||||
}
|
||||
leaksFromRepo, err := repoInfo.audit()
|
||||
|
||||
err = repo.audit()
|
||||
if err != nil {
|
||||
log.Warnf("error occured auditing repo: %s, continuing", repoInfo.name)
|
||||
log.Warnf("error occurred auditing repo: %s, continuing to next repo", repo.name)
|
||||
continue
|
||||
}
|
||||
leaks = append(leaksFromRepo, leaks...)
|
||||
repo.report()
|
||||
leaks = append(leaks, repo.leaks...)
|
||||
}
|
||||
} else if opts.GithubOrg != "" || opts.GithubUser != "" {
|
||||
leaks, err = auditGithubRepos()
|
||||
return auditGithubRepos()
|
||||
} else if opts.GitLabOrg != "" || opts.GitLabUser != "" {
|
||||
leaks, err = auditGitlabRepos()
|
||||
return auditGitlabRepos()
|
||||
} else if opts.GithubPR != "" {
|
||||
leaks, err = auditGithubPR()
|
||||
}
|
||||
|
||||
postAudit:
|
||||
if err != nil {
|
||||
if strings.Contains(err.Error(), "whitelisted") {
|
||||
log.Info(err.Error())
|
||||
os.Exit(0)
|
||||
}
|
||||
log.Error(err)
|
||||
os.Exit(ErrExit)
|
||||
return auditGithubPR()
|
||||
}
|
||||
|
||||
if opts.Report != "" {
|
||||
writeReport(leaks)
|
||||
err = writeReport(leaks)
|
||||
if err != nil {
|
||||
return NoLeaks, err
|
||||
}
|
||||
}
|
||||
|
||||
return &Report{
|
||||
Leaks: leaks,
|
||||
Duration: durafmt.Parse(time.Now().Sub(now)).String(),
|
||||
Commits: totalCommits,
|
||||
}, err
|
||||
return len(leaks), nil
|
||||
}
|
||||
+39
-22
@@ -13,7 +13,7 @@ import (
|
||||
"github.com/google/go-github/github"
|
||||
log "github.com/sirupsen/logrus"
|
||||
"golang.org/x/oauth2"
|
||||
git "gopkg.in/src-d/go-git.v4"
|
||||
"gopkg.in/src-d/go-git.v4"
|
||||
gitHttp "gopkg.in/src-d/go-git.v4/plumbing/transport/http"
|
||||
"gopkg.in/src-d/go-git.v4/storage/memory"
|
||||
)
|
||||
@@ -21,7 +21,7 @@ import (
|
||||
var githubPages = 100
|
||||
|
||||
// auditPR audits a single github PR
|
||||
func auditGithubPR() ([]Leak, error) {
|
||||
func auditGithubPR() (int, error) {
|
||||
var leaks []Leak
|
||||
ctx := context.Background()
|
||||
githubClient := github.NewClient(githubToken())
|
||||
@@ -30,7 +30,7 @@ func auditGithubPR() ([]Leak, error) {
|
||||
repo := splits[len(splits)-3]
|
||||
prNum, err := strconv.Atoi(splits[len(splits)-1])
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return NoLeaks, err
|
||||
}
|
||||
|
||||
page := 1
|
||||
@@ -40,7 +40,7 @@ func auditGithubPR() ([]Leak, error) {
|
||||
Page: page,
|
||||
})
|
||||
if err != nil {
|
||||
return leaks, err
|
||||
return NoLeaks, err
|
||||
}
|
||||
|
||||
for _, c := range commits {
|
||||
@@ -66,7 +66,7 @@ func auditGithubPR() ([]Leak, error) {
|
||||
continue
|
||||
}
|
||||
|
||||
commit := &commitInfo{
|
||||
commit := &Commit{
|
||||
sha: c.GetSHA(),
|
||||
content: *f.Patch,
|
||||
filePath: *f.Filename,
|
||||
@@ -84,14 +84,25 @@ func auditGithubPR() ([]Leak, error) {
|
||||
}
|
||||
}
|
||||
|
||||
return leaks, nil
|
||||
if len(leaks) != 0 {
|
||||
log.Warnf("%d leaks detected. %d commits inspected for PR: %s", len(leaks), totalCommits, opts.GithubPR)
|
||||
}
|
||||
|
||||
if opts.Report != "" {
|
||||
err = writeReport(leaks)
|
||||
if err != nil {
|
||||
return NoLeaks, err
|
||||
}
|
||||
}
|
||||
|
||||
return len(leaks), nil
|
||||
}
|
||||
|
||||
// auditGithubRepos kicks off audits if --github-user or --github-org options are set.
|
||||
// First, we gather all the github repositories from the github api (this doesnt actually clone the repo).
|
||||
// After all the repos have been pulled from github's api we proceed to audit the repos by calling auditGithubRepo.
|
||||
// If an error occurs during an audit of a repo, that error is logged but won't break the execution cycle.
|
||||
func auditGithubRepos() ([]Leak, error) {
|
||||
func auditGithubRepos() (int, error) {
|
||||
var (
|
||||
err error
|
||||
githubRepos []*github.Repository
|
||||
@@ -100,8 +111,8 @@ func auditGithubRepos() ([]Leak, error) {
|
||||
githubOrgOptions *github.RepositoryListByOrgOptions
|
||||
githubOptions *github.RepositoryListOptions
|
||||
done bool
|
||||
leaks []Leak
|
||||
ownerDir string
|
||||
leaks []Leak
|
||||
)
|
||||
ctx := context.Background()
|
||||
githubClient := github.NewClient(githubToken())
|
||||
@@ -163,31 +174,37 @@ func auditGithubRepos() ([]Leak, error) {
|
||||
ownerDir, _ = ioutil.TempDir(dir, opts.GithubUser)
|
||||
}
|
||||
for _, githubRepo := range githubRepos {
|
||||
repoD, err := cloneGithubRepo(githubRepo)
|
||||
repo, err := cloneGithubRepo(githubRepo)
|
||||
if err != nil {
|
||||
log.Warn(err)
|
||||
continue
|
||||
}
|
||||
leaksFromRepo, err := repoD.audit()
|
||||
err = repo.audit()
|
||||
if err != nil {
|
||||
log.Warnf("error occurred during audit of repo: %s, err: %v, continuing github audit", repo.name, err)
|
||||
}
|
||||
if opts.Disk {
|
||||
os.RemoveAll(fmt.Sprintf("%s/%s", ownerDir, *githubRepo.Name))
|
||||
}
|
||||
if len(leaksFromRepo) == 0 {
|
||||
log.Infof("no leaks found for repo %s", *githubRepo.Name)
|
||||
} else {
|
||||
log.Warnf("leaks found for repo %s", *githubRepo.Name)
|
||||
}
|
||||
if err != nil {
|
||||
log.Warn(err)
|
||||
}
|
||||
leaks = append(leaks, leaksFromRepo...)
|
||||
|
||||
repo.report()
|
||||
|
||||
leaks = append(leaks, repo.leaks...)
|
||||
}
|
||||
return leaks, nil
|
||||
|
||||
if opts.Report != "" {
|
||||
err = writeReport(leaks)
|
||||
if err != nil {
|
||||
return NoLeaks, err
|
||||
}
|
||||
}
|
||||
|
||||
return len(leaks), nil
|
||||
}
|
||||
|
||||
// cloneGithubRepo clones a repo from the url parsed from a github repo. The repo
|
||||
// will be cloned to disk if --disk is set.
|
||||
func cloneGithubRepo(githubRepo *github.Repository) (*RepoInfo, error) {
|
||||
func cloneGithubRepo(githubRepo *github.Repository) (*Repo, error) {
|
||||
var (
|
||||
repo *git.Repository
|
||||
err error
|
||||
@@ -248,7 +265,7 @@ func cloneGithubRepo(githubRepo *github.Repository) (*RepoInfo, error) {
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &RepoInfo{
|
||||
return &Repo{
|
||||
repository: repo,
|
||||
name: *githubRepo.Name,
|
||||
}, nil
|
||||
|
||||
+27
-15
@@ -8,6 +8,7 @@ import (
|
||||
log "github.com/sirupsen/logrus"
|
||||
"github.com/xanzy/go-gitlab"
|
||||
"gopkg.in/src-d/go-git.v4"
|
||||
gitHttp "gopkg.in/src-d/go-git.v4/plumbing/transport/http"
|
||||
"gopkg.in/src-d/go-git.v4/storage/memory"
|
||||
)
|
||||
|
||||
@@ -17,13 +18,13 @@ const gitlabPages = 100
|
||||
// auditGitlabRepos kicks off audits if --gitlab-user or --gitlab-org options are set.
|
||||
// Getting all repositories from the GitLab API and run audit. If an error occurs during an audit of a repo,
|
||||
// that error is logged.
|
||||
func auditGitlabRepos() ([]Leak, error) {
|
||||
func auditGitlabRepos() (int, error) {
|
||||
var (
|
||||
ps []*gitlab.Project
|
||||
resp *gitlab.Response
|
||||
leaks []Leak
|
||||
tempDir string
|
||||
err error
|
||||
leaks []Leak
|
||||
)
|
||||
|
||||
repos := make([]*gitlab.Project, 0, gitlabPages)
|
||||
@@ -80,31 +81,34 @@ func auditGitlabRepos() ([]Leak, error) {
|
||||
}
|
||||
|
||||
for _, p := range repos {
|
||||
repoInfo, err := cloneGitlabRepo(tempDir, p)
|
||||
repo, err := cloneGitlabRepo(tempDir, p)
|
||||
if err != nil {
|
||||
log.Warn(err)
|
||||
continue
|
||||
}
|
||||
|
||||
leaksFromRepo, err := repoInfo.audit()
|
||||
err = repo.audit()
|
||||
if err != nil {
|
||||
log.Warn(err)
|
||||
continue
|
||||
}
|
||||
|
||||
if opts.Disk {
|
||||
os.RemoveAll(fmt.Sprintf("%s/%d", tempDir, p.ID))
|
||||
}
|
||||
|
||||
if len(leaksFromRepo) == 0 {
|
||||
log.Infof("no leaks found for repo %s", p.Name)
|
||||
} else {
|
||||
log.Warnf("leaks found for repo %s", p.Name)
|
||||
}
|
||||
|
||||
leaks = append(leaks, leaksFromRepo...)
|
||||
repo.report()
|
||||
leaks = append(leaks, repo.leaks...)
|
||||
}
|
||||
|
||||
return leaks, nil
|
||||
if opts.Report != "" {
|
||||
err = writeReport(leaks)
|
||||
if err != nil {
|
||||
return NoLeaks, err
|
||||
}
|
||||
}
|
||||
|
||||
return len(leaks), nil
|
||||
}
|
||||
|
||||
func createGitlabTempDir() (string, error) {
|
||||
@@ -123,11 +127,14 @@ func createGitlabTempDir() (string, error) {
|
||||
return ownerDir, nil
|
||||
}
|
||||
|
||||
func cloneGitlabRepo(tempDir string, p *gitlab.Project) (*RepoInfo, error) {
|
||||
func cloneGitlabRepo(tempDir string, p *gitlab.Project) (*Repo, error) {
|
||||
var (
|
||||
repo *git.Repository
|
||||
err error
|
||||
)
|
||||
|
||||
gitLabToken := os.Getenv("GITLAB_TOKEN")
|
||||
|
||||
if opts.ExcludeForks && p.ForkedFromProject != nil {
|
||||
return nil, fmt.Errorf("skipping %s, excluding forks", p.Name)
|
||||
}
|
||||
@@ -142,9 +149,14 @@ func cloneGitlabRepo(tempDir string, p *gitlab.Project) (*RepoInfo, error) {
|
||||
URL: p.HTTPURLToRepo,
|
||||
}
|
||||
|
||||
if config.sshAuth != nil {
|
||||
if config.sshAuth != nil && gitLabToken == "" {
|
||||
opt.URL = p.SSHURLToRepo
|
||||
opt.Auth = config.sshAuth
|
||||
} else if gitLabToken != "" {
|
||||
opt.Auth = &gitHttp.BasicAuth{
|
||||
Username: "fakeUsername", // yes, this can be anything except an empty string
|
||||
Password: gitLabToken,
|
||||
}
|
||||
}
|
||||
|
||||
log.Infof("cloning: %s", p.Name)
|
||||
@@ -159,7 +171,7 @@ func cloneGitlabRepo(tempDir string, p *gitlab.Project) (*RepoInfo, error) {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &RepoInfo{
|
||||
return &Repo{
|
||||
repository: repo,
|
||||
name: p.Name,
|
||||
}, nil
|
||||
|
||||
+34
-18
@@ -6,6 +6,7 @@ import (
|
||||
"os"
|
||||
"path"
|
||||
"regexp"
|
||||
"runtime"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
@@ -63,22 +64,22 @@ func TestGetRepo(t *testing.T) {
|
||||
Repo: "https://github.com/gitleakstest/nope",
|
||||
},
|
||||
description: "test no repo",
|
||||
expectedErrMsg: "authentication required",
|
||||
expectedErrMsg: "repository not found",
|
||||
},
|
||||
{
|
||||
testOpts: &Options{
|
||||
Repo: "https://github.com/gitleakstest/private",
|
||||
},
|
||||
description: "test private repo",
|
||||
expectedErrMsg: "authentication required",
|
||||
expectedErrMsg: "repository not found",
|
||||
},
|
||||
{
|
||||
testOpts: &Options{
|
||||
Repo: "https://github.com/gitleakstest/private",
|
||||
Disk: true,
|
||||
},
|
||||
description: "test private repo",
|
||||
expectedErrMsg: "authentication required",
|
||||
description: "test private repo disk",
|
||||
expectedErrMsg: "repository not found",
|
||||
},
|
||||
}
|
||||
g := goblin.Goblin(t)
|
||||
@@ -90,7 +91,7 @@ func TestGetRepo(t *testing.T) {
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
repo, _ := newRepoInfo()
|
||||
repo, _ := newRepo()
|
||||
err := repo.clone()
|
||||
if err != nil {
|
||||
g.Assert(err.Error()).Equal(test.expectedErrMsg)
|
||||
@@ -125,6 +126,14 @@ func TestRun(t *testing.T) {
|
||||
configPath string
|
||||
commitPerPage int
|
||||
}{
|
||||
{
|
||||
testOpts: &Options{
|
||||
Repo: "https://github.com/gitleakstest/gronit.git",
|
||||
},
|
||||
description: "test leak",
|
||||
numLeaks: 2,
|
||||
expectedErrMsg: "",
|
||||
},
|
||||
{
|
||||
testOpts: &Options{
|
||||
GitLabUser: "gitleakstest",
|
||||
@@ -182,7 +191,7 @@ func TestRun(t *testing.T) {
|
||||
},
|
||||
description: "test leak",
|
||||
numLeaks: 0,
|
||||
expectedErrMsg: "unable to generate ssh key: open trash: no such file or directory",
|
||||
expectedErrMsg: fmt.Sprintf("unable to generate ssh key: open trash: %s", noSuchFileMessage()),
|
||||
},
|
||||
{
|
||||
testOpts: &Options{
|
||||
@@ -206,7 +215,7 @@ func TestRun(t *testing.T) {
|
||||
},
|
||||
description: "test empty",
|
||||
numLeaks: 0,
|
||||
expectedErrMsg: "reference not found",
|
||||
expectedErrMsg: "repository not found",
|
||||
},
|
||||
{
|
||||
testOpts: &Options{
|
||||
@@ -273,11 +282,11 @@ func TestRun(t *testing.T) {
|
||||
if test.commitPerPage != 0 {
|
||||
githubPages = test.commitPerPage
|
||||
}
|
||||
report, err := Run(test.testOpts)
|
||||
numLeaks, err := Run(test.testOpts)
|
||||
if err != nil {
|
||||
g.Assert(err.Error()).Equal(test.expectedErrMsg)
|
||||
} else {
|
||||
g.Assert(len(report.Leaks)).Equal(test.numLeaks)
|
||||
g.Assert(numLeaks).Equal(test.numLeaks)
|
||||
}
|
||||
githubPages = 100
|
||||
})
|
||||
@@ -372,7 +381,6 @@ func TestWriteReport(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestAuditRepo(t *testing.T) {
|
||||
var leaks []Leak
|
||||
configsDir := testTomlLoader()
|
||||
defer os.RemoveAll(configsDir)
|
||||
|
||||
@@ -382,7 +390,7 @@ func TestAuditRepo(t *testing.T) {
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
leaksRepo := &RepoInfo{
|
||||
leaksRepo := &Repo{
|
||||
repository: leaksR,
|
||||
name: "gronit",
|
||||
}
|
||||
@@ -393,7 +401,7 @@ func TestAuditRepo(t *testing.T) {
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
cleanRepo := &RepoInfo{
|
||||
cleanRepo := &Repo{
|
||||
repository: cleanR,
|
||||
name: "h1domains",
|
||||
}
|
||||
@@ -403,7 +411,7 @@ func TestAuditRepo(t *testing.T) {
|
||||
description string
|
||||
expectedErrMsg string
|
||||
numLeaks int
|
||||
repo *RepoInfo
|
||||
repo *Repo
|
||||
whiteListFiles []*regexp.Regexp
|
||||
whiteListCommits map[string]bool
|
||||
whiteListRepos []*regexp.Regexp
|
||||
@@ -615,7 +623,6 @@ func TestAuditRepo(t *testing.T) {
|
||||
for _, test := range tests {
|
||||
g.Describe("TestAuditRepo", func() {
|
||||
g.It(test.description, func() {
|
||||
auditDone = false
|
||||
opts = test.testOpts
|
||||
|
||||
config, err = newConfig()
|
||||
@@ -628,13 +635,14 @@ func TestAuditRepo(t *testing.T) {
|
||||
goto next
|
||||
}
|
||||
}
|
||||
leaks, err = test.repo.audit()
|
||||
err = test.repo.audit()
|
||||
if opts.Redact {
|
||||
g.Assert(leaks[0].Offender).Equal("REDACTED")
|
||||
g.Assert(test.repo.leaks[0].Offender).Equal("REDACTED")
|
||||
}
|
||||
g.Assert(len(leaks)).Equal(test.numLeaks)
|
||||
g.Assert(len(test.repo.leaks)).Equal(test.numLeaks)
|
||||
next:
|
||||
os.Setenv("GITLEAKS_CONFIG", "")
|
||||
test.repo.leaks = []Leak{}
|
||||
})
|
||||
})
|
||||
}
|
||||
@@ -749,7 +757,7 @@ func TestLoadToml(t *testing.T) {
|
||||
testOpts: &Options{},
|
||||
description: "env var path to no config",
|
||||
configPath: noConfigPath,
|
||||
expectedErrMsg: fmt.Sprintf("problem loading config: open %s: no such file or directory", noConfigPath),
|
||||
expectedErrMsg: fmt.Sprintf("problem loading config: open %s: %s", noConfigPath, noSuchFileMessage()),
|
||||
},
|
||||
}
|
||||
|
||||
@@ -773,3 +781,11 @@ func TestLoadToml(t *testing.T) {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func noSuchFileMessage() string {
|
||||
if runtime.GOOS == "windows" {
|
||||
// Adapt to Windws
|
||||
return "The system cannot find the file specified."
|
||||
}
|
||||
return "no such file or directory"
|
||||
}
|
||||
@@ -11,6 +11,7 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/jessevdk/go-flags"
|
||||
colorable "github.com/mattn/go-colorable"
|
||||
log "github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
@@ -150,6 +151,11 @@ func (opts *Options) setLogs() {
|
||||
log.SetLevel(log.InfoLevel)
|
||||
}
|
||||
log.SetFormatter(&log.TextFormatter{
|
||||
ForceColors: true,
|
||||
FullTimestamp: true,
|
||||
})
|
||||
// Fix colors on Windows
|
||||
if runtime.GOOS == "windows" {
|
||||
log.SetOutput(colorable.NewColorableStdout())
|
||||
}
|
||||
}
|
||||
+197
-63
@@ -3,6 +3,7 @@ package gitleaks
|
||||
import (
|
||||
"crypto/md5"
|
||||
"fmt"
|
||||
"github.com/hako/durafmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
@@ -10,14 +11,30 @@ import (
|
||||
"time"
|
||||
|
||||
log "github.com/sirupsen/logrus"
|
||||
git "gopkg.in/src-d/go-git.v4"
|
||||
"gopkg.in/src-d/go-git.v4"
|
||||
"gopkg.in/src-d/go-git.v4/plumbing"
|
||||
diffType "gopkg.in/src-d/go-git.v4/plumbing/format/diff"
|
||||
"gopkg.in/src-d/go-git.v4/plumbing/object"
|
||||
"gopkg.in/src-d/go-git.v4/plumbing/storer"
|
||||
gitHttp "gopkg.in/src-d/go-git.v4/plumbing/transport/http"
|
||||
"gopkg.in/src-d/go-git.v4/storage/memory"
|
||||
"gopkg.in/src-d/go-git.v4/utils/merkletrie"
|
||||
)
|
||||
|
||||
// Commit represents a git commit
|
||||
type Commit struct {
|
||||
content string
|
||||
commit *object.Commit
|
||||
filePath string
|
||||
repoName string
|
||||
sha string
|
||||
message string
|
||||
author string
|
||||
email string
|
||||
date time.Time
|
||||
}
|
||||
|
||||
|
||||
// Leak represents a leaked secret or regex match.
|
||||
type Leak struct {
|
||||
Line string `json:"line"`
|
||||
@@ -35,22 +52,25 @@ type Leak struct {
|
||||
Severity string `json:"severity"`
|
||||
}
|
||||
|
||||
// RepoInfo contains a src-d git repository and other data about the repo
|
||||
type RepoInfo struct {
|
||||
path string
|
||||
url string
|
||||
name string
|
||||
repository *git.Repository
|
||||
err error
|
||||
// Repo contains a src-d git repository and other data about the repo
|
||||
type Repo struct {
|
||||
leaks []Leak
|
||||
path string
|
||||
url string
|
||||
name string
|
||||
repository *git.Repository
|
||||
err error
|
||||
auditDuration string
|
||||
numCommits int64
|
||||
}
|
||||
|
||||
func newRepoInfo() (*RepoInfo, error) {
|
||||
func newRepo() (*Repo, error) {
|
||||
for _, re := range config.WhiteList.repos {
|
||||
if re.FindString(opts.Repo) != "" {
|
||||
return nil, fmt.Errorf("skipping %s, whitelisted", opts.Repo)
|
||||
}
|
||||
}
|
||||
return &RepoInfo{
|
||||
return &Repo{
|
||||
path: opts.RepoPath,
|
||||
url: opts.Repo,
|
||||
name: filepath.Base(opts.Repo),
|
||||
@@ -58,10 +78,10 @@ func newRepoInfo() (*RepoInfo, error) {
|
||||
}
|
||||
|
||||
// clone will clone a repo
|
||||
func (repoInfo *RepoInfo) clone() error {
|
||||
func (repo *Repo) clone() error {
|
||||
var (
|
||||
err error
|
||||
repo *git.Repository
|
||||
err error
|
||||
repository *git.Repository
|
||||
)
|
||||
|
||||
// check if cloning to disk
|
||||
@@ -70,73 +90,98 @@ func (repoInfo *RepoInfo) clone() error {
|
||||
cloneTarget := fmt.Sprintf("%s/%x", dir, md5.Sum([]byte(fmt.Sprintf("%s%s", opts.GithubUser, opts.Repo))))
|
||||
if strings.HasPrefix(opts.Repo, "git") {
|
||||
// private
|
||||
repo, err = git.PlainClone(cloneTarget, false, &git.CloneOptions{
|
||||
repository, err = git.PlainClone(cloneTarget, false, &git.CloneOptions{
|
||||
URL: opts.Repo,
|
||||
Progress: os.Stdout,
|
||||
Auth: config.sshAuth,
|
||||
})
|
||||
} else {
|
||||
// public
|
||||
repo, err = git.PlainClone(cloneTarget, false, &git.CloneOptions{
|
||||
options := &git.CloneOptions{
|
||||
URL: opts.Repo,
|
||||
Progress: os.Stdout,
|
||||
})
|
||||
}
|
||||
if os.Getenv("GITHUB_TOKEN") != "" {
|
||||
options.Auth = &gitHttp.BasicAuth{
|
||||
Username: "fakeUsername", // yes, this can be anything except an empty string
|
||||
Password: os.Getenv("GITHUB_TOKEN"),
|
||||
}
|
||||
}
|
||||
repository, err = git.PlainClone(cloneTarget, false, options)
|
||||
}
|
||||
} else if repoInfo.path != "" {
|
||||
log.Infof("opening %s", opts.RepoPath)
|
||||
repo, err = git.PlainOpen(repoInfo.path)
|
||||
} else if repo.path != "" {
|
||||
log.Infof("opening %s", repo.path)
|
||||
repository, err = git.PlainOpen(repo.path)
|
||||
if err != nil {
|
||||
log.Errorf("unable to open %s", opts.RepoPath)
|
||||
log.Errorf("unable to open %s", repo.path)
|
||||
}
|
||||
} else {
|
||||
// cloning to memory
|
||||
log.Infof("cloning %s", opts.Repo)
|
||||
if strings.HasPrefix(opts.Repo, "git") {
|
||||
repo, err = git.Clone(memory.NewStorage(), nil, &git.CloneOptions{
|
||||
repository, err = git.Clone(memory.NewStorage(), nil, &git.CloneOptions{
|
||||
URL: opts.Repo,
|
||||
Progress: os.Stdout,
|
||||
Auth: config.sshAuth,
|
||||
})
|
||||
} else {
|
||||
repo, err = git.Clone(memory.NewStorage(), nil, &git.CloneOptions{
|
||||
options := &git.CloneOptions{
|
||||
URL: opts.Repo,
|
||||
Progress: os.Stdout,
|
||||
})
|
||||
}
|
||||
if os.Getenv("GITHUB_TOKEN") != "" {
|
||||
options.Auth = &gitHttp.BasicAuth{
|
||||
Username: "fakeUsername", // yes, this can be anything except an empty string
|
||||
Password: os.Getenv("GITHUB_TOKEN"),
|
||||
}
|
||||
}
|
||||
repository, err = git.Clone(memory.NewStorage(), nil, options)
|
||||
}
|
||||
}
|
||||
repoInfo.repository = repo
|
||||
repoInfo.err = err
|
||||
repo.repository = repository
|
||||
repo.err = err
|
||||
return err
|
||||
}
|
||||
|
||||
// audit performs an audit
|
||||
func (repoInfo *RepoInfo) audit() ([]Leak, error) {
|
||||
func (repo *Repo) audit() error {
|
||||
var (
|
||||
err error
|
||||
leaks []Leak
|
||||
commitCount int64
|
||||
commitWg sync.WaitGroup
|
||||
semaphore chan bool
|
||||
logOpts git.LogOptions
|
||||
)
|
||||
for _, re := range config.WhiteList.repos {
|
||||
if re.FindString(repoInfo.name) != "" {
|
||||
return leaks, fmt.Errorf("skipping %s, whitelisted", repoInfo.name)
|
||||
if re.FindString(repo.name) != "" {
|
||||
return fmt.Errorf("skipping %s, whitelisted", repo.name)
|
||||
}
|
||||
}
|
||||
|
||||
start := time.Now()
|
||||
|
||||
// check if target contains an external gitleaks toml
|
||||
if opts.RepoConfig {
|
||||
err := config.updateFromRepo(repoInfo)
|
||||
err := config.updateFromRepo(repo)
|
||||
if err != nil {
|
||||
return leaks, nil
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
if opts.Branch != "" {
|
||||
refs, err := repoInfo.repository.Storer.IterReferences()
|
||||
if opts.Commit != "" {
|
||||
h := plumbing.NewHash(opts.Commit)
|
||||
c, err := repo.repository.CommitObject(h)
|
||||
if err != nil {
|
||||
return leaks, err
|
||||
return err
|
||||
}
|
||||
|
||||
totalCommits = totalCommits + 1
|
||||
repo.numCommits = 1
|
||||
return repo.auditSingleCommit(c)
|
||||
} else if opts.Branch != "" {
|
||||
refs, err := repo.repository.Storer.IterReferences()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = refs.ForEach(func(ref *plumbing.Reference) error {
|
||||
if ref.Name().IsTag() {
|
||||
@@ -163,10 +208,9 @@ func (repoInfo *RepoInfo) audit() ([]Leak, error) {
|
||||
}
|
||||
|
||||
// iterate all through commits
|
||||
cIter, err := repoInfo.repository.Log(&logOpts)
|
||||
|
||||
cIter, err := repo.repository.Log(&logOpts)
|
||||
if err != nil {
|
||||
return leaks, nil
|
||||
return err
|
||||
}
|
||||
|
||||
if opts.Threads != 0 {
|
||||
@@ -187,24 +231,17 @@ func (repoInfo *RepoInfo) audit() ([]Leak, error) {
|
||||
return nil
|
||||
}
|
||||
|
||||
// commits w/o parent (root of git the git ref) or option for single commit is not empty str
|
||||
if (len(c.ParentHashes) == 0 && opts.Commit == "") || (len(c.ParentHashes) == 0 && opts.Commit == c.Hash.String()) {
|
||||
// commits w/o parent (root of git the git ref)
|
||||
if len(c.ParentHashes) == 0 {
|
||||
commitCount = commitCount + 1
|
||||
totalCommits = totalCommits + 1
|
||||
leaksFromSingleCommit := repoInfo.auditSingleCommit(c)
|
||||
mutex.Lock()
|
||||
leaks = append(leaksFromSingleCommit, leaks...)
|
||||
mutex.Unlock()
|
||||
if opts.Commit == c.Hash.String() {
|
||||
return storer.ErrStop
|
||||
err := repo.auditSingleCommit(c)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
if opts.Commit != "" && opts.Commit != c.Hash.String() {
|
||||
return nil
|
||||
}
|
||||
|
||||
commitCount = commitCount + 1
|
||||
totalCommits = totalCommits + 1
|
||||
|
||||
@@ -245,8 +282,8 @@ func (repoInfo *RepoInfo) audit() ([]Leak, error) {
|
||||
for _, fr := range config.FileRules {
|
||||
for _, r := range fr.fileTypes {
|
||||
if r.FindString(filePath) != "" {
|
||||
commitInfo := &commitInfo{
|
||||
repoName: repoInfo.name,
|
||||
commitInfo := &Commit{
|
||||
repoName: repo.name,
|
||||
filePath: filePath,
|
||||
sha: c.Hash.String(),
|
||||
author: c.Author.Name,
|
||||
@@ -256,7 +293,7 @@ func (repoInfo *RepoInfo) audit() ([]Leak, error) {
|
||||
}
|
||||
leak := *newLeak("N/A", fmt.Sprintf("filetype %s found", r.String()), r.String(), fr, commitInfo)
|
||||
mutex.Lock()
|
||||
leaks = append(leaks, leak)
|
||||
repo.leaks = append(repo.leaks, leak)
|
||||
mutex.Unlock()
|
||||
}
|
||||
}
|
||||
@@ -275,8 +312,8 @@ func (repoInfo *RepoInfo) audit() ([]Leak, error) {
|
||||
chunks := f.Chunks()
|
||||
for _, chunk := range chunks {
|
||||
if chunk.Type() == diffType.Add || chunk.Type() == diffType.Delete {
|
||||
diff := &commitInfo{
|
||||
repoName: repoInfo.name,
|
||||
diff := &Commit{
|
||||
repoName: repo.name,
|
||||
filePath: filePath,
|
||||
content: chunk.Content(),
|
||||
sha: c.Hash.String(),
|
||||
@@ -288,7 +325,7 @@ func (repoInfo *RepoInfo) audit() ([]Leak, error) {
|
||||
chunkLeaks := inspect(diff)
|
||||
for _, leak := range chunkLeaks {
|
||||
mutex.Lock()
|
||||
leaks = append(leaks, leak)
|
||||
repo.leaks = append(repo.leaks, leak)
|
||||
mutex.Unlock()
|
||||
}
|
||||
}
|
||||
@@ -303,15 +340,29 @@ func (repoInfo *RepoInfo) audit() ([]Leak, error) {
|
||||
})
|
||||
|
||||
commitWg.Wait()
|
||||
return leaks, nil
|
||||
repo.numCommits = commitCount
|
||||
repo.auditDuration = durafmt.Parse(time.Now().Sub(start)).String()
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (repoInfo *RepoInfo) auditSingleCommit(c *object.Commit) []Leak {
|
||||
var leaks []Leak
|
||||
func (repo *Repo) auditSingleCommit(c *object.Commit) error {
|
||||
fIter, err := c.Files()
|
||||
if err != nil {
|
||||
return nil
|
||||
return err
|
||||
}
|
||||
|
||||
// If current commit has parents then search for leaks in tree change,
|
||||
// that means scan in changed/modified files from one commit to another.
|
||||
if len(c.ParentHashes) > 0 {
|
||||
prevCommitObject, err := c.Parents().Next()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return repo.auditTreeChange(prevCommitObject, c)
|
||||
}
|
||||
|
||||
// Scan for leaks in files related to current commit
|
||||
err = fIter.ForEach(func(f *object.File) error {
|
||||
bin, err := f.IsBinary()
|
||||
if bin || err != nil {
|
||||
@@ -327,8 +378,8 @@ func (repoInfo *RepoInfo) auditSingleCommit(c *object.Commit) []Leak {
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
diff := &commitInfo{
|
||||
repoName: repoInfo.name,
|
||||
diff := &Commit{
|
||||
repoName: repo.name,
|
||||
filePath: f.Name,
|
||||
content: content,
|
||||
sha: c.Hash.String(),
|
||||
@@ -339,9 +390,92 @@ func (repoInfo *RepoInfo) auditSingleCommit(c *object.Commit) []Leak {
|
||||
}
|
||||
fileLeaks := inspect(diff)
|
||||
mutex.Lock()
|
||||
leaks = append(leaks, fileLeaks...)
|
||||
repo.leaks = append(repo.leaks, fileLeaks...)
|
||||
mutex.Unlock()
|
||||
return nil
|
||||
})
|
||||
return leaks
|
||||
return err
|
||||
}
|
||||
|
||||
func (repo *Repo) report() {
|
||||
if len(repo.leaks) != 0 {
|
||||
log.Warnf("%d leaks detected. %d commits inspected in %s", len(repo.leaks), repo.numCommits, repo.auditDuration)
|
||||
} else {
|
||||
log.Infof("No leaks detected. %d commits inspected in %s", repo.numCommits, repo.auditDuration)
|
||||
}
|
||||
}
|
||||
|
||||
// auditTreeChange will search for leaks in changed/modified files from one
|
||||
// commit to another
|
||||
func (repo *Repo) auditTreeChange(src, dst *object.Commit) error {
|
||||
var (
|
||||
skip bool
|
||||
)
|
||||
|
||||
// Get state of src commit
|
||||
srcState, err := src.Tree()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Get state of destination commit
|
||||
dstState, err := dst.Tree()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
changes, err := srcState.Diff(dstState)
|
||||
|
||||
// Run through each change
|
||||
for _, change := range changes {
|
||||
|
||||
// Ignore deleted files
|
||||
action, err := change.Action()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if action == merkletrie.Delete {
|
||||
continue
|
||||
}
|
||||
|
||||
// Get list of involved files
|
||||
_, to, err := change.Files()
|
||||
bin, err := to.IsBinary()
|
||||
if bin || err != nil {
|
||||
continue
|
||||
}
|
||||
|
||||
for _, re := range config.WhiteList.files {
|
||||
if re.FindString(to.Name) != "" {
|
||||
log.Debugf("skipping whitelisted file (matched regex '%s'): %s", re.String(), to.Name)
|
||||
skip = true
|
||||
}
|
||||
}
|
||||
|
||||
if skip {
|
||||
skip = false
|
||||
continue
|
||||
}
|
||||
|
||||
content, err := to.Contents()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
diff := &Commit{
|
||||
repoName: repo.name,
|
||||
filePath: to.Name,
|
||||
content: content,
|
||||
sha: dst.Hash.String(),
|
||||
author: dst.Author.Name,
|
||||
email: dst.Author.Email,
|
||||
message: strings.Replace(dst.Message, "\n", " ", -1),
|
||||
date: dst.Author.When,
|
||||
}
|
||||
fileLeaks := inspect(diff)
|
||||
mutex.Lock()
|
||||
repo.leaks = append(repo.leaks, fileLeaks...)
|
||||
mutex.Unlock()
|
||||
}
|
||||
return nil
|
||||
|
||||
}
|
||||
+6
-19
@@ -11,21 +11,8 @@ import (
|
||||
"time"
|
||||
|
||||
log "github.com/sirupsen/logrus"
|
||||
"gopkg.in/src-d/go-git.v4/plumbing/object"
|
||||
)
|
||||
|
||||
type commitInfo struct {
|
||||
content string
|
||||
commit *object.Commit
|
||||
filePath string
|
||||
repoName string
|
||||
sha string
|
||||
message string
|
||||
author string
|
||||
email string
|
||||
date time.Time
|
||||
}
|
||||
|
||||
// writeReport writes a report to a file specified in the --report= option.
|
||||
// Default format for report is JSON. You can use the --csv option to write the report as a csv
|
||||
func writeReport(leaks []Leak) error {
|
||||
@@ -83,7 +70,7 @@ func writeReport(leaks []Leak) error {
|
||||
}
|
||||
|
||||
// check rule will inspect a single line and return a leak if it encounters one
|
||||
func (rule *Rule) check(line string, commit *commitInfo) (*Leak, error) {
|
||||
func (rule *Rule) check(line string, commit *Commit) (*Leak, error) {
|
||||
var (
|
||||
match string
|
||||
fileMatch string
|
||||
@@ -146,7 +133,7 @@ postEntropy:
|
||||
// a set of regexes set by the config (see gitleaks.toml for example). This function
|
||||
// will skip lines that include a whitelisted regex. A list of leaks is returned.
|
||||
// If verbose mode (-v/--verbose) is set, then checkDiff will log leaks as they are discovered.
|
||||
func inspect(commit *commitInfo) []Leak {
|
||||
func inspect(commit *Commit) []Leak {
|
||||
var leaks []Leak
|
||||
lines := strings.Split(commit.content, "\n")
|
||||
|
||||
@@ -176,7 +163,7 @@ func isLineWhitelisted(line string) bool {
|
||||
return false
|
||||
}
|
||||
|
||||
func newLeak(line string, info string, offender string, rule *Rule, commit *commitInfo) *Leak {
|
||||
func newLeak(line string, info string, offender string, rule *Rule, commit *Commit) *Leak {
|
||||
leak := &Leak{
|
||||
Line: line,
|
||||
Commit: commit.sha,
|
||||
@@ -205,10 +192,10 @@ func newLeak(line string, info string, offender string, rule *Rule, commit *comm
|
||||
|
||||
// discoverRepos walks all the children of `path`. If a child directory
|
||||
// contain a .git subdirectory then that repo will be added to the list of repos returned
|
||||
func discoverRepos(ownerPath string) ([]*RepoInfo, error) {
|
||||
func discoverRepos(ownerPath string) ([]*Repo, error) {
|
||||
var (
|
||||
err error
|
||||
repoDs []*RepoInfo
|
||||
repoDs []*Repo
|
||||
)
|
||||
files, err := ioutil.ReadDir(ownerPath)
|
||||
if err != nil {
|
||||
@@ -217,7 +204,7 @@ func discoverRepos(ownerPath string) ([]*RepoInfo, error) {
|
||||
for _, f := range files {
|
||||
repoPath := path.Join(ownerPath, f.Name())
|
||||
if f.IsDir() && containsGit(repoPath) {
|
||||
repoDs = append(repoDs, &RepoInfo{
|
||||
repoDs = append(repoDs, &Repo{
|
||||
name: f.Name(),
|
||||
path: repoPath,
|
||||
})
|
||||
|
||||
Reference in new issue
Block a user