Skip to content

Commit 7ca23b6

Browse files
committed
feat: add new function key value masking
1 parent f2bb66c commit 7ca23b6

2 files changed

Lines changed: 10 additions & 8 deletions

File tree

common-lib/utils/secretScanner/rules.go

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ const (
1515
aws = `aws_?`
1616
)
1717

18+
// credentialAssignmentRegex matches a value assigned to a credential-named key, capturing the
19+
// key+separator+quote as `pre` so it can be kept while only the value is masked.
20+
var credentialAssignmentRegex = regexp.MustCompile(`(?i)(?P<pre>(password|passwd|pwd|secret|credential|token|api[_-]?key)["']?\s*(:|=>|=)\s*["']?)[^"',}\s]{3,}`)
21+
1822
// create rule struct
1923
type Rule struct {
2024
ID string
@@ -667,12 +671,4 @@ var BuiltinRules = []Rule{
667671
SecretGroupName: "secret",
668672
Keywords: []string{"dockerc"},
669673
},
670-
{
671-
ID: "generic-credential-assignment",
672-
Title: "Generic credential assignment",
673-
Severity: "HIGH",
674-
Regex: regexp.MustCompile(`(?i)(password|passwd|pwd|secret|credential|token|api[_-]?key)["']?\s*(:|=>|=)\s*["']?(?P<secret>[^"',}\s]{3,})`),
675-
SecretGroupName: "secret",
676-
Keywords: []string{"password", "passwd", "pwd", "secret", "credential", "token", "key"},
677-
},
678674
}

common-lib/utils/secretScanner/scanner.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,12 @@ func MaskSecretsOnString(input string) string {
2222
return maskedInput
2323
}
2424

25+
// MaskCredentialKeyValues masks values assigned to credential-named keys, keeping the surrounding
26+
// structure intact so the result stays parseable (e.g. valid JSON).
27+
func MaskCredentialKeyValues(input string) string {
28+
return credentialAssignmentRegex.ReplaceAllString(input, "${pre}******")
29+
}
30+
2531
// MaskSecretsOnStream processes an input stream, masking secrets according to built-in rules.
2632
func MaskSecretsOnStream(input io.Reader) (io.Reader, error) {
2733
pr, pw := io.Pipe()

0 commit comments

Comments
 (0)