Skip to content

Commit 6639911

Browse files
feat(secret): Support for detecting Hugging Face Access Tokens (aquasecurity#6236)
Co-authored-by: DmitriyLewen <[email protected]>
1 parent 337cb75 commit 6639911

File tree

3 files changed

+42
-0
lines changed

3 files changed

+42
-0
lines changed

pkg/fanal/secret/builtin-rules.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ var (
6969
CategoryTwitch = types.SecretRuleCategory("Twitch")
7070
CategoryTypeform = types.SecretRuleCategory("Typeform")
7171
CategoryDocker = types.SecretRuleCategory("Docker")
72+
CategoryHuggingFace = types.SecretRuleCategory("HuggingFace")
7273
)
7374

7475
// Reusable regex patterns
@@ -158,6 +159,15 @@ var builtinRules = []Rule{
158159
Regex: MustCompile(`glpat-[0-9a-zA-Z\-\_]{20}`),
159160
Keywords: []string{"glpat-"},
160161
},
162+
{
163+
// cf. https://huggingface.co/docs/hub/en/security-tokens
164+
ID: "hugging-face-access-token",
165+
Category: CategoryHuggingFace,
166+
Severity: "CRITICAL",
167+
Title: "Hugging Face Access Token",
168+
Regex: MustCompile(`hf_[A-Za-z0-9]{39}`),
169+
Keywords: []string{"hf_"},
170+
},
161171
{
162172
ID: "private-key",
163173
Category: CategoryAsymmetricPrivateKey,

pkg/fanal/secret/scanner_test.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -609,6 +609,28 @@ func TestSecretScanner(t *testing.T) {
609609
},
610610
},
611611
}
612+
wantFindingHuggingFace := types.SecretFinding{
613+
RuleID: "hugging-face-access-token",
614+
Category: secret.CategoryHuggingFace,
615+
Title: "Hugging Face Access Token",
616+
Severity: "CRITICAL",
617+
StartLine: 1,
618+
EndLine: 1,
619+
Match: "HF_example_token: ******************************************",
620+
Code: types.Code{
621+
Lines: []types.Line{
622+
{
623+
Number: 1,
624+
Content: "HF_example_token: ******************************************",
625+
Highlighted: "HF_example_token: ******************************************",
626+
IsCause: true,
627+
FirstCause: true,
628+
LastCause: true,
629+
},
630+
},
631+
},
632+
}
633+
612634
wantMultiLine := types.SecretFinding{
613635
RuleID: "multi-line-secret",
614636
Category: "general",
@@ -701,6 +723,15 @@ func TestSecretScanner(t *testing.T) {
701723
Findings: []types.SecretFinding{wantFindingDockerKey1, wantFindingDockerKey2},
702724
},
703725
},
726+
{
727+
name: "find Hugging face secret",
728+
configPath: filepath.Join("testdata", "config.yaml"),
729+
inputFilePath: filepath.Join("testdata", "hugging-face-secret.txt"),
730+
want: types.Secret{
731+
FilePath: filepath.Join("testdata", "hugging-face-secret.txt"),
732+
Findings: []types.SecretFinding{wantFindingHuggingFace},
733+
},
734+
},
704735
{
705736
name: "include when keyword found",
706737
configPath: filepath.Join("testdata", "config-happy-keywords.yaml"),
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
HF_example_token: hf_Testpoiqazwsxedcrfvtgbyhn12345ujmik6789

0 commit comments

Comments
 (0)