mirror of
https://github.com/gitleaks/gitleaks.git
synced 2026-11-29 12:44:22 +01:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
9b15f0d33e | ||
|
|
817aa7b8d0 | ||
|
|
33082a9967 | ||
|
|
7dbfe8db2f | ||
|
|
fc98cbf159 | ||
|
|
3f0293dab0 | ||
|
|
4f6ee2bcf1 | ||
|
|
62020539f4 | ||
|
|
c8681e4858 |
No files matched your search
@@ -33,6 +33,7 @@ a42b32bdf11b6f4ea5c32ec76a1731b4b0c5e52a:cmd/generate/config/rules/generic.go:ge
|
|||||||
6e72472b6019d29eaa4b76b39700cd2418741f0c:detect/detect_test.go:aws-access-token:73
|
6e72472b6019d29eaa4b76b39700cd2418741f0c:detect/detect_test.go:aws-access-token:73
|
||||||
6e72472b6019d29eaa4b76b39700cd2418741f0c:detect/detect_test.go:aws-access-token:81
|
6e72472b6019d29eaa4b76b39700cd2418741f0c:detect/detect_test.go:aws-access-token:81
|
||||||
6e72472b6019d29eaa4b76b39700cd2418741f0c:detect/detect_test.go:aws-access-token:89
|
6e72472b6019d29eaa4b76b39700cd2418741f0c:detect/detect_test.go:aws-access-token:89
|
||||||
|
33082a996774ba4c8ad4ba26fd219d77497eb960:README.md:sidekiq-secret:43
|
||||||
6e72472b6019d29eaa4b76b39700cd2418741f0c:detect/detect_test.go:discord-api-token:98
|
6e72472b6019d29eaa4b76b39700cd2418741f0c:detect/detect_test.go:discord-api-token:98
|
||||||
57d2d345b6b6ea220be9d99792b21a75359555c0:detect/detect_test.go:aws-access-token:235
|
57d2d345b6b6ea220be9d99792b21a75359555c0:detect/detect_test.go:aws-access-token:235
|
||||||
57d2d345b6b6ea220be9d99792b21a75359555c0:detect/detect_test.go:aws-access-token:236
|
57d2d345b6b6ea220be9d99792b21a75359555c0:detect/detect_test.go:aws-access-token:236
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
# gitleaks
|
# Gitleaks
|
||||||
|
|
||||||
```
|
```
|
||||||
┌─○───┐
|
┌─○───┐
|
||||||
@@ -30,49 +30,49 @@
|
|||||||
|
|
||||||
Gitleaks is a SAST tool for **detecting** and **preventing** hardcoded secrets like passwords, api keys, and tokens in git repos. Gitleaks is an **easy-to-use, all-in-one solution** for detecting secrets, past or present, in your code.
|
Gitleaks is a SAST tool for **detecting** and **preventing** hardcoded secrets like passwords, api keys, and tokens in git repos. Gitleaks is an **easy-to-use, all-in-one solution** for detecting secrets, past or present, in your code.
|
||||||
|
|
||||||
| Demos |
|
```
|
||||||
| ----------- |
|
➜ ~/code(master) gitleaks detect --source . -v
|
||||||
| CLI [](https://asciinema.org/a/455683)|
|
|
||||||
| [Github-Action](https://github.com/gitleaks/gitleaks-action) |
|
○
|
||||||
|
│╲
|
||||||
|
│ ○
|
||||||
|
○ ░
|
||||||
|
░ 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: john@users.noreply.github.com
|
||||||
|
Date: 2022-08-03T12:31:40Z
|
||||||
|
Fingerprint: cd5226711335c68be1e720b318b7bc3135a30eb2:cmd/generate/config/rules/sidekiq.go:sidekiq-secret:23
|
||||||
|
```
|
||||||
|
|
||||||
## Getting Started
|
## Getting Started
|
||||||
|
|
||||||
Gitleaks 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](https://github.com/zricethezav/gitleaks/releases). In addition, Gitleaks can be implemented as a pre-commit hook directly in your repo or as a GitHub action using [Gitleaks-Action](https://github.com/gitleaks/gitleaks-action) (see demo above).
|
Gitleaks 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](https://github.com/zricethezav/gitleaks/releases). In addition, Gitleaks can be implemented as a pre-commit hook directly in your repo or as a GitHub action using [Gitleaks-Action](https://github.com/gitleaks/gitleaks-action).
|
||||||
|
|
||||||
### MacOS
|
### Installing
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
|
# MacOS
|
||||||
brew install gitleaks
|
brew install gitleaks
|
||||||
```
|
|
||||||
|
|
||||||
### Docker
|
# Docker (DockerHub)
|
||||||
|
|
||||||
#### DockerHub
|
|
||||||
|
|
||||||
```bash
|
|
||||||
docker pull zricethezav/gitleaks:latest
|
docker pull zricethezav/gitleaks:latest
|
||||||
docker run -v ${path_to_host_folder_to_scan}:/path zricethezav/gitleaks:latest [COMMAND] --source="/path" [OPTIONS]
|
docker run -v ${path_to_host_folder_to_scan}:/path zricethezav/gitleaks:latest [COMMAND] --source="/path" [OPTIONS]
|
||||||
```
|
|
||||||
|
|
||||||
#### ghcr.io
|
# Docker (ghcr.io)
|
||||||
|
|
||||||
```bash
|
|
||||||
docker pull ghcr.io/zricethezav/gitleaks:latest
|
docker pull ghcr.io/zricethezav/gitleaks:latest
|
||||||
docker run -v ${path_to_host_folder_to_scan}:/path zricethezav/gitleaks:latest [COMMAND] --source="/path" [OPTIONS]
|
docker run -v ${path_to_host_folder_to_scan}:/path zricethezav/gitleaks:latest [COMMAND] --source="/path" [OPTIONS]
|
||||||
```
|
|
||||||
|
|
||||||
### From Source
|
# From Source
|
||||||
|
|
||||||
1. Download and install Go from https://golang.org/dl/
|
|
||||||
2. Clone the repo
|
|
||||||
|
|
||||||
```bash
|
|
||||||
git clone https://github.com/zricethezav/gitleaks.git
|
git clone https://github.com/zricethezav/gitleaks.git
|
||||||
```
|
|
||||||
|
|
||||||
3. Build the binary
|
|
||||||
|
|
||||||
```bash
|
|
||||||
cd gitleaks
|
cd gitleaks
|
||||||
make build
|
make build
|
||||||
```
|
```
|
||||||
@@ -106,15 +106,16 @@ jobs:
|
|||||||
```
|
```
|
||||||
repos:
|
repos:
|
||||||
- repo: https://github.com/zricethezav/gitleaks
|
- repo: https://github.com/zricethezav/gitleaks
|
||||||
rev: v8.11.2
|
rev: v8.12.0
|
||||||
hooks:
|
hooks:
|
||||||
- id: gitleaks
|
- id: gitleaks
|
||||||
```
|
```
|
||||||
|
|
||||||
for a [native execution of GitLeaks](https://github.com/zricethezav/gitleaks/releases) or use the [`gitleaks-docker` pre-commit ID](https://github.com/zricethezav/gitleaks/blob/master/.pre-commit-hooks.yaml) for executing GitLeaks using the [official Docker images](#docker)
|
for a [native execution of GitLeaks](https://github.com/zricethezav/gitleaks/releases) or use the [`gitleaks-docker` pre-commit ID](https://github.com/zricethezav/gitleaks/blob/master/.pre-commit-hooks.yaml) for executing GitLeaks using the [official Docker images](#docker)
|
||||||
|
|
||||||
3. Install with `pre-commit install`
|
3. Auto-update the config to the latest repos' versions by executing `pre-commit autoupdate`
|
||||||
4. Now you're all set!
|
4. Install with `pre-commit install`
|
||||||
|
5. Now you're all set!
|
||||||
|
|
||||||
```
|
```
|
||||||
➜ git commit -m "this commit contains a secret"
|
➜ git commit -m "this commit contains a secret"
|
||||||
@@ -152,9 +153,11 @@ Flags:
|
|||||||
--exit-code string exit code when leaks have been encountered (default: 1)
|
--exit-code string exit code when leaks have been encountered (default: 1)
|
||||||
-h, --help help for gitleaks
|
-h, --help help for gitleaks
|
||||||
-l, --log-level string log level (debug, info, warn, error, fatal) (default "info")
|
-l, --log-level string log level (debug, info, warn, error, fatal) (default "info")
|
||||||
|
--no-banner suppress banner
|
||||||
--redact redact secrets from logs and stdout
|
--redact redact secrets from logs and stdout
|
||||||
-f, --report-format string output format (json, csv, sarif)
|
-f, --report-format string output format (json, csv, sarif)
|
||||||
-r, --report-path string report file
|
-r, --report-path string report file
|
||||||
|
-b, --baseline-path path to a previously generated report with known issues that gitleaks should ignore
|
||||||
-s, --source string path to source (git repo, directory, file)
|
-s, --source string path to source (git repo, directory, file)
|
||||||
-v, --verbose show verbose output from scan
|
-v, --verbose show verbose output from scan
|
||||||
|
|
||||||
@@ -189,6 +192,23 @@ as a pre-commit.
|
|||||||
|
|
||||||
**NOTE**: the `protect` command can only be used on git repos, running `protect` on files or directories will result in an error message.
|
**NOTE**: the `protect` command can only be used on git repos, running `protect` on files or directories will result in an error message.
|
||||||
|
|
||||||
|
### Creating a baseline
|
||||||
|
|
||||||
|
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 detect --report-path gitleaks-report.json # This will save the report in a file called gitleaks-report.json
|
||||||
|
```
|
||||||
|
|
||||||
|
Once as baseline is created it can be applied when running the detect command again:
|
||||||
|
|
||||||
|
```
|
||||||
|
gitleaks detect --baseline-path gitleaks-report.json --report-path findings.json
|
||||||
|
```
|
||||||
|
|
||||||
|
After running the detect command with the --baseline-path parameter, report output (findings.json) will only contain new issues.
|
||||||
|
|
||||||
### Verify Findings
|
### Verify Findings
|
||||||
|
|
||||||
You can verify a finding found by gitleaks using a `git log` command.
|
You can verify a finding found by gitleaks using a `git log` command.
|
||||||
|
|||||||
@@ -75,6 +75,15 @@ func runDetect(cmd *cobra.Command, args []string) {
|
|||||||
detector.AddGitleaksIgnore(filepath.Join(source, ".gitleaksignore"))
|
detector.AddGitleaksIgnore(filepath.Join(source, ".gitleaksignore"))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ignore findings from the baseline (an existing report in json format generated earlier)
|
||||||
|
baselinePath, _ := cmd.Flags().GetString("baseline-path")
|
||||||
|
if baselinePath != "" {
|
||||||
|
err = detector.AddBaseline(baselinePath)
|
||||||
|
if err != nil {
|
||||||
|
log.Error().Msgf("Could not load baseline. The path must point of a gitleaks report generated using the default format: %s", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// set exit code
|
// set exit code
|
||||||
exitCode, err := cmd.Flags().GetInt("exit-code")
|
exitCode, err := cmd.Flags().GetInt("exit-code")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
@@ -141,6 +141,8 @@ func main() {
|
|||||||
configRules = append(configRules, rules.SquareSpaceAccessToken())
|
configRules = append(configRules, rules.SquareSpaceAccessToken())
|
||||||
configRules = append(configRules, rules.SumoLogicAccessID())
|
configRules = append(configRules, rules.SumoLogicAccessID())
|
||||||
configRules = append(configRules, rules.SumoLogicAccessToken())
|
configRules = append(configRules, rules.SumoLogicAccessToken())
|
||||||
|
configRules = append(configRules, rules.TeamsWebhook())
|
||||||
|
configRules = append(configRules, rules.TelegramBotToken())
|
||||||
configRules = append(configRules, rules.TravisCIAccessToken())
|
configRules = append(configRules, rules.TravisCIAccessToken())
|
||||||
configRules = append(configRules, rules.Twilio())
|
configRules = append(configRules, rules.Twilio())
|
||||||
configRules = append(configRules, rules.TwitchAPIToken())
|
configRules = append(configRules, rules.TwitchAPIToken())
|
||||||
|
|||||||
@@ -11,7 +11,7 @@ func PrivateKey() *config.Rule {
|
|||||||
r := config.Rule{
|
r := config.Rule{
|
||||||
Description: "Private Key",
|
Description: "Private Key",
|
||||||
RuleID: "private-key",
|
RuleID: "private-key",
|
||||||
Regex: regexp.MustCompile(`(?i)-----BEGIN[ A-Z0-9_-]{0,100}PRIVATE KEY-----[\s\S-]*KEY----`),
|
Regex: regexp.MustCompile(`(?i)-----BEGIN[ A-Z0-9_-]{0,100}PRIVATE KEY( BLOCK)?-----[\s\S-]*KEY----`),
|
||||||
Keywords: []string{"-----BEGIN"},
|
Keywords: []string{"-----BEGIN"},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,29 @@
|
|||||||
|
package rules
|
||||||
|
|
||||||
|
import (
|
||||||
|
"regexp"
|
||||||
|
|
||||||
|
"github.com/zricethezav/gitleaks/v8/cmd/generate/secrets"
|
||||||
|
"github.com/zricethezav/gitleaks/v8/config"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TeamsWebhook() *config.Rule {
|
||||||
|
// define rule
|
||||||
|
r := config.Rule{
|
||||||
|
Description: "Microsoft Teams Webhook",
|
||||||
|
RuleID: "microsoft-teams-webhook",
|
||||||
|
Regex: regexp.MustCompile(
|
||||||
|
`https:\/\/[a-z0-9]+\.webhook\.office\.com\/webhookb2\/[a-z0-9]{8}-([a-z0-9]{4}-){3}[a-z0-9]{12}@[a-z0-9]{8}-([a-z0-9]{4}-){3}[a-z0-9]{12}\/IncomingWebhook\/[a-z0-9]{32}\/[a-z0-9]{8}-([a-z0-9]{4}-){3}[a-z0-9]{12}`),
|
||||||
|
Keywords: []string{
|
||||||
|
"webhook.office.com",
|
||||||
|
"webhookb2",
|
||||||
|
"IncomingWebhook",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
// validate
|
||||||
|
tps := []string{
|
||||||
|
"https://mycompany.webhook.office.com/webhookb2/" + secrets.NewSecret(`[a-z0-9]{8}-([a-z0-9]{4}-){3}[a-z0-9]{12}@[a-z0-9]{8}-([a-z0-9]{4}-){3}[a-z0-9]{12}\/IncomingWebhook\/[a-z0-9]{32}\/[a-z0-9]{8}-([a-z0-9]{4}-){3}[a-z0-9]{12}`), // gitleaks:allow
|
||||||
|
}
|
||||||
|
return validate(r, tps, nil)
|
||||||
|
}
|
||||||
@@ -0,0 +1,57 @@
|
|||||||
|
package rules
|
||||||
|
|
||||||
|
import (
|
||||||
|
"regexp"
|
||||||
|
|
||||||
|
"github.com/zricethezav/gitleaks/v8/cmd/generate/secrets"
|
||||||
|
"github.com/zricethezav/gitleaks/v8/config"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TelegramBotToken() *config.Rule {
|
||||||
|
// define rule
|
||||||
|
r := config.Rule{
|
||||||
|
Description: "Telegram Bot API Token",
|
||||||
|
RuleID: "telegram-bot-api-token",
|
||||||
|
SecretGroup: 1,
|
||||||
|
Regex: regexp.MustCompile(`(?i)(?:^|[^0-9])([0-9]{5,16}:A[a-zA-Z0-9_\-]{34})(?:$|[^a-zA-Z0-9_\-])`),
|
||||||
|
Keywords: []string{
|
||||||
|
"telegram",
|
||||||
|
"api",
|
||||||
|
"bot",
|
||||||
|
"token",
|
||||||
|
"url",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
// validate
|
||||||
|
validToken := secrets.NewSecret(numeric("8") + ":A" + alphaNumericExtendedShort("34"))
|
||||||
|
minToken := secrets.NewSecret(numeric("5") + ":A" + alphaNumericExtendedShort("34"))
|
||||||
|
maxToken := secrets.NewSecret(numeric("16") + ":A" + alphaNumericExtendedShort("34"))
|
||||||
|
tps := []string{
|
||||||
|
// variable assigment
|
||||||
|
generateSampleSecret("telegram", validToken),
|
||||||
|
// URL contaning token
|
||||||
|
generateSampleSecret("url", "https://api.telegram.org/bot"+validToken+"/sendMessage"),
|
||||||
|
// object constructor
|
||||||
|
`const bot = new Telegraf("` + validToken + `")`,
|
||||||
|
// .env
|
||||||
|
`API_TOKEN = ` + validToken,
|
||||||
|
// YAML
|
||||||
|
`bot: ` + validToken,
|
||||||
|
// Token with min bot_id
|
||||||
|
generateSampleSecret("telegram", minToken),
|
||||||
|
// Token with max bot_id
|
||||||
|
generateSampleSecret("telegram", maxToken),
|
||||||
|
}
|
||||||
|
|
||||||
|
tooSmallToken := secrets.NewSecret(numeric("4") + ":A" + alphaNumericExtendedShort("34"))
|
||||||
|
tooBigToken := secrets.NewSecret(numeric("17") + ":A" + alphaNumericExtendedShort("34"))
|
||||||
|
fps := []string{
|
||||||
|
// Token with too small bot_id
|
||||||
|
generateSampleSecret("telegram", tooSmallToken),
|
||||||
|
// Token with too big bot_id
|
||||||
|
generateSampleSecret("telegram", tooBigToken),
|
||||||
|
}
|
||||||
|
|
||||||
|
return validate(r, tps, fps)
|
||||||
|
}
|
||||||
+6
-1
@@ -42,9 +42,11 @@ func init() {
|
|||||||
rootCmd.PersistentFlags().StringP("source", "s", ".", "path to source (default: $PWD)")
|
rootCmd.PersistentFlags().StringP("source", "s", ".", "path to source (default: $PWD)")
|
||||||
rootCmd.PersistentFlags().StringP("report-path", "r", "", "report file")
|
rootCmd.PersistentFlags().StringP("report-path", "r", "", "report file")
|
||||||
rootCmd.PersistentFlags().StringP("report-format", "f", "json", "output format (json, csv, sarif)")
|
rootCmd.PersistentFlags().StringP("report-format", "f", "json", "output format (json, csv, sarif)")
|
||||||
|
rootCmd.PersistentFlags().StringP("baseline-path", "b", "", "path to baseline with issues that can be ignored")
|
||||||
rootCmd.PersistentFlags().StringP("log-level", "l", "info", "log level (trace, debug, info, warn, error, fatal)")
|
rootCmd.PersistentFlags().StringP("log-level", "l", "info", "log level (trace, debug, info, warn, error, fatal)")
|
||||||
rootCmd.PersistentFlags().BoolP("verbose", "v", false, "show verbose output from scan")
|
rootCmd.PersistentFlags().BoolP("verbose", "v", false, "show verbose output from scan")
|
||||||
rootCmd.PersistentFlags().Bool("redact", false, "redact secrets from logs and stdout")
|
rootCmd.PersistentFlags().Bool("redact", false, "redact secrets from logs and stdout")
|
||||||
|
rootCmd.PersistentFlags().Bool("no-banner", false, "suppress banner")
|
||||||
err := viper.BindPFlag("config", rootCmd.PersistentFlags().Lookup("config"))
|
err := viper.BindPFlag("config", rootCmd.PersistentFlags().Lookup("config"))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal().Msgf("err binding config %s", err.Error())
|
log.Fatal().Msgf("err binding config %s", err.Error())
|
||||||
@@ -76,7 +78,10 @@ func initLog() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func initConfig() {
|
func initConfig() {
|
||||||
fmt.Fprint(os.Stderr, banner)
|
hideBanner, err := rootCmd.Flags().GetBool("no-banner")
|
||||||
|
if !hideBanner {
|
||||||
|
fmt.Fprint(os.Stderr, banner)
|
||||||
|
}
|
||||||
cfgPath, err := rootCmd.Flags().GetString("config")
|
cfgPath, err := rootCmd.Flags().GetString("config")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal().Msg(err.Error())
|
log.Fatal().Msg(err.Error())
|
||||||
|
|||||||
+18
-1
@@ -2227,6 +2227,14 @@ keywords = [
|
|||||||
"messagebird","message-bird","message_bird",
|
"messagebird","message-bird","message_bird",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[rules]]
|
||||||
|
description = "Microsoft Teams Webhook"
|
||||||
|
id = "microsoft-teams-webhook"
|
||||||
|
regex = '''https:\/\/[a-z0-9]+\.webhook\.office\.com\/webhookb2\/[a-z0-9]{8}-([a-z0-9]{4}-){3}[a-z0-9]{12}@[a-z0-9]{8}-([a-z0-9]{4}-){3}[a-z0-9]{12}\/IncomingWebhook\/[a-z0-9]{32}\/[a-z0-9]{8}-([a-z0-9]{4}-){3}[a-z0-9]{12}'''
|
||||||
|
keywords = [
|
||||||
|
"webhook.office.com","webhookb2","incomingwebhook",
|
||||||
|
]
|
||||||
|
|
||||||
[[rules]]
|
[[rules]]
|
||||||
description = "Netlify Access Token"
|
description = "Netlify Access Token"
|
||||||
id = "netlify-access-token"
|
id = "netlify-access-token"
|
||||||
@@ -2365,7 +2373,7 @@ keywords = [
|
|||||||
[[rules]]
|
[[rules]]
|
||||||
description = "Private Key"
|
description = "Private Key"
|
||||||
id = "private-key"
|
id = "private-key"
|
||||||
regex = '''(?i)-----BEGIN[ A-Z0-9_-]{0,100}PRIVATE KEY-----[\s\S-]*KEY----'''
|
regex = '''(?i)-----BEGIN[ A-Z0-9_-]{0,100}PRIVATE KEY( BLOCK)?-----[\s\S-]*KEY----'''
|
||||||
keywords = [
|
keywords = [
|
||||||
"-----begin",
|
"-----begin",
|
||||||
]
|
]
|
||||||
@@ -2577,6 +2585,15 @@ keywords = [
|
|||||||
"sumo",
|
"sumo",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[rules]]
|
||||||
|
description = "Telegram Bot API Token"
|
||||||
|
id = "telegram-bot-api-token"
|
||||||
|
regex = '''(?i)(?:^|[^0-9])([0-9]{5,16}:A[a-zA-Z0-9_\-]{34})(?:$|[^a-zA-Z0-9_\-])'''
|
||||||
|
secretGroup = 1
|
||||||
|
keywords = [
|
||||||
|
"telegram","api","bot","token","url",
|
||||||
|
]
|
||||||
|
|
||||||
[[rules]]
|
[[rules]]
|
||||||
description = "Travis CI Access Token"
|
description = "Travis CI Access Token"
|
||||||
id = "travisci-access-token"
|
id = "travisci-access-token"
|
||||||
|
|||||||
@@ -0,0 +1,58 @@
|
|||||||
|
package detect
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
"io/ioutil"
|
||||||
|
"os"
|
||||||
|
|
||||||
|
"github.com/zricethezav/gitleaks/v8/report"
|
||||||
|
)
|
||||||
|
|
||||||
|
func IsNew(finding report.Finding, baseline []report.Finding) bool {
|
||||||
|
// Explicitly testing each property as it gives significantly better performance in comparison to cmp.Equal(). Drawback is that
|
||||||
|
// the code requires maintanance if/when the Finding struct changes
|
||||||
|
for _, b := range baseline {
|
||||||
|
|
||||||
|
if finding.Author == b.Author &&
|
||||||
|
finding.Commit == b.Commit &&
|
||||||
|
finding.Date == b.Date &&
|
||||||
|
finding.Description == b.Description &&
|
||||||
|
finding.Email == b.Email &&
|
||||||
|
finding.EndColumn == b.EndColumn &&
|
||||||
|
finding.EndLine == b.EndLine &&
|
||||||
|
finding.Entropy == b.Entropy &&
|
||||||
|
finding.File == b.File &&
|
||||||
|
// Omit checking finding.Fingerprint - if the format of the fingerprint changes, the users will see unexpected behaviour
|
||||||
|
finding.Match == b.Match &&
|
||||||
|
finding.Message == b.Message &&
|
||||||
|
finding.RuleID == b.RuleID &&
|
||||||
|
finding.Secret == b.Secret &&
|
||||||
|
finding.StartColumn == b.StartColumn &&
|
||||||
|
finding.StartLine == b.StartLine {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
func LoadBaseline(baselinePath string) ([]report.Finding, error) {
|
||||||
|
var previousFindings []report.Finding
|
||||||
|
jsonFile, err := os.Open(baselinePath)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("could not open %s", baselinePath)
|
||||||
|
}
|
||||||
|
|
||||||
|
bytes, err := ioutil.ReadAll(jsonFile)
|
||||||
|
jsonFile.Close()
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("could not read data from the file %s", baselinePath)
|
||||||
|
}
|
||||||
|
|
||||||
|
err = json.Unmarshal(bytes, &previousFindings)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("the format of the file %s is not supported", baselinePath)
|
||||||
|
}
|
||||||
|
|
||||||
|
return previousFindings, nil
|
||||||
|
}
|
||||||
@@ -0,0 +1,137 @@
|
|||||||
|
package detect
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
"github.com/zricethezav/gitleaks/v8/report"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestIsNew(t *testing.T) {
|
||||||
|
tests := []struct {
|
||||||
|
findings report.Finding
|
||||||
|
baseline []report.Finding
|
||||||
|
expect bool
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
findings: report.Finding{
|
||||||
|
Author: "a",
|
||||||
|
Commit: "0000",
|
||||||
|
},
|
||||||
|
baseline: []report.Finding{
|
||||||
|
{
|
||||||
|
Author: "a",
|
||||||
|
Commit: "0000",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
expect: false,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
findings: report.Finding{
|
||||||
|
Author: "a",
|
||||||
|
Commit: "0000",
|
||||||
|
},
|
||||||
|
baseline: []report.Finding{
|
||||||
|
{
|
||||||
|
Author: "a",
|
||||||
|
Commit: "0002",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
expect: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
findings: report.Finding{
|
||||||
|
Author: "a",
|
||||||
|
Commit: "0000",
|
||||||
|
Tags: []string{"a", "b"},
|
||||||
|
},
|
||||||
|
baseline: []report.Finding{
|
||||||
|
{
|
||||||
|
Author: "a",
|
||||||
|
Commit: "0000",
|
||||||
|
Tags: []string{"a", "c"},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
expect: false, // Updated tags doesn't make it a new finding
|
||||||
|
},
|
||||||
|
}
|
||||||
|
for _, test := range tests {
|
||||||
|
assert.Equal(t, test.expect, IsNew(test.findings, test.baseline))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestFileLoadBaseline(t *testing.T) {
|
||||||
|
tests := []struct {
|
||||||
|
Filename string
|
||||||
|
ExpectedError error
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
Filename: "../testdata/baseline/baseline.csv",
|
||||||
|
ExpectedError: errors.New("the format of the file ../testdata/baseline/baseline.csv is not supported"),
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Filename: "../testdata/baseline/baseline.sarif",
|
||||||
|
ExpectedError: errors.New("the format of the file ../testdata/baseline/baseline.sarif is not supported"),
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Filename: "../testdata/baseline/notfound.json",
|
||||||
|
ExpectedError: errors.New("could not open ../testdata/baseline/notfound.json"),
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, test := range tests {
|
||||||
|
_, err := LoadBaseline(test.Filename)
|
||||||
|
assert.Equal(t, test.ExpectedError.Error(), err.Error())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestIgnoreIssuesInBaseline(t *testing.T) {
|
||||||
|
tests := []struct {
|
||||||
|
findings []report.Finding
|
||||||
|
baseline []report.Finding
|
||||||
|
expectCount int
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
findings: []report.Finding{
|
||||||
|
{
|
||||||
|
Author: "a",
|
||||||
|
Commit: "5",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
baseline: []report.Finding{
|
||||||
|
{
|
||||||
|
Author: "a",
|
||||||
|
Commit: "5",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
expectCount: 0,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
findings: []report.Finding{
|
||||||
|
{
|
||||||
|
Author: "a",
|
||||||
|
Commit: "5",
|
||||||
|
Fingerprint: "a",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
baseline: []report.Finding{
|
||||||
|
{
|
||||||
|
Author: "a",
|
||||||
|
Commit: "5",
|
||||||
|
Fingerprint: "b",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
expectCount: 0,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, test := range tests {
|
||||||
|
d, _ := NewDetectorDefaultConfig()
|
||||||
|
d.baseline = test.baseline
|
||||||
|
for _, finding := range test.findings {
|
||||||
|
d.addFinding(finding)
|
||||||
|
}
|
||||||
|
assert.Equal(t, test.expectCount, len(d.findings))
|
||||||
|
}
|
||||||
|
}
|
||||||
+24
-1
@@ -66,6 +66,12 @@ type Detector struct {
|
|||||||
// matching given a set of words (keywords from the rules in the config)
|
// matching given a set of words (keywords from the rules in the config)
|
||||||
prefilter ahocorasick.AhoCorasick
|
prefilter ahocorasick.AhoCorasick
|
||||||
|
|
||||||
|
// a list of known findings that should be ignored
|
||||||
|
baseline []report.Finding
|
||||||
|
|
||||||
|
// path to baseline
|
||||||
|
baselinePath string
|
||||||
|
|
||||||
// gitleaksIgnore
|
// gitleaksIgnore
|
||||||
gitleaksIgnore map[string]bool
|
gitleaksIgnore map[string]bool
|
||||||
}
|
}
|
||||||
@@ -145,6 +151,18 @@ func (d *Detector) AddGitleaksIgnore(gitleaksIgnorePath string) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (d *Detector) AddBaseline(baselinePath string) error {
|
||||||
|
if baselinePath != "" {
|
||||||
|
baseline, err := LoadBaseline(baselinePath)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
d.baseline = baseline
|
||||||
|
}
|
||||||
|
d.baselinePath = baselinePath
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
// DetectBytes scans the given bytes and returns a list of findings
|
// DetectBytes scans the given bytes and returns a list of findings
|
||||||
func (d *Detector) DetectBytes(content []byte) []report.Finding {
|
func (d *Detector) DetectBytes(content []byte) []report.Finding {
|
||||||
return d.DetectString(string(content))
|
return d.DetectString(string(content))
|
||||||
@@ -424,7 +442,7 @@ func (d *Detector) Detect(fragment Fragment) []report.Finding {
|
|||||||
|
|
||||||
// check if filepath is allowed
|
// check if filepath is allowed
|
||||||
if fragment.FilePath != "" && (d.Config.Allowlist.PathAllowed(fragment.FilePath) ||
|
if fragment.FilePath != "" && (d.Config.Allowlist.PathAllowed(fragment.FilePath) ||
|
||||||
fragment.FilePath == d.Config.Path) {
|
fragment.FilePath == d.Config.Path || (d.baselinePath != "" && fragment.FilePath == d.baselinePath)) {
|
||||||
return findings
|
return findings
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -473,6 +491,11 @@ func (d *Detector) addFinding(finding report.Finding) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if d.baseline != nil && !IsNew(finding, d.baseline) {
|
||||||
|
log.Debug().Msgf("baseline duplicate -- ignoring finding with Fingerprint %s", finding.Fingerprint)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
d.findingMutex.Lock()
|
d.findingMutex.Lock()
|
||||||
d.findings = append(d.findings, finding)
|
d.findings = append(d.findings, finding)
|
||||||
if d.Verbose {
|
if d.Verbose {
|
||||||
|
|||||||
Vendored
+2
@@ -0,0 +1,2 @@
|
|||||||
|
RuleID,Commit,File,Secret,Match,StartLine,EndLine,StartColumn,EndColumn,Author,Message,Date,Email,Fingerprint
|
||||||
|
1,b,c,f,s,m,s,e,s,e,a,m,f,r,f
|
||||||
|
Vendored
+40
@@ -0,0 +1,40 @@
|
|||||||
|
[
|
||||||
|
{
|
||||||
|
"Description": "PyPI upload token",
|
||||||
|
"StartLine": 32,
|
||||||
|
"EndLine": 32,
|
||||||
|
"StartColumn": 21,
|
||||||
|
"EndColumn": 106,
|
||||||
|
"Match": "************************",
|
||||||
|
"Secret": "************************",
|
||||||
|
"File": "detect/detect_test.go",
|
||||||
|
"Commit": "9326f35380636bcbe61e94b0584d1618c4b5c2c2",
|
||||||
|
"Entropy": 1.9606875,
|
||||||
|
"Author": "****",
|
||||||
|
"Email": "****",
|
||||||
|
"Date": "2022-03-07T14:33:06Z",
|
||||||
|
"Message": "Escape - character in regex character groups (#802)\n\n* fix char escape\n\n* add test\n\n* fix verbosity in make test",
|
||||||
|
"Tags": [],
|
||||||
|
"RuleID": "pypi-upload-token",
|
||||||
|
"Fingerprint": "9326f35380636bcbe61e94b0584d1618c4b5c2c2:detect/detect_test.go:pypi-upload-token:32"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"Description": "PyPI upload token",
|
||||||
|
"StartLine": 33,
|
||||||
|
"EndLine": 33,
|
||||||
|
"StartColumn": 21,
|
||||||
|
"EndColumn": 106,
|
||||||
|
"Match": "************************",
|
||||||
|
"Secret": "************************",
|
||||||
|
"File": "detect/detect_test.go",
|
||||||
|
"Commit": "9326f35380636bcbe61e94b0584d1618c4b5c2c2",
|
||||||
|
"Entropy": 1.9606875,
|
||||||
|
"Author": "****",
|
||||||
|
"Email": "****",
|
||||||
|
"Date": "2022-03-07T14:33:06Z",
|
||||||
|
"Message": "Escape - character in regex character groups (#802)\n\n* fix char escape\n\n* add test\n\n* fix verbosity in make test",
|
||||||
|
"Tags": [],
|
||||||
|
"RuleID": "pypi-upload-token",
|
||||||
|
"Fingerprint": "9326f35380636bcbe61e94b0584d1618c4b5c2c2:detect/detect_test.go:pypi-upload-token:33"
|
||||||
|
}
|
||||||
|
]
|
||||||
Vendored
+6
@@ -0,0 +1,6 @@
|
|||||||
|
{
|
||||||
|
"$schema": "https://schemastore.azurewebsites.net/schemas/json/sarif-2.1.0-rtm.5.json",
|
||||||
|
"version": "2.1.0",
|
||||||
|
"runs": [
|
||||||
|
]
|
||||||
|
}
|
||||||
Reference in new issue
Block a user