Skip to content

Commit 3e1fa21

Browse files
authored
fix(secret): change grafana token regex to find them without unquoted (#7627)
1 parent 8735242 commit 3e1fa21

File tree

3 files changed

+77
-1
lines changed

3 files changed

+77
-1
lines changed

pkg/fanal/secret/builtin-rules.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -553,7 +553,7 @@ var builtinRules = []Rule{
553553
Category: CategoryGrafana,
554554
Title: "Grafana API token",
555555
Severity: "MEDIUM",
556-
Regex: MustCompile(`['\"]eyJrIjoi(?i)[a-z0-9\-_=]{72,92}['\"]`),
556+
Regex: MustCompile(`['\"]?eyJrIjoi(?i)[a-z0-9\-_=]{72,92}['\"]?`),
557557
Keywords: []string{"eyJrIjoi"},
558558
},
559559
{

pkg/fanal/secret/scanner_test.go

+74
Original file line numberDiff line numberDiff line change
@@ -690,6 +690,71 @@ func TestSecretScanner(t *testing.T) {
690690
},
691691
}
692692

693+
wantFindingGrafanaQuoted := types.SecretFinding{
694+
RuleID: "grafana-api-token",
695+
Category: secret.CategoryGrafana,
696+
Title: "Grafana API token",
697+
Severity: "MEDIUM",
698+
StartLine: 1,
699+
EndLine: 1,
700+
Match: "GRAFANA_TOKEN=**********************************************************************************************",
701+
Code: types.Code{
702+
Lines: []types.Line{
703+
{
704+
Number: 1,
705+
Content: "GRAFANA_TOKEN=**********************************************************************************************",
706+
Highlighted: "GRAFANA_TOKEN=**********************************************************************************************",
707+
IsCause: true,
708+
FirstCause: true,
709+
LastCause: true,
710+
},
711+
{
712+
Number: 2,
713+
Content: "GRAFANA_TOKEN=**************************************************************************************",
714+
Highlighted: "GRAFANA_TOKEN=**************************************************************************************",
715+
IsCause: false,
716+
FirstCause: false,
717+
LastCause: false,
718+
},
719+
},
720+
},
721+
}
722+
723+
wantFindingGrafanaUnquoted := types.SecretFinding{
724+
RuleID: "grafana-api-token",
725+
Category: secret.CategoryGrafana,
726+
Title: "Grafana API token",
727+
Severity: "MEDIUM",
728+
StartLine: 2,
729+
EndLine: 2,
730+
Match: "GRAFANA_TOKEN=********************************************************************************************",
731+
Code: types.Code{
732+
Lines: []types.Line{
733+
{
734+
Number: 1,
735+
Content: "GRAFANA_TOKEN=**************************************************************************************",
736+
Highlighted: "GRAFANA_TOKEN=**************************************************************************************",
737+
IsCause: false,
738+
FirstCause: false,
739+
LastCause: false,
740+
},
741+
{
742+
Number: 2,
743+
Content: "GRAFANA_TOKEN=********************************************************************************************",
744+
Highlighted: "GRAFANA_TOKEN=********************************************************************************************",
745+
IsCause: true,
746+
FirstCause: true,
747+
LastCause: true,
748+
},
749+
{
750+
Number: 3,
751+
Content: "",
752+
Highlighted: "",
753+
},
754+
},
755+
},
756+
}
757+
693758
wantMultiLine := types.SecretFinding{
694759
RuleID: "multi-line-secret",
695760
Category: "general",
@@ -858,6 +923,15 @@ func TestSecretScanner(t *testing.T) {
858923
Findings: []types.SecretFinding{wantFindingHuggingFace},
859924
},
860925
},
926+
{
927+
name: "find grafana secret",
928+
configPath: filepath.Join("testdata", "config.yaml"),
929+
inputFilePath: filepath.Join("testdata", "grafana-env.txt"),
930+
want: types.Secret{
931+
FilePath: filepath.Join("testdata", "grafana-env.txt"),
932+
Findings: []types.SecretFinding{wantFindingGrafanaUnquoted, wantFindingGrafanaQuoted},
933+
},
934+
},
861935
{
862936
name: "find JWT token",
863937
configPath: filepath.Join("testdata", "config.yaml"),
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
GRAFANA_TOKEN="eyJrIjoiT0tTcG1pUlY2RnVKZTFVaDFsNFZXdE9ZWmNrMkZYbkT0tTcG1pUlY2RnVKZTFVaDFsNFZXdE9ZWmNrMkZYbk"
2+
GRAFANA_TOKEN=eyJrIjoiT0tTcG1pUlY2RnVKZTFVaDFsNFZXdE9ZWmNrMkZYbkT0tTcG1pUlY2RnVKZTFVaDFsNFZXdE9ZWmNrMkZYbk

0 commit comments

Comments
 (0)