-
-
Notifications
You must be signed in to change notification settings - Fork 489
/
utils.go
171 lines (162 loc) · 3.43 KB
/
utils.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
package pkg
import (
"html"
"net/http"
"regexp"
"strings"
"go.keploy.io/server/pkg/service/run"
)
func IsValidPath(s string) bool {
return !strings.HasPrefix(s, "/etc/passwd") && !strings.Contains(s, "../")
}
// sanitiseInput sanitises user input strings before logging them for safety, removing newlines
// and escaping HTML tags. This is to prevent log injection, including forgery of log records.
// Reference: https://www.owasp.org/index.php/Log_Injection
func SanitiseInput(s string) string {
re := regexp.MustCompile(`(\n|\n\r|\r\n|\r)`)
return html.EscapeString(string(re.ReplaceAll([]byte(s), []byte(""))))
}
func CompareHeaders(h1 http.Header, h2 http.Header, res *[]run.HeaderResult, noise map[string]string) bool {
if res == nil {
return false
}
match := true
_, isHeaderNoisy := noise["header"]
for k, v := range h1 {
// Ignore go http router default headers
// if k == "Date" || k == "Content-Length" || k == "date" || k == "connection" {
// continue
// }
_, isNoisy := noise[k]
isNoisy = isNoisy || isHeaderNoisy
val, ok := h2[k]
if !isNoisy {
if !ok {
//fmt.Println("header not present", k)
if checkKey(res, k) {
*res = append(*res, run.HeaderResult{
Normal: false,
Expected: run.Header{
Key: k,
Value: v,
},
Actual: run.Header{
Key: k,
Value: nil,
},
})
}
match = false
continue
}
if len(v) != len(val) {
//fmt.Println("value not same", k, v, val)
if checkKey(res, k) {
*res = append(*res, run.HeaderResult{
Normal: false,
Expected: run.Header{
Key: k,
Value: v,
},
Actual: run.Header{
Key: k,
Value: val,
},
})
}
match = false
continue
}
for i, e := range v {
if val[i] != e {
//fmt.Println("value not same", k, v, val)
if checkKey(res, k) {
*res = append(*res, run.HeaderResult{
Normal: false,
Expected: run.Header{
Key: k,
Value: v,
},
Actual: run.Header{
Key: k,
Value: val,
},
})
}
match = false
continue
}
}
}
if checkKey(res, k) {
*res = append(*res, run.HeaderResult{
Normal: true,
Expected: run.Header{
Key: k,
Value: v,
},
Actual: run.Header{
Key: k,
Value: val,
},
})
}
}
for k, v := range h2 {
// Ignore go http router default headers
// if k == "Date" || k == "Content-Length" || k == "date" || k == "connection" {
// continue
// }
_, isNoisy := noise[k]
isNoisy = isNoisy || isHeaderNoisy
val, ok := h1[k]
if isNoisy && checkKey(res, k) {
*res = append(*res, run.HeaderResult{
Normal: true,
Expected: run.Header{
Key: k,
Value: val,
},
Actual: run.Header{
Key: k,
Value: v,
},
})
continue
}
if !ok {
//fmt.Println("header not present", k)
if checkKey(res, k) {
*res = append(*res, run.HeaderResult{
Normal: false,
Expected: run.Header{
Key: k,
Value: nil,
},
Actual: run.Header{
Key: k,
Value: v,
},
})
}
match = false
}
}
return match
}
func checkKey(res *[]run.HeaderResult, key string) bool {
for _, v := range *res {
if key == v.Expected.Key {
return false
}
}
return true
}
func Contains(elems []string, v string) bool {
for _, s := range elems {
if v == s {
return true
}
}
return false
}