|
|
@@ -17,6 +17,7 @@ import (
|
|
|
|
|
|
"github.com/zricethezav/gitleaks/v8/cmd/scm"
|
|
|
"github.com/zricethezav/gitleaks/v8/config"
|
|
|
+ "github.com/zricethezav/gitleaks/v8/detect/codec"
|
|
|
"github.com/zricethezav/gitleaks/v8/logging"
|
|
|
"github.com/zricethezav/gitleaks/v8/regexp"
|
|
|
"github.com/zricethezav/gitleaks/v8/report"
|
|
|
@@ -26,7 +27,7 @@ import (
|
|
|
const maxDecodeDepth = 8
|
|
|
const configPath = "../testdata/config/"
|
|
|
const repoBasePath = "../testdata/repos/"
|
|
|
-const b64TestValues = `
|
|
|
+const encodedTestValues = `
|
|
|
# Decoded
|
|
|
-----BEGIN PRIVATE KEY-----
|
|
|
135f/bRUBHrbHqLY/xS3I7Oth+8rgG+0tBwfMcbk05Sgxq6QUzSYIQAop+WvsTwk2sR+C38g0Mnb
|
|
|
@@ -44,16 +45,48 @@ eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwiY29uZmlnIjoiVzJ
|
|
|
c21hbGwtc2VjcmV0
|
|
|
|
|
|
# This tests how it handles when the match bounds go outside the decoded value
|
|
|
-secret=ZGVjb2RlZC1zZWNyZXQtdmFsdWU=
|
|
|
+secret=ZGVjb2RlZC1zZWNyZXQtdmFsdWUwMA==
|
|
|
# The above encoded again
|
|
|
c2VjcmV0PVpHVmpiMlJsWkMxelpXTnlaWFF0ZG1Gc2RXVT0=
|
|
|
|
|
|
# Confirm you can ignore on the decoded value
|
|
|
password="bFJxQkstejVrZjQtcGxlYXNlLWlnbm9yZS1tZS1YLVhJSk0yUGRkdw=="
|
|
|
+
|
|
|
+# This tests that it can do hex encoded data
|
|
|
+secret=6465636F6465642D7365637265742D76616C756576484558
|
|
|
+
|
|
|
+# This tests that it can do percent encoded data
|
|
|
+## partial encoded data
|
|
|
+secret=decoded-%73%65%63%72%65%74-valuev2
|
|
|
+## scattered encoded
|
|
|
+secret=%64%65coded-%73%65%63%72%65%74-valuev3
|
|
|
+
|
|
|
+# Test multi levels of encoding where the source is a partal encoding
|
|
|
+# it is important that the bounds of the predecessors are properly
|
|
|
+# considered
|
|
|
+## single percent encoding in the middle of multi layer b64
|
|
|
+c2VjcmV0PVpHVmpiMl%4AsWkMxelpXTnlaWFF0ZG1Gc2RXVjJOQT09
|
|
|
+## single percent encoding at the beginning of hex
|
|
|
+secret%3d6465636F6465642D7365637265742D76616C75657635
|
|
|
+## multiple percent encodings in a single layer base64
|
|
|
+secret=ZGVjb2%52lZC1zZWNyZXQtdm%46sdWV4ODY= # ends in x86
|
|
|
+## base64 encoded partially percent encoded value
|
|
|
+secret=ZGVjb2RlZC0lNzMlNjUlNjMlNzIlNjUlNzQtdmFsdWU=
|
|
|
+## one of the lines above that went through... a lot
|
|
|
+## and there's surrounding text around it
|
|
|
+Look at this value: %4EjMzMjU2NkE2MzZENTYzMDUwNTY3MDQ4%4eTY2RDcwNjk0RDY5NTUzMTRENkQ3ODYx%25%34%65TE3QTQ2MzY1NzZDNjQ0RjY1NTY3MDU5NTU1ODUyNkI2MjUzNTUzMDRFNkU0RTZCNTYzMTU1MzkwQQ== # isn't it crazy?
|
|
|
+## Multi percent encode two random characters close to the bounds of the base64
|
|
|
+## encoded data to make sure that the bounds are still correctly calculated
|
|
|
+secret=ZG%25%32%35%25%33%32%25%33%35%25%32%35%25%33%33%25%33%35%25%32%35%25%33%33%25%33%36%25%32%35%25%33%32%25%33%35%25%32%35%25%33%33%25%33%36%25%32%35%25%33%36%25%33%31%25%32%35%25%33%32%25%33%35%25%32%35%25%33%33%25%33%36%25%32%35%25%33%33%25%33%322RlZC1zZWNyZXQtd%25%36%64%25%34%36%25%37%33dWU=
|
|
|
+## The similar to the above but also touching the edge of the base64
|
|
|
+secret=%25%35%61%25%34%37%25%35%36jb2RlZC1zZWNyZXQtdmFsdWU%25%32%35%25%33%33%25%36%34
|
|
|
+## The similar to the above but also touching and overlapping the base64
|
|
|
+secret%3D%25%35%61%25%34%37%25%35%36jb2RlZC1zZWNyZXQtdmFsdWU%25%32%35%25%33%33%25%36%34
|
|
|
`
|
|
|
|
|
|
func TestDetect(t *testing.T) {
|
|
|
- tests := []struct {
|
|
|
+ logging.Logger = logging.Logger.Level(zerolog.TraceLevel)
|
|
|
+ tests := map[string]struct {
|
|
|
cfgName string
|
|
|
baselinePath string
|
|
|
fragment Fragment
|
|
|
@@ -65,14 +98,15 @@ func TestDetect(t *testing.T) {
|
|
|
expectedFindings []report.Finding
|
|
|
wantError error
|
|
|
}{
|
|
|
- {
|
|
|
+ // General
|
|
|
+ "valid allow comment (1)": {
|
|
|
cfgName: "simple",
|
|
|
fragment: Fragment{
|
|
|
Raw: `awsToken := \"AKIALALEMEL33243OKIA\ // gitleaks:allow"`,
|
|
|
FilePath: "tmp.go",
|
|
|
},
|
|
|
},
|
|
|
- {
|
|
|
+ "valid allow comment (2)": {
|
|
|
cfgName: "simple",
|
|
|
fragment: Fragment{
|
|
|
Raw: `awsToken := \
|
|
|
@@ -83,7 +117,7 @@ func TestDetect(t *testing.T) {
|
|
|
FilePath: "tmp.go",
|
|
|
},
|
|
|
},
|
|
|
- {
|
|
|
+ "invalid allow comment": {
|
|
|
cfgName: "simple",
|
|
|
fragment: Fragment{
|
|
|
Raw: `awsToken := \"AKIALALEMEL33243OKIA\"
|
|
|
@@ -110,30 +144,7 @@ func TestDetect(t *testing.T) {
|
|
|
},
|
|
|
},
|
|
|
},
|
|
|
- {
|
|
|
- cfgName: "escaped_character_group",
|
|
|
- fragment: Fragment{
|
|
|
- Raw: `pypi-AgEIcHlwaS5vcmcAAAAAAAAAA-AAAAAAAAAA-AAAAAAAAAA-AAAAAAAAAA-AAAAAAAAAA-AAAAAAAAAAB`,
|
|
|
- FilePath: "tmp.go",
|
|
|
- },
|
|
|
- expectedFindings: []report.Finding{
|
|
|
- {
|
|
|
- Description: "PyPI upload token",
|
|
|
- Secret: "pypi-AgEIcHlwaS5vcmcAAAAAAAAAA-AAAAAAAAAA-AAAAAAAAAA-AAAAAAAAAA-AAAAAAAAAA-AAAAAAAAAAB",
|
|
|
- Match: "pypi-AgEIcHlwaS5vcmcAAAAAAAAAA-AAAAAAAAAA-AAAAAAAAAA-AAAAAAAAAA-AAAAAAAAAA-AAAAAAAAAAB",
|
|
|
- Line: `pypi-AgEIcHlwaS5vcmcAAAAAAAAAA-AAAAAAAAAA-AAAAAAAAAA-AAAAAAAAAA-AAAAAAAAAA-AAAAAAAAAAB`,
|
|
|
- File: "tmp.go",
|
|
|
- RuleID: "pypi-upload-token",
|
|
|
- Tags: []string{"key", "pypi"},
|
|
|
- StartLine: 0,
|
|
|
- EndLine: 0,
|
|
|
- StartColumn: 1,
|
|
|
- EndColumn: 86,
|
|
|
- Entropy: 1.9606875,
|
|
|
- },
|
|
|
- },
|
|
|
- },
|
|
|
- {
|
|
|
+ "detect finding - aws": {
|
|
|
cfgName: "simple",
|
|
|
fragment: Fragment{
|
|
|
Raw: `awsToken := \"AKIALALEMEL33243OLIA\"`,
|
|
|
@@ -141,22 +152,22 @@ func TestDetect(t *testing.T) {
|
|
|
},
|
|
|
expectedFindings: []report.Finding{
|
|
|
{
|
|
|
+ RuleID: "aws-access-key",
|
|
|
Description: "AWS Access Key",
|
|
|
- Secret: "AKIALALEMEL33243OLIA",
|
|
|
- Match: "AKIALALEMEL33243OLIA",
|
|
|
- Line: `awsToken := \"AKIALALEMEL33243OLIA\"`,
|
|
|
File: "tmp.go",
|
|
|
- RuleID: "aws-access-key",
|
|
|
- Tags: []string{"key", "AWS"},
|
|
|
+ Line: `awsToken := \"AKIALALEMEL33243OLIA\"`,
|
|
|
+ Match: "AKIALALEMEL33243OLIA",
|
|
|
+ Secret: "AKIALALEMEL33243OLIA",
|
|
|
+ Entropy: 3.0841837,
|
|
|
StartLine: 0,
|
|
|
EndLine: 0,
|
|
|
StartColumn: 15,
|
|
|
EndColumn: 34,
|
|
|
- Entropy: 3.0841837,
|
|
|
+ Tags: []string{"key", "AWS"},
|
|
|
},
|
|
|
},
|
|
|
},
|
|
|
- {
|
|
|
+ "detect finding - sidekiq env var": {
|
|
|
cfgName: "simple",
|
|
|
fragment: Fragment{
|
|
|
Raw: `export BUNDLE_ENTERPRISE__CONTRIBSYS__COM=cafebabe:deadbeef;`,
|
|
|
@@ -164,22 +175,22 @@ func TestDetect(t *testing.T) {
|
|
|
},
|
|
|
expectedFindings: []report.Finding{
|
|
|
{
|
|
|
+ RuleID: "sidekiq-secret",
|
|
|
Description: "Sidekiq Secret",
|
|
|
+ File: "tmp.sh",
|
|
|
+ Line: `export BUNDLE_ENTERPRISE__CONTRIBSYS__COM=cafebabe:deadbeef;`,
|
|
|
Match: "BUNDLE_ENTERPRISE__CONTRIBSYS__COM=cafebabe:deadbeef;",
|
|
|
Secret: "cafebabe:deadbeef",
|
|
|
- Line: `export BUNDLE_ENTERPRISE__CONTRIBSYS__COM=cafebabe:deadbeef;`,
|
|
|
- File: "tmp.sh",
|
|
|
- RuleID: "sidekiq-secret",
|
|
|
- Tags: []string{},
|
|
|
Entropy: 2.6098502,
|
|
|
StartLine: 0,
|
|
|
EndLine: 0,
|
|
|
StartColumn: 8,
|
|
|
EndColumn: 60,
|
|
|
+ Tags: []string{},
|
|
|
},
|
|
|
},
|
|
|
},
|
|
|
- {
|
|
|
+ "detect finding - sidekiq env var, semicolon": {
|
|
|
cfgName: "simple",
|
|
|
fragment: Fragment{
|
|
|
Raw: `echo hello1; export BUNDLE_ENTERPRISE__CONTRIBSYS__COM="cafebabe:deadbeef" && echo hello2`,
|
|
|
@@ -187,22 +198,22 @@ func TestDetect(t *testing.T) {
|
|
|
},
|
|
|
expectedFindings: []report.Finding{
|
|
|
{
|
|
|
+ RuleID: "sidekiq-secret",
|
|
|
Description: "Sidekiq Secret",
|
|
|
- Match: "BUNDLE_ENTERPRISE__CONTRIBSYS__COM=\"cafebabe:deadbeef\"",
|
|
|
- Secret: "cafebabe:deadbeef",
|
|
|
File: "tmp.sh",
|
|
|
Line: `echo hello1; export BUNDLE_ENTERPRISE__CONTRIBSYS__COM="cafebabe:deadbeef" && echo hello2`,
|
|
|
- RuleID: "sidekiq-secret",
|
|
|
- Tags: []string{},
|
|
|
+ Match: "BUNDLE_ENTERPRISE__CONTRIBSYS__COM=\"cafebabe:deadbeef\"",
|
|
|
+ Secret: "cafebabe:deadbeef",
|
|
|
Entropy: 2.6098502,
|
|
|
StartLine: 0,
|
|
|
EndLine: 0,
|
|
|
StartColumn: 21,
|
|
|
EndColumn: 74,
|
|
|
+ Tags: []string{},
|
|
|
},
|
|
|
},
|
|
|
},
|
|
|
- {
|
|
|
+ "detect finding - sidekiq url": {
|
|
|
cfgName: "simple",
|
|
|
fragment: Fragment{
|
|
|
Raw: `url = "http://cafeb4b3:d3adb33f@enterprise.contribsys.com:80/path?param1=true¶m2=false#heading1"`,
|
|
|
@@ -210,153 +221,212 @@ func TestDetect(t *testing.T) {
|
|
|
},
|
|
|
expectedFindings: []report.Finding{
|
|
|
{
|
|
|
+ RuleID: "sidekiq-sensitive-url",
|
|
|
Description: "Sidekiq Sensitive URL",
|
|
|
- Match: "http://cafeb4b3:d3adb33f@enterprise.contribsys.com:",
|
|
|
- Secret: "cafeb4b3:d3adb33f",
|
|
|
File: "tmp.sh",
|
|
|
Line: `url = "http://cafeb4b3:d3adb33f@enterprise.contribsys.com:80/path?param1=true¶m2=false#heading1"`,
|
|
|
- RuleID: "sidekiq-sensitive-url",
|
|
|
- Tags: []string{},
|
|
|
+ Match: "http://cafeb4b3:d3adb33f@enterprise.contribsys.com:",
|
|
|
+ Secret: "cafeb4b3:d3adb33f",
|
|
|
Entropy: 2.984234,
|
|
|
StartLine: 0,
|
|
|
EndLine: 0,
|
|
|
StartColumn: 8,
|
|
|
EndColumn: 58,
|
|
|
+ Tags: []string{},
|
|
|
},
|
|
|
},
|
|
|
},
|
|
|
- {
|
|
|
- cfgName: "allow_aws_re",
|
|
|
+ "ignore finding - our config file": {
|
|
|
+ cfgName: "simple",
|
|
|
fragment: Fragment{
|
|
|
Raw: `awsToken := \"AKIALALEMEL33243OLIA\"`,
|
|
|
- FilePath: "tmp.go",
|
|
|
+ FilePath: filepath.Join(configPath, "simple.toml"),
|
|
|
},
|
|
|
},
|
|
|
- {
|
|
|
- cfgName: "allow_path",
|
|
|
+ "ignore finding - doesn't match path": {
|
|
|
+ cfgName: "generic_with_py_path",
|
|
|
fragment: Fragment{
|
|
|
- Raw: `awsToken := \"AKIALALEMEL33243OLIA\"`,
|
|
|
+ Raw: `const Discord_Public_Key = "e7322523fb86ed64c836a979cf8465fbd436378c653c1db38f9ae87bc62a6fd5"`,
|
|
|
FilePath: "tmp.go",
|
|
|
},
|
|
|
},
|
|
|
- {
|
|
|
- cfgName: "allow_commit",
|
|
|
- fragment: Fragment{
|
|
|
- Raw: `awsToken := \"AKIALALEMEL33243OLIA\"`,
|
|
|
- FilePath: "tmp.go",
|
|
|
- CommitSHA: "allowthiscommit",
|
|
|
- },
|
|
|
- },
|
|
|
- {
|
|
|
- cfgName: "entropy_group",
|
|
|
+ "detect finding - matches path,regex,entropy": {
|
|
|
+ cfgName: "generic_with_py_path",
|
|
|
fragment: Fragment{
|
|
|
Raw: `const Discord_Public_Key = "e7322523fb86ed64c836a979cf8465fbd436378c653c1db38f9ae87bc62a6fd5"`,
|
|
|
- FilePath: "tmp.go",
|
|
|
+ FilePath: "tmp.py",
|
|
|
},
|
|
|
expectedFindings: []report.Finding{
|
|
|
{
|
|
|
- Description: "Discord API key",
|
|
|
- Match: "Discord_Public_Key = \"e7322523fb86ed64c836a979cf8465fbd436378c653c1db38f9ae87bc62a6fd5\"",
|
|
|
- Secret: "e7322523fb86ed64c836a979cf8465fbd436378c653c1db38f9ae87bc62a6fd5",
|
|
|
+ RuleID: "generic-api-key",
|
|
|
+ Description: "Generic API Key",
|
|
|
+ File: "tmp.py",
|
|
|
Line: `const Discord_Public_Key = "e7322523fb86ed64c836a979cf8465fbd436378c653c1db38f9ae87bc62a6fd5"`,
|
|
|
- File: "tmp.go",
|
|
|
- RuleID: "discord-api-key",
|
|
|
- Tags: []string{},
|
|
|
+ Match: "Key = \"e7322523fb86ed64c836a979cf8465fbd436378c653c1db38f9ae87bc62a6fd5\"",
|
|
|
+ Secret: "e7322523fb86ed64c836a979cf8465fbd436378c653c1db38f9ae87bc62a6fd5",
|
|
|
Entropy: 3.7906237,
|
|
|
StartLine: 0,
|
|
|
EndLine: 0,
|
|
|
- StartColumn: 7,
|
|
|
+ StartColumn: 22,
|
|
|
EndColumn: 93,
|
|
|
+ Tags: []string{},
|
|
|
},
|
|
|
},
|
|
|
},
|
|
|
- {
|
|
|
+ "ignore finding - allowlist regex": {
|
|
|
cfgName: "generic_with_py_path",
|
|
|
+ fragment: Fragment{
|
|
|
+ Raw: `const Discord_Public_Key = "load2523fb86ed64c836a979cf8465fbd436378c653c1db38f9ae87bc62a6fd5"`,
|
|
|
+ FilePath: "tmp.py",
|
|
|
+ },
|
|
|
+ },
|
|
|
+
|
|
|
+ // Rule
|
|
|
+ "rule - ignore path": {
|
|
|
+ cfgName: "valid/rule_path_only",
|
|
|
+ baselinePath: ".baseline.json",
|
|
|
fragment: Fragment{
|
|
|
Raw: `const Discord_Public_Key = "e7322523fb86ed64c836a979cf8465fbd436378c653c1db38f9ae87bc62a6fd5"`,
|
|
|
- FilePath: "tmp.go",
|
|
|
+ FilePath: ".baseline.json",
|
|
|
},
|
|
|
},
|
|
|
- {
|
|
|
- cfgName: "generic_with_py_path",
|
|
|
+ "rule - detect path ": {
|
|
|
+ cfgName: "valid/rule_path_only",
|
|
|
fragment: Fragment{
|
|
|
Raw: `const Discord_Public_Key = "e7322523fb86ed64c836a979cf8465fbd436378c653c1db38f9ae87bc62a6fd5"`,
|
|
|
FilePath: "tmp.py",
|
|
|
},
|
|
|
expectedFindings: []report.Finding{
|
|
|
{
|
|
|
- Description: "Generic API Key",
|
|
|
- Match: "Key = \"e7322523fb86ed64c836a979cf8465fbd436378c653c1db38f9ae87bc62a6fd5\"",
|
|
|
- Secret: "e7322523fb86ed64c836a979cf8465fbd436378c653c1db38f9ae87bc62a6fd5",
|
|
|
- Line: `const Discord_Public_Key = "e7322523fb86ed64c836a979cf8465fbd436378c653c1db38f9ae87bc62a6fd5"`,
|
|
|
+ Description: "Python Files",
|
|
|
+ Match: "file detected: tmp.py",
|
|
|
File: "tmp.py",
|
|
|
- RuleID: "generic-api-key",
|
|
|
+ RuleID: "python-files-only",
|
|
|
Tags: []string{},
|
|
|
+ },
|
|
|
+ },
|
|
|
+ },
|
|
|
+ "rule - match based on entropy": {
|
|
|
+ cfgName: "valid/rule_entropy_group",
|
|
|
+ fragment: Fragment{
|
|
|
+ Raw: `const Discord_Public_Key = "e7322523fb86ed64c836a979cf8465fbd436378c653c1db38f9ae87bc62a6fd5"
|
|
|
+//const Discord_Public_Key = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
|
|
|
+`,
|
|
|
+ FilePath: "tmp.go",
|
|
|
+ },
|
|
|
+ expectedFindings: []report.Finding{
|
|
|
+ {
|
|
|
+ RuleID: "discord-api-key",
|
|
|
+ Description: "Discord API key",
|
|
|
+ File: "tmp.go",
|
|
|
+ Line: `const Discord_Public_Key = "e7322523fb86ed64c836a979cf8465fbd436378c653c1db38f9ae87bc62a6fd5"`,
|
|
|
+ Match: "Discord_Public_Key = \"e7322523fb86ed64c836a979cf8465fbd436378c653c1db38f9ae87bc62a6fd5\"",
|
|
|
+ Secret: "e7322523fb86ed64c836a979cf8465fbd436378c653c1db38f9ae87bc62a6fd5",
|
|
|
Entropy: 3.7906237,
|
|
|
StartLine: 0,
|
|
|
EndLine: 0,
|
|
|
- StartColumn: 22,
|
|
|
+ StartColumn: 7,
|
|
|
EndColumn: 93,
|
|
|
+ Tags: []string{},
|
|
|
},
|
|
|
},
|
|
|
},
|
|
|
- {
|
|
|
- cfgName: "path_only",
|
|
|
+
|
|
|
+ // Allowlists
|
|
|
+ "global allowlist - ignore regex": {
|
|
|
+ cfgName: "valid/allowlist_global_regex",
|
|
|
fragment: Fragment{
|
|
|
- Raw: `const Discord_Public_Key = "e7322523fb86ed64c836a979cf8465fbd436378c653c1db38f9ae87bc62a6fd5"`,
|
|
|
- FilePath: "tmp.py",
|
|
|
+ Raw: `awsToken := \"AKIALALEMEL33243OLIA\"`,
|
|
|
+ FilePath: "tmp.go",
|
|
|
+ },
|
|
|
+ },
|
|
|
+ "global allowlist - detect, doesn't match all conditions": {
|
|
|
+ cfgName: "valid/allowlist_global_multiple",
|
|
|
+ fragment: Fragment{
|
|
|
+ Raw: `
|
|
|
+const token = "mockSecret";
|
|
|
+// const token = "changeit";`,
|
|
|
+ FilePath: "config.txt",
|
|
|
},
|
|
|
expectedFindings: []report.Finding{
|
|
|
{
|
|
|
- Description: "Python Files",
|
|
|
- Match: "file detected: tmp.py",
|
|
|
- File: "tmp.py",
|
|
|
- RuleID: "python-files-only",
|
|
|
+ RuleID: "test",
|
|
|
+ File: "config.txt",
|
|
|
+ Line: "\nconst token = \"mockSecret\";",
|
|
|
+ Match: `token = "mockSecret"`,
|
|
|
+ Secret: "mockSecret",
|
|
|
+ Entropy: 2.9219282,
|
|
|
+ StartLine: 1,
|
|
|
+ EndLine: 1,
|
|
|
+ StartColumn: 8,
|
|
|
+ EndColumn: 27,
|
|
|
Tags: []string{},
|
|
|
},
|
|
|
},
|
|
|
},
|
|
|
- {
|
|
|
- cfgName: "bad_entropy_group",
|
|
|
+ "global allowlist - ignore, matches all conditions": {
|
|
|
+ cfgName: "valid/allowlist_global_multiple",
|
|
|
fragment: Fragment{
|
|
|
- Raw: `const Discord_Public_Key = "e7322523fb86ed64c836a979cf8465fbd436378c653c1db38f9ae87bc62a6fd5"`,
|
|
|
- FilePath: "tmp.go",
|
|
|
+ Raw: `token := "mockSecret";`,
|
|
|
+ FilePath: "node_modules/config.txt",
|
|
|
},
|
|
|
- wantError: fmt.Errorf("discord-api-key: invalid regex secret group 5, max regex secret group 3"),
|
|
|
},
|
|
|
- {
|
|
|
- cfgName: "simple",
|
|
|
+ "global allowlist - detect path, doesn't match all conditions": {
|
|
|
+ cfgName: "valid/allowlist_global_multiple",
|
|
|
fragment: Fragment{
|
|
|
- Raw: `awsToken := \"AKIALALEMEL33243OLIA\"`,
|
|
|
- FilePath: filepath.Join(configPath, "simple.toml"),
|
|
|
+ Raw: `var token = "fakeSecret";`,
|
|
|
+ FilePath: "node_modules/config.txt",
|
|
|
+ },
|
|
|
+ expectedFindings: []report.Finding{
|
|
|
+ {
|
|
|
+ RuleID: "test",
|
|
|
+ File: "node_modules/config.txt",
|
|
|
+ Line: "var token = \"fakeSecret\";",
|
|
|
+ Match: `token = "fakeSecret"`,
|
|
|
+ Secret: "fakeSecret",
|
|
|
+ Entropy: 2.8464394,
|
|
|
+ StartLine: 0,
|
|
|
+ EndLine: 0,
|
|
|
+ StartColumn: 5,
|
|
|
+ EndColumn: 24,
|
|
|
+ Tags: []string{},
|
|
|
+ },
|
|
|
},
|
|
|
},
|
|
|
- {
|
|
|
- cfgName: "allow_global_aws_re",
|
|
|
+ "allowlist - ignore commit": {
|
|
|
+ cfgName: "valid/allowlist_rule_commit",
|
|
|
+ fragment: Fragment{
|
|
|
+ Raw: `awsToken := \"AKIALALEMEL33243OLIA\"`,
|
|
|
+ FilePath: "tmp.go",
|
|
|
+ CommitSHA: "allowthiscommit",
|
|
|
+ },
|
|
|
+ },
|
|
|
+ "allowlist - ignore path": {
|
|
|
+ cfgName: "valid/allowlist_rule_path",
|
|
|
fragment: Fragment{
|
|
|
Raw: `awsToken := \"AKIALALEMEL33243OLIA\"`,
|
|
|
FilePath: "tmp.go",
|
|
|
},
|
|
|
},
|
|
|
- {
|
|
|
- cfgName: "generic_with_py_path",
|
|
|
+ "allowlist - ignore path when extending": {
|
|
|
+ cfgName: "valid/allowlist_rule_extend_default",
|
|
|
fragment: Fragment{
|
|
|
- Raw: `const Discord_Public_Key = "load2523fb86ed64c836a979cf8465fbd436378c653c1db38f9ae87bc62a6fd5"`,
|
|
|
- FilePath: "tmp.py",
|
|
|
+ Raw: `token = "aebfab88-7596-481d-82e8-c60c8f7de0c0"`,
|
|
|
+ FilePath: "path/to/your/problematic/file.js",
|
|
|
},
|
|
|
},
|
|
|
- {
|
|
|
- cfgName: "path_only",
|
|
|
- baselinePath: ".baseline.json",
|
|
|
+ "allowlist - ignore regex": {
|
|
|
+ cfgName: "valid/allowlist_rule_regex",
|
|
|
fragment: Fragment{
|
|
|
- Raw: `const Discord_Public_Key = "e7322523fb86ed64c836a979cf8465fbd436378c653c1db38f9ae87bc62a6fd5"`,
|
|
|
- FilePath: ".baseline.json",
|
|
|
+ Raw: `awsToken := \"AKIALALEMEL33243OLIA\"`,
|
|
|
+ FilePath: "tmp.go",
|
|
|
},
|
|
|
},
|
|
|
- {
|
|
|
- cfgName: "base64_encoded",
|
|
|
+ // Decoding
|
|
|
+ "detect encoded": {
|
|
|
+ cfgName: "encoded",
|
|
|
fragment: Fragment{
|
|
|
- Raw: b64TestValues,
|
|
|
+ Raw: encodedTestValues,
|
|
|
FilePath: "tmp.go",
|
|
|
},
|
|
|
expectedFindings: []report.Finding{
|
|
|
@@ -402,6 +472,90 @@ func TestDetect(t *testing.T) {
|
|
|
EndColumn: 207,
|
|
|
Entropy: 5.350665,
|
|
|
},
|
|
|
+ { // Encoded Small secret at the end to make sure it's picked up by the decoding
|
|
|
+ Description: "Small Secret",
|
|
|
+ Secret: "small-secret",
|
|
|
+ Match: "small-secret",
|
|
|
+ File: "tmp.go",
|
|
|
+ Line: "\nc21hbGwtc2VjcmV0",
|
|
|
+ RuleID: "small-secret",
|
|
|
+ Tags: []string{"small", "secret", "decoded:base64", "decode-depth:1"},
|
|
|
+ StartLine: 15,
|
|
|
+ EndLine: 15,
|
|
|
+ StartColumn: 2,
|
|
|
+ EndColumn: 17,
|
|
|
+ Entropy: 3.0849626,
|
|
|
+ },
|
|
|
+ { // Secret where the decoded match goes outside the encoded value
|
|
|
+ Description: "Overlapping",
|
|
|
+ Secret: "decoded-secret-value00",
|
|
|
+ Match: "secret=decoded-secret-value00",
|
|
|
+ File: "tmp.go",
|
|
|
+ Line: "\nsecret=ZGVjb2RlZC1zZWNyZXQtdmFsdWUwMA==",
|
|
|
+ RuleID: "overlapping",
|
|
|
+ Tags: []string{"overlapping", "decoded:base64", "decode-depth:1"},
|
|
|
+ StartLine: 18,
|
|
|
+ EndLine: 18,
|
|
|
+ StartColumn: 2,
|
|
|
+ EndColumn: 40,
|
|
|
+ Entropy: 3.4428623,
|
|
|
+ },
|
|
|
+ { // This just confirms that with no allowlist the pattern is detected (i.e. the regex is good)
|
|
|
+ Description: "Make sure this would be detected with no allowlist",
|
|
|
+ Secret: "lRqBK-z5kf4-please-ignore-me-X-XIJM2Pddw",
|
|
|
+ Match: "password=\"lRqBK-z5kf4-please-ignore-me-X-XIJM2Pddw\"",
|
|
|
+ File: "tmp.go",
|
|
|
+ Line: "\npassword=\"bFJxQkstejVrZjQtcGxlYXNlLWlnbm9yZS1tZS1YLVhJSk0yUGRkdw==\"",
|
|
|
+ RuleID: "decoded-password-dont-ignore",
|
|
|
+ Tags: []string{"decode-ignore", "decoded:base64", "decode-depth:1"},
|
|
|
+ StartLine: 23,
|
|
|
+ EndLine: 23,
|
|
|
+ StartColumn: 2,
|
|
|
+ EndColumn: 68,
|
|
|
+ Entropy: 4.5841837,
|
|
|
+ },
|
|
|
+ { // Hex encoded data check
|
|
|
+ Description: "Overlapping",
|
|
|
+ Secret: "decoded-secret-valuevHEX",
|
|
|
+ Match: "secret=decoded-secret-valuevHEX",
|
|
|
+ File: "tmp.go",
|
|
|
+ Line: "\nsecret=6465636F6465642D7365637265742D76616C756576484558",
|
|
|
+ RuleID: "overlapping",
|
|
|
+ Tags: []string{"overlapping", "decoded:hex", "decode-depth:1"},
|
|
|
+ StartLine: 26,
|
|
|
+ EndLine: 26,
|
|
|
+ StartColumn: 2,
|
|
|
+ EndColumn: 56,
|
|
|
+ Entropy: 3.6531072,
|
|
|
+ },
|
|
|
+ { // handle partial encoded percent data
|
|
|
+ Description: "Overlapping",
|
|
|
+ Secret: "decoded-secret-valuev2",
|
|
|
+ Match: "secret=decoded-secret-valuev2",
|
|
|
+ File: "tmp.go",
|
|
|
+ Line: "\nsecret=decoded-%73%65%63%72%65%74-valuev2",
|
|
|
+ RuleID: "overlapping",
|
|
|
+ Tags: []string{"overlapping", "decoded:percent", "decode-depth:1"},
|
|
|
+ StartLine: 30,
|
|
|
+ EndLine: 30,
|
|
|
+ StartColumn: 2,
|
|
|
+ EndColumn: 42,
|
|
|
+ Entropy: 3.4428623,
|
|
|
+ },
|
|
|
+ { // handle partial encoded percent data
|
|
|
+ Description: "Overlapping",
|
|
|
+ Secret: "decoded-secret-valuev3",
|
|
|
+ Match: "secret=decoded-secret-valuev3",
|
|
|
+ File: "tmp.go",
|
|
|
+ Line: "\nsecret=%64%65coded-%73%65%63%72%65%74-valuev3",
|
|
|
+ RuleID: "overlapping",
|
|
|
+ Tags: []string{"overlapping", "decoded:percent", "decode-depth:1"},
|
|
|
+ StartLine: 32,
|
|
|
+ EndLine: 32,
|
|
|
+ StartColumn: 2,
|
|
|
+ EndColumn: 46,
|
|
|
+ Entropy: 3.4428623,
|
|
|
+ },
|
|
|
{ // Encoded AWS config with a access key id inside a JWT
|
|
|
Description: "AWS IAM Unique Identifier",
|
|
|
Secret: "ASIAIOSFODNN7LXM10JI",
|
|
|
@@ -430,68 +584,138 @@ func TestDetect(t *testing.T) {
|
|
|
EndColumn: 344,
|
|
|
Entropy: 4.721928,
|
|
|
},
|
|
|
- { // Encoded Small secret at the end to make sure it's picked up by the decoding
|
|
|
- Description: "Small Secret",
|
|
|
- Secret: "small-secret",
|
|
|
- Match: "small-secret",
|
|
|
+ { // Secret where the decoded match goes outside the encoded value and then encoded again
|
|
|
+ Description: "Overlapping",
|
|
|
+ Secret: "decoded-secret-value",
|
|
|
+ Match: "secret=decoded-secret-value",
|
|
|
File: "tmp.go",
|
|
|
- Line: "\nc21hbGwtc2VjcmV0",
|
|
|
- RuleID: "small-secret",
|
|
|
- Tags: []string{"small", "secret", "decoded:base64", "decode-depth:1"},
|
|
|
- StartLine: 15,
|
|
|
- EndLine: 15,
|
|
|
+ Line: "\nc2VjcmV0PVpHVmpiMlJsWkMxelpXTnlaWFF0ZG1Gc2RXVT0=",
|
|
|
+ RuleID: "overlapping",
|
|
|
+ Tags: []string{"overlapping", "decoded:base64", "decode-depth:2"},
|
|
|
+ StartLine: 20,
|
|
|
+ EndLine: 20,
|
|
|
StartColumn: 2,
|
|
|
- EndColumn: 17,
|
|
|
- Entropy: 3.0849626,
|
|
|
+ EndColumn: 49,
|
|
|
+ Entropy: 3.3037016,
|
|
|
},
|
|
|
- { // Secret where the decoded match goes outside the encoded value
|
|
|
+ { // handle encodings that touch eachother
|
|
|
+ Description: "Overlapping",
|
|
|
+ Secret: "decoded-secret-valuev5",
|
|
|
+ Match: "secret=decoded-secret-valuev5",
|
|
|
+ File: "tmp.go",
|
|
|
+ Line: "\nsecret%3d6465636F6465642D7365637265742D76616C75657635",
|
|
|
+ RuleID: "overlapping",
|
|
|
+ Tags: []string{"overlapping", "decoded:percent", "decoded:hex", "decode-depth:2"},
|
|
|
+ StartLine: 40,
|
|
|
+ EndLine: 40,
|
|
|
+ StartColumn: 2,
|
|
|
+ EndColumn: 54,
|
|
|
+ Entropy: 3.4428623,
|
|
|
+ },
|
|
|
+ { // handle partial encoded percent data465642D7365637265742D76616C75657635
|
|
|
+ Description: "Overlapping",
|
|
|
+ Secret: "decoded-secret-valuev4",
|
|
|
+ Match: "secret=decoded-secret-valuev4",
|
|
|
+ File: "tmp.go",
|
|
|
+ Line: "\nc2VjcmV0PVpHVmpiMl%4AsWkMxelpXTnlaWFF0ZG1Gc2RXVjJOQT09",
|
|
|
+ RuleID: "overlapping",
|
|
|
+ Tags: []string{"overlapping", "decoded:percent", "decoded:base64", "decode-depth:3"},
|
|
|
+ StartLine: 38,
|
|
|
+ EndLine: 38,
|
|
|
+ StartColumn: 2,
|
|
|
+ EndColumn: 55,
|
|
|
+ Entropy: 3.4428623,
|
|
|
+ },
|
|
|
+ { // multiple percent encodings in a single layer base64
|
|
|
+ Description: "Overlapping",
|
|
|
+ Secret: "decoded-secret-valuex86",
|
|
|
+ Match: "secret=decoded-secret-valuex86",
|
|
|
+ File: "tmp.go",
|
|
|
+ Line: "\nsecret=ZGVjb2%52lZC1zZWNyZXQtdm%46sdWV4ODY= # ends in x86",
|
|
|
+ RuleID: "overlapping",
|
|
|
+ Tags: []string{"overlapping", "decoded:percent", "decoded:base64", "decode-depth:2"},
|
|
|
+ StartLine: 42,
|
|
|
+ EndLine: 42,
|
|
|
+ StartColumn: 2,
|
|
|
+ EndColumn: 44,
|
|
|
+ Entropy: 3.6381476,
|
|
|
+ },
|
|
|
+ { // base64 encoded partially percent encoded value
|
|
|
Description: "Overlapping",
|
|
|
Secret: "decoded-secret-value",
|
|
|
Match: "secret=decoded-secret-value",
|
|
|
File: "tmp.go",
|
|
|
- Line: "\nsecret=ZGVjb2RlZC1zZWNyZXQtdmFsdWU=",
|
|
|
+ Line: "\nsecret=ZGVjb2RlZC0lNzMlNjUlNjMlNzIlNjUlNzQtdmFsdWU=",
|
|
|
RuleID: "overlapping",
|
|
|
- Tags: []string{"overlapping", "decoded:base64", "decode-depth:1"},
|
|
|
- StartLine: 18,
|
|
|
- EndLine: 18,
|
|
|
+ Tags: []string{"overlapping", "decoded:percent", "decoded:base64", "decode-depth:2"},
|
|
|
+ StartLine: 44,
|
|
|
+ EndLine: 44,
|
|
|
StartColumn: 2,
|
|
|
- EndColumn: 36,
|
|
|
+ EndColumn: 52,
|
|
|
Entropy: 3.3037016,
|
|
|
},
|
|
|
- { // Secret where the decoded match goes outside the encoded value and then encoded again
|
|
|
+ { // one of the lines above that went through... a lot
|
|
|
Description: "Overlapping",
|
|
|
Secret: "decoded-secret-value",
|
|
|
Match: "secret=decoded-secret-value",
|
|
|
File: "tmp.go",
|
|
|
- Line: "\nc2VjcmV0PVpHVmpiMlJsWkMxelpXTnlaWFF0ZG1Gc2RXVT0=",
|
|
|
+ Line: "\nLook at this value: %4EjMzMjU2NkE2MzZENTYzMDUwNTY3MDQ4%4eTY2RDcwNjk0RDY5NTUzMTRENkQ3ODYx%25%34%65TE3QTQ2MzY1NzZDNjQ0RjY1NTY3MDU5NTU1ODUyNkI2MjUzNTUzMDRFNkU0RTZCNTYzMTU1MzkwQQ== # isn't it crazy?",
|
|
|
RuleID: "overlapping",
|
|
|
- Tags: []string{"overlapping", "decoded:base64", "decode-depth:2"},
|
|
|
- StartLine: 20,
|
|
|
- EndLine: 20,
|
|
|
+ Tags: []string{"overlapping", "decoded:percent", "decoded:hex", "decoded:base64", "decode-depth:7"},
|
|
|
+ StartLine: 47,
|
|
|
+ EndLine: 47,
|
|
|
+ StartColumn: 22,
|
|
|
+ EndColumn: 177,
|
|
|
+ Entropy: 3.3037016,
|
|
|
+ },
|
|
|
+ { // Multi percent encode two random characters close to the bounds of the base64
|
|
|
+ Description: "Overlapping",
|
|
|
+ Secret: "decoded-secret-value",
|
|
|
+ Match: "secret=decoded-secret-value",
|
|
|
+ File: "tmp.go",
|
|
|
+ Line: "\nsecret=ZG%25%32%35%25%33%32%25%33%35%25%32%35%25%33%33%25%33%35%25%32%35%25%33%33%25%33%36%25%32%35%25%33%32%25%33%35%25%32%35%25%33%33%25%33%36%25%32%35%25%33%36%25%33%31%25%32%35%25%33%32%25%33%35%25%32%35%25%33%33%25%33%36%25%32%35%25%33%33%25%33%322RlZC1zZWNyZXQtd%25%36%64%25%34%36%25%37%33dWU=",
|
|
|
+ RuleID: "overlapping",
|
|
|
+ Tags: []string{"overlapping", "decoded:percent", "decoded:base64", "decode-depth:5"},
|
|
|
+ StartLine: 50,
|
|
|
+ EndLine: 50,
|
|
|
StartColumn: 2,
|
|
|
- EndColumn: 49,
|
|
|
+ EndColumn: 300,
|
|
|
Entropy: 3.3037016,
|
|
|
},
|
|
|
- { // This just confirms that with no allowlist the pattern is detected (i.e. the regex is good)
|
|
|
- Description: "Make sure this would be detected with no allowlist",
|
|
|
- Secret: "lRqBK-z5kf4-please-ignore-me-X-XIJM2Pddw",
|
|
|
- Match: "password=\"lRqBK-z5kf4-please-ignore-me-X-XIJM2Pddw\"",
|
|
|
+ { // The similar to the above but also touching the edge of the base64
|
|
|
+ Description: "Overlapping",
|
|
|
+ Secret: "decoded-secret-value",
|
|
|
+ Match: "secret=decoded-secret-value",
|
|
|
File: "tmp.go",
|
|
|
- Line: "\npassword=\"bFJxQkstejVrZjQtcGxlYXNlLWlnbm9yZS1tZS1YLVhJSk0yUGRkdw==\"",
|
|
|
- RuleID: "decoded-password-dont-ignore",
|
|
|
- Tags: []string{"decode-ignore", "decoded:base64", "decode-depth:1"},
|
|
|
- StartLine: 23,
|
|
|
- EndLine: 23,
|
|
|
+ Line: "\nsecret=%25%35%61%25%34%37%25%35%36jb2RlZC1zZWNyZXQtdmFsdWU%25%32%35%25%33%33%25%36%34",
|
|
|
+ RuleID: "overlapping",
|
|
|
+ Tags: []string{"overlapping", "decoded:percent", "decoded:base64", "decode-depth:4"},
|
|
|
+ StartLine: 52,
|
|
|
+ EndLine: 52,
|
|
|
StartColumn: 2,
|
|
|
- EndColumn: 68,
|
|
|
- Entropy: 4.5841837,
|
|
|
+ EndColumn: 86,
|
|
|
+ Entropy: 3.3037016,
|
|
|
+ },
|
|
|
+ { // The similar to the above but also touching and overlapping the base64
|
|
|
+ Description: "Overlapping",
|
|
|
+ Secret: "decoded-secret-value",
|
|
|
+ Match: "secret=decoded-secret-value",
|
|
|
+ File: "tmp.go",
|
|
|
+ Line: "\nsecret%3D%25%35%61%25%34%37%25%35%36jb2RlZC1zZWNyZXQtdmFsdWU%25%32%35%25%33%33%25%36%34",
|
|
|
+ RuleID: "overlapping",
|
|
|
+ Tags: []string{"overlapping", "decoded:percent", "decoded:base64", "decode-depth:4"},
|
|
|
+ StartLine: 54,
|
|
|
+ EndLine: 54,
|
|
|
+ StartColumn: 2,
|
|
|
+ EndColumn: 88,
|
|
|
+ Entropy: 3.3037016,
|
|
|
},
|
|
|
},
|
|
|
},
|
|
|
}
|
|
|
|
|
|
- for _, tt := range tests {
|
|
|
- t.Run(fmt.Sprintf("%s - %s", tt.cfgName, tt.fragment.FilePath), func(t *testing.T) {
|
|
|
+ for name, tt := range tests {
|
|
|
+ t.Run(name, func(t *testing.T) {
|
|
|
viper.Reset()
|
|
|
viper.AddConfigPath(configPath)
|
|
|
viper.SetConfigName(tt.cfgName)
|
|
|
@@ -851,7 +1075,7 @@ func TestFromFiles(t *testing.T) {
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
detector.FollowSymlinks = true
|
|
|
- paths, err := sources.DirectoryTargets(tt.source, detector.Sema, true, cfg.Allowlist.PathAllowed)
|
|
|
+ paths, err := sources.DirectoryTargets(tt.source, detector.Sema, true, cfg.Allowlists)
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
findings, err := detector.DetectFiles(paths)
|
|
|
@@ -925,7 +1149,7 @@ func TestDetectWithSymlinks(t *testing.T) {
|
|
|
cfg, _ := vc.Translate()
|
|
|
detector := NewDetector(cfg)
|
|
|
detector.FollowSymlinks = true
|
|
|
- paths, err := sources.DirectoryTargets(tt.source, detector.Sema, true, cfg.Allowlist.PathAllowed)
|
|
|
+ paths, err := sources.DirectoryTargets(tt.source, detector.Sema, true, cfg.Allowlists)
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
findings, err := detector.DetectFiles(paths)
|
|
|
@@ -1157,7 +1381,7 @@ let password = 'Summer2024!';`
|
|
|
|
|
|
f := tc.fragment
|
|
|
f.Raw = raw
|
|
|
- actual := d.detectRule(f, raw, rule, []EncodedSegment{})
|
|
|
+ actual := d.detectRule(f, raw, rule, []*codec.EncodedSegment{})
|
|
|
if diff := cmp.Diff(tc.expected, actual); diff != "" {
|
|
|
t.Errorf("diff: (-want +got)\n%s", diff)
|
|
|
}
|
|
|
@@ -1211,7 +1435,6 @@ func TestNormalizeGitleaksIgnorePaths(t *testing.T) {
|
|
|
}
|
|
|
|
|
|
func TestWindowsFileSeparator_RulePath(t *testing.T) {
|
|
|
- logging.Logger = logging.Logger.Level(zerolog.TraceLevel)
|
|
|
unixRule := config.Rule{
|
|
|
RuleID: "test-rule",
|
|
|
Path: regexp.MustCompile(`(^|/)\.m2/settings\.xml`),
|
|
|
@@ -1319,7 +1542,7 @@ func TestWindowsFileSeparator_RulePath(t *testing.T) {
|
|
|
require.NoError(t, err)
|
|
|
for name, test := range tests {
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
- actual := d.detectRule(test.fragment, test.fragment.Raw, test.rule, []EncodedSegment{})
|
|
|
+ actual := d.detectRule(test.fragment, test.fragment.Raw, test.rule, []*codec.EncodedSegment{})
|
|
|
if diff := cmp.Diff(test.expected, actual); diff != "" {
|
|
|
t.Errorf("diff: (-want +got)\n%s", diff)
|
|
|
}
|
|
|
@@ -1505,7 +1728,7 @@ func TestWindowsFileSeparator_RuleAllowlistPaths(t *testing.T) {
|
|
|
require.NoError(t, err)
|
|
|
for name, test := range tests {
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
- actual := d.detectRule(test.fragment, test.fragment.Raw, test.rule, []EncodedSegment{})
|
|
|
+ actual := d.detectRule(test.fragment, test.fragment.Raw, test.rule, []*codec.EncodedSegment{})
|
|
|
if diff := cmp.Diff(test.expected, actual); diff != "" {
|
|
|
t.Errorf("diff: (-want +got)\n%s", diff)
|
|
|
}
|