Skip to content

Commit 919e8c9

Browse files
authored
docs: add info about multi-line mode for regexp from custom secret rules (#4159)
1 parent 50fe43f commit 919e8c9

File tree

5 files changed

+68
-0
lines changed

5 files changed

+68
-0
lines changed

docs/docs/scanner/secret.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,9 @@ Trivy tries to load `trivy-secret.yaml` in the current directory by default.
9595
If the file doesn't exist, only built-in rules are used.
9696
You can customize the config file path via the `--secret-config` flag.
9797

98+
!!! warning
99+
Trivy uses [Golang regexp package](https://pkg.go.dev/regexp/syntax#hdr-Syntax). To use `^` and `$` as simbols of begin and end of line use multi-line mode -`(?m)`.
100+
98101
### Custom Rules
99102
Trivy allows defining custom rules.
100103

pkg/fanal/secret/scanner_test.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -527,6 +527,37 @@ func TestSecretScanner(t *testing.T) {
527527
},
528528
},
529529
}
530+
wantMultiLine := types.SecretFinding{
531+
RuleID: "multi-line-secret",
532+
Category: "general",
533+
Title: "Generic Rule",
534+
Severity: "HIGH",
535+
StartLine: 2,
536+
EndLine: 2,
537+
Match: "***************",
538+
Code: types.Code{
539+
Lines: []types.Line{
540+
{
541+
Number: 1,
542+
Content: "123",
543+
Highlighted: "123",
544+
},
545+
{
546+
Number: 2,
547+
Content: "***************",
548+
Highlighted: "***************",
549+
IsCause: true,
550+
FirstCause: true,
551+
LastCause: true,
552+
},
553+
{
554+
Number: 3,
555+
Content: "123",
556+
Highlighted: "123",
557+
},
558+
},
559+
},
560+
}
530561

531562
tests := []struct {
532563
name string
@@ -751,6 +782,21 @@ func TestSecretScanner(t *testing.T) {
751782
Findings: []types.SecretFinding{wantFindingAsymmSecretKey},
752783
},
753784
},
785+
{
786+
name: "begin/end line symbols without multi-line mode",
787+
configPath: filepath.Join("testdata", "multi-line-off.yaml"),
788+
inputFilePath: "testdata/multi-line.txt",
789+
want: types.Secret{},
790+
},
791+
{
792+
name: "begin/end line symbols with multi-line mode",
793+
configPath: filepath.Join("testdata", "multi-line-on.yaml"),
794+
inputFilePath: "testdata/multi-line.txt",
795+
want: types.Secret{
796+
FilePath: "testdata/multi-line.txt",
797+
Findings: []types.SecretFinding{wantMultiLine},
798+
},
799+
},
754800
}
755801

756802
for _, tt := range tests {
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
rules:
2+
- id: multi-line-sect
3+
category: general
4+
title: Generic Rule
5+
severity: HIGH
6+
regex: '^multi-line: \d+$'
7+
disable-allow-rules:
8+
- tests
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
rules:
2+
- id: multi-line-secret
3+
category: general
4+
title: Generic Rule
5+
severity: HIGH
6+
regex: '(?m)^multi-line: \d+$'
7+
disable-allow-rules:
8+
- tests
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
123
2+
multi-line: 123
3+
123

0 commit comments

Comments
 (0)