mirror of
https://github.com/gitleaks/gitleaks.git
synced 2026-11-27 03:34:22 +01:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
aae23acff3 | ||
|
|
705595aaf6 | ||
|
|
43ec22429f | ||
|
|
36779df3bb | ||
|
|
3d3d801729 | ||
|
|
0f2ffee6f9 | ||
|
|
f4d37a2c0d | ||
|
|
be5596df1d | ||
|
|
e97f26bb5f | ||
|
|
ae13c96df8 | ||
|
|
d15dfdc387 | ||
|
|
631d8dcf03 | ||
|
|
192b96289f | ||
|
|
b8141713e8 | ||
|
|
3fedf6f8e7 | ||
|
|
6f6ebd454f | ||
|
|
ce42947cae | ||
|
|
84e285e4ce | ||
|
|
928c6a6043 | ||
|
|
106897fdf0 |
No files matched your search
@@ -9,6 +9,12 @@ builds:
|
||||
- windows
|
||||
goarch:
|
||||
- amd64
|
||||
- "386"
|
||||
- arm
|
||||
- arm64
|
||||
goarm:
|
||||
- "6"
|
||||
- "7"
|
||||
ldflags:
|
||||
- -s -w -X=github.com/zricethezav/gitleaks/v8/cmd.Version={{.Version}}
|
||||
archives:
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
- id: gitleaks
|
||||
name: Detect hardcoded secrets
|
||||
description: Detect hardcoded secrets using Gitleaks
|
||||
entry: gitleaks
|
||||
entry: gitleaks protect --verbose --redact --staged
|
||||
language: golang
|
||||
pass_filenames: false
|
||||
@@ -22,11 +22,9 @@
|
||||
</p>
|
||||
</p>
|
||||
|
||||
Gitleaks is a SAST tool for detecting 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.
|
||||
|
||||
#### 💫⭐✨ Temporary README message 💫⭐✨
|
||||
It would be so great if you could fill out this quick gitleaks [user survey](https://docs.google.com/forms/d/1poUqZfEamDY1kCp8v8hU6N3fUj8C5_lVNBD_cDe-GT4).
|
||||
<3
|
||||
[](https://asciinema.org/a/455683)
|
||||
|
||||
## 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.
|
||||
@@ -61,6 +59,28 @@ cd gitleaks
|
||||
make build
|
||||
```
|
||||
|
||||
### Pre-Commit
|
||||
1. Install pre-commit from https://pre-commit.com/#install
|
||||
2. Create a `.pre-commit-config.yaml` file at the root of your repository with the following content:
|
||||
```
|
||||
repos:
|
||||
- repo: https://github.com/zricethezav/gitleaks
|
||||
rev: v8.2.0
|
||||
hooks:
|
||||
- id: gitleaks
|
||||
```
|
||||
3. Install with `pre-commit install`
|
||||
4. Now you're all set!
|
||||
```
|
||||
➜ git commit -m "this commit contains a secret"
|
||||
Detect hardcoded secrets.................................................Failed
|
||||
```
|
||||
Note: to disable the gitleaks pre-commit hook you can prepend `SKIP=gitleaks` to the commit command
|
||||
and it will skip running gitleaks
|
||||
```
|
||||
➜ SKIP=gitleaks git commit -m "skip gitleaks check"
|
||||
Detect hardcoded secrets................................................Skipped
|
||||
```
|
||||
|
||||
## Usage
|
||||
```
|
||||
@@ -78,9 +98,9 @@ Flags:
|
||||
-c, --config string config file path
|
||||
order of precedence:
|
||||
1. --config/-c
|
||||
2. (--source/-s)/.gitleaks.toml
|
||||
if --config/-c is not set and no (--source/-s)/.gitleaks.toml present
|
||||
then .gitleaks.toml will be written to (--source/-s)/.gitleaks.toml for future use
|
||||
2. env var GITLEAKS_CONFIG
|
||||
3. (--source/-s)/.gitleaks.toml
|
||||
If none of the three options are used, then gitleaks will use the default config
|
||||
--exit-code string exit code when leaks have been encountered (default: 1)
|
||||
-h, --help help for gitleaks
|
||||
-l, --log-level string log level (debug, info, warn, error, fatal) (default "info")
|
||||
@@ -96,22 +116,22 @@ Use "gitleaks [command] --help" for more information about a command.
|
||||
### Commands
|
||||
There are two commands you will use to detect secrets; `detect` and `protect`.
|
||||
#### Detect
|
||||
The `detect` command is used to scan repos, directories, and files. This comand can be used on developer machines and in CI environments.
|
||||
The `detect` command is used to scan repos, directories, and files. This comand can be used on developer machines and in CI environments.
|
||||
|
||||
When running `detect` on a git repository, gitleaks will parse the output of a `git log -p` command (you can see how this executed
|
||||
[here](https://github.com/zricethezav/gitleaks/blob/7240e16769b92d2a1b137c17d6bf9d55a8562899/git/git.go#L17-L25)).
|
||||
[`git log -p` generates patches](https://git-scm.com/docs/git-log#_generating_patch_text_with_p) which gitleaks will use to detect secrets.
|
||||
You can configure what commits `git log` will range over by using the `--log-opts` flag. `--log-opts` accepts any option for `git log -p`.
|
||||
For example, if you wanted to run gitleaks on a range of commits you could use the following command: `gitleaks --source . --log-opts="--all commitA..commitB"`.
|
||||
When running `detect` on a git repository, gitleaks will parse the output of a `git log -p` command (you can see how this executed
|
||||
[here](https://github.com/zricethezav/gitleaks/blob/7240e16769b92d2a1b137c17d6bf9d55a8562899/git/git.go#L17-L25)).
|
||||
[`git log -p` generates patches](https://git-scm.com/docs/git-log#_generating_patch_text_with_p) which gitleaks will use to detect secrets.
|
||||
You can configure what commits `git log` will range over by using the `--log-opts` flag. `--log-opts` accepts any option for `git log -p`.
|
||||
For example, if you wanted to run gitleaks on a range of commits you could use the following command: `gitleaks --source . --log-opts="--all commitA..commitB"`.
|
||||
See the `git log` [documentation](https://git-scm.com/docs/git-log) for more information.
|
||||
|
||||
You can scan files and directories by using the `--no-git` option.
|
||||
|
||||
#### Protect
|
||||
The `protect` command is used to uncommitted changes in a git repo. This command should be used on developer machines in accordance with
|
||||
[shifting left on security](https://cloud.google.com/architecture/devops/devops-tech-shifting-left-on-security).
|
||||
When running `protect` on a git repository, gitleaks will parse the output of a `git diff` command (you can see how this executed
|
||||
[here](https://github.com/zricethezav/gitleaks/blob/7240e16769b92d2a1b137c17d6bf9d55a8562899/git/git.go#L48-L49)). You can set the
|
||||
The `protect` command is used to uncommitted changes in a git repo. This command should be used on developer machines in accordance with
|
||||
[shifting left on security](https://cloud.google.com/architecture/devops/devops-tech-shifting-left-on-security).
|
||||
When running `protect` on a git repository, gitleaks will parse the output of a `git diff` command (you can see how this executed
|
||||
[here](https://github.com/zricethezav/gitleaks/blob/7240e16769b92d2a1b137c17d6bf9d55a8562899/git/git.go#L48-L49)). You can set the
|
||||
`--staged` flag to check for changes in commits that have been `git add`ed. The `--staged` flag should be used when running Gitleaks
|
||||
as a pre-commit.
|
||||
|
||||
@@ -168,34 +188,34 @@ diff --git a/checks_test.go b/checks_test.go
|
||||
```
|
||||
|
||||
## Pre-Commit hook
|
||||
You can run Gitleaks as a pre-commit hook by copying the example `pre-commit.py` script into
|
||||
You can run Gitleaks as a pre-commit hook by copying the example `pre-commit.py` script into
|
||||
your `.git/hooks/` directory.
|
||||
|
||||
## Configuration
|
||||
Gitleaks offers a configuration format you can follow to write your own secret detection rules:
|
||||
```toml
|
||||
# Title for the gitleaks configuration file.
|
||||
# Title for the gitleaks configuration file.
|
||||
title = "Gitleaks title"
|
||||
|
||||
# An array of tables that contain information that define instructions
|
||||
# on how to detect secrets
|
||||
# on how to detect secrets
|
||||
[[rules]]
|
||||
# Unique identifier for this rule
|
||||
id = "awesome-rule-1"
|
||||
# Short human readable description of the rule.
|
||||
description = "awsome rule 1"
|
||||
description = "awsome rule 1"
|
||||
# Golang regular expression used to detect secrets. Note Golang's regex engine
|
||||
# does not support lookaheads.
|
||||
regex = '''one-go-style-regex-for-this-rule'''
|
||||
regex = '''one-go-style-regex-for-this-rule'''
|
||||
# Golang regular expression used to match paths. This can be used as a standalone rule or it can be used
|
||||
# in conjunction with a valid `regex` entry.
|
||||
path = '''a-file-path-regex'''
|
||||
# Array of strings used for metadata and reporting purposes.
|
||||
tags = ["tag","another tag"]
|
||||
# Int used to extract secret from regex match and used as the group that will have
|
||||
# its entropy checked if `entropy` is set.
|
||||
# Int used to extract secret from regex match and used as the group that will have
|
||||
# its entropy checked if `entropy` is set.
|
||||
secretGroup = 3
|
||||
# Float representing the minimum shannon entropy a regex group must have to be considered a secret.
|
||||
# Float representing the minimum shannon entropy a regex group must have to be considered a secret.
|
||||
entropy = 3.5
|
||||
# You can include an allowlist table for a single rule to reduce false positives or ignore commits
|
||||
# with known/rotated secrets
|
||||
@@ -206,7 +226,7 @@ paths = ['''one-file-path-regex''']
|
||||
regexes = ['''one-regex-within-the-already-matched-regex''']
|
||||
|
||||
# This is a global allowlist which has a higher order of precendence than rule-specific allowlists.
|
||||
# If a commit listed in the `commits` field below is encountered then that commit will be skipped and no
|
||||
# If a commit listed in the `commits` field below is encountered then that commit will be skipped and no
|
||||
# secrets will be detected for said commit. The same logic applies for regexes and paths.
|
||||
[allowlist]
|
||||
description = "ignore commit A"
|
||||
@@ -219,36 +239,36 @@ Refer to the default [gitleaks config](https://github.com/zricethezav/gitleaks/b
|
||||
### Tips on Writing Regular Expressions
|
||||
Gitleaks rules are defined by regular expressions and entropy ranges.
|
||||
Some secrets have unique signatures which make detecting those secrets easy.
|
||||
Examples of those secrets would be Gitlab Personal Access Tokens, AWS keys, and Github Access Tokens.
|
||||
Examples of those secrets would be Gitlab Personal Access Tokens, AWS keys, and Github Access Tokens.
|
||||
All these examples have defined prefixes like `glpat`, `AKIA`, `ghp_`, etc.
|
||||
|
||||
|
||||
Other secrets might just be a hash which means we need to write more complex rules to verify
|
||||
that what we are matching is a secret.
|
||||
|
||||
|
||||
Here is an example of a semi-generic secret
|
||||
```
|
||||
discord_client_secret = "8dyfuiRyq=vVc3RRr_edRk-fK__JItpZ"
|
||||
```
|
||||
We can write a regular expression to capture the variable name (identifier),
|
||||
We can write a regular expression to capture the variable name (identifier),
|
||||
the assignment symbol (like '=' or ':='), and finally the actual secret.
|
||||
The structure of a rule to match this example secret is below:
|
||||
|
||||
Beginning string
|
||||
quotation
|
||||
│ End string quotation
|
||||
│ │
|
||||
▼ ▼
|
||||
(?i)(discord[a-z0-9_ .\-,]{0,25})(=|>|:=|\|\|:|<=|=>|:).{0,5}['\"]([a-z0-9=_\-]{32})['\"]
|
||||
|
||||
▲ ▲ ▲
|
||||
│ │ │
|
||||
│ │ │
|
||||
identifier assignment symbol
|
||||
Secret
|
||||
|
||||
Beginning string
|
||||
quotation
|
||||
│ End string quotation
|
||||
│ │
|
||||
▼ ▼
|
||||
(?i)(discord[a-z0-9_ .\-,]{0,25})(=|>|:=|\|\|:|<=|=>|:).{0,5}['\"]([a-z0-9=_\-]{32})['\"]
|
||||
|
||||
▲ ▲ ▲
|
||||
│ │ │
|
||||
│ │ │
|
||||
identifier assignment symbol
|
||||
Secret
|
||||
|
||||
|
||||
#### A Note on Generic Secrets
|
||||
Let's continue with the example `discord_client_secret = "8dyfuiRyq=vVc3RRr_edRk-fK__JItpZ"`.
|
||||
Let's continue with the example `discord_client_secret = "8dyfuiRyq=vVc3RRr_edRk-fK__JItpZ"`.
|
||||
This secret would match both the `discord-client-secret` rule and the `generic-api-key` rule in the default config.
|
||||
```
|
||||
[[rules]]
|
||||
@@ -264,9 +284,23 @@ regex = '''(?i)((key|api|token|secret|password)[a-z0-9_ .\-,]{0,25})(=|>|:=|\|\|
|
||||
entropy = 3.7
|
||||
secretGroup = 4
|
||||
```
|
||||
If gitleaks encountered `discord_client_secret = "8dyfuiRyq=vVc3RRr_edRk-fK__JItpZ"`, only the `discord` rule would report a finding because
|
||||
If gitleaks encountered `discord_client_secret = "8dyfuiRyq=vVc3RRr_edRk-fK__JItpZ"`, only the `discord` rule would report a finding because
|
||||
the generic rule has the string `generic` somewhere in the rule's `id`. If a secret is encountered and both a `generic` and non-generic rule have discovered the same secret, the non-generic
|
||||
will be given precedence.
|
||||
will be given precedence.
|
||||
|
||||
|
||||
## Sponsorships
|
||||
<p align="left">
|
||||
<a href="https://www.tines.com/?utm_source=oss&utm_medium=sponsorship&utm_campaign=gitleaks">
|
||||
<img alt="Tines Sponsorship" src="https://user-images.githubusercontent.com/15034943/146411864-4878f936-b4f7-49a0-b625-f9f40c704bfa.png" width=200>
|
||||
</a>
|
||||
</p>
|
||||
<a href="https://www.typeform.com/">
|
||||
<img src="https://user-images.githubusercontent.com/15034943/146945294-a6473d55-86b8-4f53-941e-7f185ad6b3d7.png" width=200/>
|
||||
</a>
|
||||
</p>
|
||||
|
||||
|
||||
|
||||
|
||||
## Exit Codes
|
||||
|
||||
+1
-1
@@ -31,7 +31,7 @@ func runDetect(cmd *cobra.Command, args []string) {
|
||||
initConfig()
|
||||
var (
|
||||
vc config.ViperConfig
|
||||
findings []*report.Finding
|
||||
findings []report.Finding
|
||||
err error
|
||||
)
|
||||
|
||||
|
||||
+16
-15
@@ -26,9 +26,9 @@ const banner = `
|
||||
const configDescription = `config file path
|
||||
order of precedence:
|
||||
1. --config/-c
|
||||
2. (--source/-s)/.gitleaks.toml
|
||||
if --config/-c is not set and no (--source/s)/.gitleaks.toml is present
|
||||
then .gitleaks.toml will be written to (--source/-s)/.gitleaks.toml for future use`
|
||||
2. env var GITLEAKS_CONFIG
|
||||
3. (--source/-s)/.gitleaks.toml
|
||||
If none of the three options are used, then gitleaks will use the default config`
|
||||
|
||||
var rootCmd = &cobra.Command{
|
||||
Use: "gitleaks",
|
||||
@@ -38,10 +38,10 @@ var rootCmd = &cobra.Command{
|
||||
func init() {
|
||||
cobra.OnInitialize(initLog)
|
||||
rootCmd.PersistentFlags().StringP("config", "c", "", configDescription)
|
||||
rootCmd.PersistentFlags().Int("exit-code", 1, "exit code when leaks have been encountered (default: 1)")
|
||||
rootCmd.PersistentFlags().Int("exit-code", 1, "exit code when leaks have been encountered")
|
||||
rootCmd.PersistentFlags().StringP("source", "s", ".", "path to source (default: $PWD)")
|
||||
rootCmd.PersistentFlags().StringP("report-path", "r", "", "report file")
|
||||
rootCmd.PersistentFlags().StringP("report-format", "f", "", "output format (json, csv, sarif)")
|
||||
rootCmd.PersistentFlags().StringP("report-format", "f", "json", "output format (json, csv, sarif)")
|
||||
rootCmd.PersistentFlags().StringP("log-level", "l", "info", "log level (debug, info, warn, error, fatal)")
|
||||
rootCmd.PersistentFlags().BoolP("verbose", "v", false, "show verbose output from scan")
|
||||
rootCmd.PersistentFlags().Bool("redact", false, "redact secrets from logs and stdout")
|
||||
@@ -78,7 +78,11 @@ func initConfig() {
|
||||
}
|
||||
if cfgPath != "" {
|
||||
viper.SetConfigFile(cfgPath)
|
||||
log.Debug().Msgf("Using gitleaks config %s", cfgPath)
|
||||
log.Debug().Msgf("Using gitleaks config %s from `--config`", cfgPath)
|
||||
} else if os.Getenv("GITLEAKS_CONFIG") != "" {
|
||||
envPath := os.Getenv("GITLEAKS_CONFIG")
|
||||
viper.SetConfigFile(envPath)
|
||||
log.Debug().Msgf("Using gitleaks config from GITLEAKS_CONFIG env var: %s", envPath)
|
||||
} else {
|
||||
source, err := rootCmd.Flags().GetString("source")
|
||||
if err != nil {
|
||||
@@ -90,7 +94,7 @@ func initConfig() {
|
||||
}
|
||||
|
||||
if !fileInfo.IsDir() {
|
||||
log.Debug().Msgf("Unable to write default gitleaks config to %s since --source=%s is a file, using default config",
|
||||
log.Debug().Msgf("Unable to load gitleaks config from %s since --source=%s is a file, using default config",
|
||||
filepath.Join(source, ".gitleaks.toml"), source)
|
||||
viper.SetConfigType("toml")
|
||||
viper.ReadConfig(strings.NewReader(config.DefaultConfig))
|
||||
@@ -98,15 +102,12 @@ func initConfig() {
|
||||
}
|
||||
|
||||
if _, err := os.Stat(filepath.Join(source, ".gitleaks.toml")); os.IsNotExist(err) {
|
||||
log.Debug().Msgf("No gitleaks config found, writing default gitleaks config to %s", filepath.Join(source, ".gitleaks.toml"))
|
||||
if err := os.WriteFile(filepath.Join(source, ".gitleaks.toml"), []byte(config.DefaultConfig), os.ModePerm); err != nil {
|
||||
log.Debug().Msgf("Unable to write default gitleaks config to %s, using default config", filepath.Join(source, ".gitleaks.toml"))
|
||||
viper.SetConfigType("toml")
|
||||
viper.ReadConfig(strings.NewReader(config.DefaultConfig))
|
||||
return
|
||||
}
|
||||
log.Debug().Msgf("No gitleaks config found in path %s, using default gitleaks config", filepath.Join(source, ".gitleaks.toml"))
|
||||
viper.SetConfigType("toml")
|
||||
viper.ReadConfig(strings.NewReader(config.DefaultConfig))
|
||||
return
|
||||
} else {
|
||||
log.Debug().Msgf("Using existing gitleaks config %s", filepath.Join(source, ".gitleaks.toml"))
|
||||
log.Debug().Msgf("Using existing gitleaks config %s from `(--source)/.gitleaks.toml`", filepath.Join(source, ".gitleaks.toml"))
|
||||
}
|
||||
|
||||
viper.AddConfigPath(source)
|
||||
|
||||
+33
-25
@@ -2,33 +2,33 @@ title = "gitleaks config"
|
||||
|
||||
# Gitleaks rules are defined by regular expressions and entropy ranges.
|
||||
# Some secrets have unique signatures which make detecting those secrets easy.
|
||||
# Examples of those secrets would be Gitlab Personal Access Tokens, AWS keys, and Github Access Tokens.
|
||||
# Examples of those secrets would be Gitlab Personal Access Tokens, AWS keys, and Github Access Tokens.
|
||||
# All these examples have defined prefixes like `glpat`, `AKIA`, `ghp_`, etc.
|
||||
#
|
||||
#
|
||||
# Other secrets might just be a hash which means we need to write more complex rules to verify
|
||||
# that what we are matching is a secret.
|
||||
#
|
||||
#
|
||||
# Here is an example of a semi-generic secret
|
||||
#
|
||||
# discord_client_secret = "8dyfuiRyq=vVc3RRr_edRk-fK__JItpZ"
|
||||
#
|
||||
# We can write a regular expression to capture the variable name (identifier),
|
||||
#
|
||||
# We can write a regular expression to capture the variable name (identifier),
|
||||
# the assignment symbol (like '=' or ':='), and finally the actual secret.
|
||||
# The structure of a rule to match this example secret is below:
|
||||
#
|
||||
# Beginning string
|
||||
# quotation
|
||||
# │ End string quotation
|
||||
# │ │
|
||||
# ▼ ▼
|
||||
# (?i)(discord[a-z0-9_ .\-,]{0,25})(=|>|:=|\|\|:|<=|=>|:).{0,5}['\"]([a-z0-9=_\-]{32})['\"]
|
||||
#
|
||||
# ▲ ▲ ▲
|
||||
# │ │ │
|
||||
# │ │ │
|
||||
# identifier assignment symbol
|
||||
# Secret
|
||||
#
|
||||
# Beginning string
|
||||
# quotation
|
||||
# │ End string quotation
|
||||
# │ │
|
||||
# ▼ ▼
|
||||
# (?i)(discord[a-z0-9_ .\-,]{0,25})(=|>|:=|\|\|:|<=|=>|:).{0,5}['\"]([a-z0-9=_\-]{32})['\"]
|
||||
#
|
||||
# ▲ ▲ ▲
|
||||
# │ │ │
|
||||
# │ │ │
|
||||
# identifier assignment symbol
|
||||
# Secret
|
||||
#
|
||||
[[rules]]
|
||||
id = "gitlab-pat"
|
||||
description = "GitLab Personal Access Token"
|
||||
@@ -37,7 +37,7 @@ regex = '''glpat-[0-9a-zA-Z\-]{20}'''
|
||||
[[rules]]
|
||||
id = "aws-access-token"
|
||||
description = "AWS"
|
||||
regex = '''AKIA[0-9A-Z]{16}'''
|
||||
regex = '''(A3T[A-Z0-9]|AKIA|AGPA|AIDA|AROA|AIPA|ANPA|ANVA|ASIA)[A-Z0-9]{16}'''
|
||||
|
||||
# Cryptographic keys
|
||||
[[rules]]
|
||||
@@ -140,7 +140,7 @@ secretGroup = 3
|
||||
[[rules]]
|
||||
id = "slack-web-hook"
|
||||
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}'''
|
||||
regex = '''https://hooks.slack.com/services/T[a-zA-Z0-9_]{8}/B[a-zA-Z0-9_]{8,12}/[a-zA-Z0-9_]{24}'''
|
||||
|
||||
[[rules]]
|
||||
id = "twilio-api-key"
|
||||
@@ -273,14 +273,19 @@ description = "Dropbox API secret/key"
|
||||
regex = '''(?i)(dropbox[a-z0-9_ .\-,]{0,25})(=|>|:=|\|\|:|<=|=>|:).{0,5}['\"]([a-z0-9]{15})['\"]'''
|
||||
|
||||
[[rules]]
|
||||
id = "dropbox-sl-api-token"
|
||||
id = "dropbox--api-key"
|
||||
description = "Dropbox API secret/key"
|
||||
regex = '''(?i)(dropbox[a-z0-9_ .\-,]{0,25})(=|>|:=|\|\|:|<=|=>|:).{0,5}['\"]([a-z0-9]{15})['\"]'''
|
||||
|
||||
[[rules]]
|
||||
id = "dropbox-short-lived-api-token"
|
||||
description = "Dropbox short lived API token"
|
||||
regex = '''(?i)(dropbox[a-z0-9_ .\-,]{0,25})(=|>|:=|\|\|:|<=|=>|:).{0,5}['\"](sl\.[a-z0-9\-=_]{135})['\"]'''
|
||||
|
||||
[[rules]]
|
||||
id = "dropbox-ll-api-token"
|
||||
id = "dropbox-long-lived-api-token"
|
||||
description = "Dropbox long lived API token"
|
||||
regex = '''(?i)(dropbox)(.{0,20})['\"](?i)[a-z0-9]{11}(AAAAAAAAAA)[a-z0-9-_=]{43}['\"]'''
|
||||
regex = '''(?i)(dropbox[a-z0-9_ .\-,]{0,25})(=|>|:=|\|\|:|<=|=>|:).{0,5}['\"][a-z0-9]{11}(AAAAAAAAAA)[a-z0-9\-_=]{43}['\"]'''
|
||||
|
||||
[[rules]]
|
||||
id = "duffel-api-token"
|
||||
@@ -376,7 +381,7 @@ secretGroup = 3
|
||||
[[rules]]
|
||||
id = "ionic-api-token"
|
||||
description = "Ionic API token"
|
||||
regex = '''ion_(?i)[a-z0-9]{42}'''
|
||||
regex = '''(?i)(ionic[a-z0-9_ .\-,]{0,25})(=|>|:=|\|\|:|<=|=>|:).{0,5}['\"](ion_[a-z0-9]{42})['\"]'''
|
||||
|
||||
[[rules]]
|
||||
id = "linear-api-token"
|
||||
@@ -538,4 +543,7 @@ secretGroup = 4
|
||||
[allowlist]
|
||||
description = "global allow lists"
|
||||
regexes = ['''219-09-9999''', '''078-05-1120''', '''(9[0-9]{2}|666)-\d{2}-\d{4}''']
|
||||
paths = ['''(.*?)(jpg|gif|doc|pdf|bin|svg|socket)$''']
|
||||
paths = [
|
||||
'''gitleaks.toml''',
|
||||
'''(.*?)(jpg|gif|doc|pdf|bin|svg|socket)$'''
|
||||
]
|
||||
@@ -7,6 +7,7 @@ import (
|
||||
"strings"
|
||||
|
||||
"github.com/rs/zerolog/log"
|
||||
|
||||
"github.com/zricethezav/gitleaks/v8/config"
|
||||
"github.com/zricethezav/gitleaks/v8/report"
|
||||
)
|
||||
|
||||
@@ -34,6 +34,10 @@ func TestDetectFindings(t *testing.T) {
|
||||
File: "tmp.go",
|
||||
RuleID: "aws-access-key",
|
||||
Tags: []string{"key", "AWS"},
|
||||
StartLine: 1,
|
||||
EndLine: 1,
|
||||
StartColumn: 15,
|
||||
EndColumn: 34,
|
||||
},
|
||||
},
|
||||
},
|
||||
@@ -69,6 +73,10 @@ func TestDetectFindings(t *testing.T) {
|
||||
RuleID: "discord-api-key",
|
||||
Tags: []string{},
|
||||
Entropy: 3.7906237,
|
||||
StartLine: 1,
|
||||
EndLine: 1,
|
||||
StartColumn: 7,
|
||||
EndColumn: 93,
|
||||
},
|
||||
},
|
||||
},
|
||||
@@ -91,6 +99,10 @@ func TestDetectFindings(t *testing.T) {
|
||||
RuleID: "generic-api-key",
|
||||
Tags: []string{},
|
||||
Entropy: 3.7906237,
|
||||
StartLine: 1,
|
||||
EndLine: 1,
|
||||
StartColumn: 22,
|
||||
EndColumn: 93,
|
||||
},
|
||||
},
|
||||
},
|
||||
|
||||
+12
-4
@@ -15,11 +15,12 @@ import (
|
||||
|
||||
// FromFiles opens the directory or file specified in source and checks each file against the rules
|
||||
// from the configuration. If any secrets are found, they are added to the list of findings.
|
||||
func FromFiles(source string, cfg config.Config, outputOptions Options) ([]*report.Finding, error) {
|
||||
func FromFiles(source string, cfg config.Config, outputOptions Options) ([]report.Finding, error) {
|
||||
var (
|
||||
findings []*report.Finding
|
||||
findings []report.Finding
|
||||
mu sync.Mutex
|
||||
)
|
||||
concurrentGoroutines := make(chan struct{}, 4)
|
||||
g, _ := errgroup.WithContext(context.Background())
|
||||
paths := make(chan string)
|
||||
g.Go(func() error {
|
||||
@@ -41,17 +42,23 @@ func FromFiles(source string, cfg config.Config, outputOptions Options) ([]*repo
|
||||
for pa := range paths {
|
||||
p := pa
|
||||
g.Go(func() error {
|
||||
concurrentGoroutines <- struct{}{}
|
||||
b, err := os.ReadFile(p)
|
||||
if err != nil {
|
||||
<-concurrentGoroutines
|
||||
return err
|
||||
}
|
||||
|
||||
if !godocutil.IsText(b) {
|
||||
<-concurrentGoroutines
|
||||
return nil
|
||||
}
|
||||
fis := DetectFindings(cfg, b, p, "")
|
||||
for _, fi := range fis {
|
||||
fi.File = p
|
||||
// need to add 1 since line counting starts at 1
|
||||
fi.StartLine++
|
||||
fi.EndLine++
|
||||
|
||||
if outputOptions.Redact {
|
||||
fi.Redact()
|
||||
}
|
||||
@@ -59,9 +66,10 @@ func FromFiles(source string, cfg config.Config, outputOptions Options) ([]*repo
|
||||
printFinding(fi)
|
||||
}
|
||||
mu.Lock()
|
||||
findings = append(findings, &fi)
|
||||
findings = append(findings, fi)
|
||||
mu.Unlock()
|
||||
}
|
||||
<-concurrentGoroutines
|
||||
return nil
|
||||
})
|
||||
}
|
||||
|
||||
@@ -17,16 +17,16 @@ func TestFromFiles(t *testing.T) {
|
||||
cfgName string
|
||||
opts Options
|
||||
source string
|
||||
expectedFindings []*report.Finding
|
||||
expectedFindings []report.Finding
|
||||
}{
|
||||
{
|
||||
source: filepath.Join(repoBasePath, "nogit"),
|
||||
cfgName: "simple",
|
||||
expectedFindings: []*report.Finding{
|
||||
expectedFindings: []report.Finding{
|
||||
{
|
||||
Description: "AWS Access Key",
|
||||
StartLine: 19,
|
||||
EndLine: 19,
|
||||
StartLine: 20,
|
||||
EndLine: 20,
|
||||
StartColumn: 16,
|
||||
EndColumn: 35,
|
||||
Match: "AKIALALEMEL33243OLIA",
|
||||
@@ -40,11 +40,11 @@ func TestFromFiles(t *testing.T) {
|
||||
{
|
||||
source: filepath.Join(repoBasePath, "nogit", "main.go"),
|
||||
cfgName: "simple",
|
||||
expectedFindings: []*report.Finding{
|
||||
expectedFindings: []report.Finding{
|
||||
{
|
||||
Description: "AWS Access Key",
|
||||
StartLine: 19,
|
||||
EndLine: 19,
|
||||
StartLine: 20,
|
||||
EndLine: 20,
|
||||
StartColumn: 16,
|
||||
EndColumn: 35,
|
||||
Match: "AKIALALEMEL33243OLIA",
|
||||
|
||||
+5
-4
@@ -3,6 +3,7 @@ package detect
|
||||
import (
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/gitleaks/go-gitdiff/gitdiff"
|
||||
"github.com/rs/zerolog/log"
|
||||
@@ -14,8 +15,8 @@ import (
|
||||
// FromGit accepts a gitdiff.File channel (structure output from `git log -p`) and a configuration
|
||||
// struct. Files from the gitdiff.File channel are then checked against each rule in the configuration to
|
||||
// check for secrets. If any secrets are found, they are added to the list of findings.
|
||||
func FromGit(files <-chan *gitdiff.File, cfg config.Config, outputOptions Options) []*report.Finding {
|
||||
var findings []*report.Finding
|
||||
func FromGit(files <-chan *gitdiff.File, cfg config.Config, outputOptions Options) []report.Finding {
|
||||
var findings []report.Finding
|
||||
mu := sync.Mutex{}
|
||||
wg := sync.WaitGroup{}
|
||||
commitMap := make(map[string]bool)
|
||||
@@ -70,7 +71,7 @@ func FromGit(files <-chan *gitdiff.File, cfg config.Config, outputOptions Option
|
||||
fi.Author = f.PatchHeader.Author.Name
|
||||
fi.Email = f.PatchHeader.Author.Email
|
||||
}
|
||||
fi.Date = f.PatchHeader.AuthorDate.String()
|
||||
fi.Date = f.PatchHeader.AuthorDate.UTC().Format(time.RFC3339)
|
||||
}
|
||||
|
||||
if outputOptions.Redact {
|
||||
@@ -81,7 +82,7 @@ func FromGit(files <-chan *gitdiff.File, cfg config.Config, outputOptions Option
|
||||
printFinding(fi)
|
||||
}
|
||||
mu.Lock()
|
||||
findings = append(findings, &fi)
|
||||
findings = append(findings, fi)
|
||||
mu.Unlock()
|
||||
|
||||
}
|
||||
|
||||
+28
-26
@@ -26,13 +26,13 @@ func TestFromGit(t *testing.T) {
|
||||
source string
|
||||
logOpts string
|
||||
expected string
|
||||
expectedFindings []*report.Finding
|
||||
expectedFindings []report.Finding
|
||||
}{
|
||||
{
|
||||
source: filepath.Join(repoBasePath, "small"),
|
||||
expected: filepath.Join(expectPath, "git", "small.txt"),
|
||||
cfgName: "simple",
|
||||
expectedFindings: []*report.Finding{
|
||||
expectedFindings: []report.Finding{
|
||||
{
|
||||
Description: "AWS Access Key",
|
||||
StartLine: 20,
|
||||
@@ -40,14 +40,15 @@ func TestFromGit(t *testing.T) {
|
||||
StartColumn: 19,
|
||||
EndColumn: 38,
|
||||
Secret: "AKIALALEMEL33243OLIA",
|
||||
Match: "AKIALALEMEL33243OLIA",
|
||||
File: "main.go",
|
||||
// Line: "\tawsToken := \"AKIALALEMEL33243OLIA\"",
|
||||
Commit: "1b6da43b82b22e4eaa10bcf8ee591e91abbfc587",
|
||||
Author: "Zachary Rice",
|
||||
Email: "zricer@protonmail.com",
|
||||
Message: "Accidentally add a secret",
|
||||
RuleID: "aws-access-key",
|
||||
Tags: []string{"key", "AWS"},
|
||||
Date: "2021-11-02T23:37:53Z",
|
||||
Commit: "1b6da43b82b22e4eaa10bcf8ee591e91abbfc587",
|
||||
Author: "Zachary Rice",
|
||||
Email: "zricer@protonmail.com",
|
||||
Message: "Accidentally add a secret",
|
||||
RuleID: "aws-access-key",
|
||||
Tags: []string{"key", "AWS"},
|
||||
},
|
||||
{
|
||||
Description: "AWS Access Key",
|
||||
@@ -56,14 +57,15 @@ func TestFromGit(t *testing.T) {
|
||||
StartColumn: 17,
|
||||
EndColumn: 36,
|
||||
Secret: "AKIALALEMEL33243OLIA",
|
||||
Match: "AKIALALEMEL33243OLIA",
|
||||
File: "foo/foo.go",
|
||||
// Line: "\taws_token := \"AKIALALEMEL33243OLIA\"",
|
||||
Commit: "491504d5a31946ce75e22554cc34203d8e5ff3ca",
|
||||
Author: "Zach Rice",
|
||||
Email: "zricer@protonmail.com",
|
||||
Message: "adding foo package with secret",
|
||||
RuleID: "aws-access-key",
|
||||
Tags: []string{"key", "AWS"},
|
||||
Date: "2021-11-02T23:48:06Z",
|
||||
Commit: "491504d5a31946ce75e22554cc34203d8e5ff3ca",
|
||||
Author: "Zach Rice",
|
||||
Email: "zricer@protonmail.com",
|
||||
Message: "adding foo package with secret",
|
||||
RuleID: "aws-access-key",
|
||||
Tags: []string{"key", "AWS"},
|
||||
},
|
||||
},
|
||||
},
|
||||
@@ -72,7 +74,7 @@ func TestFromGit(t *testing.T) {
|
||||
expected: filepath.Join(expectPath, "git", "small-branch-foo.txt"),
|
||||
logOpts: "--all foo...",
|
||||
cfgName: "simple",
|
||||
expectedFindings: []*report.Finding{
|
||||
expectedFindings: []report.Finding{
|
||||
{
|
||||
Description: "AWS Access Key",
|
||||
StartLine: 9,
|
||||
@@ -80,14 +82,15 @@ func TestFromGit(t *testing.T) {
|
||||
StartColumn: 17,
|
||||
EndColumn: 36,
|
||||
Secret: "AKIALALEMEL33243OLIA",
|
||||
// Line: "\taws_token := \"AKIALALEMEL33243OLIA\"",
|
||||
File: "foo/foo.go",
|
||||
Commit: "491504d5a31946ce75e22554cc34203d8e5ff3ca",
|
||||
Author: "Zach Rice",
|
||||
Email: "zricer@protonmail.com",
|
||||
Message: "adding foo package with secret",
|
||||
RuleID: "aws-access-key",
|
||||
Tags: []string{"key", "AWS"},
|
||||
Match: "AKIALALEMEL33243OLIA",
|
||||
Date: "2021-11-02T23:48:06Z",
|
||||
File: "foo/foo.go",
|
||||
Commit: "491504d5a31946ce75e22554cc34203d8e5ff3ca",
|
||||
Author: "Zach Rice",
|
||||
Email: "zricer@protonmail.com",
|
||||
Message: "adding foo package with secret",
|
||||
RuleID: "aws-access-key",
|
||||
Tags: []string{"key", "AWS"},
|
||||
},
|
||||
},
|
||||
},
|
||||
@@ -120,7 +123,6 @@ func TestFromGit(t *testing.T) {
|
||||
findings := FromGit(files, cfg, tt.opts)
|
||||
for _, f := range findings {
|
||||
f.Match = "" // remove lines cause copying and pasting them has some wack formatting
|
||||
f.Date = ""
|
||||
}
|
||||
assert.ElementsMatch(t, tt.expectedFindings, findings)
|
||||
}
|
||||
|
||||
+13
-1
@@ -14,11 +14,15 @@ func getLocation(linePairs [][]int, start int, end int) Location {
|
||||
var (
|
||||
prevNewLine int
|
||||
location Location
|
||||
lineSet bool
|
||||
_lineNum int
|
||||
)
|
||||
|
||||
for lineNum, pair := range linePairs {
|
||||
_lineNum = lineNum
|
||||
newLineByteIndex := pair[0]
|
||||
if prevNewLine <= start && start < newLineByteIndex {
|
||||
lineSet = true
|
||||
location.startLine = lineNum
|
||||
location.endLine = lineNum
|
||||
location.startColumn = (start - prevNewLine) + 1 // +1 because counting starts at 1
|
||||
@@ -30,9 +34,17 @@ func getLocation(linePairs [][]int, start int, end int) Location {
|
||||
location.endColumn = (end - prevNewLine)
|
||||
location.endLineIndex = newLineByteIndex
|
||||
}
|
||||
|
||||
prevNewLine = pair[0]
|
||||
}
|
||||
|
||||
if !lineSet {
|
||||
// if lines never get set then that means the secret is most likely
|
||||
// on the last line of the diff output and the diff output does not have
|
||||
// a newline
|
||||
location.startColumn = (start - prevNewLine) + 1 // +1 because counting starts at 1
|
||||
location.endColumn = (end - prevNewLine)
|
||||
location.startLine = _lineNum + 1
|
||||
location.endLine = _lineNum + 1
|
||||
}
|
||||
return location
|
||||
}
|
||||
+32
-8
@@ -13,7 +13,8 @@ import (
|
||||
"github.com/rs/zerolog/log"
|
||||
)
|
||||
|
||||
// GitLog returns a channel of gitdiff.File objects from the git log -p command for the given source.
|
||||
// GitLog returns a channel of gitdiff.File objects from the
|
||||
// git log -p command for the given source.
|
||||
func GitLog(source string, logOpts string) (<-chan *gitdiff.File, error) {
|
||||
sourceClean := filepath.Clean(source)
|
||||
var cmd *exec.Cmd
|
||||
@@ -22,7 +23,8 @@ func GitLog(source string, logOpts string) (<-chan *gitdiff.File, error) {
|
||||
args = append(args, strings.Split(logOpts, " ")...)
|
||||
cmd = exec.Command("git", args...)
|
||||
} else {
|
||||
cmd = exec.Command("git", "-C", sourceClean, "log", "-p", "-U0", "--full-history", "--all")
|
||||
cmd = exec.Command("git", "-C", sourceClean, "log", "-p", "-U0",
|
||||
"--full-history", "--all")
|
||||
}
|
||||
|
||||
log.Debug().Msgf("executing: %s", cmd.String())
|
||||
@@ -40,18 +42,21 @@ func GitLog(source string, logOpts string) (<-chan *gitdiff.File, error) {
|
||||
}
|
||||
|
||||
go listenForStdErr(stderr)
|
||||
time.Sleep(50 * time.Millisecond) // HACK: to avoid https://github.com/zricethezav/gitleaks/issues/722
|
||||
// HACK: to avoid https://github.com/zricethezav/gitleaks/issues/722
|
||||
time.Sleep(50 * time.Millisecond)
|
||||
|
||||
return gitdiff.Parse(stdout)
|
||||
}
|
||||
|
||||
// GitDiff returns a channel of gitdiff.File objects from the git diff command for the given source.
|
||||
// GitDiff returns a channel of gitdiff.File objects from
|
||||
// the git diff command for the given source.
|
||||
func GitDiff(source string, staged bool) (<-chan *gitdiff.File, error) {
|
||||
sourceClean := filepath.Clean(source)
|
||||
var cmd *exec.Cmd
|
||||
cmd = exec.Command("git", "-C", sourceClean, "diff", "-U0", ".")
|
||||
if staged {
|
||||
cmd = exec.Command("git", "-C", sourceClean, "diff", "-U0", "--staged", ".")
|
||||
cmd = exec.Command("git", "-C", sourceClean, "diff", "-U0",
|
||||
"--staged", ".")
|
||||
}
|
||||
log.Debug().Msgf("executing: %s", cmd.String())
|
||||
|
||||
@@ -68,7 +73,8 @@ func GitDiff(source string, staged bool) (<-chan *gitdiff.File, error) {
|
||||
}
|
||||
|
||||
go listenForStdErr(stderr)
|
||||
time.Sleep(50 * time.Millisecond) // HACK: to avoid https://github.com/zricethezav/gitleaks/issues/722
|
||||
// HACK: to avoid https://github.com/zricethezav/gitleaks/issues/722
|
||||
time.Sleep(50 * time.Millisecond)
|
||||
|
||||
return gitdiff.Parse(stdout)
|
||||
}
|
||||
@@ -79,8 +85,26 @@ func listenForStdErr(stderr io.ReadCloser) {
|
||||
scanner := bufio.NewScanner(stderr)
|
||||
errEncountered := false
|
||||
for scanner.Scan() {
|
||||
log.Error().Msg(scanner.Text())
|
||||
errEncountered = true
|
||||
// if git throws the following error:
|
||||
//
|
||||
// exhaustive rename detection was skipped due to too many files.
|
||||
// you may want to set your diff.renameLimit variable to at least
|
||||
// (some large number) and retry the command.
|
||||
//
|
||||
// we skip exiting the program as git log -p/git diff will continue
|
||||
// to send data to stdout and finish executing. This next bit of
|
||||
// code prevents gitleaks from stopping mid scan if this error is
|
||||
// encountered
|
||||
if strings.Contains(scanner.Text(),
|
||||
"exhaustive rename detection was skipped") ||
|
||||
strings.Contains(scanner.Text(),
|
||||
"you may want to set your diff.renameLimit") {
|
||||
|
||||
log.Warn().Msg(scanner.Text())
|
||||
} else {
|
||||
log.Error().Msg(scanner.Text())
|
||||
errEncountered = true
|
||||
}
|
||||
}
|
||||
if errEncountered {
|
||||
os.Exit(1)
|
||||
|
||||
@@ -3,7 +3,7 @@ module github.com/zricethezav/gitleaks/v8
|
||||
go 1.16
|
||||
|
||||
require (
|
||||
github.com/gitleaks/go-gitdiff v0.7.3
|
||||
github.com/gitleaks/go-gitdiff v0.7.4
|
||||
github.com/rs/zerolog v1.25.0
|
||||
github.com/spf13/cobra v1.2.1
|
||||
github.com/spf13/viper v1.8.1
|
||||
|
||||
@@ -70,8 +70,8 @@ github.com/fatih/color v1.7.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5Kwzbycv
|
||||
github.com/fsnotify/fsnotify v1.4.9 h1:hsms1Qyu0jgnwNXIxa+/V/PDsU6CfLf6CNO8H7IWoS4=
|
||||
github.com/fsnotify/fsnotify v1.4.9/go.mod h1:znqG4EE+3YCdAaPaxE2ZRY/06pZUdp0tY4IgpuI1SZQ=
|
||||
github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04=
|
||||
github.com/gitleaks/go-gitdiff v0.7.3 h1:VA9MyNEI0/NGukCztbfyJzf1MmFTVHBByBGIDW0F+a0=
|
||||
github.com/gitleaks/go-gitdiff v0.7.3/go.mod h1:pKz0X4YzCKZs30BL+weqBIG7mx0jl4tF1uXV9ZyNvrA=
|
||||
github.com/gitleaks/go-gitdiff v0.7.4 h1:8vICc4moyRR2poklblThdQ0ckMet22mEvFJSxPsiDlk=
|
||||
github.com/gitleaks/go-gitdiff v0.7.4/go.mod h1:pKz0X4YzCKZs30BL+weqBIG7mx0jl4tF1uXV9ZyNvrA=
|
||||
github.com/go-gl/glfw v0.0.0-20190409004039-e6da0acd62b1/go.mod h1:vR7hzQXu2zJy9AVAgeJqvqgH9Q5CA+iKCZ2gyEVpxRU=
|
||||
github.com/go-gl/glfw/v3.3/glfw v0.0.0-20191125211704-12ad95a8df72/go.mod h1:tQ2UAYgL5IevRw8kRxooKSPJfGvJ9fJQFa0TUsXzTg8=
|
||||
github.com/go-gl/glfw/v3.3/glfw v0.0.0-20200222043503-6f7a984d4dc4/go.mod h1:tQ2UAYgL5IevRw8kRxooKSPJfGvJ9fJQFa0TUsXzTg8=
|
||||
|
||||
+1
-1
@@ -7,7 +7,7 @@ import (
|
||||
)
|
||||
|
||||
// writeCsv writes the list of findings to a writeCloser.
|
||||
func writeCsv(f []*Finding, w io.WriteCloser) error {
|
||||
func writeCsv(f []Finding, w io.WriteCloser) error {
|
||||
if len(f) == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
+3
-3
@@ -9,7 +9,7 @@ import (
|
||||
|
||||
func TestWriteCSV(t *testing.T) {
|
||||
tests := []struct {
|
||||
findings []*Finding
|
||||
findings []Finding
|
||||
testReportName string
|
||||
expected string
|
||||
wantEmpty bool
|
||||
@@ -17,7 +17,7 @@ func TestWriteCSV(t *testing.T) {
|
||||
{
|
||||
testReportName: "simple",
|
||||
expected: filepath.Join(expectPath, "report", "csv_simple.csv"),
|
||||
findings: []*Finding{
|
||||
findings: []Finding{
|
||||
{
|
||||
RuleID: "test-rule",
|
||||
Match: "line containing secret",
|
||||
@@ -39,7 +39,7 @@ func TestWriteCSV(t *testing.T) {
|
||||
wantEmpty: true,
|
||||
testReportName: "empty",
|
||||
expected: filepath.Join(expectPath, "report", "this_should_not_exist.csv"),
|
||||
findings: []*Finding{}},
|
||||
findings: []Finding{}},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
|
||||
+11
-1
@@ -1,6 +1,9 @@
|
||||
package report
|
||||
|
||||
import "strings"
|
||||
import (
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// Finding contains information about strings that
|
||||
// have been captured by a tree-sitter query.
|
||||
@@ -40,3 +43,10 @@ func (f *Finding) Redact() {
|
||||
f.Match = strings.Replace(f.Match, f.Secret, "REDACTED", -1)
|
||||
f.Secret = "REDACT"
|
||||
}
|
||||
|
||||
func (f *Finding) Hash() string {
|
||||
return f.Secret + f.Commit +
|
||||
strconv.Itoa(f.EndLine) +
|
||||
strconv.Itoa(f.StartLine)
|
||||
|
||||
}
|
||||
+2
-2
@@ -5,9 +5,9 @@ import (
|
||||
"io"
|
||||
)
|
||||
|
||||
func writeJson(findings []*Finding, w io.WriteCloser) error {
|
||||
func writeJson(findings []Finding, w io.WriteCloser) error {
|
||||
if len(findings) == 0 {
|
||||
return nil
|
||||
findings = []Finding{}
|
||||
}
|
||||
encoder := json.NewEncoder(w)
|
||||
encoder.SetIndent("", " ")
|
||||
|
||||
+4
-5
@@ -9,7 +9,7 @@ import (
|
||||
|
||||
func TestWriteJSON(t *testing.T) {
|
||||
tests := []struct {
|
||||
findings []*Finding
|
||||
findings []Finding
|
||||
testReportName string
|
||||
expected string
|
||||
wantEmpty bool
|
||||
@@ -17,7 +17,7 @@ func TestWriteJSON(t *testing.T) {
|
||||
{
|
||||
testReportName: "simple",
|
||||
expected: filepath.Join(expectPath, "report", "json_simple.json"),
|
||||
findings: []*Finding{
|
||||
findings: []Finding{
|
||||
{
|
||||
|
||||
Description: "",
|
||||
@@ -39,10 +39,9 @@ func TestWriteJSON(t *testing.T) {
|
||||
}},
|
||||
{
|
||||
|
||||
wantEmpty: true,
|
||||
testReportName: "empty",
|
||||
expected: filepath.Join(expectPath, "report", "this_should_not_exist.json"),
|
||||
findings: []*Finding{}},
|
||||
expected: filepath.Join(expectPath, "report", "empty.json"),
|
||||
findings: []Finding{}},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
|
||||
+1
-4
@@ -13,10 +13,7 @@ const (
|
||||
CWE_DESCRIPTION = "Use of Hard-coded Credentials"
|
||||
)
|
||||
|
||||
func Write(findings []*Finding, cfg config.Config, ext string, reportPath string) error {
|
||||
if len(findings) == 0 {
|
||||
return nil
|
||||
}
|
||||
func Write(findings []Finding, cfg config.Config, ext string, reportPath string) error {
|
||||
file, err := os.Create(reportPath)
|
||||
if err != nil {
|
||||
return err
|
||||
|
||||
@@ -16,13 +16,13 @@ const (
|
||||
|
||||
func TestReport(t *testing.T) {
|
||||
tests := []struct {
|
||||
findings []*Finding
|
||||
findings []Finding
|
||||
ext string
|
||||
wantEmpty bool
|
||||
}{
|
||||
{
|
||||
ext: "json",
|
||||
findings: []*Finding{
|
||||
findings: []Finding{
|
||||
{
|
||||
RuleID: "test-rule",
|
||||
},
|
||||
@@ -30,7 +30,7 @@ func TestReport(t *testing.T) {
|
||||
},
|
||||
{
|
||||
ext: ".json",
|
||||
findings: []*Finding{
|
||||
findings: []Finding{
|
||||
{
|
||||
RuleID: "test-rule",
|
||||
},
|
||||
@@ -38,7 +38,7 @@ func TestReport(t *testing.T) {
|
||||
},
|
||||
{
|
||||
ext: ".jsonj",
|
||||
findings: []*Finding{
|
||||
findings: []Finding{
|
||||
{
|
||||
RuleID: "test-rule",
|
||||
},
|
||||
@@ -47,7 +47,7 @@ func TestReport(t *testing.T) {
|
||||
},
|
||||
{
|
||||
ext: ".csv",
|
||||
findings: []*Finding{
|
||||
findings: []Finding{
|
||||
{
|
||||
RuleID: "test-rule",
|
||||
},
|
||||
@@ -55,7 +55,7 @@ func TestReport(t *testing.T) {
|
||||
},
|
||||
{
|
||||
ext: "csv",
|
||||
findings: []*Finding{
|
||||
findings: []Finding{
|
||||
{
|
||||
RuleID: "test-rule",
|
||||
},
|
||||
@@ -63,7 +63,7 @@ func TestReport(t *testing.T) {
|
||||
},
|
||||
{
|
||||
ext: "CSV",
|
||||
findings: []*Finding{
|
||||
findings: []Finding{
|
||||
{
|
||||
RuleID: "test-rule",
|
||||
},
|
||||
|
||||
+5
-5
@@ -8,7 +8,7 @@ import (
|
||||
"github.com/zricethezav/gitleaks/v8/config"
|
||||
)
|
||||
|
||||
func writeSarif(cfg config.Config, findings []*Finding, w io.WriteCloser) error {
|
||||
func writeSarif(cfg config.Config, findings []Finding, w io.WriteCloser) error {
|
||||
sarif := Sarif{
|
||||
Schema: "https://schemastore.azurewebsites.net/schemas/json/sarif-2.1.0-rtm.5.json",
|
||||
Version: "2.1.0",
|
||||
@@ -20,7 +20,7 @@ func writeSarif(cfg config.Config, findings []*Finding, w io.WriteCloser) error
|
||||
return encoder.Encode(sarif)
|
||||
}
|
||||
|
||||
func getRuns(cfg config.Config, findings []*Finding) []Runs {
|
||||
func getRuns(cfg config.Config, findings []Finding) []Runs {
|
||||
return []Runs{
|
||||
{
|
||||
Tool: getTool(cfg),
|
||||
@@ -54,7 +54,7 @@ func getRules(cfg config.Config) []Rules {
|
||||
return rules
|
||||
}
|
||||
|
||||
func messageText(f *Finding) string {
|
||||
func messageText(f Finding) string {
|
||||
if f.Commit == "" {
|
||||
return fmt.Sprintf("%s has detected secret for file %s.", f.RuleID, f.File)
|
||||
}
|
||||
@@ -63,7 +63,7 @@ func messageText(f *Finding) string {
|
||||
|
||||
}
|
||||
|
||||
func getResults(findings []*Finding) []Results {
|
||||
func getResults(findings []Finding) []Results {
|
||||
var results []Results
|
||||
for _, f := range findings {
|
||||
r := Results{
|
||||
@@ -87,7 +87,7 @@ func getResults(findings []*Finding) []Results {
|
||||
return results
|
||||
}
|
||||
|
||||
func getLocation(f *Finding) []Locations {
|
||||
func getLocation(f Finding) []Locations {
|
||||
return []Locations{
|
||||
{
|
||||
PhysicalLocation: PhysicalLocation{
|
||||
|
||||
@@ -14,7 +14,7 @@ const configPath = "../testdata/config/"
|
||||
|
||||
func TestWriteSarif(t *testing.T) {
|
||||
tests := []struct {
|
||||
findings []*Finding
|
||||
findings []Finding
|
||||
testReportName string
|
||||
expected string
|
||||
wantEmpty bool
|
||||
@@ -24,7 +24,7 @@ func TestWriteSarif(t *testing.T) {
|
||||
cfgName: "simple",
|
||||
testReportName: "simple",
|
||||
expected: filepath.Join(expectPath, "report", "sarif_simple.sarif"),
|
||||
findings: []*Finding{
|
||||
findings: []Finding{
|
||||
{
|
||||
|
||||
Description: "",
|
||||
|
||||
Vendored
+1
@@ -0,0 +1 @@
|
||||
[]
|
||||
Vendored
BIN
Binary file not shown.
Reference in new issue
Block a user