Compare commits

...
7 Commits
Author SHA1 Message Date
Zachary Rice 4b5e8e1829 Feat/allowlist regex target (#1107)
* adding regexTarget entry in allowlist

* update readme

* add globalregextarget to check

* update readme
2023-02-26 08:51:59 -06:00
C.J. May 343e69399b ignore package-lock.json (#1076)
* ignore package-lock.json

`package-lock.json` is automatically generated by npm

* add package-lock.json to global allowlist
2023-02-25 08:39:18 -06:00
Sandeep Bhat 0060ab636a Fix typos in README.md and CONTRIBUTING.md (#1090) 2023-02-25 08:38:41 -06:00
raffis 025908808f fix: ignore baseline if path was not relative in source (#1101)
* fix: ignore baseline if path was not relative in source

Signed-off-by: Raffael Sahli <raffael.sahli@doodle.com>

* test: ignore test secret

---------

Signed-off-by: Raffael Sahli <raffael.sahli@doodle.com>
2023-02-25 08:36:23 -06:00
SADIK KUZU 088f8b8074 Fix H in GitHub and update pre-commit rev tag in README (#1087) 2023-02-02 08:45:54 -06:00
Nejc Habjan afdccad46f Add missing GitLab token patterns (#1077) 2023-01-21 20:03:05 -06:00
very-doge-wowandFabian F Groß e002920355 Fix rule for private keys (#1072)
* refactor: fix rule for private keys

Wouldn't match old keys which have been created with the BLOCK statement at the beginning and end of the key.

* fix: rule for private keys

* fix: missing comma

Co-authored-by: Fabian F Groß <fabian.f.gross@deutschebahn.com>
2023-01-12 08:38:48 -06:00
13 changed files with 150 additions and 33 deletions

No files matched your search

+1 -1
View File
@@ -749,4 +749,4 @@ acce01f2338434a78f6a4a06a097b0fd23280484:README.md:aws-access-token:220
111c8b47b65627db117bd20be85ff0aee8961eb9:detect/detect_test.go:private-key:567 111c8b47b65627db117bd20be85ff0aee8961eb9:detect/detect_test.go:private-key:567
99d620c719020fa24baa831299f0c964da79875e:detect/detect_test.go:aws-access-token:512 99d620c719020fa24baa831299f0c964da79875e:detect/detect_test.go:aws-access-token:512
99d620c719020fa24baa831299f0c964da79875e:detect/detect_test.go:aws-access-token:513 99d620c719020fa24baa831299f0c964da79875e:detect/detect_test.go:aws-access-token:513
69f161c0a0cc0f7858f7d0609d656f4edcedd84c:detect/detect_test.go:discord-api-token:326
+1 -1
View File
@@ -28,7 +28,7 @@ If you want to add a new rule to the [default Gitleaks configuration](https://gi
func Beamer() *config.Rule { func Beamer() *config.Rule {
// Define Rule // Define Rule
r := config.Rule{ r := config.Rule{
// Human redable description of the rule // Human readable description of the rule
Description: "Beamer API token", Description: "Beamer API token",
// Unique ID for the rule // Unique ID for the rule
+13 -3
View File
@@ -77,7 +77,7 @@ cd gitleaks
make build make build
``` ```
### Github Action ### GitHub Action
Check out the official [Gitleaks GitHub Action](https://github.com/gitleaks/gitleaks-action) Check out the official [Gitleaks GitHub Action](https://github.com/gitleaks/gitleaks-action)
@@ -106,7 +106,7 @@ jobs:
``` ```
repos: repos:
- repo: https://github.com/zricethezav/gitleaks - repo: https://github.com/zricethezav/gitleaks
rev: v8.12.0 rev: v8.15.3
hooks: hooks:
- id: gitleaks - id: gitleaks
``` ```
@@ -335,6 +335,10 @@ paths = [
'''go\.mod''', '''go\.mod''',
'''go\.sum''' '''go\.sum'''
] ]
# note: (rule) regexTarget defaults to check the _Secret_ in the finding.
# if regexTarget is not specified then _Secret_ will be used.
# Acceptable values for regexTarget are "match" and "line"
regexTarget = "match"
regexes = [ regexes = [
'''process''', '''process''',
'''getenv''', '''getenv''',
@@ -357,6 +361,12 @@ paths = [
'''gitleaks\.toml''', '''gitleaks\.toml''',
'''(.*?)(jpg|gif|doc)''' '''(.*?)(jpg|gif|doc)'''
] ]
# note: (global) regexTarget defaults to check the _Secret_ in the finding.
# if regexTarget is not specified then _Secret_ will be used.
# Acceptable values for regexTarget are "match" and "line"
regexTarget = "match"
regexes = [ regexes = [
'''219-09-9999''', '''219-09-9999''',
'''078-05-1120''', '''078-05-1120''',
@@ -387,7 +397,7 @@ class CustomClass:
#### .gitleaksignore #### .gitleaksignore
You can ignore specific findings by creating a `.gitleaksignore` file at the root of your repo. In release v8.10.0 Gitleaks added a `Fingerprint` value to the Gitleaks report. Each leak, or finding, has a Fingerprint that uniquely identifies a secret. Add this fingerprint to the `.gitleaksignore` file to ignore that specific secret. See Gitleaks' [.gitleaksignore](https://github.com/zricethezav/gitleaks/blob/master/.gitleaksignore) for an example. Note: this feature is expirmental and is subject to change in the future. You can ignore specific findings by creating a `.gitleaksignore` file at the root of your repo. In release v8.10.0 Gitleaks added a `Fingerprint` value to the Gitleaks report. Each leak, or finding, has a Fingerprint that uniquely identifies a secret. Add this fingerprint to the `.gitleaksignore` file to ignore that specific secret. See Gitleaks' [.gitleaksignore](https://github.com/zricethezav/gitleaks/blob/master/.gitleaksignore) for an example. Note: this feature is experimental and is subject to change in the future.
## Secured by Jit ## Secured by Jit
+1 -1
View File
@@ -86,7 +86,7 @@ func runDetect(cmd *cobra.Command, args []string) {
// ignore findings from the baseline (an existing report in json format generated earlier) // ignore findings from the baseline (an existing report in json format generated earlier)
baselinePath, _ := cmd.Flags().GetString("baseline-path") baselinePath, _ := cmd.Flags().GetString("baseline-path")
if baselinePath != "" { if baselinePath != "" {
err = detector.AddBaseline(baselinePath) err = detector.AddBaseline(baselinePath, source)
if err != nil { if err != nil {
log.Error().Msgf("Could not load baseline. The path must point of a gitleaks report generated using the default format: %s", err) log.Error().Msgf("Could not load baseline. The path must point of a gitleaks report generated using the default format: %s", err)
} }
+3 -1
View File
@@ -77,7 +77,9 @@ func main() {
configRules = append(configRules, rules.GitHubOauth()) configRules = append(configRules, rules.GitHubOauth())
configRules = append(configRules, rules.GitHubApp()) configRules = append(configRules, rules.GitHubApp())
configRules = append(configRules, rules.GitHubRefresh()) configRules = append(configRules, rules.GitHubRefresh())
configRules = append(configRules, rules.Gitlab()) configRules = append(configRules, rules.GitlabPat())
configRules = append(configRules, rules.GitlabPipelineTriggerToken())
configRules = append(configRules, rules.GitlabRunnerRegistrationToken())
configRules = append(configRules, rules.GitterAccessToken()) configRules = append(configRules, rules.GitterAccessToken())
configRules = append(configRules, rules.GrafanaApiKey()) configRules = append(configRules, rules.GrafanaApiKey())
configRules = append(configRules, rules.GrafanaCloudApiToken()) configRules = append(configRules, rules.GrafanaCloudApiToken())
+2 -1
View File
@@ -17,6 +17,7 @@ paths = [
'''(.*?)(jpg|gif|doc|docx|zip|xls|pdf|bin|svg|socket)$''', '''(.*?)(jpg|gif|doc|docx|zip|xls|pdf|bin|svg|socket)$''',
'''(go.mod|go.sum)$''', '''(go.mod|go.sum)$''',
'''node_modules''', '''node_modules''',
'''package-lock.json''',
'''vendor''', '''vendor''',
] ]
@@ -57,4 +58,4 @@ keywords = [
{{ range $j, $keyword := $rule.Keywords }}"{{$keyword}}",{{end}} {{ range $j, $keyword := $rule.Keywords }}"{{$keyword}}",{{end}}
] ]
{{end}}{{end}} {{end}}{{end}}
+33 -1
View File
@@ -7,7 +7,7 @@ import (
"github.com/zricethezav/gitleaks/v8/config" "github.com/zricethezav/gitleaks/v8/config"
) )
func Gitlab() *config.Rule { func GitlabPat() *config.Rule {
// define rule // define rule
r := config.Rule{ r := config.Rule{
Description: "GitLab Personal Access Token", Description: "GitLab Personal Access Token",
@@ -22,3 +22,35 @@ func Gitlab() *config.Rule {
} }
return validate(r, tps, nil) return validate(r, tps, nil)
} }
func GitlabPipelineTriggerToken() *config.Rule {
// define rule
r := config.Rule{
Description: "GitLab Pipeline Trigger Token",
RuleID: "gitlab-ptt",
Regex: regexp.MustCompile(`glptt-[0-9a-f]{40}`),
Keywords: []string{"glptt-"},
}
// validate
tps := []string{
generateSampleSecret("gitlab", "glptt-"+secrets.NewSecret(hex("40"))),
}
return validate(r, tps, nil)
}
func GitlabRunnerRegistrationToken() *config.Rule {
// define rule
r := config.Rule{
Description: "GitLab Runner Registration Token",
RuleID: "gitlab-rrt",
Regex: regexp.MustCompile(`GR1348941[0-9a-zA-Z\-\_]{20}`),
Keywords: []string{"GR1348941"},
}
// validate
tps := []string{
generateSampleSecret("gitlab", "GR1348941"+secrets.NewSecret(alphaNumeric("20"))),
}
return validate(r, tps, nil)
}
+4 -1
View File
@@ -11,7 +11,7 @@ func PrivateKey() *config.Rule {
r := config.Rule{ r := config.Rule{
Description: "Private Key", Description: "Private Key",
RuleID: "private-key", RuleID: "private-key",
Regex: regexp.MustCompile(`(?i)-----BEGIN[ A-Z0-9_-]{0,100}PRIVATE KEY( BLOCK)?-----[\s\S-]*KEY----`), Regex: regexp.MustCompile(`(?i)-----BEGIN[ A-Z0-9_-]{0,100}PRIVATE KEY( BLOCK)?-----[\s\S-]*KEY( BLOCK)?----`),
Keywords: []string{"-----BEGIN"}, Keywords: []string{"-----BEGIN"},
} }
@@ -23,6 +23,9 @@ anything
abcdefghijklmnopqrstuvwxyz abcdefghijklmnopqrstuvwxyz
-----END RSA PRIVATE KEY----- -----END RSA PRIVATE KEY-----
`, `,
`-----BEGIN PRIVATE KEY BLOCK-----
anything
-----END PRIVATE KEY BLOCK-----`,
} // gitleaks:allow } // gitleaks:allow
return validate(r, tps, nil) return validate(r, tps, nil)
} }
+3
View File
@@ -14,6 +14,9 @@ type Allowlist struct {
// Regexes is slice of content regular expressions that are allowed to be ignored. // Regexes is slice of content regular expressions that are allowed to be ignored.
Regexes []*regexp.Regexp Regexes []*regexp.Regexp
// RegexTarget
RegexTarget string
// Paths is a slice of path regular expressions that are allowed to be ignored. // Paths is a slice of path regular expressions that are allowed to be ignored.
Paths []*regexp.Regexp Paths []*regexp.Regexp
+20 -16
View File
@@ -36,17 +36,19 @@ type ViperConfig struct {
Tags []string Tags []string
Allowlist struct { Allowlist struct {
Regexes []string RegexTarget string
Paths []string Regexes []string
Commits []string Paths []string
StopWords []string Commits []string
StopWords []string
} }
} }
Allowlist struct { Allowlist struct {
Regexes []string RegexTarget string
Paths []string Regexes []string
Commits []string Paths []string
StopWords []string Commits []string
StopWords []string
} }
} }
@@ -122,10 +124,11 @@ func (vc *ViperConfig) Translate() (Config, error) {
Tags: r.Tags, Tags: r.Tags,
Keywords: r.Keywords, Keywords: r.Keywords,
Allowlist: Allowlist{ Allowlist: Allowlist{
Regexes: allowlistRegexes, RegexTarget: r.Allowlist.RegexTarget,
Paths: allowlistPaths, Regexes: allowlistRegexes,
Commits: r.Allowlist.Commits, Paths: allowlistPaths,
StopWords: r.Allowlist.StopWords, Commits: r.Allowlist.Commits,
StopWords: r.Allowlist.StopWords,
}, },
} }
orderedRules = append(orderedRules, r.RuleID) orderedRules = append(orderedRules, r.RuleID)
@@ -148,10 +151,11 @@ func (vc *ViperConfig) Translate() (Config, error) {
Extend: vc.Extend, Extend: vc.Extend,
Rules: rulesMap, Rules: rulesMap,
Allowlist: Allowlist{ Allowlist: Allowlist{
Regexes: allowlistRegexes, RegexTarget: vc.Allowlist.RegexTarget,
Paths: allowlistPaths, Regexes: allowlistRegexes,
Commits: vc.Allowlist.Commits, Paths: allowlistPaths,
StopWords: vc.Allowlist.StopWords, Commits: vc.Allowlist.Commits,
StopWords: vc.Allowlist.StopWords,
}, },
Keywords: keywords, Keywords: keywords,
orderedRules: orderedRules, orderedRules: orderedRules,
+18 -1
View File
@@ -17,6 +17,7 @@ paths = [
'''(.*?)(jpg|gif|doc|docx|zip|xls|pdf|bin|svg|socket)$''', '''(.*?)(jpg|gif|doc|docx|zip|xls|pdf|bin|svg|socket)$''',
'''(go.mod|go.sum)$''', '''(go.mod|go.sum)$''',
'''node_modules''', '''node_modules''',
'''package-lock.json''',
'''vendor''', '''vendor''',
] ]
@@ -2016,6 +2017,22 @@ keywords = [
"glpat-", "glpat-",
] ]
[[rules]]
description = "GitLab Pipeline Trigger Token"
id = "gitlab-ptt"
regex = '''glptt-[0-9a-f]{40}'''
keywords = [
"glptt-",
]
[[rules]]
description = "GitLab Runner Registration Token"
id = "gitlab-rrt"
regex = '''GR1348941[0-9a-zA-Z\-\_]{20}'''
keywords = [
"gr1348941",
]
[[rules]] [[rules]]
description = "Gitter Access Token" description = "Gitter Access Token"
id = "gitter-access-token" id = "gitter-access-token"
@@ -2411,7 +2428,7 @@ keywords = [
[[rules]] [[rules]]
description = "Private Key" description = "Private Key"
id = "private-key" id = "private-key"
regex = '''(?i)-----BEGIN[ A-Z0-9_-]{0,100}PRIVATE KEY( BLOCK)?-----[\s\S-]*KEY----''' regex = '''(?i)-----BEGIN[ A-Z0-9_-]{0,100}PRIVATE KEY( BLOCK)?-----[\s\S-]*KEY( BLOCK)?----'''
keywords = [ keywords = [
"-----begin", "-----begin",
] ]
+38 -4
View File
@@ -160,14 +160,33 @@ func (d *Detector) AddGitleaksIgnore(gitleaksIgnorePath string) error {
return nil return nil
} }
func (d *Detector) AddBaseline(baselinePath string) error { func (d *Detector) AddBaseline(baselinePath string, source string) error {
if baselinePath != "" { if baselinePath != "" {
absoluteSource, err := filepath.Abs(source)
if err != nil {
return err
}
absoluteBaseline, err := filepath.Abs(baselinePath)
if err != nil {
return err
}
relativeBaseline, err := filepath.Rel(absoluteSource, absoluteBaseline)
if err != nil {
return err
}
baseline, err := LoadBaseline(baselinePath) baseline, err := LoadBaseline(baselinePath)
if err != nil { if err != nil {
return err return err
} }
d.baseline = baseline d.baseline = baseline
baselinePath = relativeBaseline
} }
d.baselinePath = baselinePath d.baselinePath = baselinePath
return nil return nil
} }
@@ -265,9 +284,24 @@ func (d *Detector) detectRule(fragment Fragment, rule config.Rule) []report.Find
continue continue
} }
// check if the secret is in the allowlist // check if the regexTarget is defined in the allowlist "regexes" entry
if rule.Allowlist.RegexAllowed(finding.Secret) || allowlistTarget := finding.Secret
d.Config.Allowlist.RegexAllowed(finding.Secret) { switch rule.Allowlist.RegexTarget {
case "match":
allowlistTarget = finding.Match
case "line":
allowlistTarget = finding.Line
}
globalAllowlistTarget := finding.Secret
switch d.Config.Allowlist.RegexTarget {
case "match":
globalAllowlistTarget = finding.Match
case "line":
globalAllowlistTarget = finding.Line
}
if rule.Allowlist.RegexAllowed(allowlistTarget) ||
d.Config.Allowlist.RegexAllowed(globalAllowlistTarget) {
continue continue
} }
+13 -2
View File
@@ -18,8 +18,9 @@ const repoBasePath = "../testdata/repos/"
func TestDetect(t *testing.T) { func TestDetect(t *testing.T) {
tests := []struct { tests := []struct {
cfgName string cfgName string
fragment Fragment baselinePath string
fragment Fragment
// NOTE: for expected findings, all line numbers will be 0 // NOTE: for expected findings, all line numbers will be 0
// because line deltas are added _after_ the finding is created. // because line deltas are added _after_ the finding is created.
// I.e, if the finding is from a --no-git file, the line number will be // I.e, if the finding is from a --no-git file, the line number will be
@@ -318,6 +319,15 @@ func TestDetect(t *testing.T) {
}, },
expectedFindings: []report.Finding{}, expectedFindings: []report.Finding{},
}, },
{
cfgName: "path_only",
baselinePath: ".baseline.json",
fragment: Fragment{
Raw: `const Discord_Public_Key = "e7322523fb86ed64c836a979cf8465fbd436378c653c1db38f9ae87bc62a6fd5"`,
FilePath: ".baseline.json",
},
expectedFindings: []report.Finding{},
},
} }
for _, tt := range tests { for _, tt := range tests {
@@ -344,6 +354,7 @@ func TestDetect(t *testing.T) {
assert.Equal(t, tt.wantError, err) assert.Equal(t, tt.wantError, err)
} }
d := NewDetector(cfg) d := NewDetector(cfg)
d.baselinePath = tt.baselinePath
findings := d.Detect(tt.fragment) findings := d.Detect(tt.fragment)
assert.ElementsMatch(t, tt.expectedFindings, findings) assert.ElementsMatch(t, tt.expectedFindings, findings)