-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathutils.go
More file actions
279 lines (237 loc) · 5.78 KB
/
Copy pathutils.go
File metadata and controls
279 lines (237 loc) · 5.78 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
package utils
import (
"encoding/json"
"fmt"
"main/globals"
"main/log"
"net"
"net/url"
"runtime"
"strings"
"github.com/seancfoley/ipaddress-go/ipaddr"
)
func KeyExists[K comparable, V any](m map[K]V, key K) bool {
_, exists := m[key]
return exists
}
func KeyMustExist[K comparable, V any](m map[K]V, key K) {
if _, exists := m[key]; !exists {
panic(fmt.Sprintf("Key %v does not exist in map!", key))
}
}
func GetFromMap[T any](m map[string]interface{}, key string) *T {
value, ok := m[key]
if !ok {
return nil
}
result, ok := value.(T)
if !ok {
return nil
}
return &result
}
func MustGetFromMap[T any](m map[string]interface{}, key string) T {
value := GetFromMap[T](m, key)
if value == nil {
panic(fmt.Sprintf("Error parsing JSON: key %s does not exist or it has an incorrect type", key))
}
return *value
}
func ParseFormData(data string, separator string) map[string]interface{} {
result := map[string]interface{}{}
parts := strings.Split(data, separator)
for _, part := range parts {
keyValue := strings.Split(part, "=")
if len(keyValue) != 2 {
continue
}
result[keyValue[0]] = keyValue[1]
decodedValue, err := url.QueryUnescape(keyValue[1])
if err == nil && decodedValue != keyValue[1] {
result[keyValue[0]] = decodedValue
}
}
return result
}
func ParseBody(body string) map[string]interface{} {
// first we check if the body is a string, and if it is, we try to parse it as JSON
// if it fails, we parse it as form data
if strings.HasPrefix(body, "{") && strings.HasSuffix(body, "}") {
// if the body is a JSON object, we parse it as JSON
jsonBody := map[string]interface{}{}
err := json.Unmarshal([]byte(body), &jsonBody)
if err == nil {
return jsonBody
}
}
return ParseFormData(body, "&")
}
func ParseQuery(query string) map[string]interface{} {
jsonQuery := map[string]interface{}{}
err := json.Unmarshal([]byte(query), &jsonQuery)
if err == nil {
return jsonQuery
}
return ParseFormData(query, "&")
}
func ParseCookies(cookies string) map[string]interface{} {
return ParseFormData(cookies, ";")
}
func ParseHeaders(headers string) map[string]interface{} {
j := map[string]interface{}{}
err := json.Unmarshal([]byte(headers), &j)
if err != nil {
return map[string]interface{}{}
}
return j
}
func isIP(ip string) bool {
return net.ParseIP(ip) != nil
}
func isLocalhost(ip string) bool {
parsedIP := net.ParseIP(ip)
if parsedIP == nil {
return false
}
return parsedIP.IsLoopback()
}
func IsIpAllowed(allowedIps map[string]bool, ip string) bool {
if globals.EnvironmentConfig.LocalhostAllowedByDefault && isLocalhost(ip) {
return true
}
if len(allowedIps) == 0 {
// No IPs configured in the allow list -> no restrictions
return true
}
if KeyExists(allowedIps, ip) {
return true
}
return false
}
func IsIpBypassed(ip string) bool {
globals.CloudConfigMutex.Lock()
defer globals.CloudConfigMutex.Unlock()
if globals.EnvironmentConfig.LocalhostAllowedByDefault && isLocalhost(ip) {
return true
}
if KeyExists(globals.CloudConfig.BypassedIps, ip) {
return true
}
return false
}
func getIpFromXForwardedFor(value string) string {
forwardedIps := strings.Split(value, ",")
for _, ip := range forwardedIps {
ip = strings.TrimSpace(ip)
if strings.Contains(ip, ":") {
parts := strings.Split(ip, ":")
if len(parts) == 2 {
ip = parts[0]
}
}
if isIP(ip) {
return ip
}
}
return ""
}
func GetIpFromRequest(remoteAddress string, xForwardedFor string) string {
if xForwardedFor != "" && globals.EnvironmentConfig.TrustProxy {
ip := getIpFromXForwardedFor(xForwardedFor)
if isIP(ip) {
return ip
}
}
if remoteAddress != "" && isIP(remoteAddress) {
return remoteAddress
}
return ""
}
func GetBlockingMode() int {
globals.CloudConfigMutex.Lock()
defer globals.CloudConfigMutex.Unlock()
return globals.CloudConfig.Block
}
func IsBlockingEnabled() bool {
return GetBlockingMode() == 1
}
func IsUserBlocked(userID string) bool {
globals.CloudConfigMutex.Lock()
defer globals.CloudConfigMutex.Unlock()
return KeyExists(globals.CloudConfig.BlockedUserIds, userID)
}
func IsIpBlocked(ip string) (bool, string) {
globals.CloudConfigMutex.Lock()
defer globals.CloudConfigMutex.Unlock()
ipAddress, err := ipaddr.NewIPAddressString(ip).ToAddress()
if err != nil {
log.Infof("Invalid ip address: %s\n", ip)
return false, ""
}
for _, ipBlocklist := range globals.CloudConfig.BlockedIps {
if (ipAddress.IsIPv4() && ipBlocklist.TrieV4.ElementContains(ipAddress.ToIPv4())) ||
(ipAddress.IsIPv6() && ipBlocklist.TrieV6.ElementContains(ipAddress.ToIPv6())) {
return true, ipBlocklist.Description
}
}
return false, ""
}
type DatabaseType int
const (
Generic DatabaseType = iota
Ansi
BigQuery
Clickhouse
Databricks
DuckDB
Hive
MSSQL
MySQL
PostgreSQL
Redshift
Snowflake
SQLite
)
func GetSqlDialectFromString(dialect string) int {
dialect = strings.ToLower(dialect)
switch dialect {
case "mysql":
return int(MySQL)
case "sqlite":
return int(SQLite)
case "postgres":
return int(PostgreSQL)
default:
return int(Generic)
}
}
// StringPointer is a helper function to return a pointer to a string value.
func StringPointer(s string) *string {
return &s
}
func BoolPointer(b bool) *bool {
return &b
}
func ArrayContains(array []string, search string) bool {
for _, member := range array {
if member == search {
return true
}
}
return false
}
func GetArch() string {
switch runtime.GOARCH {
case "amd64":
return "x86_64"
case "arm64":
return "aarch64"
}
panic(fmt.Sprintf("Running on unsupported architecture \"%s\"!", runtime.GOARCH))
}
func GetAikidoInstallDir() string {
if runtime.GOOS == "darwin" {
return fmt.Sprintf("/opt/homebrew/Cellar/aikido-php-firewall/%s/aikido-%s-", globals.Version, globals.Version)
}
return fmt.Sprintf("/opt/aikido-" + globals.Version)
}