-
Notifications
You must be signed in to change notification settings - Fork 106
/
Copy pathsecret-matchers.go
166 lines (131 loc) · 3.59 KB
/
secret-matchers.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
package jsluice
import (
"strings"
)
// A Secret represents any secret or otherwise interesting data
// found within a JavaScript file. E.g. an AWS access key.
type Secret struct {
Kind string `json:"kind"`
Data any `json:"data"`
Filename string `json:"filename,omitempty"`
Severity Severity `json:"severity"`
Context any `json:"context"`
}
// Severity indicates how serious a finding is
type Severity string
const (
SeverityInfo Severity = "info"
SeverityLow Severity = "low"
SeverityMedium Severity = "medium"
SeverityHigh Severity = "high"
)
// AddSecretMatcher allows custom SecretMatchers to be added to the Analyzer
func (a *Analyzer) AddSecretMatcher(s SecretMatcher) {
if a.userSecretMatchers == nil {
a.userSecretMatchers = make([]SecretMatcher, 0)
}
a.userSecretMatchers = append(a.userSecretMatchers, s)
}
// AddSecretMatchers allows multiple custom SecretMatchers to be added to the Analyzer
func (a *Analyzer) AddSecretMatchers(ss []SecretMatcher) {
if a.userSecretMatchers == nil {
a.userSecretMatchers = make([]SecretMatcher, 0)
}
a.userSecretMatchers = append(a.userSecretMatchers, ss...)
}
// GetSecrets uses the parse tree and a set of Matchers (those provided
// by AllSecretMatchers()) to find secrets in JavaScript source code.
func (a *Analyzer) GetSecrets() []*Secret {
out := make([]*Secret, 0)
// we only want to run each query once so let's cache them
nodeCache := make(map[string][]*Node)
matchers := AllSecretMatchers()
if a.userSecretMatchers != nil {
matchers = append(matchers, a.userSecretMatchers...)
}
for _, m := range matchers {
if _, exists := nodeCache[m.Query]; !exists {
nodes := make([]*Node, 0)
a.Query(m.Query, func(n *Node) {
nodes = append(nodes, n)
})
nodeCache[m.Query] = nodes
}
nodes := nodeCache[m.Query]
for _, n := range nodes {
match := m.Fn(n)
if match == nil {
continue
}
out = append(out, match)
}
}
return out
}
// A SecretMatcher is a tree-sitter query to find relevant nodes
// in the parse tree, and a function to inspect those nodes,
// returning any Secret that is found.
type SecretMatcher struct {
Query string
Fn func(*Node) *Secret
}
// AllSecretMatchers returns the default list of SecretMatchers
func AllSecretMatchers() []SecretMatcher {
return []SecretMatcher{
awsMatcher(),
gcpKeyMatcher(),
firebaseMatcher(),
githubKeyMatcher(),
// REACT_APP_... containing objects
{"(object) @matches", func(n *Node) *Secret {
// disabled due to high false positive rate
return nil
o := n.AsObject()
hasReactAppKeys := false
for _, k := range o.GetKeys() {
if strings.HasPrefix(k, "REACT_APP_") {
hasReactAppKeys = true
break
}
}
if !hasReactAppKeys {
return nil
}
return &Secret{
Kind: "reactApp",
Data: o.AsMap(),
}
}},
// generic secrets
{"(pair) @matches", func(n *Node) *Secret {
// disabled due to very high false positive rate
// but left easy to enable for research purposes
return nil
key := n.ChildByFieldName("key")
if key == nil {
return nil
}
keyStr := strings.ToLower(key.RawString())
if !strings.Contains(keyStr, "secret") {
return nil
}
value := n.ChildByFieldName("value")
if value == nil || value.Type() != "string" {
return nil
}
data := map[string]string{
"key": value.RawString(),
}
match := &Secret{
Kind: "genericSecret",
Data: data,
}
parent := n.Parent()
if parent == nil || parent.Type() != "object" {
return match
}
match.Context = parent.AsObject().AsMap()
return match
}},
}
}