-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathcache.go
More file actions
288 lines (234 loc) · 7.2 KB
/
cache.go
File metadata and controls
288 lines (234 loc) · 7.2 KB
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
package context
// #include "../../API.h"
import "C"
import (
"main/globals"
"main/helpers"
"main/log"
"main/utils"
"strconv"
"strings"
)
/*
Context caching functions are present in this package.
These cache data for each request instance.
In this way, the code can request data on demand from the request context cache,
and if the data it not yet initialized, only than it is requested from PHP (C++ extension) via callback.
This allows to copy data from PHP only once per request and only when needed.
*/
type ParseFunction func(string) map[string]interface{}
func ContextSetMap(contextId int, rawDataPtr **string, parsedPtr **map[string]interface{}, stringsPtr **map[string]string, parseFunc ParseFunction) {
if stringsPtr != nil && *stringsPtr != nil {
return
}
contextData := Context.Callback(contextId)
if rawDataPtr != nil {
*rawDataPtr = &contextData
}
if parsedPtr != nil {
parsed := parseFunc(contextData)
*parsedPtr = &parsed
if stringsPtr != nil {
strings := helpers.ExtractStringsFromUserInput(parsed, []helpers.PathPart{}, 0)
*stringsPtr = &strings
}
}
}
func ContextSetString(context_id int, m **string) {
if *m != nil {
return
}
temp := Context.Callback(context_id)
*m = &temp
}
func ContextSetBody() {
ContextSetMap(C.CONTEXT_BODY, &Context.BodyRaw, &Context.BodyParsed, &Context.BodyParsedFlattened, utils.ParseBody)
}
func ContextSetQuery() {
ContextSetMap(C.CONTEXT_QUERY, nil, &Context.QueryParsed, &Context.QueryParsedFlattened, utils.ParseQuery)
}
func ContextSetCookies() {
ContextSetMap(C.CONTEXT_COOKIES, nil, &Context.CookiesParsed, &Context.CookiesParsedFlattened, utils.ParseCookies)
}
func ContextSetHeaders() {
ContextSetMap(C.CONTEXT_HEADERS, nil, &Context.HeadersParsed, &Context.HeadersParsedFlattened, utils.ParseHeaders)
}
func ContextSetRouteParams() {
ContextSetMap(C.CONTEXT_ROUTE, &Context.RouteParamsRaw, &Context.RouteParamsParsed, &Context.RouteParamsParsedFlattened, utils.ParseRouteParams)
}
func ContextSetStatusCode() {
if Context.StatusCode != nil {
return
}
status_code_str := Context.Callback(C.CONTEXT_STATUS_CODE)
status_code, err := strconv.Atoi(status_code_str)
if err != nil {
log.Warnf("Error parsing status code %v: %v", status_code_str, err)
return
}
Context.StatusCode = &status_code
}
func ContextSetRoute() {
ContextSetString(C.CONTEXT_ROUTE, &Context.Route)
}
func ContextSetParsedRoute() {
parsedRoute := utils.BuildRouteFromURL(GetRoute())
Context.RouteParsed = &parsedRoute
}
func ContextSetMethod() {
ContextSetString(C.CONTEXT_METHOD, &Context.Method)
}
func ContextSetUrl() {
ContextSetString(C.CONTEXT_URL, &Context.URL)
}
func ContextSetUserAgent() {
ContextSetString(C.CONTEXT_HEADER_USER_AGENT, &Context.UserAgent)
}
func ContextSetIp() {
if Context.IP != nil {
return
}
remoteAddress := Context.Callback(C.CONTEXT_REMOTE_ADDRESS)
xForwardedFor := Context.Callback(C.CONTEXT_HEADER_X_FORWARDED_FOR)
ip := utils.GetIpFromRequest(globals.GetCurrentServer(), remoteAddress, xForwardedFor)
Context.IP = &ip
}
func ContextSetIsIpBypassed() {
if Context.IsIpBypassed != nil {
return
}
isIpBypassed := utils.IsIpBypassed(globals.GetCurrentServer(), GetIp())
Context.IsIpBypassed = &isIpBypassed
}
func ContextSetUserId() {
ContextSetString(C.CONTEXT_USER_ID, &Context.UserId)
}
func ContextSetUserName() {
ContextSetString(C.CONTEXT_USER_NAME, &Context.UserName)
}
func ContextSetRateLimitGroup() {
if Context.RateLimitGroup != nil {
return
}
rateLimitGroup := Context.Callback(C.CONTEXT_RATE_LIMIT_GROUP)
Context.RateLimitGroup = &rateLimitGroup
}
func ContextSetEndpointConfig() {
if Context.EndpointConfig != nil {
return
}
endpointConfig := utils.GetEndpointConfig(globals.GetCurrentServer(), GetMethod(), GetParsedRoute())
Context.EndpointConfig = &endpointConfig
}
func ContextSetWildcardEndpointsConfigs() {
if Context.WildcardEndpointsConfigs != nil {
return
}
wildcardEndpointsConfigs := utils.GetWildcardEndpointsConfigs(globals.GetCurrentServer(), GetMethod(), GetParsedRoute())
Context.WildcardEndpointsConfigs = &wildcardEndpointsConfigs
}
func ContextSetIsEndpointProtectionTurnedOff() {
if Context.IsEndpointProtectionTurnedOff != nil {
return
}
isEndpointProtectionTurnedOff := false
endpointConfig := GetEndpointConfig()
if endpointConfig != nil {
isEndpointProtectionTurnedOff = endpointConfig.ForceProtectionOff
}
if !isEndpointProtectionTurnedOff {
for _, wildcardEndpointConfig := range GetWildcardEndpointsConfig() {
if wildcardEndpointConfig.ForceProtectionOff {
isEndpointProtectionTurnedOff = true
break
}
}
}
Context.IsEndpointProtectionTurnedOff = &isEndpointProtectionTurnedOff
}
func ContextSetIsEndpointConfigured() {
if Context.IsEndpointConfigured != nil {
return
}
IsEndpointConfigured := false
endpointConfig := GetEndpointConfig()
if endpointConfig != nil {
IsEndpointConfigured = true
}
if !IsEndpointConfigured {
if len(GetWildcardEndpointsConfig()) != 0 {
IsEndpointConfigured = true
}
}
Context.IsEndpointConfigured = &IsEndpointConfigured
}
func ContextSetIsEndpointRateLimitingEnabled() {
if Context.IsEndpointRateLimitingEnabled != nil {
return
}
IsEndpointRateLimitingEnabled := false
endpointConfig := GetEndpointConfig()
if endpointConfig != nil {
IsEndpointRateLimitingEnabled = endpointConfig.RateLimiting.Enabled
}
if !IsEndpointRateLimitingEnabled {
for _, wildcardEndpointConfig := range GetWildcardEndpointsConfig() {
if wildcardEndpointConfig.RateLimiting.Enabled {
IsEndpointRateLimitingEnabled = true
break
}
}
}
Context.IsEndpointRateLimitingEnabled = &IsEndpointRateLimitingEnabled
}
func ContextSetIsEndpointIpAllowed() {
if Context.IsEndpointIpAllowed != nil {
return
}
ip := GetIp()
isEndpointIpAllowed := utils.NoConfig
endpointConfig := GetEndpointConfig()
if endpointConfig != nil {
isEndpointIpAllowed = utils.IsIpAllowedOnEndpoint(globals.GetCurrentServer(), endpointConfig.AllowedIPAddresses, ip)
}
if isEndpointIpAllowed == utils.NoConfig {
for _, wildcardEndpointConfig := range GetWildcardEndpointsConfig() {
isEndpointIpAllowed = utils.IsIpAllowedOnEndpoint(globals.GetCurrentServer(), wildcardEndpointConfig.AllowedIPAddresses, ip)
if isEndpointIpAllowed != utils.NoConfig {
break
}
}
}
isEndpointIpAllowedBool := isEndpointIpAllowed != utils.NotFound
Context.IsEndpointIpAllowed = &isEndpointIpAllowedBool
}
func ContextSetIsEndpointRateLimited() {
Context.IsEndpointRateLimited = true
}
func ContextSetGraphQL() {
if Context.GraphQLParsedFlattened != nil {
return
}
Context.GraphQLParsedFlattened = &map[string]string{}
method := GetMethod()
url := GetUrl()
// Get content-type from headers
var contentType string
headers := GetHeadersParsed()
if ct, ok := headers["content_type"].(string); ok {
contentType = ct
} else {
contentType = ""
}
contentType = strings.ToLower(strings.TrimSpace(contentType))
body := GetBodyParsed()
query := GetQueryParsed()
isGraphQL := utils.IsGraphQLOverHTTP(method, url, contentType, body, query)
if isGraphQL {
log.Debug("Detected GraphQL request")
// Extract GraphQL inputs
graphqlInputs := utils.ExtractInputsFromGraphQL(body, query, method)
Context.GraphQLParsedFlattened = &graphqlInputs
return
}
}