Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(secret): use only line with secret for long secret lines #7412

Merged
merged 2 commits into from
Aug 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions pkg/fanal/secret/scanner.go
Original file line number Diff line number Diff line change
Expand Up @@ -504,8 +504,8 @@ func findLocation(start, end int, content []byte) (int, int, types.Code, string)
}

if lineEnd-lineStart > 100 {
lineStart = lo.Ternary(start-30 < 0, 0, start-30)
lineEnd = lo.Ternary(end+20 > len(content), len(content), end+20)
lineStart = lo.Ternary(start-lineStart-30 < 0, lineStart, start-30)
lineEnd = lo.Ternary(end+20 > lineEnd, lineEnd, end+20)
}
matchLine := string(content[lineStart:lineEnd])
endLineNum := startLineNum + bytes.Count(content[start:end], lineSep)
Expand Down
45 changes: 45 additions & 0 deletions pkg/fanal/secret/scanner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -742,6 +742,42 @@ func TestSecretScanner(t *testing.T) {
},
},
}
wantFindingJWT := types.SecretFinding{
RuleID: "jwt-token",
Category: "JWT",
Title: "JWT token",
Severity: "MEDIUM",
StartLine: 3,
EndLine: 3,
Match: "jwt: ***********************************************************************************************************************************************************",
Code: types.Code{
Lines: []types.Line{
{
Number: 1,
Content: "asd",
Highlighted: "asd",
},
{
Number: 2,
Content: "aaaa",
Highlighted: "aaaa",
},
{
Number: 3,
Content: "jwt: ***********************************************************************************************************************************************************",
Highlighted: "jwt: ***********************************************************************************************************************************************************",
IsCause: true,
FirstCause: true,
LastCause: true,
},
{
Number: 4,
Content: "asda",
Highlighted: "asda",
},
},
},
}

tests := []struct {
name string
Expand Down Expand Up @@ -822,6 +858,15 @@ func TestSecretScanner(t *testing.T) {
Findings: []types.SecretFinding{wantFindingHuggingFace},
},
},
{
name: "find JWT token",
configPath: filepath.Join("testdata", "config.yaml"),
inputFilePath: filepath.Join("testdata", "jwt-secret.txt"),
want: types.Secret{
FilePath: filepath.Join("testdata", "jwt-secret.txt"),
Findings: []types.SecretFinding{wantFindingJWT},
},
},
{
name: "include when keyword found",
configPath: filepath.Join("testdata", "config-happy-keywords.yaml"),
Expand Down
4 changes: 4 additions & 0 deletions pkg/fanal/secret/testdata/jwt-secret.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
asd
aaaa
jwt: eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c
asda