Skip to content

Commit 183c8bf

Browse files
fix: add special characters support for generic credential rule (#377)
**Proposed Changes** <!-- Add special characters support for generic credential rule --> **Checklist** - [X] I covered my changes with tests. - [ ] I Updated the documentation that is affected by my changes: - [ ] Change in the CLI arguments - [ ] Change in the configuration file I submit this contribution under the Apache-2.0 license.
1 parent 9fecada commit 183c8bf

3 files changed

Lines changed: 22 additions & 3 deletions

File tree

.2ms.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1383,3 +1383,9 @@ ignore-result:
13831383
- fcaf9a97bbcf85b33f88738b1e817e098e9c37e1 # unit test from e2e_test.go
13841384
- fe08c7c6c7f8bb715022aa4cd16ae1c69906ebba # test data from expectedReportWithValidation.json
13851385
- 5858849b35c4d0c6061a61c28c39e28b98844333 # unit test from rule_test.go
1386+
- 93a8246bc5d82a9d47d8b683b4343b51dc3bb918 # unit test from generic_credential_test.go
1387+
- ce952993fb8d6762a1feb0a1860dd806e0a36d40 # unit test from generic_credential_test.go
1388+
- 0ee50cf76ca12b4b03bfb8f233527d846965ae8a # unit test from generic_credential_test.go (remove later)
1389+
- 1ab798f14ecce9ea8a9229803c33f06e0093306a # unit test from generic_credential_test.go (remove later)
1390+
- 4154ccf54f5d43a54103495dcf0e228353dc02f4 # unit test from generic_credential_test.go (remove later)
1391+
- 783d3aa8f0e14f6d1527879bbcb3ae6195134b33 # unit test from generic_credential_test.go (remove later)

engine/rules/ruledefine/generic_credential.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ var genericCredentialRegex = generateSemiGenericRegexIncludingXml([]string{
1616
"passw(?:or)?d",
1717
"secret",
1818
"token",
19-
}, `[\w.=-]{10,150}|[a-z0-9][a-z0-9+/]{11,}={0,3}`, true).String()
19+
}, `[\w.=\-~?!:@]{10,150}|[a-z0-9][a-z0-9+/]{11,}={0,3}`, true).String()
2020

2121
func GenericCredential() *Rule {
2222
return &Rule{
@@ -42,7 +42,7 @@ func GenericCredential() *Rule {
4242
// NOTE: this is a goofy hack to get around the fact there golang's regex engine does not support positive lookaheads.
4343
// Ideally we would want to ensure the secret contains both numbers and alphabetical characters, not just alphabetical characters.
4444
Regexes: []string{
45-
regexp.MustCompile(`^[a-zA-Z_.-]+$`).String(),
45+
regexp.MustCompile(`^[a-zA-Z_.-]+:?$`).String(),
4646
},
4747
},
4848
{
@@ -60,7 +60,7 @@ func GenericCredential() *Rule {
6060
`|rapid|capital` + // common words containing "api"
6161
`|[a-z0-9-]*?api[a-z0-9-]*?:jar:` + // Maven META-INF dependencies that contain "api" in the name.
6262
// Auth
63-
`|author` +
63+
`|\bauthor\b` +
6464
`|X-MS-Exchange-Organization-Auth` + // email header
6565
`|Authentication-Results` + // email header
6666
// Credentials
@@ -94,6 +94,10 @@ func GenericCredential() *Rule {
9494
// Empty variables capturing the next line (e.g., .env files)
9595
`|(?-i:(?:[A-Z_]+=\n[A-Z_]+=|[a-z_]+=\n[a-z_]+=)(?:\n|\z))` +
9696
`|(?-i:(?:[A-Z.]+=\n[A-Z.]+=|[a-z.]+=\n[a-z.]+=)(?:\n|\z))` +
97+
// Code constant references (e.g. AnnotationWithConstants::INTEGER).
98+
`|(?-i:\w+::[A-Z][A-Z0-9_]*)` +
99+
// Any secret in valid date/datetime format (e.g. ISO 8601: 2018-04-22T10:28:49.876Z) — not a credential
100+
`|\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(?:\.\d+)?(?:Z|[+-]\d{2}:\d{2})?` +
97101
`)`).String(),
98102
},
99103
StopWords: append(DefaultStopWords,

engine/rules/ruledefine/generic_credential_test.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,8 @@ func TestGenericCredential(t *testing.T) {
8585
" utils.GetEnvOrDefault(\"api_token\", \"dafa7817-e246-48f3-91a7-e87653d587b8\")",
8686
// xml cases
8787
"<key>API_KEY</key>\n<string>AIzaSyATDL7Wz3Ze6BU31Yv3fVVth30Skyib29g</string>",
88+
"Authorization.ClientSecret: e55wsdasfsgs-sdsdas_2sdasjVM~ggadASaADASsad",
89+
"Authorization.ClientSecret: e55wsdasfsgs-sds::das_2sdasjVM~ggad?ASaAD!ASs@ad",
8890
},
8991
falsePositives: []string{
9092
"issuerKeyHash=npmXsmT2_C1iJZ-SD7RuL8exZ=6ucd",
@@ -96,6 +98,13 @@ func TestGenericCredential(t *testing.T) {
9698
"<key>GOOGLE_APP_ID</key>\n<string>1:407966239993:ios:0d7534f14f8cfe19</string>",
9799
"\"a_b_key\": \"x-someval-127.0.0.1\",",
98100
"KeyVaultSecretsUser: '62168719-64c5-453d-b4ef-b51d8b1ad44d'",
101+
"maxAPIResponseBytes: tc.maxAPIBytes , maxTotalScanBytes: tc.maxTotalBytes,",
102+
"SOME_KEY = AnnotationWithConstants::INTEGER",
103+
"SOME_KEY = AnnotationWithConstants::TANTO_FAZ",
104+
"AuthnInstant=2018-04-22T10:28:49.876Z",
105+
"AuthnInstant=2018-04-22T10:28:49Z",
106+
"AuthnInstant=2018-04-22T10:28:49+00:00",
107+
"PasswordStorage::SECTION_DELIMITER",
99108
},
100109
},
101110
}

0 commit comments

Comments
 (0)