mirror of
https://github.com/gitleaks/gitleaks.git
synced 2026-11-30 13:14:22 +01:00
feat: Add optional redaction value, default 100 (#1229)
This commit introduces an optional value for the --redact flag, allowing users to specify the extent of redaction for secrets. The valid range of values is from 0 (no redaction) to 100 (full redaction). If no value is provided, the default redaction value is set to 100, resulting in complete redaction by replacing the secret with the term 'REDACTED'. This preserves the original behavior of the --redact flag. Co-authored-by: Zachary Rice <zachary.rice@trufflesec.com>
This commit is contained in:
1 parent
e9135cf0b5
commit
30c6117aaa
7 files changed
+97
-14
No files matched your search
+66
-3
@@ -11,17 +11,80 @@ func TestRedact(t *testing.T) {
|
||||
redact: true,
|
||||
findings: []Finding{
|
||||
{
|
||||
Secret: "line containing secret",
|
||||
Match: "secret",
|
||||
Match: "line containing secret",
|
||||
Secret: "secret",
|
||||
},
|
||||
}},
|
||||
}
|
||||
for _, test := range tests {
|
||||
for _, f := range test.findings {
|
||||
f.Redact()
|
||||
f.Redact(100)
|
||||
if f.Secret != "REDACTED" {
|
||||
t.Error("redact not redacting: ", f.Secret)
|
||||
}
|
||||
if f.Match != "line containing REDACTED" {
|
||||
t.Error("redact not redacting: ", f.Secret)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestMask(t *testing.T) {
|
||||
|
||||
tests := map[string]struct {
|
||||
finding Finding
|
||||
percent uint
|
||||
expect Finding
|
||||
}{
|
||||
"normal secret": {
|
||||
finding: Finding{Match: "line containing secret", Secret: "secret"},
|
||||
expect: Finding{Match: "line containing se...", Secret: "se..."},
|
||||
percent: 75,
|
||||
},
|
||||
"empty secret": {
|
||||
finding: Finding{Match: "line containing", Secret: ""},
|
||||
expect: Finding{Match: "line containing", Secret: ""},
|
||||
percent: 75,
|
||||
},
|
||||
"short secret": {
|
||||
finding: Finding{Match: "line containing", Secret: "ss"},
|
||||
expect: Finding{Match: "line containing", Secret: "..."},
|
||||
percent: 75,
|
||||
},
|
||||
}
|
||||
for name, test := range tests {
|
||||
t.Run(name, func(t *testing.T) {
|
||||
f := test.finding
|
||||
e := test.expect
|
||||
f.Redact(test.percent)
|
||||
if f.Secret != e.Secret {
|
||||
t.Error("redact not redacting: ", f.Secret)
|
||||
}
|
||||
if f.Match != e.Match {
|
||||
t.Error("redact not redacting: ", f.Match)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestMaskSecret(t *testing.T) {
|
||||
|
||||
tests := map[string]struct {
|
||||
secret string
|
||||
percent uint
|
||||
expect string
|
||||
}{
|
||||
"normal masking": {secret: "secret", percent: 75, expect: "se..."},
|
||||
"high masking": {secret: "secret", percent: 90, expect: "s..."},
|
||||
"low masking": {secret: "secret", percent: 10, expect: "secre..."},
|
||||
"invalid masking": {secret: "secret", percent: 1000, expect: "..."},
|
||||
}
|
||||
for name, test := range tests {
|
||||
t.Run(name, func(t *testing.T) {
|
||||
got := maskSecret(test.secret, test.percent)
|
||||
if got != test.expect {
|
||||
t.Error("redact not redacting: ", got)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user