-
Notifications
You must be signed in to change notification settings - Fork 92
/
user-patterns.go
229 lines (185 loc) · 4.76 KB
/
user-patterns.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
package jsluice
import (
"encoding/json"
"errors"
"io"
"regexp"
)
// A UserPattern represents a pattern that was provided by a
// when using the command-line tool. When using the package
// directly, a SecretMatcher can be created directly instead
// of creating a UserPattern
type UserPattern struct {
Name string `json:"name"`
Key string `json:"key"`
Value string `json:"value"`
Severity Severity `json:"severity"`
Object []*UserPattern `json:"object"`
reKey *regexp.Regexp
reValue *regexp.Regexp
}
// ParseRegex parses all of the user-provided regular expressions
// for a pattern into Go *regexp.Regexp types
func (u *UserPattern) ParseRegex() error {
if u.Value != "" {
re, err := regexp.Compile(u.Value)
if err != nil {
return err
}
u.reValue = re
}
if u.Key != "" {
re, err := regexp.Compile(u.Key)
if err != nil {
return err
}
u.reKey = re
}
if len(u.Object) > 0 {
for _, m := range u.Object {
m.ParseRegex()
}
}
if u.Severity == "" {
u.Severity = SeverityInfo
}
if u.reValue == nil && u.reKey == nil && len(u.Object) == 0 {
return errors.New("'key', 'value', both, or 'object' must be supplied in user-defined matcher")
}
return nil
}
// MatchValue returns true if a pattern's value regex matches
// the supplied value, or if there is no value regex.
func (u *UserPattern) MatchValue(in string) bool {
if u.reValue == nil {
return true
}
return u.reValue.MatchString(in)
}
// MatchKey returns true if a pattern's key regex matches
// the supplied value, or if there is no key regex
func (u *UserPattern) MatchKey(in string) bool {
if u.reKey == nil {
return true
}
return u.reKey.MatchString(in)
}
// SecretMatcher returns a SecretMatcher based on the UserPattern,
// for use with (*Analyzer).AddSecretMatcher()
func (u *UserPattern) SecretMatcher() SecretMatcher {
if len(u.Object) > 0 {
return u.objectMatcher()
}
if u.reKey != nil {
return u.pairMatcher()
}
return u.stringMatcher()
}
// objectMatcher returns a SecretMatcher for matching against objects
func (u *UserPattern) objectMatcher() SecretMatcher {
return SecretMatcher{"(object) @matches", func(n *Node) *Secret {
pairs := n.NamedChildren()
matched := 0
for _, pat := range u.Object {
matcher := pat.pairMatcher()
for _, pair := range pairs {
if matcher.Fn(pair) != nil {
matched++
break
}
}
}
if matched != len(u.Object) {
return nil
}
secret := &Secret{
Kind: u.Name,
Data: n.AsObject().AsMap(),
Severity: u.Severity,
}
return secret
}}
}
// pairMatcher returns a SecretMatcher for matching against key/value pairs
func (u *UserPattern) pairMatcher() SecretMatcher {
return SecretMatcher{"(pair) @matches", func(n *Node) *Secret {
key := n.ChildByFieldName("key")
if key == nil || !u.MatchKey(key.RawString()) {
return nil
}
value := n.ChildByFieldName("value")
if value == nil || value.Type() != "string" {
return nil
}
if !u.MatchValue(value.RawString()) {
return nil
}
secret := &Secret{
Kind: u.Name,
Data: map[string]string{
"key": key.RawString(),
"value": value.RawString(),
},
Severity: u.Severity,
}
parent := n.Parent()
if parent == nil || parent.Type() != "object" {
return secret
}
secret.Context = parent.AsObject().AsMap()
return secret
}}
}
// stringMatcher returns a SecretMatcher for matching against string literals
func (u *UserPattern) stringMatcher() SecretMatcher {
return SecretMatcher{"(string) @matches", func(n *Node) *Secret {
in := n.RawString()
if !u.MatchValue(in) {
return nil
}
secret := &Secret{
Kind: u.Name,
Data: map[string]string{"match": in},
Severity: u.Severity,
}
parent := n.Parent()
if parent == nil || parent.Type() != "pair" {
return secret
}
grandParent := parent.Parent()
if grandParent == nil || grandParent.Type() != "object" {
return secret
}
secret.Context = grandParent.AsObject().AsMap()
return secret
}}
}
// UserPatterns is an alias for a slice of *UserPattern
type UserPatterns []*UserPattern
// SecretMatchers returns a slice of SecretMatcher for use with
// (*Analyzer).AddSecretMatchers()
func (u UserPatterns) SecretMatchers() []SecretMatcher {
out := make([]SecretMatcher, 0)
for _, p := range u {
out = append(out, p.SecretMatcher())
}
return out
}
// ParseUserPatterns accepts an io.Reader pointing to a JSON user-pattern
// definition file, and returns a list of UserPatterns, and any error that
// occurred.
func ParseUserPatterns(r io.Reader) (UserPatterns, error) {
out := make(UserPatterns, 0)
dec := json.NewDecoder(r)
err := dec.Decode(&out)
if err != nil {
return out, err
}
for _, p := range out {
err = p.ParseRegex()
if err != nil {
return out, err
}
}
return out, nil
}