Skip to content

Commit be3993b

Browse files
authored
fix(secrets): skip aws secrets of greater length (fanal#514)
1 parent 3487acc commit be3993b

File tree

4 files changed

+62
-8
lines changed

4 files changed

+62
-8
lines changed

secret/builtin-rules.go

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package secret
22

33
import (
4+
"fmt"
5+
46
"github.com/aquasecurity/fanal/types"
57
)
68

@@ -64,21 +66,32 @@ var (
6466
CategoryTypeform = types.SecretRuleCategory("Typeform")
6567
)
6668

69+
// Reusable regex patterns
70+
const (
71+
quote = `["']?`
72+
connect = `\s*(:|=>|=)\s*`
73+
startSecret = `(^|\s+)`
74+
endSecret = `(\s+|$)`
75+
76+
aws = `(aws)?_?`
77+
)
78+
6779
var builtinRules = []Rule{
6880
{
69-
ID: "aws-access-key-id",
70-
Category: CategoryAWS,
71-
Severity: "CRITICAL",
72-
Title: "AWS Access Key ID",
73-
Regex: MustCompile(`(A3T[A-Z0-9]|AKIA|AGPA|AIDA|AROA|AIPA|ANPA|ANVA|ASIA)[A-Z0-9]{16}`),
74-
Keywords: []string{"AKIA", "AGPA", "AIDA", "AROA", "AIPA", "ANPA", "ANVA", "ASIA"},
81+
ID: "aws-access-key-id",
82+
Category: CategoryAWS,
83+
Severity: "CRITICAL",
84+
Title: "AWS Access Key ID",
85+
Regex: MustCompile(fmt.Sprintf(`(?P<secret>(A3T[A-Z0-9]|AKIA|AGPA|AIDA|AROA|AIPA|ANPA|ANVA|ASIA)[A-Z0-9]{16})%s`, endSecret)),
86+
SecretGroupName: "secret",
87+
Keywords: []string{"AKIA", "AGPA", "AIDA", "AROA", "AIPA", "ANPA", "ANVA", "ASIA"},
7588
},
7689
{
7790
ID: "aws-secret-access-key",
7891
Category: CategoryAWS,
7992
Severity: "CRITICAL",
8093
Title: "AWS Secret Access Key",
81-
Regex: MustCompile(`(?i)["']?(aws)?_?(secret)?_?(access)?_?key["']?\s*(:|=>|=)\s*(?P<secret>["']?[A-Za-z0-9\/\+=]{40})["']?`),
94+
Regex: MustCompile(fmt.Sprintf(`(?i)%s%s%s(secret)?_?(access)?_?key%s%s%s(?P<secret>[A-Za-z0-9\/\+=]{40})%s%s`, startSecret, quote, aws, quote, connect, quote, quote, endSecret)),
8295
SecretGroupName: "secret",
8396
Keywords: []string{"key"},
8497
},
@@ -87,7 +100,7 @@ var builtinRules = []Rule{
87100
Category: CategoryAWS,
88101
Severity: "HIGH",
89102
Title: "AWS Account ID",
90-
Regex: MustCompile(`(?i)["']?(aws)?_?account_?(id)?["']?\s*(:|=>|=)\s*['"]?(?P<secret>[0-9]{4}\-?[0-9]{4}\-?[0-9]{4})['"]?`),
103+
Regex: MustCompile(fmt.Sprintf(`(?i)%s%s%saccount_?(id)?%s%s%s(?P<secret>[0-9]{4}\-?[0-9]{4}\-?[0-9]{4})%s%s`, startSecret, quote, aws, quote, connect, quote, quote, endSecret)),
91104
SecretGroupName: "secret",
92105
Keywords: []string{"account"},
93106
},

secret/scanner_test.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,24 @@ func TestSecretScanner(t *testing.T) {
9494
EndLine: 2,
9595
Match: "generic secret line secret=\"*****\"",
9696
}
97+
wantFinding9 := types.SecretFinding{
98+
RuleID: "aws-secret-access-key",
99+
Category: secret.CategoryAWS,
100+
Title: "AWS Secret Access Key",
101+
Severity: "CRITICAL",
102+
StartLine: 1,
103+
EndLine: 1,
104+
Match: `'AWS_secret_KEY'="*****"`,
105+
}
106+
wantFinding10 := types.SecretFinding{
107+
RuleID: "aws-account-id",
108+
Category: secret.CategoryAWS,
109+
Title: "AWS Account ID",
110+
Severity: "HIGH",
111+
StartLine: 3,
112+
EndLine: 3,
113+
Match: `"aws_account_ID":'*****'`,
114+
}
97115

98116
tests := []struct {
99117
name string
@@ -110,6 +128,15 @@ func TestSecretScanner(t *testing.T) {
110128
Findings: []types.SecretFinding{wantFinding1, wantFinding2},
111129
},
112130
},
131+
{
132+
name: "find aws secrets",
133+
configPath: "testdata/config.yaml",
134+
inputFilePath: "testdata/aws-secrets.txt",
135+
want: types.Secret{
136+
FilePath: "testdata/aws-secrets.txt",
137+
Findings: []types.SecretFinding{wantFinding5, wantFinding9, wantFinding10},
138+
},
139+
},
113140
{
114141
name: "include when keyword found",
115142
configPath: "testdata/config-happy-keywords.yaml",
@@ -258,6 +285,11 @@ func TestSecretScanner(t *testing.T) {
258285
Findings: []types.SecretFinding{wantFinding8},
259286
},
260287
},
288+
{
289+
name: "invalid aws secrets",
290+
inputFilePath: "testdata/invalid-aws-secrets.txt",
291+
want: types.Secret{},
292+
},
261293
}
262294

263295
for _, tt := range tests {

secret/testdata/aws-secrets.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
'AWS_secret_KEY'="12ASD34qwe56CXZ78tyH10Tna543VBokN85RHCas"
2+
AWS_ACCESS_KEY_ID=AKIA0123456789ABCDEF
3+
"aws_account_ID":'1234-5678-9123'
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
base64:
2+
YXdzLWFjY2AIPA1rZXktaWQgdGVzdCBzdHJpbmc=
3+
web.config file in windows containers:
4+
publicKey="F1645C4C0C93C1AB99285D622CAA652C1DFAD63D745D6F2DE5F17E5EAF0FC4963D261C8A12436518206DC093344D5AD293"
5+
length is too long:
6+
"AWS_key":HGM724ngha9785NGKbbar6jk76mnLL80BHJnabyhdngha9785NGKbb6jk76mnLL8

0 commit comments

Comments
 (0)