mirror of
https://github.com/gitleaks/gitleaks.git
synced 2026-11-27 03:34:22 +01:00
Compare commits
7
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
ed65b65095 | ||
|
|
c5ccbb9468 | ||
|
|
3821f30b06 | ||
|
|
d22371873b | ||
|
|
87d96295d6 | ||
|
|
228396b37b | ||
|
|
a82bc53d89 |
No files matched your search
+1
-1
@@ -18,7 +18,7 @@ builds:
|
||||
tags:
|
||||
- gore2regex
|
||||
ldflags:
|
||||
- -s -w -X=github.com/zricethezav/gitleaks/v8/cmd.Version={{.Version}}
|
||||
- -s -w -X=github.com/zricethezav/gitleaks/v8/version.Version={{.Version}}
|
||||
archives:
|
||||
- builds: [gitleaks]
|
||||
format_overrides:
|
||||
|
||||
@@ -32,12 +32,8 @@
|
||||
[![GoReportCard][go-report-card-badge]][go-report-card]
|
||||
[![License][badge-license]][license]
|
||||
|
||||
|
||||
### Join our Discord! [](https://discord.gg/8Hzbrnkr7E)
|
||||
|
||||
Gitleaks is a tool for **detecting** secrets like passwords, API keys, and tokens in git repos, files, and whatever else you wanna throw at it via `stdin`. If you wanna learn more about how the detection engine works check out this blog: [Regex is (almost) all you need](https://lookingatcomputer.substack.com/p/regex-is-almost-all-you-need).
|
||||
|
||||
|
||||
```
|
||||
➜ ~/code(master) gitleaks git -v
|
||||
|
||||
@@ -61,6 +57,11 @@ Date: 2022-08-03T12:31:40Z
|
||||
Fingerprint: cd5226711335c68be1e720b318b7bc3135a30eb2:cmd/generate/config/rules/sidekiq.go:sidekiq-secret:23
|
||||
```
|
||||
|
||||
### GitHub Sponsors
|
||||
|
||||
Sponsor [@zricethezav on GitHub](https://github.com/sponsors/zricethezav/) to get
|
||||
featured on this README.
|
||||
|
||||
## 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/gitleaks/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).
|
||||
@@ -141,10 +142,13 @@ Detect hardcoded secrets................................................Skipped
|
||||
## Usage
|
||||
|
||||
```
|
||||
Gitleaks scans code, past or present, for secrets
|
||||
|
||||
Usage:
|
||||
gitleaks [command]
|
||||
|
||||
Available Commands:
|
||||
completion Generate the autocompletion script for the specified shell
|
||||
dir scan directories or files for secrets
|
||||
git scan git repositories for secrets
|
||||
help Help about any command
|
||||
@@ -160,16 +164,16 @@ Flags:
|
||||
3. env var GITLEAKS_CONFIG_TOML with the file content
|
||||
4. (target path)/.gitleaks.toml
|
||||
If none of the four options are used, then gitleaks will use the default config
|
||||
--diagnostics string enable diagnostics (comma-separated list: cpu,mem,trace). cpu=CPU profiling, mem=memory profiling, trace=execution tracing
|
||||
--diagnostics-dir string directory to store diagnostics output files (defaults to current directory)
|
||||
--diagnostics string enable diagnostics (http OR comma-separated list: cpu,mem,trace). cpu=CPU prof, mem=memory prof, trace=exec tracing, http=serve via net/http/pprof
|
||||
--diagnostics-dir string directory to store diagnostics output files when not using http mode (defaults to current directory)
|
||||
--enable-rule strings only enable specific rules by id
|
||||
--exit-code int exit code when leaks have been encountered (default 1)
|
||||
-i, --gitleaks-ignore-path string path to .gitleaksignore file or folder containing one (default ".")
|
||||
-h, --help help for gitleaks
|
||||
--ignore-gitleaks-allow ignore gitleaks:allow comments
|
||||
-l, --log-level string log level (trace, debug, info, warn, error, fatal) (default "info")
|
||||
--max-decode-depth int allow recursive decoding up to this depth (default "0", no decoding is done)
|
||||
--max-archive-depth int allow scanning into nested archives up to this depth (default "0", no archive traversal is done)
|
||||
--max-decode-depth int allow recursive decoding up to this depth (default "0", no decoding is done)
|
||||
--max-target-megabytes int files larger than this will be skipped
|
||||
--no-banner suppress banner
|
||||
--no-color turn off color for verbose output
|
||||
@@ -177,6 +181,7 @@ Flags:
|
||||
-f, --report-format string output format (json, csv, junit, sarif, template)
|
||||
-r, --report-path string report file
|
||||
--report-template string template file used to generate the report (implies --report-format=template)
|
||||
--timeout int set a timeout for gitleaks commands in seconds (default "0", no timeout is set)
|
||||
-v, --verbose show verbose output from scan
|
||||
--version version for gitleaks
|
||||
|
||||
@@ -421,7 +426,7 @@ In v8.28.0 Gitleaks introduced composite rules, which are made up of a single "p
|
||||
**Proximity matching:** Using the `withinLines` and `withinColumns` fields instructs the primary rule to only report a finding if the auxiliary `required` rules also find matches within the specified proximity. You can set:
|
||||
|
||||
- **`withinLines: N`** - required findings must be within N lines (vertically)
|
||||
- **`withinColumns: N`** - required findings must be within N characters (horizontally)
|
||||
- **`withinColumns: N`** - required findings must be within N characters (horizontally)
|
||||
- **Both** - creates a rectangular search area (both constraints must be satisfied)
|
||||
- **Neither** - fragment-level matching (required findings can be anywhere in the same fragment)
|
||||
|
||||
@@ -433,76 +438,76 @@ a = auxiliary (required) captured secret
|
||||
fragment = section of data gitleaks is looking at
|
||||
|
||||
|
||||
*Fragment-level proximity*
|
||||
*Fragment-level proximity*
|
||||
Any required finding in the fragment
|
||||
┌────────┐
|
||||
┌──────┤fragment├─────┐
|
||||
│ └──────┬─┤ │ ┌───────┐
|
||||
│ │a│◀────┼─│✓ MATCH│
|
||||
│ ┌─┐└─┘ │ └───────┘
|
||||
│┌─┐ │p│ │
|
||||
││a│ ┌─┐└─┘ │ ┌───────┐
|
||||
│└─┘ │a│◀──────────┼─│✓ MATCH│
|
||||
└─▲─────┴─┴───────────┘ └───────┘
|
||||
│ ┌───────┐
|
||||
└────│✓ MATCH│
|
||||
└───────┘
|
||||
|
||||
|
||||
┌────────┐
|
||||
┌──────┤fragment├─────┐
|
||||
│ └──────┬─┤ │ ┌───────┐
|
||||
│ │a│◀────┼─│✓ MATCH│
|
||||
│ ┌─┐└─┘ │ └───────┘
|
||||
│┌─┐ │p│ │
|
||||
││a│ ┌─┐└─┘ │ ┌───────┐
|
||||
│└─┘ │a│◀──────────┼─│✓ MATCH│
|
||||
└─▲─────┴─┴───────────┘ └───────┘
|
||||
│ ┌───────┐
|
||||
└────│✓ MATCH│
|
||||
└───────┘
|
||||
|
||||
|
||||
*Column bounded proximity*
|
||||
`withinColumns = 3`
|
||||
┌────────┐
|
||||
┌────┬─┤fragment├─┬───┐
|
||||
│ └──────┬─┤ │ ┌───────────┐
|
||||
│ │ │a│◀┼───┼─│+1C ✓ MATCH│
|
||||
│ ┌─┐└─┘ │ └───────────┘
|
||||
│┌─┐ │ │p│ │ │
|
||||
┌──▶│a│ ┌─┐ └─┘ │ ┌───────────┐
|
||||
│ │└─┘ ││a│◀────────┼───┼─│-2C ✓ MATCH│
|
||||
│ │ ┘ │ └───────────┘
|
||||
│ └── -3C ───0C─── +3C ─┘
|
||||
│ ┌─────────┐
|
||||
│ │ -4C ✗ NO│
|
||||
└──│ MATCH │
|
||||
└─────────┘
|
||||
|
||||
|
||||
`withinColumns = 3`
|
||||
┌────────┐
|
||||
┌────┬─┤fragment├─┬───┐
|
||||
│ └──────┬─┤ │ ┌───────────┐
|
||||
│ │ │a│◀┼───┼─│+1C ✓ MATCH│
|
||||
│ ┌─┐└─┘ │ └───────────┘
|
||||
│┌─┐ │ │p│ │ │
|
||||
┌──▶│a│ ┌─┐ └─┘ │ ┌───────────┐
|
||||
│ │└─┘ ││a│◀────────┼───┼─│-2C ✓ MATCH│
|
||||
│ │ ┘ │ └───────────┘
|
||||
│ └── -3C ───0C─── +3C ─┘
|
||||
│ ┌─────────┐
|
||||
│ │ -4C ✗ NO│
|
||||
└──│ MATCH │
|
||||
└─────────┘
|
||||
|
||||
|
||||
*Line bounded proximity*
|
||||
`withinLines = 4`
|
||||
┌────────┐
|
||||
┌─────┤fragment├─────┐
|
||||
+4L─ ─ ┴────────┘─ ─ ─│
|
||||
│ │
|
||||
│ ┌─┐ │ ┌────────────┐
|
||||
│ ┌─┐ │a│◀──┼─│+1L ✓ MATCH │
|
||||
0L ┌─┐ │p│ └─┘ │ ├────────────┤
|
||||
│ │a│◀──┴─┴────────┼─│-1L ✓ MATCH │
|
||||
│ └─┘ │ └────────────┘
|
||||
│ │ ┌─────────┐
|
||||
-4L─ ─ ─ ─ ─ ─ ─ ─┌─┐─│ │-5L ✗ NO │
|
||||
│ │a│◀┼─│ MATCH │
|
||||
└────────────────┴─┴─┘ └─────────┘
|
||||
|
||||
|
||||
`withinLines = 4`
|
||||
┌────────┐
|
||||
┌─────┤fragment├─────┐
|
||||
+4L─ ─ ┴────────┘─ ─ ─│
|
||||
│ │
|
||||
│ ┌─┐ │ ┌────────────┐
|
||||
│ ┌─┐ │a│◀──┼─│+1L ✓ MATCH │
|
||||
0L ┌─┐ │p│ └─┘ │ ├────────────┤
|
||||
│ │a│◀──┴─┴────────┼─│-1L ✓ MATCH │
|
||||
│ └─┘ │ └────────────┘
|
||||
│ │ ┌─────────┐
|
||||
-4L─ ─ ─ ─ ─ ─ ─ ─┌─┐─│ │-5L ✗ NO │
|
||||
│ │a│◀┼─│ MATCH │
|
||||
└────────────────┴─┴─┘ └─────────┘
|
||||
|
||||
|
||||
*Line and column bounded proximity*
|
||||
`withinLines = 4`
|
||||
`withinColumns = 3`
|
||||
┌────────┐
|
||||
┌─────┤fragment├─────┐
|
||||
+4L ┌└────────┴ ┐ │
|
||||
`withinLines = 4`
|
||||
`withinColumns = 3`
|
||||
┌────────┐
|
||||
┌─────┤fragment├─────┐
|
||||
+4L ┌└────────┴ ┐ │
|
||||
│ ┌─┐ │ ┌───────────────┐
|
||||
│ │ │a│◀┼───┼─│+2L/+1C ✓ MATCH│
|
||||
│ ┌─┐└─┘ │ └───────────────┘
|
||||
0L │ │p│ │ │
|
||||
│ └─┘ │
|
||||
│ │ │ │ ┌────────────┐
|
||||
-4L ─ ─ ─ ─ ─ ─┌─┐ │ │-5L/+3C ✗ NO│
|
||||
│ │a│◀┼─│ MATCH │
|
||||
└───-3C────0L───+3C┴─┘ └────────────┘
|
||||
0L │ │p│ │ │
|
||||
│ └─┘ │
|
||||
│ │ │ │ ┌────────────┐
|
||||
-4L ─ ─ ─ ─ ─ ─┌─┐ │ │-5L/+3C ✗ NO│
|
||||
│ │a│◀┼─│ MATCH │
|
||||
└───-3C────0L───+3C┴─┘ └────────────┘
|
||||
```
|
||||
|
||||
<details><summary>Some final quick thoughts on composite rules.</summary>This is an experimental feature! It's subject to change so don't go sellin' a new B2B SaaS feature built ontop of this feature. Scan type (git vs dir) based context is interesting. I'm monitoring the situation. Composite rules might not be super useful for git scans because gitleaks only looks at additions in the git history. It could be useful to scan non-additions in git history for `required` rules. Oh, right this is a readme, I'll shut up now.</details>
|
||||
|
||||
|
||||
#### gitleaks:allow
|
||||
|
||||
If you are knowingly committing a test secret that gitleaks will catch you can add a `gitleaks:allow` comment to that line which will instruct gitleaks
|
||||
@@ -646,3 +651,5 @@ You can always set the exit code when leaks are encountered with the --exit-code
|
||||
1 - leaks or error encountered
|
||||
126 - unknown flag
|
||||
```
|
||||
|
||||
### Join the Discord! [](https://discord.gg/8Hzbrnkr7E)
|
||||
+5
-8
@@ -19,7 +19,6 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"context"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
@@ -66,18 +65,16 @@ func runDetect(cmd *cobra.Command, args []string) {
|
||||
exitCode := mustGetIntFlag(cmd, "exit-code")
|
||||
noGit := mustGetBoolFlag(cmd, "no-git")
|
||||
fromPipe := mustGetBoolFlag(cmd, "pipe")
|
||||
|
||||
// determine what type of scan:
|
||||
// - git: scan the history of the repo
|
||||
// - no-git: scan files by treating the repo as a plain directory
|
||||
var (
|
||||
err error
|
||||
findings []report.Finding
|
||||
ctx = context.Background()
|
||||
)
|
||||
if noGit {
|
||||
findings, err = detector.DetectSource(
|
||||
ctx, &sources.Files{
|
||||
cmd.Context(), &sources.Files{
|
||||
Config: &cfg,
|
||||
FollowSymlinks: detector.FollowSymlinks,
|
||||
MaxFileSize: detector.MaxTargetMegaBytes * 1_000_000,
|
||||
@@ -93,7 +90,7 @@ func runDetect(cmd *cobra.Command, args []string) {
|
||||
}
|
||||
} else if fromPipe {
|
||||
findings, err = detector.DetectSource(
|
||||
ctx, &sources.File{
|
||||
cmd.Context(), &sources.File{
|
||||
Content: os.Stdin,
|
||||
MaxArchiveDepth: detector.MaxArchiveDepth,
|
||||
},
|
||||
@@ -111,7 +108,7 @@ func runDetect(cmd *cobra.Command, args []string) {
|
||||
)
|
||||
|
||||
logOpts := mustGetStringFlag(cmd, "log-opts")
|
||||
if gitCmd, err = sources.NewGitLogCmd(sourcePath, logOpts); err != nil {
|
||||
if gitCmd, err = sources.NewGitLogCmdContext(cmd.Context(), sourcePath, logOpts); err != nil {
|
||||
logging.Fatal().Err(err).Msg("could not create Git cmd")
|
||||
}
|
||||
|
||||
@@ -120,10 +117,10 @@ func runDetect(cmd *cobra.Command, args []string) {
|
||||
}
|
||||
|
||||
findings, err = detector.DetectSource(
|
||||
ctx, &sources.Git{
|
||||
cmd.Context(), &sources.Git{
|
||||
Cmd: gitCmd,
|
||||
Config: &detector.Config,
|
||||
Remote: sources.NewRemoteInfo(scmPlatform, sourcePath),
|
||||
Remote: sources.NewRemoteInfoContext(cmd.Context(), scmPlatform, sourcePath),
|
||||
Sema: detector.Sema,
|
||||
MaxArchiveDepth: detector.MaxArchiveDepth,
|
||||
},
|
||||
|
||||
+1
-2
@@ -1,7 +1,6 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
@@ -56,7 +55,7 @@ func runDirectory(cmd *cobra.Command, args []string) {
|
||||
}
|
||||
|
||||
findings, err := detector.DetectSource(
|
||||
context.Background(),
|
||||
cmd.Context(),
|
||||
&sources.Files{
|
||||
Config: &cfg,
|
||||
FollowSymlinks: detector.FollowSymlinks,
|
||||
|
||||
@@ -35,6 +35,8 @@ func main() {
|
||||
rules.AlgoliaApiKey(),
|
||||
rules.AlibabaAccessKey(),
|
||||
rules.AlibabaSecretKey(),
|
||||
rules.AmazonBedrockAPIKeyLongLived(),
|
||||
rules.AmazonBedrockAPIKeyShortLived(),
|
||||
rules.AnthropicAdminApiKey(),
|
||||
rules.AnthropicApiKey(),
|
||||
rules.ArtifactoryApiKey(),
|
||||
|
||||
@@ -47,3 +47,63 @@ func AWS() *config.Rule {
|
||||
}
|
||||
return utils.Validate(r, tps, fps)
|
||||
}
|
||||
|
||||
func AmazonBedrockAPIKeyLongLived() *config.Rule {
|
||||
// https://docs.aws.amazon.com/bedrock/latest/userguide/api-keys-how.html
|
||||
// https://medium.com/@adan.alvarez/api-keys-for-bedrock-a-brief-security-overview-2133ed9a2b3f
|
||||
r := config.Rule{
|
||||
RuleID: "aws-amazon-bedrock-api-key-long-lived",
|
||||
Description: "Identified a pattern that may indicate long-lived Amazon Bedrock API keys, risking unauthorized Amazon Bedrock usage",
|
||||
Regex: utils.GenerateUniqueTokenRegex(`ABSK[A-Za-z0-9+/]{109,269}={0,2}`, false),
|
||||
Entropy: 3,
|
||||
Keywords: []string{
|
||||
"ABSK", // Amazon Bedrock API Key (long-lived)
|
||||
},
|
||||
}
|
||||
|
||||
// validate
|
||||
tps := []string{
|
||||
// Valid API key example
|
||||
"ABSKQmVkcm9ja0FQSUtleS1EXAMPLEEXAMPLEEXAMPLEEXAMPLEEXAMPLEEXAMPLEEXAMPLEEXAMPLEEXAMPLEEXAMPLEEXAMPLEEXAMPLEEXAMPLEEXAMPLEEXAMPLEEXM=",
|
||||
// Generate additional random test keys
|
||||
utils.GenerateSampleSecret("bedrock", "ABSKQmVkcm9ja0FQSUtleS1"+secrets.NewSecret(utils.AlphaNumeric("108"))+"="),
|
||||
utils.GenerateSampleSecret("bedrock", "ABSKQmVkcm9ja0FQSUtleS1"+secrets.NewSecret(utils.AlphaNumeric("246"))),
|
||||
}
|
||||
|
||||
fps := []string{
|
||||
// Too short key (missing characters)
|
||||
"ABSKQmVkcm9ja0FQSUtleS1EXAMPLE",
|
||||
// Too long
|
||||
"ABSKQmVkcm9ja0FQSUtleS1EXAMPLEEXAMPLEEXAMPLEEXAMPLEEXAMPLEEXAMPLEEXAMPLEEXAMPLEEXAMPLEEXAMPLEEXAMPLEEXAMPLEEXAMPLEEXAMPLEEXAMPLEEXAMPLEEXAMPLEEXAMPLEEXAMPLEEXAMPLEEXAMPLEEXAMPLEEXAMPLEEXAMPLEEXAMPLEEXAMPLEEXAMPLEEXAMPLEEXAMPLEEXAMPLEEXAMPLEEXAMPLEEXAMPLEEXAMPLEEXAMPLEEXAMPLEEXAMPLEEXAMPLEEXAMPLEEXAMPLEEXAMPLEEXAMPLEEXAMPLEEXAMPLEEXAMPLEEXAMPLEEXAMPLEEXAMPLE=",
|
||||
// Wrong prefix
|
||||
"AXSKQmVkcm9ja0FQSUtleS1EXAMPLEEXAMPLEEXAMPLEEXAMPLEEXAMPLEEXAMPLEEXAMPLEEXAMPLEEXAMPLEEXAMPLEEXAMPLEEXAMPLEEXAMPLEEXAMPLEEXAMPLEEXM=",
|
||||
}
|
||||
|
||||
return utils.Validate(r, tps, fps)
|
||||
}
|
||||
|
||||
func AmazonBedrockAPIKeyShortLived() *config.Rule {
|
||||
// https://docs.aws.amazon.com/bedrock/latest/userguide/api-keys-how.html
|
||||
// https://github.com/aws/aws-bedrock-token-generator-js/blob/86277e1489354192c64ffc8f995601daacc1f715/src/token.ts#L21
|
||||
r := config.Rule{
|
||||
RuleID: "aws-amazon-bedrock-api-key-short-lived",
|
||||
Description: "Identified a pattern that may indicate short-lived Amazon Bedrock API keys, risking unauthorized Amazon Bedrock usage",
|
||||
Regex: regexp.MustCompile(`bedrock-api-key-YmVkcm9jay5hbWF6b25hd3MuY29t`),
|
||||
Entropy: 3,
|
||||
Keywords: []string{
|
||||
"bedrock-api-key-", // Amazon Bedrock API Key (short lived)
|
||||
},
|
||||
}
|
||||
|
||||
// validate
|
||||
tps := utils.GenerateSampleSecrets("AmazonBedrockAPIKeyShortLived", `bedrock-api-key-YmVkcm9jay5hbWF6b25hd3MuY29t`)
|
||||
|
||||
fps := []string{
|
||||
// Too short key (missing characters)
|
||||
"bedrock-api-key-",
|
||||
// Wrong prefix
|
||||
"bedrock-api-key-YmVkcm9jay5hbWF6b25hd3MuY29x",
|
||||
}
|
||||
|
||||
return utils.Validate(r, tps, fps)
|
||||
}
|
||||
@@ -13,6 +13,11 @@
|
||||
|
||||
title = "{{.Title}}"
|
||||
|
||||
# minVersion indicates the minimum Gitleaks version required to use this config.
|
||||
# If the running version is older, a warning will be logged and not all
|
||||
# config-enabled features are guaranteed to work.
|
||||
minVersion = "v8.25.0"
|
||||
|
||||
{{ with .Allowlists }}{{ range $i, $allowlist := . }}{{ if or $allowlist.Regexes $allowlist.Paths $allowlist.Commits $allowlist.StopWords }}# TODO: change to [[allowlists]]{{println}}[allowlist]
|
||||
{{- with .Description }}{{println}}description = "{{ . }}"{{ end }}
|
||||
{{- with .MatchCondition }}{{println}}condition = "{{ .String }}"{{ end }}
|
||||
|
||||
@@ -6,34 +6,34 @@ import (
|
||||
)
|
||||
|
||||
func Notion() *config.Rule {
|
||||
// Define the identifiers that match the Keywords
|
||||
identifiers := []string{"ntn_"}
|
||||
|
||||
// Define the regex pattern for Notion API token
|
||||
secretRegex := `ntn_[0-9]{11}[A-Za-z0-9]{32}[A-Za-z0-9]{3}`
|
||||
|
||||
regex := utils.GenerateUniqueTokenRegex(secretRegex, false)
|
||||
|
||||
r := config.Rule{
|
||||
Description: "Notion API token",
|
||||
RuleID: "notion-api-token",
|
||||
Regex: regex,
|
||||
Entropy: 4,
|
||||
Keywords: identifiers,
|
||||
}
|
||||
// Define the identifiers that match the Keywords
|
||||
identifiers := []string{"ntn_"}
|
||||
|
||||
// validate
|
||||
// Define the regex pattern for Notion API token
|
||||
secretRegex := `ntn_[0-9]{11}[A-Za-z0-9]{32}[A-Za-z0-9]{3}`
|
||||
|
||||
regex := utils.GenerateUniqueTokenRegex(secretRegex, false)
|
||||
|
||||
r := config.Rule{
|
||||
Description: "Notion API token",
|
||||
RuleID: "notion-api-token",
|
||||
Regex: regex,
|
||||
Entropy: 4,
|
||||
Keywords: identifiers,
|
||||
}
|
||||
|
||||
// validate
|
||||
tps := []string{
|
||||
"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",
|
||||
"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",
|
||||
"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",
|
||||
}
|
||||
|
||||
fps:= []string{
|
||||
fps := []string{
|
||||
"ntn_12345678901",
|
||||
"ntn_123456789012345678901234567890123456789012345678901234567890",
|
||||
"ntn_12345678901abc",
|
||||
}
|
||||
|
||||
return utils.Validate(r, tps, fps)
|
||||
}
|
||||
return utils.Validate(r, tps, fps)
|
||||
}
|
||||
@@ -10,18 +10,22 @@ func Sonar() *config.Rule {
|
||||
r := config.Rule{
|
||||
Description: "Uncovered a Sonar API token, potentially compromising software vulnerability scanning and code security.",
|
||||
RuleID: "sonar-api-token",
|
||||
Regex: utils.GenerateSemiGenericRegex([]string{"sonar[_.-]?(login|token)"}, utils.AlphaNumericExtended("40"), true),
|
||||
Regex: utils.GenerateSemiGenericRegex([]string{"sonar[_.-]?(login|token)"}, "(?:squ_|sqp_|sqa_)?"+utils.AlphaNumericExtended("40"), true),
|
||||
Keywords: []string{"sonar"},
|
||||
SecretGroup: 2,
|
||||
}
|
||||
|
||||
// validate
|
||||
tps := utils.GenerateSampleSecrets("sonar", "12345678ABCDEFH1234567890ABCDEFH12345678")
|
||||
tps = append(tps,
|
||||
`const SONAR_LOGIN = "12345678ABCDEFH1234567890ABCDEFH12345678"`, // gitleaks:allow
|
||||
`SONAR_LOGIN := "12345678ABCDEFH1234567890ABCDEFH12345678"`, // gitleaks:allow
|
||||
`SONAR.LOGIN ::= "12345678ABCDEFH1234567890ABCDEFH12345678"`, // gitleaks:allow
|
||||
`SONAR.LOGIN :::= "12345678ABCDEFH1234567890ABCDEFH12345678"`, // gitleaks:allow
|
||||
`SONAR.LOGIN ?= "12345678ABCDEFH1234567890ABCDEFH12345678"`, // gitleaks:allow
|
||||
`const SONAR_LOGIN = "12345678ABCDEFH1234567890ABCDEFH12345678"`, // gitleaks:allow
|
||||
`SONAR_LOGIN := "12345678ABCDEFH1234567890ABCDEFH12345678"`, // gitleaks:allow
|
||||
`SONAR.LOGIN ::= "12345678ABCDEFH1234567890ABCDEFH12345678"`, // gitleaks:allow
|
||||
`SONAR.LOGIN :::= "12345678ABCDEFH1234567890ABCDEFH12345678"`, // gitleaks:allow
|
||||
`SONAR.LOGIN ?= "12345678ABCDEFH1234567890ABCDEFH12345678"`, // gitleaks:allow
|
||||
`const SONAR_TOKEN = "squ_12345678ABCDEFH1234567890ABCDEFH12345678"`, // gitleaks:allow
|
||||
`SONAR_LOGIN := "sqp_12345678ABCDEFH1234567890ABCDEFH12345678"`, // gitleaks:allow
|
||||
`SONAR.TOKEN = "sqa_12345678ABCDEFH1234567890ABCDEFH12345678"`, // gitleaks:allow
|
||||
)
|
||||
return utils.Validate(r, tps, nil)
|
||||
}
|
||||
+4
-5
@@ -1,7 +1,6 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
@@ -63,13 +62,13 @@ func runGit(cmd *cobra.Command, args []string) {
|
||||
)
|
||||
|
||||
if preCommit || staged {
|
||||
if gitCmd, err = sources.NewGitDiffCmd(source, staged); err != nil {
|
||||
if gitCmd, err = sources.NewGitDiffCmdContext(cmd.Context(), source, staged); err != nil {
|
||||
logging.Fatal().Err(err).Msg("could not create Git diff cmd")
|
||||
}
|
||||
// Remote info + links are irrelevant for staged changes.
|
||||
scmPlatform = scm.NoPlatform
|
||||
} else {
|
||||
if gitCmd, err = sources.NewGitLogCmd(source, logOpts); err != nil {
|
||||
if gitCmd, err = sources.NewGitLogCmdContext(cmd.Context(), source, logOpts); err != nil {
|
||||
logging.Fatal().Err(err).Msg("could not create Git log cmd")
|
||||
}
|
||||
if scmPlatform, err = scm.PlatformFromString(mustGetStringFlag(cmd, "platform")); err != nil {
|
||||
@@ -78,11 +77,11 @@ func runGit(cmd *cobra.Command, args []string) {
|
||||
}
|
||||
|
||||
findings, err = detector.DetectSource(
|
||||
context.Background(),
|
||||
cmd.Context(),
|
||||
&sources.Git{
|
||||
Cmd: gitCmd,
|
||||
Config: &detector.Config,
|
||||
Remote: sources.NewRemoteInfo(scmPlatform, source),
|
||||
Remote: sources.NewRemoteInfoContext(cmd.Context(), scmPlatform, source),
|
||||
Sema: detector.Sema,
|
||||
MaxArchiveDepth: detector.MaxArchiveDepth,
|
||||
},
|
||||
|
||||
+1
-1
@@ -53,7 +53,7 @@ func runProtect(cmd *cobra.Command, args []string) {
|
||||
remote *detect.RemoteInfo
|
||||
)
|
||||
|
||||
if gitCmd, err = sources.NewGitDiffCmd(source, staged); err != nil {
|
||||
if gitCmd, err = sources.NewGitDiffCmdContext(cmd.Context(), source, staged); err != nil {
|
||||
logging.Fatal().Err(err).Msg("could not create Git diff cmd")
|
||||
}
|
||||
remote = &detect.RemoteInfo{Platform: scm.NoPlatform}
|
||||
|
||||
+16
-2
@@ -2,6 +2,7 @@ package cmd
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
@@ -19,6 +20,7 @@ import (
|
||||
"github.com/zricethezav/gitleaks/v8/logging"
|
||||
"github.com/zricethezav/gitleaks/v8/regexp"
|
||||
"github.com/zricethezav/gitleaks/v8/report"
|
||||
"github.com/zricethezav/gitleaks/v8/version"
|
||||
)
|
||||
|
||||
const banner = `
|
||||
@@ -42,7 +44,18 @@ var (
|
||||
rootCmd = &cobra.Command{
|
||||
Use: "gitleaks",
|
||||
Short: "Gitleaks scans code, past or present, for secrets",
|
||||
Version: Version,
|
||||
Version: version.Version,
|
||||
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
|
||||
// Set the timeout for all the commands
|
||||
if timeout, err := cmd.Flags().GetInt("timeout"); err != nil {
|
||||
return err
|
||||
} else if timeout > 0 {
|
||||
ctx, cancel := context.WithTimeout(cmd.Context(), time.Duration(timeout)*time.Second)
|
||||
cmd.SetContext(ctx)
|
||||
cobra.OnFinalize(cancel)
|
||||
}
|
||||
return nil
|
||||
},
|
||||
}
|
||||
|
||||
// diagnostics manager is global to ensure it can be started before a scan begins
|
||||
@@ -77,6 +90,7 @@ func init() {
|
||||
rootCmd.PersistentFlags().StringP("gitleaks-ignore-path", "i", ".", "path to .gitleaksignore file or folder containing one")
|
||||
rootCmd.PersistentFlags().Int("max-decode-depth", 0, "allow recursive decoding up to this depth (default \"0\", no decoding is done)")
|
||||
rootCmd.PersistentFlags().Int("max-archive-depth", 0, "allow scanning into nested archives up to this depth (default \"0\", no archive traversal is done)")
|
||||
rootCmd.PersistentFlags().Int("timeout", 0, "set a timeout for gitleaks commands in seconds (default \"0\", no timeout is set)")
|
||||
|
||||
// Add diagnostics flags
|
||||
rootCmd.PersistentFlags().String("diagnostics", "", "enable diagnostics (http OR comma-separated list: cpu,mem,trace). cpu=CPU prof, mem=memory prof, trace=exec tracing, http=serve via net/http/pprof")
|
||||
@@ -236,7 +250,7 @@ func Detector(cmd *cobra.Command, cfg config.Config, source string) *detect.Dete
|
||||
var err error
|
||||
|
||||
// Setup common detector
|
||||
detector := detect.NewDetector(cfg)
|
||||
detector := detect.NewDetectorContext(cmd.Context(), cfg)
|
||||
|
||||
if detector.MaxDecodeDepth, err = cmd.Flags().GetInt("max-decode-depth"); err != nil {
|
||||
logging.Fatal().Err(err).Send()
|
||||
|
||||
+1
-2
@@ -1,7 +1,6 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"context"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
@@ -38,7 +37,7 @@ func runStdIn(cmd *cobra.Command, _ []string) {
|
||||
exitCode := mustGetIntFlag(cmd, "exit-code")
|
||||
|
||||
findings, err := detector.DetectSource(
|
||||
context.Background(),
|
||||
cmd.Context(),
|
||||
&sources.File{
|
||||
Content: os.Stdin,
|
||||
MaxArchiveDepth: detector.MaxArchiveDepth,
|
||||
|
||||
+2
-3
@@ -4,10 +4,9 @@ import (
|
||||
"fmt"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
"github.com/zricethezav/gitleaks/v8/version"
|
||||
)
|
||||
|
||||
var Version = "version is set by build process"
|
||||
|
||||
func init() {
|
||||
rootCmd.AddCommand(versionCmd)
|
||||
}
|
||||
@@ -19,5 +18,5 @@ var versionCmd = &cobra.Command{
|
||||
}
|
||||
|
||||
func runVersion(cmd *cobra.Command, args []string) {
|
||||
fmt.Println(Version)
|
||||
fmt.Println(version.Version)
|
||||
}
|
||||
+64
-3
@@ -7,10 +7,12 @@ import (
|
||||
"sort"
|
||||
"strings"
|
||||
|
||||
gv "github.com/hashicorp/go-version"
|
||||
"github.com/spf13/viper"
|
||||
|
||||
"github.com/zricethezav/gitleaks/v8/logging"
|
||||
"github.com/zricethezav/gitleaks/v8/regexp"
|
||||
"github.com/zricethezav/gitleaks/v8/version"
|
||||
)
|
||||
|
||||
var (
|
||||
@@ -54,6 +56,10 @@ type ViperConfig struct {
|
||||
AllowList *viperGlobalAllowlist
|
||||
|
||||
Allowlists []*viperGlobalAllowlist
|
||||
|
||||
MinVersion string
|
||||
|
||||
configPath string
|
||||
}
|
||||
|
||||
type viperRequired struct {
|
||||
@@ -88,6 +94,7 @@ type Config struct {
|
||||
// used to keep sarif results consistent
|
||||
OrderedRules []string
|
||||
Allowlists []*Allowlist
|
||||
MinVersion string
|
||||
}
|
||||
|
||||
// Extend is a struct that allows users to define how they want their
|
||||
@@ -194,7 +201,22 @@ func (vc *ViperConfig) Translate() (Config, error) {
|
||||
Rules: rulesMap,
|
||||
Keywords: keywords,
|
||||
OrderedRules: orderedRules,
|
||||
MinVersion: vc.MinVersion,
|
||||
}
|
||||
|
||||
if extendDepth > 0 {
|
||||
// annoying hack to set the current config with the extended path
|
||||
// since if extendDepth > 0 we are operating an extended config.
|
||||
c.Path = vc.configPath
|
||||
} else {
|
||||
// I don't love this
|
||||
c.Path = viper.ConfigFileUsed()
|
||||
}
|
||||
|
||||
if err := validateMinVersion(c.MinVersion, c.Path); err != nil {
|
||||
return Config{}, err
|
||||
}
|
||||
|
||||
// Parse the config allowlists, including the older format for backwards compatibility.
|
||||
if vc.AllowList != nil {
|
||||
// TODO: Remove this in v9.
|
||||
@@ -219,7 +241,8 @@ func (vc *ViperConfig) Translate() (Config, error) {
|
||||
}
|
||||
}
|
||||
|
||||
if maxExtendDepth != extendDepth {
|
||||
currentExtendDepth := extendDepth
|
||||
if maxExtendDepth != currentExtendDepth {
|
||||
// disallow both usedefault and path from being set
|
||||
if c.Extend.Path != "" && c.Extend.UseDefault {
|
||||
return Config{}, errors.New("unable to load config due to extend.path and extend.useDefault being set")
|
||||
@@ -236,7 +259,7 @@ func (vc *ViperConfig) Translate() (Config, error) {
|
||||
}
|
||||
|
||||
// Validate the rules after everything has been assembled (including extended configs).
|
||||
if extendDepth == 0 {
|
||||
if currentExtendDepth == 0 {
|
||||
for _, rule := range c.Rules {
|
||||
if err := rule.Validate(); err != nil {
|
||||
return Config{}, err
|
||||
@@ -257,6 +280,41 @@ func (vc *ViperConfig) Translate() (Config, error) {
|
||||
return c, nil
|
||||
}
|
||||
|
||||
func validateMinVersion(minVer string, configPath string) error {
|
||||
if minVer == "" {
|
||||
logging.Debug().Str("config path", configPath).
|
||||
Msg("no minVersion specified in config... consider adding minVersion to ensure compatibility.")
|
||||
return nil
|
||||
}
|
||||
|
||||
if version.Version == version.DefaultMsg {
|
||||
logging.Debug().
|
||||
Str("required", minVer).
|
||||
Msg("dev build, skipping config version check.")
|
||||
return nil
|
||||
}
|
||||
|
||||
minSemVer, err := gv.NewSemver(minVer)
|
||||
if err != nil {
|
||||
return fmt.Errorf("invalid minVersion '%s': %w", minVer, err)
|
||||
}
|
||||
|
||||
currentSemVer, err := gv.NewSemver(version.Version)
|
||||
if err != nil {
|
||||
return fmt.Errorf("unable to parse current version: %w", err)
|
||||
}
|
||||
|
||||
if currentSemVer.LessThan(minSemVer) {
|
||||
logging.Warn().
|
||||
Str("required", minVer).
|
||||
Str("current", version.Version).
|
||||
Str("config path", configPath).
|
||||
Msg("config requires a newer Gitleaks version...")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (vc *ViperConfig) parseAllowlist(a *viperRuleAllowlist) (*Allowlist, error) {
|
||||
var matchCondition AllowlistMatchCondition
|
||||
switch strings.ToUpper(a.Condition) {
|
||||
@@ -344,11 +402,13 @@ func (c *Config) extendPath(parent *ViperConfig) error {
|
||||
if err := viper.Unmarshal(&extensionViperConfig); err != nil {
|
||||
return fmt.Errorf("failed to load extended config, err: %w", err)
|
||||
}
|
||||
|
||||
extensionViperConfig.configPath = c.Extend.Path
|
||||
logging.Debug().Msgf("extending config with %s", c.Extend.Path)
|
||||
cfg, err := extensionViperConfig.Translate()
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to load extended config, err: %w", err)
|
||||
}
|
||||
logging.Debug().Msgf("extending config with %s", c.Extend.Path)
|
||||
c.extend(cfg)
|
||||
return nil
|
||||
}
|
||||
@@ -428,4 +488,5 @@ func (c *Config) extend(extensionConfig Config) {
|
||||
|
||||
// sort to keep extended rules in order
|
||||
sort.Strings(c.OrderedRules)
|
||||
return
|
||||
}
|
||||
+17
-4
@@ -93,7 +93,7 @@ func TestTranslate(t *testing.T) {
|
||||
{
|
||||
cfgName: "invalid/rule_missing_id",
|
||||
cfg: Config{},
|
||||
wantError: errors.New("rule |id| is missing or empty, regex: (?i)(discord[a-z0-9_ .\\-,]{0,25})(=|>|:=|\\|\\|:|<=|=>|:).{0,5}['\\\"]([a-h0-9]{64})['\\\"]"),
|
||||
wantError: errors.New("rule |id| is missing or empty, description: Discord API key, regex: (?i)(discord[a-z0-9_ .\\-,]{0,25})(=|>|:=|\\|\\|:|<=|=>|:).{0,5}['\\\"]([a-h0-9]{64})['\\\"]"),
|
||||
},
|
||||
{
|
||||
cfgName: "invalid/rule_no_regex_or_path",
|
||||
@@ -449,7 +449,7 @@ func TestTranslateExtend(t *testing.T) {
|
||||
Rules: map[string]Rule{"aws-access-key": {
|
||||
RuleID: "aws-access-key",
|
||||
Description: "AWS Access Key",
|
||||
Regex: regexp.MustCompile("(?:a)(?:a)"),
|
||||
Regex: regexp.MustCompile("(a)(a)"),
|
||||
SecretGroup: 2,
|
||||
Keywords: []string{},
|
||||
Tags: []string{"key", "AWS"},
|
||||
@@ -563,6 +563,10 @@ func TestTranslateExtend(t *testing.T) {
|
||||
},
|
||||
|
||||
// Invalid
|
||||
{
|
||||
cfgName: "invalid/extend_invalid_ruleid",
|
||||
wantError: errors.New("rule |id| is missing or empty"),
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
@@ -589,8 +593,17 @@ func testTranslate(t *testing.T, test translateCase) {
|
||||
err = viper.Unmarshal(&vc)
|
||||
require.NoError(t, err)
|
||||
cfg, err := vc.Translate()
|
||||
if err != nil && !assert.EqualError(t, err, test.wantError.Error()) {
|
||||
return
|
||||
if err != nil {
|
||||
if test.wantError != nil {
|
||||
assert.EqualError(t, err, test.wantError.Error())
|
||||
} else {
|
||||
require.NoError(t, err)
|
||||
}
|
||||
} else {
|
||||
if test.wantError != nil {
|
||||
t.Fatalf("expected error but got none: %v", test.wantError)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
if len(test.rules) > 0 {
|
||||
|
||||
+21
-1
@@ -13,6 +13,11 @@
|
||||
|
||||
title = "gitleaks config"
|
||||
|
||||
# minVersion indicates the minimum Gitleaks version required to use this config.
|
||||
# If the running version is older, a warning will be logged and not all
|
||||
# config-enabled features are guaranteed to work.
|
||||
minVersion = "v8.25.0"
|
||||
|
||||
# TODO: change to [[allowlists]]
|
||||
[allowlist]
|
||||
description = "global allow lists"
|
||||
@@ -208,6 +213,20 @@ regexes = [
|
||||
'''.+EXAMPLE$''',
|
||||
]
|
||||
|
||||
[[rules]]
|
||||
id = "aws-amazon-bedrock-api-key-long-lived"
|
||||
description = "Identified a pattern that may indicate long-lived Amazon Bedrock API keys, risking unauthorized Amazon Bedrock usage"
|
||||
regex = '''\b(ABSK[A-Za-z0-9+/]{109,269}={0,2})(?:[\x60'"\s;]|\\[nr]|$)'''
|
||||
entropy = 3
|
||||
keywords = ["absk"]
|
||||
|
||||
[[rules]]
|
||||
id = "aws-amazon-bedrock-api-key-short-lived"
|
||||
description = "Identified a pattern that may indicate short-lived Amazon Bedrock API keys, risking unauthorized Amazon Bedrock usage"
|
||||
regex = '''bedrock-api-key-YmVkcm9jay5hbWF6b25hd3MuY29t'''
|
||||
entropy = 3
|
||||
keywords = ["bedrock-api-key-"]
|
||||
|
||||
[[rules]]
|
||||
id = "azure-ad-client-secret"
|
||||
description = "Azure AD Client Secret"
|
||||
@@ -3006,7 +3025,8 @@ keywords = ["snyk"]
|
||||
[[rules]]
|
||||
id = "sonar-api-token"
|
||||
description = "Uncovered a Sonar API token, potentially compromising software vulnerability scanning and code security."
|
||||
regex = '''(?i)[\w.-]{0,50}?(?:sonar[_.-]?(login|token))(?:[ \t\w.-]{0,20})[\s'"]{0,3}(?:=|>|:{1,3}=|\|\||:|=>|\?=|,)[\x60'"\s=]{0,5}([a-z0-9=_\-]{40})(?:[\x60'"\s;]|\\[nr]|$)'''
|
||||
regex = '''(?i)[\w.-]{0,50}?(?:sonar[_.-]?(login|token))(?:[ \t\w.-]{0,20})[\s'"]{0,3}(?:=|>|:{1,3}=|\|\||:|=>|\?=|,)[\x60'"\s=]{0,5}((?:squ_|sqp_|sqa_)?[a-z0-9=_\-]{40})(?:[\x60'"\s;]|\\[nr]|$)'''
|
||||
secretGroup = 2
|
||||
keywords = ["sonar"]
|
||||
|
||||
[[rules]]
|
||||
|
||||
+10
-8
@@ -69,15 +69,17 @@ func (r *Rule) Validate() error {
|
||||
// Ensure |id| is present.
|
||||
if strings.TrimSpace(r.RuleID) == "" {
|
||||
// Try to provide helpful context, since |id| is empty.
|
||||
var context string
|
||||
if r.Regex != nil {
|
||||
context = ", regex: " + r.Regex.String()
|
||||
} else if r.Path != nil {
|
||||
context = ", path: " + r.Path.String()
|
||||
} else if r.Description != "" {
|
||||
context = ", description: " + r.Description
|
||||
var sb strings.Builder
|
||||
if r.Description != "" {
|
||||
sb.WriteString(", description: " + r.Description)
|
||||
}
|
||||
return errors.New("rule |id| is missing or empty" + context)
|
||||
if r.Regex != nil {
|
||||
sb.WriteString(", regex: " + r.Regex.String())
|
||||
}
|
||||
if r.Path != nil {
|
||||
sb.WriteString(", path: " + r.Path.String())
|
||||
}
|
||||
return errors.New("rule |id| is missing or empty" + sb.String())
|
||||
}
|
||||
|
||||
// Ensure the rule actually matches something.
|
||||
|
||||
+57
-34
@@ -113,6 +113,12 @@ type Fragment sources.Fragment
|
||||
|
||||
// NewDetector creates a new detector with the given config
|
||||
func NewDetector(cfg config.Config) *Detector {
|
||||
return NewDetectorContext(context.Background(), cfg)
|
||||
}
|
||||
|
||||
// NewDetectorContext is the same as NewDetector but supports passing in a
|
||||
// context to use for timeouts
|
||||
func NewDetectorContext(ctx context.Context, cfg config.Config) *Detector {
|
||||
return &Detector{
|
||||
commitMap: make(map[string]bool),
|
||||
gitleaksIgnore: make(map[string]struct{}),
|
||||
@@ -121,7 +127,7 @@ func NewDetector(cfg config.Config) *Detector {
|
||||
findings: make([]report.Finding, 0),
|
||||
Config: cfg,
|
||||
prefilter: *ahocorasick.NewTrieBuilder().AddStrings(maps.Keys(cfg.Keywords)).Build(),
|
||||
Sema: semgroup.NewGroup(context.Background(), 40),
|
||||
Sema: semgroup.NewGroup(ctx, 40),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -240,7 +246,7 @@ func (d *Detector) DetectSource(ctx context.Context, source sources.Source) ([]r
|
||||
})
|
||||
}
|
||||
|
||||
for _, finding := range d.Detect(Fragment(fragment)) {
|
||||
for _, finding := range d.DetectContext(ctx, Fragment(fragment)) {
|
||||
d.AddFinding(finding)
|
||||
}
|
||||
|
||||
@@ -262,6 +268,12 @@ func (d *Detector) DetectSource(ctx context.Context, source sources.Source) ([]r
|
||||
|
||||
// Detect scans the given fragment and returns a list of findings
|
||||
func (d *Detector) Detect(fragment Fragment) []report.Finding {
|
||||
return d.DetectContext(context.Background(), fragment)
|
||||
}
|
||||
|
||||
// DetectContext is the same as Detect but supports passing in a
|
||||
// context to use for timeouts
|
||||
func (d *Detector) DetectContext(ctx context.Context, fragment Fragment) []report.Finding {
|
||||
if fragment.Bytes == nil {
|
||||
d.TotalBytes.Add(uint64(len(fragment.Raw)))
|
||||
}
|
||||
@@ -298,46 +310,57 @@ func (d *Detector) Detect(fragment Fragment) []report.Finding {
|
||||
currentDecodeDepth := 0
|
||||
decoder := codec.NewDecoder()
|
||||
|
||||
ScanLoop:
|
||||
for {
|
||||
// build keyword map for prefiltering rules
|
||||
keywords := make(map[string]bool)
|
||||
normalizedRaw := strings.ToLower(currentRaw)
|
||||
matches := d.prefilter.MatchString(normalizedRaw)
|
||||
for _, m := range matches {
|
||||
keywords[normalizedRaw[m.Pos():int(m.Pos())+len(m.Match())]] = true
|
||||
}
|
||||
|
||||
for _, rule := range d.Config.Rules {
|
||||
if len(rule.Keywords) == 0 {
|
||||
// if no keywords are associated with the rule always scan the
|
||||
// fragment using the rule
|
||||
findings = append(findings, d.detectRule(fragment, currentRaw, rule, encodedSegments)...)
|
||||
continue
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
break ScanLoop
|
||||
default:
|
||||
// build keyword map for prefiltering rules
|
||||
keywords := make(map[string]bool)
|
||||
normalizedRaw := strings.ToLower(currentRaw)
|
||||
matches := d.prefilter.MatchString(normalizedRaw)
|
||||
for _, m := range matches {
|
||||
keywords[normalizedRaw[m.Pos():int(m.Pos())+len(m.Match())]] = true
|
||||
}
|
||||
|
||||
// check if keywords are in the fragment
|
||||
for _, k := range rule.Keywords {
|
||||
if _, ok := keywords[strings.ToLower(k)]; ok {
|
||||
findings = append(findings, d.detectRule(fragment, currentRaw, rule, encodedSegments)...)
|
||||
break
|
||||
for _, rule := range d.Config.Rules {
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
break ScanLoop
|
||||
default:
|
||||
if len(rule.Keywords) == 0 {
|
||||
// if no keywords are associated with the rule always scan the
|
||||
// fragment using the rule
|
||||
findings = append(findings, d.detectRule(fragment, currentRaw, rule, encodedSegments)...)
|
||||
continue
|
||||
}
|
||||
|
||||
// check if keywords are in the fragment
|
||||
for _, k := range rule.Keywords {
|
||||
if _, ok := keywords[strings.ToLower(k)]; ok {
|
||||
findings = append(findings, d.detectRule(fragment, currentRaw, rule, encodedSegments)...)
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// increment the depth by 1 as we start our decoding pass
|
||||
currentDecodeDepth++
|
||||
// increment the depth by 1 as we start our decoding pass
|
||||
currentDecodeDepth++
|
||||
|
||||
// stop the loop if we've hit our max decoding depth
|
||||
if currentDecodeDepth > d.MaxDecodeDepth {
|
||||
break
|
||||
}
|
||||
// stop the loop if we've hit our max decoding depth
|
||||
if currentDecodeDepth > d.MaxDecodeDepth {
|
||||
break ScanLoop
|
||||
}
|
||||
|
||||
// decode the currentRaw for the next pass
|
||||
currentRaw, encodedSegments = decoder.Decode(currentRaw, encodedSegments)
|
||||
// decode the currentRaw for the next pass
|
||||
currentRaw, encodedSegments = decoder.Decode(currentRaw, encodedSegments)
|
||||
|
||||
// stop the loop when there's nothing else to decode
|
||||
if len(encodedSegments) == 0 {
|
||||
break
|
||||
// stop the loop when there's nothing else to decode
|
||||
if len(encodedSegments) == 0 {
|
||||
break ScanLoop
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -357,7 +380,7 @@ func (d *Detector) detectRule(fragment Fragment, currentRaw string, r config.Rul
|
||||
}()
|
||||
)
|
||||
|
||||
if r.SkipReport == true && !fragment.InheritedFromFinding {
|
||||
if r.SkipReport && !fragment.InheritedFromFinding {
|
||||
return findings
|
||||
}
|
||||
|
||||
|
||||
+21
-4
@@ -1530,6 +1530,7 @@ func TestDetectWithArchives(t *testing.T) {
|
||||
tests := []struct {
|
||||
cfgName string
|
||||
source string
|
||||
expireContext bool
|
||||
expectedFindings []report.Finding
|
||||
}{
|
||||
{
|
||||
@@ -2059,6 +2060,12 @@ func TestDetectWithArchives(t *testing.T) {
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
source: filepath.Join(archivesBasePath, "nested.tar.gz"),
|
||||
cfgName: "archives",
|
||||
expireContext: true,
|
||||
expectedFindings: []report.Finding{},
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
@@ -2073,13 +2080,17 @@ func TestDetectWithArchives(t *testing.T) {
|
||||
err = viper.Unmarshal(&vc)
|
||||
require.NoError(t, err)
|
||||
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
if tt.expireContext {
|
||||
cancel()
|
||||
}
|
||||
|
||||
cfg, _ := vc.Translate()
|
||||
detector := NewDetector(cfg)
|
||||
detector := NewDetectorContext(ctx, cfg)
|
||||
detector.MaxArchiveDepth = 8
|
||||
|
||||
findings, err := detector.DetectSource(
|
||||
context.Background(),
|
||||
&sources.Files{
|
||||
ctx, &sources.Files{
|
||||
Path: tt.source,
|
||||
Sema: detector.Sema,
|
||||
Config: &cfg,
|
||||
@@ -2087,7 +2098,13 @@ func TestDetectWithArchives(t *testing.T) {
|
||||
},
|
||||
)
|
||||
|
||||
require.NoError(t, err)
|
||||
if tt.expireContext {
|
||||
require.EqualError(t, err, "context canceled")
|
||||
} else {
|
||||
cancel()
|
||||
require.NoError(t, err)
|
||||
}
|
||||
|
||||
// TODO: Temporary mitigation.
|
||||
// https://github.com/gitleaks/gitleaks/issues/1641
|
||||
normalizedFindings := make([]report.Finding, len(findings))
|
||||
|
||||
@@ -10,6 +10,7 @@ require (
|
||||
github.com/gitleaks/go-gitdiff v0.9.1
|
||||
github.com/google/go-cmp v0.6.0
|
||||
github.com/h2non/filetype v1.1.3
|
||||
github.com/hashicorp/go-version v1.7.0
|
||||
github.com/mholt/archives v0.1.2
|
||||
github.com/rs/zerolog v1.33.0
|
||||
github.com/spf13/cobra v1.9.1
|
||||
|
||||
@@ -105,6 +105,8 @@ github.com/hashicorp/errwrap v1.1.0 h1:OxrOeh75EUXMY8TBjag2fzXGZ40LB6IKw45YeGUDY
|
||||
github.com/hashicorp/errwrap v1.1.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4=
|
||||
github.com/hashicorp/go-multierror v1.1.1 h1:H5DkEtf6CXdFp0N0Em5UCwQpXMWke8IA0+lD48awMYo=
|
||||
github.com/hashicorp/go-multierror v1.1.1/go.mod h1:iw975J/qwKPdAO1clOe2L8331t/9/fmwbPZ6JB6eMoM=
|
||||
github.com/hashicorp/go-version v1.7.0 h1:5tqGy27NaOTB8yJKUZELlFAS/LTKJkrmONwQKeRZfjY=
|
||||
github.com/hashicorp/go-version v1.7.0/go.mod h1:fltr4n8CU8Ke44wwGCBoEymUuxUHl09ZGVZPK5anwXA=
|
||||
github.com/hashicorp/golang-lru v0.5.0/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
|
||||
github.com/hashicorp/golang-lru v0.5.1/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
|
||||
github.com/hashicorp/golang-lru/v2 v2.0.7 h1:a+bsQ5rvGLjzHuww6tVxozPZFVghXaHOwFs4luLUK2k=
|
||||
|
||||
+80
-67
@@ -12,6 +12,7 @@ import (
|
||||
|
||||
"github.com/h2non/filetype"
|
||||
"github.com/mholt/archives"
|
||||
"github.com/rs/zerolog"
|
||||
|
||||
"github.com/zricethezav/gitleaks/v8/config"
|
||||
"github.com/zricethezav/gitleaks/v8/logging"
|
||||
@@ -56,26 +57,33 @@ func (s *File) Fragments(ctx context.Context, yield FragmentsFunc) error {
|
||||
// decide what to do with it.
|
||||
if err == nil && format != nil {
|
||||
if s.archiveDepth+1 > s.MaxArchiveDepth {
|
||||
// Only warn when the feature is enabled
|
||||
var event *zerolog.Event
|
||||
|
||||
// Warn if the feature is enabled; else emit a trace log.
|
||||
if s.MaxArchiveDepth != 0 {
|
||||
logging.Warn().Str(
|
||||
"path", s.FullPath(),
|
||||
).Int(
|
||||
"max_archive_depth", s.MaxArchiveDepth,
|
||||
).Msg("skipping archive: exceeds max archive depth")
|
||||
event = logging.Warn()
|
||||
} else {
|
||||
event = logging.Trace()
|
||||
}
|
||||
|
||||
event.Str(
|
||||
"path", s.FullPath(),
|
||||
).Int(
|
||||
"max_archive_depth", s.MaxArchiveDepth,
|
||||
).Msg("skipping archive: exceeds max archive depth")
|
||||
|
||||
return nil
|
||||
}
|
||||
if extractor, ok := format.(archives.Extractor); ok {
|
||||
return s.extractorFragments(ctx, extractor, s.Content, yield)
|
||||
}
|
||||
if decompressor, ok := format.(archives.Decompressor); ok {
|
||||
return s.decompressorFragments(decompressor, s.Content, yield)
|
||||
return s.decompressorFragments(ctx, decompressor, s.Content, yield)
|
||||
}
|
||||
logging.Warn().Str("path", s.FullPath()).Msg("skipping unknown archive type")
|
||||
}
|
||||
|
||||
return s.fileFragments(bufio.NewReader(s.Content), yield)
|
||||
return s.fileFragments(ctx, bufio.NewReader(s.Content), yield)
|
||||
}
|
||||
|
||||
// extractorFragments recursively crawls archives and yields fragments
|
||||
@@ -139,14 +147,14 @@ func (s *File) extractorFragments(ctx context.Context, extractor archives.Extrac
|
||||
}
|
||||
|
||||
// decompressorFragments recursively crawls archives and yields fragments
|
||||
func (s *File) decompressorFragments(decompressor archives.Decompressor, reader io.Reader, yield FragmentsFunc) error {
|
||||
func (s *File) decompressorFragments(ctx context.Context, decompressor archives.Decompressor, reader io.Reader, yield FragmentsFunc) error {
|
||||
innerReader, err := decompressor.OpenReader(reader)
|
||||
if err != nil {
|
||||
logging.Error().Str("path", s.FullPath()).Msg("could read compressed file")
|
||||
return nil
|
||||
}
|
||||
|
||||
if err := s.fileFragments(bufio.NewReader(innerReader), yield); err != nil {
|
||||
if err := s.fileFragments(ctx, bufio.NewReader(innerReader), yield); err != nil {
|
||||
_ = innerReader.Close()
|
||||
return err
|
||||
}
|
||||
@@ -156,7 +164,7 @@ func (s *File) decompressorFragments(decompressor archives.Decompressor, reader
|
||||
}
|
||||
|
||||
// fileFragments reads the file into fragments to yield
|
||||
func (s *File) fileFragments(reader *bufio.Reader, yield FragmentsFunc) error {
|
||||
func (s *File) fileFragments(ctx context.Context, reader *bufio.Reader, yield FragmentsFunc) error {
|
||||
// Create a buffer if the caller hasn't provided one
|
||||
if s.Buffer == nil {
|
||||
s.Buffer = make([]byte, defaultBufferSize)
|
||||
@@ -164,75 +172,80 @@ func (s *File) fileFragments(reader *bufio.Reader, yield FragmentsFunc) error {
|
||||
|
||||
totalLines := 0
|
||||
for {
|
||||
fragment := Fragment{
|
||||
FilePath: s.FullPath(),
|
||||
}
|
||||
|
||||
n, err := reader.Read(s.Buffer)
|
||||
if n == 0 {
|
||||
if err != nil && err != io.EOF {
|
||||
return yield(fragment, fmt.Errorf("could not read file: %w", err))
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return ctx.Err()
|
||||
default:
|
||||
fragment := Fragment{
|
||||
FilePath: s.FullPath(),
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// Only check the filetype at the start of file.
|
||||
if totalLines == 0 {
|
||||
// TODO: could other optimizations be introduced here?
|
||||
if mimetype, err := filetype.Match(s.Buffer[:n]); err != nil {
|
||||
return yield(
|
||||
fragment,
|
||||
fmt.Errorf("could not read file: could not determine type: %w", err),
|
||||
)
|
||||
} else if mimetype.MIME.Type == "application" {
|
||||
logging.Debug().
|
||||
Str("mime_type", mimetype.MIME.Value).
|
||||
Str("path", s.FullPath()).
|
||||
Msgf("skipping binary file")
|
||||
n, err := reader.Read(s.Buffer)
|
||||
if n == 0 {
|
||||
if err != nil && err != io.EOF {
|
||||
return yield(fragment, fmt.Errorf("could not read file: %w", err))
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
// Try to split chunks across large areas of whitespace, if possible.
|
||||
peekBuf := bytes.NewBuffer(s.Buffer[:n])
|
||||
if err := readUntilSafeBoundary(reader, n, maxPeekSize, peekBuf); err != nil {
|
||||
return yield(
|
||||
fragment,
|
||||
fmt.Errorf("could not read file: could not read until safe boundary: %w", err),
|
||||
)
|
||||
}
|
||||
// Only check the filetype at the start of file.
|
||||
if totalLines == 0 {
|
||||
// TODO: could other optimizations be introduced here?
|
||||
if mimetype, err := filetype.Match(s.Buffer[:n]); err != nil {
|
||||
return yield(
|
||||
fragment,
|
||||
fmt.Errorf("could not read file: could not determine type: %w", err),
|
||||
)
|
||||
} else if mimetype.MIME.Type == "application" {
|
||||
logging.Debug().
|
||||
Str("mime_type", mimetype.MIME.Value).
|
||||
Str("path", s.FullPath()).
|
||||
Msgf("skipping binary file")
|
||||
|
||||
fragment.Raw = peekBuf.String()
|
||||
fragment.Bytes = peekBuf.Bytes()
|
||||
fragment.StartLine = totalLines + 1
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
// Count the number of newlines in this chunk
|
||||
totalLines += strings.Count(fragment.Raw, "\n")
|
||||
// Try to split chunks across large areas of whitespace, if possible.
|
||||
peekBuf := bytes.NewBuffer(s.Buffer[:n])
|
||||
if err := readUntilSafeBoundary(reader, n, maxPeekSize, peekBuf); err != nil {
|
||||
return yield(
|
||||
fragment,
|
||||
fmt.Errorf("could not read file: could not read until safe boundary: %w", err),
|
||||
)
|
||||
}
|
||||
|
||||
if len(s.Symlink) > 0 {
|
||||
fragment.SymlinkFile = s.Symlink
|
||||
}
|
||||
fragment.Raw = peekBuf.String()
|
||||
fragment.Bytes = peekBuf.Bytes()
|
||||
fragment.StartLine = totalLines + 1
|
||||
|
||||
if isWindows {
|
||||
fragment.FilePath = filepath.ToSlash(fragment.FilePath)
|
||||
fragment.SymlinkFile = filepath.ToSlash(s.Symlink)
|
||||
fragment.WindowsFilePath = s.FullPath()
|
||||
}
|
||||
// Count the number of newlines in this chunk
|
||||
totalLines += strings.Count(fragment.Raw, "\n")
|
||||
|
||||
// log errors but continue since there's content
|
||||
if err != nil && err != io.EOF {
|
||||
logging.Warn().Err(err).Msgf("issue reading file")
|
||||
}
|
||||
if len(s.Symlink) > 0 {
|
||||
fragment.SymlinkFile = s.Symlink
|
||||
}
|
||||
|
||||
// Done with the file!
|
||||
if err == io.EOF {
|
||||
return yield(fragment, nil)
|
||||
}
|
||||
if isWindows {
|
||||
fragment.FilePath = filepath.ToSlash(fragment.FilePath)
|
||||
fragment.SymlinkFile = filepath.ToSlash(s.Symlink)
|
||||
fragment.WindowsFilePath = s.FullPath()
|
||||
}
|
||||
|
||||
if err := yield(fragment, err); err != nil {
|
||||
return err
|
||||
// log errors but continue since there's content
|
||||
if err != nil && err != io.EOF {
|
||||
logging.Warn().Err(err).Msgf("issue reading file")
|
||||
}
|
||||
|
||||
// Done with the file!
|
||||
if err == io.EOF {
|
||||
return yield(fragment, nil)
|
||||
}
|
||||
|
||||
if err := yield(fragment, err); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+43
-32
@@ -34,7 +34,8 @@ func DirectoryTargets(sourcePath string, s *semgroup.Group, followSymlinks bool,
|
||||
}
|
||||
|
||||
s.Go(func() error {
|
||||
err := files.scanTargets(func(scanTarget ScanTarget, err error) error {
|
||||
ctx := context.Background()
|
||||
err := files.scanTargets(ctx, func(scanTarget ScanTarget, err error) error {
|
||||
paths <- scanTarget
|
||||
return nil
|
||||
})
|
||||
@@ -56,7 +57,7 @@ type Files struct {
|
||||
}
|
||||
|
||||
// scanTargets yields scan targets to a callback func
|
||||
func (s *Files) scanTargets(yield func(ScanTarget, error) error) error {
|
||||
func (s *Files) scanTargets(ctx context.Context, yield func(ScanTarget, error) error) error {
|
||||
return filepath.WalkDir(s.Path, func(path string, d fs.DirEntry, err error) error {
|
||||
scanTarget := ScanTarget{Path: path}
|
||||
logger := logging.With().Str("path", path).Logger()
|
||||
@@ -141,40 +142,50 @@ func (s *Files) scanTargets(yield func(ScanTarget, error) error) error {
|
||||
func (s *Files) Fragments(ctx context.Context, yield FragmentsFunc) error {
|
||||
var wg sync.WaitGroup
|
||||
|
||||
err := s.scanTargets(func(scanTarget ScanTarget, err error) error {
|
||||
wg.Add(1)
|
||||
s.Sema.Go(func() error {
|
||||
logger := logging.With().Str("path", scanTarget.Path).Logger()
|
||||
logger.Trace().Msg("scanning path")
|
||||
err := s.scanTargets(ctx, func(scanTarget ScanTarget, err error) error {
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return ctx.Err()
|
||||
default:
|
||||
wg.Add(1)
|
||||
s.Sema.Go(func() error {
|
||||
logger := logging.With().Str("path", scanTarget.Path).Logger()
|
||||
logger.Trace().Msg("scanning path")
|
||||
|
||||
f, err := os.Open(scanTarget.Path)
|
||||
if err != nil {
|
||||
if os.IsPermission(err) {
|
||||
logger.Warn().Msg("skipping file: permission denied")
|
||||
f, err := os.Open(scanTarget.Path)
|
||||
if err != nil {
|
||||
if os.IsPermission(err) {
|
||||
logger.Warn().Msg("skipping file: permission denied")
|
||||
}
|
||||
wg.Done()
|
||||
return nil
|
||||
}
|
||||
|
||||
// Convert this to a file source
|
||||
file := File{
|
||||
Content: f,
|
||||
Path: scanTarget.Path,
|
||||
Symlink: scanTarget.Symlink,
|
||||
Config: s.Config,
|
||||
MaxArchiveDepth: s.MaxArchiveDepth,
|
||||
}
|
||||
|
||||
err = file.Fragments(ctx, yield)
|
||||
// Avoiding a defer in a hot loop
|
||||
_ = f.Close()
|
||||
wg.Done()
|
||||
return nil
|
||||
}
|
||||
return err
|
||||
})
|
||||
|
||||
// Convert this to a file source
|
||||
file := File{
|
||||
Content: f,
|
||||
Path: scanTarget.Path,
|
||||
Symlink: scanTarget.Symlink,
|
||||
Config: s.Config,
|
||||
MaxArchiveDepth: s.MaxArchiveDepth,
|
||||
}
|
||||
|
||||
err = file.Fragments(ctx, yield)
|
||||
// Avoiding a defer in a hot loop
|
||||
_ = f.Close()
|
||||
wg.Done()
|
||||
return err
|
||||
})
|
||||
|
||||
return nil
|
||||
return nil
|
||||
}
|
||||
})
|
||||
|
||||
wg.Wait()
|
||||
return err
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return ctx.Err()
|
||||
default:
|
||||
wg.Wait()
|
||||
return err
|
||||
}
|
||||
}
|
||||
+50
-12
@@ -63,6 +63,12 @@ func (br *blobReader) Close() error {
|
||||
// Caller should read everything from channels until receiving a signal about their closure and call
|
||||
// the `func (*DiffFilesCmd) Wait()` error in order to release resources.
|
||||
func NewGitLogCmd(source string, logOpts string) (*GitCmd, error) {
|
||||
return NewGitLogCmdContext(context.Background(), source, logOpts)
|
||||
}
|
||||
|
||||
// NewGitLogCmdContext is the same as NewGitLogCmd but supports passing in a
|
||||
// context to use for timeouts
|
||||
func NewGitLogCmdContext(ctx context.Context, source string, logOpts string) (*GitCmd, error) {
|
||||
sourceClean := filepath.Clean(source)
|
||||
var cmd *exec.Cmd
|
||||
if logOpts != "" {
|
||||
@@ -82,9 +88,9 @@ func NewGitLogCmd(source string, logOpts string) (*GitCmd, error) {
|
||||
}
|
||||
|
||||
args = append(args, userArgs...)
|
||||
cmd = exec.Command("git", args...)
|
||||
cmd = exec.CommandContext(ctx, "git", args...)
|
||||
} else {
|
||||
cmd = exec.Command("git", "-C", sourceClean, "log", "-p", "-U0",
|
||||
cmd = exec.CommandContext(ctx, "git", "-C", sourceClean, "log", "-p", "-U0",
|
||||
"--full-history", "--all", "--diff-filter=tuxdb")
|
||||
}
|
||||
|
||||
@@ -122,11 +128,17 @@ func NewGitLogCmd(source string, logOpts string) (*GitCmd, error) {
|
||||
// Caller should read everything from channels until receiving a signal about their closure and call
|
||||
// the `func (*DiffFilesCmd) Wait()` error in order to release resources.
|
||||
func NewGitDiffCmd(source string, staged bool) (*GitCmd, error) {
|
||||
return NewGitDiffCmdContext(context.Background(), source, staged)
|
||||
}
|
||||
|
||||
// NewGitDiffCmdContext is the same as NewGitDiffCmd but supports passing in a
|
||||
// context to use for timeouts
|
||||
func NewGitDiffCmdContext(ctx context.Context, source string, staged bool) (*GitCmd, error) {
|
||||
sourceClean := filepath.Clean(source)
|
||||
var cmd *exec.Cmd
|
||||
cmd = exec.Command("git", "-C", sourceClean, "diff", "-U0", "--no-ext-diff", ".")
|
||||
cmd = exec.CommandContext(ctx, "git", "-C", sourceClean, "diff", "-U0", "--no-ext-diff", ".")
|
||||
if staged {
|
||||
cmd = exec.Command("git", "-C", sourceClean, "diff", "-U0", "--no-ext-diff",
|
||||
cmd = exec.CommandContext(ctx, "git", "-C", sourceClean, "diff", "-U0", "--no-ext-diff",
|
||||
"--staged", ".")
|
||||
}
|
||||
logging.Debug().Msgf("executing: %s", cmd.String())
|
||||
@@ -177,13 +189,24 @@ func (c *GitCmd) Wait() error {
|
||||
return c.cmd.Wait()
|
||||
}
|
||||
|
||||
// String displays the command used for GitCmd
|
||||
func (c *GitCmd) String() string {
|
||||
return c.cmd.String()
|
||||
}
|
||||
|
||||
// NewBlobReader returns an io.ReadCloser that can be used to read a blob
|
||||
// within the git repo used to create the GitCmd.
|
||||
//
|
||||
// The caller is responsible for closing the reader.
|
||||
func (c *GitCmd) NewBlobReader(commit, path string) (io.ReadCloser, error) {
|
||||
return c.NewBlobReaderContext(context.Background(), commit, path)
|
||||
}
|
||||
|
||||
// NewBlobReaderContext is the same as NewBlobReader but supports passing in a
|
||||
// context to use for timeouts
|
||||
func (c *GitCmd) NewBlobReaderContext(ctx context.Context, commit, path string) (io.ReadCloser, error) {
|
||||
gitArgs := []string{"-C", c.repoPath, "cat-file", "blob", commit + ":" + path}
|
||||
cmd := exec.Command("git", gitArgs...)
|
||||
cmd := exec.CommandContext(ctx, "git", gitArgs...)
|
||||
cmd.Stderr = io.Discard
|
||||
stdout, err := cmd.StdoutPipe()
|
||||
if err != nil {
|
||||
@@ -275,7 +298,9 @@ type CommitInfo struct {
|
||||
// Fragments yields fragments from a git repo
|
||||
func (s *Git) Fragments(ctx context.Context, yield FragmentsFunc) error {
|
||||
defer func() {
|
||||
_ = s.Cmd.Wait()
|
||||
if err := s.Cmd.Wait(); err != nil {
|
||||
logging.Debug().Err(err).Str("cmd", s.Cmd.String()).Msg("command aborted")
|
||||
}
|
||||
}()
|
||||
|
||||
var (
|
||||
@@ -287,6 +312,8 @@ func (s *Git) Fragments(ctx context.Context, yield FragmentsFunc) error {
|
||||
// loop to range over both DiffFiles (stdout) and ErrCh (stderr)
|
||||
for diffFilesCh != nil || errCh != nil {
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return ctx.Err()
|
||||
case gitdiffFile, open := <-diffFilesCh:
|
||||
if !open {
|
||||
diffFilesCh = nil
|
||||
@@ -336,7 +363,7 @@ func (s *Git) Fragments(ctx context.Context, yield FragmentsFunc) error {
|
||||
defer wg.Done()
|
||||
|
||||
if yieldAsArchive {
|
||||
blob, err := s.Cmd.NewBlobReader(commitSHA, gitdiffFile.NewName)
|
||||
blob, err := s.Cmd.NewBlobReaderContext(ctx, commitSHA, gitdiffFile.NewName)
|
||||
if err != nil {
|
||||
logging.Error().Err(err).Msg("could not read archive blob")
|
||||
return nil
|
||||
@@ -394,17 +421,28 @@ func (s *Git) Fragments(ctx context.Context, yield FragmentsFunc) error {
|
||||
}
|
||||
}
|
||||
|
||||
wg.Wait()
|
||||
return nil
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return ctx.Err()
|
||||
default:
|
||||
wg.Wait()
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
// NewRemoteInfo builds a new RemoteInfo for generating finding links
|
||||
func NewRemoteInfo(platform scm.Platform, source string) *RemoteInfo {
|
||||
return NewRemoteInfoContext(context.Background(), platform, source)
|
||||
}
|
||||
|
||||
// NewRemoteInfoContext is the same as NewRemoteInfo but supports passing in a
|
||||
// context to use for timeouts
|
||||
func NewRemoteInfoContext(ctx context.Context, platform scm.Platform, source string) *RemoteInfo {
|
||||
if platform == scm.NoPlatform {
|
||||
return &RemoteInfo{Platform: platform}
|
||||
}
|
||||
|
||||
remoteUrl, err := getRemoteUrl(source)
|
||||
remoteUrl, err := getRemoteUrl(ctx, source)
|
||||
if err != nil {
|
||||
if strings.Contains(err.Error(), "No remote configured") {
|
||||
logging.Debug().Msg("skipping finding links: repository has no configured remote.")
|
||||
@@ -442,9 +480,9 @@ End:
|
||||
|
||||
var sshUrlpat = regexp.MustCompile(`^git@([a-zA-Z0-9.-]+):(?:\d{1,5}/)?([\w/.-]+?)(?:\.git)?$`)
|
||||
|
||||
func getRemoteUrl(source string) (*url.URL, error) {
|
||||
func getRemoteUrl(ctx context.Context, source string) (*url.URL, error) {
|
||||
// This will return the first remote — typically, "origin".
|
||||
cmd := exec.Command("git", "ls-remote", "--quiet", "--get-url")
|
||||
cmd := exec.CommandContext(ctx, "git", "ls-remote", "--quiet", "--get-url")
|
||||
if source != "." {
|
||||
cmd.Dir = source
|
||||
}
|
||||
|
||||
@@ -0,0 +1,7 @@
|
||||
[extend]
|
||||
useDefault = true
|
||||
|
||||
[[rules]]
|
||||
|
||||
[[rules.allowlists]]
|
||||
paths = ['^.*\.(xml|log|json)$']
|
||||
@@ -5,5 +5,5 @@ path = "../testdata/config/simple.toml"
|
||||
|
||||
[[rules]]
|
||||
id = "aws-access-key"
|
||||
regex = '''(?:a)(?:a)'''
|
||||
regex = '''(a)(a)'''
|
||||
secretGroup = 2
|
||||
@@ -0,0 +1,5 @@
|
||||
package version
|
||||
|
||||
// these two gotta be the same
|
||||
var DefaultMsg = "version is set by build process"
|
||||
var Version = "version is set by build process"
|
||||
Reference in new issue
Block a user