Skip to content

Commit 92b13be

Browse files
authored
fix(secret): trim excessively long lines (#7192)
1 parent 9269563 commit 92b13be

File tree

3 files changed

+51
-10
lines changed

3 files changed

+51
-10
lines changed

pkg/fanal/secret/scanner.go

+12-2
Original file line numberDiff line numberDiff line change
@@ -476,7 +476,10 @@ func toFinding(rule Rule, loc Location, content []byte) types.SecretFinding {
476476
}
477477
}
478478

479-
const secretHighlightRadius = 2 // number of lines above + below each secret to include in code output
479+
const (
480+
secretHighlightRadius = 2 // number of lines above + below each secret to include in code output
481+
maxLineLength = 100 // all lines longer will be cut off
482+
)
480483

481484
func findLocation(start, end int, content []byte) (int, int, types.Code, string) {
482485
startLineNum := bytes.Count(content[:start], lineSep)
@@ -511,9 +514,16 @@ func findLocation(start, end int, content []byte) (int, int, types.Code, string)
511514
rawLines := lines[codeStart:codeEnd]
512515
var foundFirst bool
513516
for i, rawLine := range rawLines {
514-
strRawLine := string(rawLine)
515517
realLine := codeStart + i
516518
inCause := realLine >= startLineNum && realLine <= endLineNum
519+
520+
var strRawLine string
521+
if len(rawLine) > maxLineLength {
522+
strRawLine = lo.Ternary(inCause, matchLine, string(rawLine[:maxLineLength]))
523+
} else {
524+
strRawLine = string(rawLine)
525+
}
526+
517527
code.Lines = append(code.Lines, types.Line{
518528
Number: codeStart + i + 1,
519529
Content: strRawLine,

pkg/fanal/secret/scanner_test.go

+38-8
Original file line numberDiff line numberDiff line change
@@ -353,8 +353,8 @@ func TestSecretScanner(t *testing.T) {
353353
Lines: []types.Line{
354354
{
355355
Number: 1,
356-
Content: "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa GITHUB_PAT=**************************************** bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb",
357-
Highlighted: "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa GITHUB_PAT=**************************************** bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb",
356+
Content: "aaaaaaaaaaaaaaaaaa GITHUB_PAT=**************************************** bbbbbbbbbbbbbbbbbbb",
357+
Highlighted: "aaaaaaaaaaaaaaaaaa GITHUB_PAT=**************************************** bbbbbbbbbbbbbbbbbbb",
358358
IsCause: true,
359359
FirstCause: true,
360360
LastCause: true,
@@ -462,8 +462,8 @@ func TestSecretScanner(t *testing.T) {
462462
Lines: []types.Line{
463463
{
464464
Number: 1,
465-
Content: "{\"key\": \"-----BEGIN RSA PRIVATE KEY-----**************************************************************************************************************************-----END RSA PRIVATE KEY-----\\n\"}",
466-
Highlighted: "{\"key\": \"-----BEGIN RSA PRIVATE KEY-----**************************************************************************************************************************-----END RSA PRIVATE KEY-----\\n\"}",
465+
Content: "----BEGIN RSA PRIVATE KEY-----**************************************************************************************************************************-----END RSA PRIVATE",
466+
Highlighted: "----BEGIN RSA PRIVATE KEY-----**************************************************************************************************************************-----END RSA PRIVATE",
467467
IsCause: true,
468468
FirstCause: true,
469469
LastCause: true,
@@ -483,8 +483,8 @@ func TestSecretScanner(t *testing.T) {
483483
Lines: []types.Line{
484484
{
485485
Number: 1,
486-
Content: "-----BEGIN RSA PRIVATE KEY-----****************************************************************************************************************************************************************************************-----END RSA PRIVATE KEY-----",
487-
Highlighted: "-----BEGIN RSA PRIVATE KEY-----****************************************************************************************************************************************************************************************-----END RSA PRIVATE KEY-----",
486+
Content: "----BEGIN RSA PRIVATE KEY-----****************************************************************************************************************************************************************************************-----END RSA PRIVATE",
487+
Highlighted: "----BEGIN RSA PRIVATE KEY-----****************************************************************************************************************************************************************************************-----END RSA PRIVATE",
488488
IsCause: true,
489489
FirstCause: true,
490490
LastCause: true,
@@ -504,8 +504,8 @@ func TestSecretScanner(t *testing.T) {
504504
Lines: []types.Line{
505505
{
506506
Number: 1,
507-
Content: "-----BEGIN RSA PRIVATE KEY-----**************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************-----END RSA PRIVATE KEY-----",
508-
Highlighted: "-----BEGIN RSA PRIVATE KEY-----**************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************-----END RSA PRIVATE KEY-----",
507+
Content: "----BEGIN RSA PRIVATE KEY-----**************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************-----END RSA PRIVATE",
508+
Highlighted: "----BEGIN RSA PRIVATE KEY-----**************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************-----END RSA PRIVATE",
509509
IsCause: true,
510510
FirstCause: true,
511511
LastCause: true,
@@ -667,6 +667,27 @@ func TestSecretScanner(t *testing.T) {
667667
},
668668
},
669669
}
670+
wantFindingTokenInsideJs := types.SecretFinding{
671+
RuleID: "stripe-publishable-token",
672+
Category: "Stripe",
673+
Title: "Stripe Publishable Key",
674+
Severity: "LOW",
675+
StartLine: 1,
676+
EndLine: 1,
677+
Match: "){case a.ez.PRODUCTION:return\"********************************\";case a.ez.TEST:cas",
678+
Code: types.Code{
679+
Lines: []types.Line{
680+
{
681+
Number: 1,
682+
Content: "){case a.ez.PRODUCTION:return\"********************************\";case a.ez.TEST:cas",
683+
Highlighted: "){case a.ez.PRODUCTION:return\"********************************\";case a.ez.TEST:cas",
684+
IsCause: true,
685+
FirstCause: true,
686+
LastCause: true,
687+
},
688+
},
689+
},
690+
}
670691

671692
tests := []struct {
672693
name string
@@ -982,6 +1003,15 @@ func TestSecretScanner(t *testing.T) {
9821003
Findings: []types.SecretFinding{wantMultiLine},
9831004
},
9841005
},
1006+
{
1007+
name: "long obfuscated js code with secrets",
1008+
configPath: filepath.Join("testdata", "skip-test.yaml"),
1009+
inputFilePath: filepath.Join("testdata", "obfuscated.js"),
1010+
want: types.Secret{
1011+
FilePath: filepath.Join("testdata", "obfuscated.js"),
1012+
Findings: []types.SecretFinding{wantFindingTokenInsideJs},
1013+
},
1014+
},
9851015
}
9861016

9871017
for _, tt := range tests {

pkg/fanal/secret/testdata/obfuscated.js

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)