5
5
"strings"
6
6
"time"
7
7
8
+ "github.com/samber/lo"
9
+
8
10
"github.com/aquasecurity/trivy/pkg/iac/types"
9
11
"github.com/aquasecurity/trivy/pkg/log"
10
12
)
@@ -36,23 +38,34 @@ func Parse(src, path string, parsers ...RuleSectionParser) Rules {
36
38
func parseLine (line string , rng types.Range , parsers []RuleSectionParser ) []Rule {
37
39
var rules []Rule
38
40
39
- sections := strings .Split (strings .TrimSpace (line ), " " )
40
- for _ , section := range sections {
41
- section : = strings .TrimSpace (section )
42
- section = strings .TrimLeftFunc (section , func (r rune ) bool {
41
+ parts := strings .Split (strings .TrimSpace (line ), " " )
42
+ parts = lo . FilterMap ( parts , func ( part string , _ int ) ( string , bool ) {
43
+ part = strings .TrimSpace (part )
44
+ part = strings .TrimLeftFunc (part , func (r rune ) bool {
43
45
return r == '#' || r == '/' || r == '*'
44
46
})
45
47
46
- section , exists := hasIgnoreRulePrefix (section )
48
+ return part , part != ""
49
+ })
50
+
51
+ for i , part := range parts {
52
+ part , exists := hasIgnoreRulePrefix (part )
47
53
if ! exists {
48
54
continue
49
55
}
50
56
51
- rule , err := parseComment ( section , rng , parsers )
57
+ sections , err := parseRuleSections ( part , rng , parsers )
52
58
if err != nil {
53
59
log .Debug ("Failed to parse rule" , log .String ("range" , rng .String ()), log .Err (err ))
54
60
continue
55
61
}
62
+
63
+ rule := Rule {
64
+ rng : rng ,
65
+ isStartLine : i == 0 || (len (rules ) > 0 && rules [0 ].isStartLine ),
66
+ sections : sections ,
67
+ }
68
+
56
69
rules = append (rules , rule )
57
70
}
58
71
@@ -72,11 +85,8 @@ func hasIgnoreRulePrefix(s string) (string, bool) {
72
85
return "" , false
73
86
}
74
87
75
- func parseComment (input string , rng types.Range , parsers []RuleSectionParser ) (Rule , error ) {
76
- rule := Rule {
77
- rng : rng ,
78
- sections : make (map [string ]any ),
79
- }
88
+ func parseRuleSections (input string , rng types.Range , parsers []RuleSectionParser ) (map [string ]any , error ) {
89
+ sections := make (map [string ]any )
80
90
81
91
parsers = append (parsers , & expiryDateParser {
82
92
rng : rng ,
@@ -93,7 +103,7 @@ func parseComment(input string, rng types.Range, parsers []RuleSectionParser) (R
93
103
StringMatchParser {SectionKey : "id" },
94
104
}
95
105
if idParser .Parse (val ) {
96
- rule . sections [idParser .Key ()] = idParser .Param ()
106
+ sections [idParser .Key ()] = idParser .Param ()
97
107
}
98
108
}
99
109
@@ -103,16 +113,16 @@ func parseComment(input string, rng types.Range, parsers []RuleSectionParser) (R
103
113
}
104
114
105
115
if parser .Parse (val ) {
106
- rule . sections [parser .Key ()] = parser .Param ()
116
+ sections [parser .Key ()] = parser .Param ()
107
117
}
108
118
}
109
119
}
110
120
111
- if _ , exists := rule . sections ["id" ]; ! exists {
112
- return Rule {} , errors .New ("rule section with the `ignore` key is required" )
121
+ if _ , exists := sections ["id" ]; ! exists {
122
+ return nil , errors .New ("rule section with the `ignore` key is required" )
113
123
}
114
124
115
- return rule , nil
125
+ return sections , nil
116
126
}
117
127
118
128
type StringMatchParser struct {
0 commit comments