-
Notifications
You must be signed in to change notification settings - Fork 107
/
Copy pathurl-matchers.go
324 lines (266 loc) · 7.94 KB
/
url-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
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
package jsluice
import (
"net/url"
"regexp"
"strings"
)
// A URL is any URL found in the source code with accompanying details
type URL struct {
URL string `json:"url"`
QueryParams []string `json:"queryParams"`
BodyParams []string `json:"bodyParams"`
Method string `json:"method"`
Headers map[string]string `json:"headers,omitempty"`
ContentType string `json:"contentType,omitempty"`
// some description like locationAssignment, fetch, $.post or something like that
Type string `json:"type"`
// full source/content of the node; is optional
Source string `json:"source,omitempty"`
// the filename in which the match was found
Filename string `json:"filename,omitempty"`
}
// GetURLs searches the JavaScript source code for absolute and relative URLs and returns
// a slice of results.
func (a *Analyzer) GetURLs() []*URL {
matches := make([]*URL, 0)
re := regexp.MustCompile("[^A-Z-a-z]")
// function to run on entry to each node in the tree
enter := func(n *Node) {
for _, matcher := range a.urlMatchers {
if matcher.Type != n.Type() {
continue
}
match := matcher.Fn(n)
if match == nil {
continue
}
// decode any escapes in the URL
match.URL = DecodeString(match.URL)
// an empty slice is easier to deal with than null, e.g when using jq
if match.QueryParams == nil {
match.QueryParams = []string{}
}
if match.BodyParams == nil {
match.BodyParams = []string{}
}
// Filter out data: and tel: schemes etc
lower := strings.ToLower(match.URL)
if strings.HasPrefix(lower, "data:") ||
strings.HasPrefix(lower, "tel:") ||
strings.HasPrefix(lower, "about:") ||
strings.HasPrefix(lower, "javascript:") {
continue
}
// Look for URLs that are entirely made up of EXPR replacements
// and skip them. Maybe this should be optional? Maybe it should
// remove things like EXPR#EXPR etc too
letters := re.ReplaceAllString(match.URL, "")
if strings.ReplaceAll(letters, ExpressionPlaceholder, "") == "" {
continue
}
// Parse any query params out of the URL and add them. Some, but not
// all of the matchers will add query params, so we want to do it here
// and then remove duplicates
u, err := url.Parse(match.URL)
if err == nil {
// manually disallow www.w3.org just because it shows up so damn often
if u.Hostname() == "www.w3.org" {
continue
}
for p, _ := range u.Query() {
// Ignore params that were expressions
if p == ExpressionPlaceholder {
continue
}
match.QueryParams = append(match.QueryParams, p)
}
}
match.QueryParams = unique(match.QueryParams)
matches = append(matches, match)
}
}
// find the nodes we need in the the tree and run the enter function for every node
a.Query("[(assignment_expression) (call_expression) (string)] @matches", enter)
return matches
}
func unique[T comparable](items []T) []T {
set := make(map[T]any)
for _, item := range items {
set[item] = struct{}{}
}
out := make([]T, len(set))
i := 0
for item, _ := range set {
out[i] = item
i++
}
return out
}
// A URLMatcher has a type of thing it matches against (e.g. assignment_expression),
// and a function to actually do the matching and producing of the *URL
type URLMatcher struct {
Type string
Fn func(*Node) *URL
}
// AddURLMatcher allows custom URLMatchers to be added to the Analyzer
func (a *Analyzer) AddURLMatcher(u URLMatcher) {
if a.urlMatchers == nil {
a.urlMatchers = make([]URLMatcher, 0)
}
a.urlMatchers = append(a.urlMatchers, u)
}
// DisableDefaultURLMatchers disables the default URLMatchers, so that
// only user-added URLMatchers are used.
func (a *Analyzer) DisableDefaultURLMatchers() {
a.urlMatchers = make([]URLMatcher, 0)
}
// AllURLMatchers returns the detault list of URLMatchers
func AllURLMatchers() []URLMatcher {
assignmentNames := newSet([]string{
"location",
"this.url",
"this._url",
"this.baseUrl",
})
isInterestingAssignment := func(name string) bool {
if assignmentNames.Contains(name) {
return true
}
if strings.HasSuffix(name, ".href") {
return true
}
if strings.HasSuffix(name, ".src") {
return true
}
if strings.HasSuffix(name, ".location") {
return true
}
return false
}
matchers := []URLMatcher{
// XMLHttpRequest.open(method, url)
matchXHR(),
// $.post, $.get, and $.ajax
matchJQuery(),
// location assignment
{"assignment_expression", func(n *Node) *URL {
left := n.ChildByFieldName("left")
right := n.ChildByFieldName("right")
if !isInterestingAssignment(left.Content()) {
return nil
}
// We want to find values that at least *start* with a string of some kind.
// This might be kind of useful to crawlers etc:
//
// location.href = "/somePath/" + someVar;
//
// Where as this tends to end up being kind of useless:
//
// location.href = someVar + "/somePath/";
//
// So while we might miss out on some things this way, they probably wouldn't
// have been super useful to anything automated anyway.
if !right.IsStringy() {
return nil
}
return &URL{
URL: right.CollapsedString(),
Method: "GET",
Type: "locationAssignment",
Source: n.Content(),
}
}},
// location replacement
{"call_expression", func(n *Node) *URL {
callName := n.ChildByFieldName("function").Content()
if !strings.HasSuffix(callName, "location.replace") {
return nil
}
arguments := n.ChildByFieldName("arguments")
// check the argument contains at least one string literal
if !arguments.NamedChild(0).IsStringy() {
return nil
}
return &URL{
URL: arguments.NamedChild(0).CollapsedString(),
Method: "GET",
Type: "locationReplacement",
Source: n.Content(),
}
}},
// window.open(url)
{"call_expression", func(n *Node) *URL {
callName := n.ChildByFieldName("function").Content()
if callName != "window.open" && callName != "open" {
return nil
}
arguments := n.ChildByFieldName("arguments")
// check the argument contains at least one string literal
if !arguments.NamedChild(0).IsStringy() {
return nil
}
return &URL{
URL: arguments.NamedChild(0).CollapsedString(),
Method: "GET",
Type: "window.open",
Source: n.Content(),
}
return nil
}},
// fetch(url, [init])
{"call_expression", func(n *Node) *URL {
callName := n.ChildByFieldName("function").Content()
if callName != "fetch" {
return nil
}
arguments := n.ChildByFieldName("arguments")
// check the argument contains at least one string literal
if !arguments.NamedChild(0).IsStringy() {
return nil
}
init := arguments.NamedChild(1).AsObject()
return &URL{
URL: arguments.NamedChild(0).CollapsedString(),
Method: init.GetString("method", "GET"),
Headers: init.GetObject("headers").AsMap(),
ContentType: init.GetObject("headers").GetStringI("content-type", ""),
Type: "fetch",
Source: n.Content(),
}
return nil
}},
// other function calls with a URL-like argument
{"call_expression", func(n *Node) *URL {
callName := n.ChildByFieldName("function").Content()
arguments := n.ChildByFieldName("arguments")
if !arguments.NamedChild(0).IsStringy() {
return nil
}
if !MaybeURL(arguments.NamedChild(0).CollapsedString()) {
return nil
}
return &URL{
URL: arguments.NamedChild(0).CollapsedString(),
Type: callName,
Source: n.Content(),
}
}},
// string literals
// This should always go last because it's the matcher
// that provides the least amount of context. When doing
// de-duplication based on the path that means that a
// duplicate with more context would "win" if one exists
{"string", func(n *Node) *URL {
trimmed := n.RawString()
if !MaybeURL(trimmed) {
return nil
}
return &URL{
URL: trimmed,
Type: "stringLiteral",
Source: n.Content(),
}
}},
}
return matchers
}