-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
e2e.go
345 lines (300 loc) · 10.7 KB
/
e2e.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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
// Copyright 2023 Juan Pablo Tosso and the OWASP Coraza contributors
// SPDX-License-Identifier: Apache-2.0
//go:build !tinygo
// +build !tinygo
package e2e
import (
"fmt"
"io"
"net/http"
"strconv"
"strings"
"time"
)
const (
configCheckStatusCode = 424
healthCheckTimeout = 15 // Seconds
Directives = `
SecRuleEngine On
SecRequestBodyAccess On
SecResponseBodyAccess On
SecResponseBodyMimeType application/json
# Custom rule for Coraza config check (ensuring that these configs are used)
SecRule &REQUEST_HEADERS:coraza-e2e "@eq 0" "id:100,phase:1,deny,status:424,log,msg:'Coraza E2E - Missing header'"
# Custom rules for e2e testing
SecRule REQUEST_URI "@streq /admin" "id:101,phase:1,t:lowercase,log,deny"
SecRule REQUEST_BODY "@rx maliciouspayload" "id:102,phase:2,t:lowercase,log,deny"
SecRule RESPONSE_HEADERS:pass "@rx leak" "id:103,phase:3,t:lowercase,log,deny"
SecRule RESPONSE_BODY "@contains responsebodycode" "id:104,phase:4,t:lowercase,log,deny"
# Custom rules mimicking the following CRS rules: 941100, 942100, 913100
SecRule ARGS_NAMES|ARGS "@detectXSS" "id:9411,phase:2,t:none,t:utf8toUnicode,t:urlDecodeUni,t:htmlEntityDecode,t:jsDecode,t:cssDecode,t:removeNulls,log,deny"
SecRule ARGS_NAMES|ARGS "@detectSQLi" "id:9421,phase:2,t:none,t:utf8toUnicode,t:urlDecodeUni,t:removeNulls,multiMatch,log,deny"
SecRule REQUEST_HEADERS:User-Agent "@pm grabber masscan" "id:9131,phase:1,t:none,log,deny"
`
)
type Config struct {
NulledBody bool
ProxiedEntrypoint string
HttpbinEntrypoint string
}
// statusCodeExpectation is a function that checks the status code of a response
// Some connectors (such as coraza-proxy-wasm) might not be able to change anymore the status code at phase:4,
// therefore, if nulledBody parameter is true, we expect a 200, but with a nulled body
type statusCodeExpectation func(int) error
func expectStatusCode(expectedCode int) statusCodeExpectation {
return func(code int) error {
if code != expectedCode {
return fmt.Errorf("expected status code %d, got %d", expectedCode, code)
}
return nil
}
}
func expectNulledBodyStatusCode(nulledBody bool, expectedEmptyBodyCode, expectedNulledBodyCode int) statusCodeExpectation {
return func(code int) error {
if nulledBody {
if code != expectedNulledBodyCode {
return fmt.Errorf("expected status code %d, got %d", expectedNulledBodyCode, code)
}
return nil
}
if code != expectedEmptyBodyCode {
return fmt.Errorf("expected status code %d, got %d", expectedEmptyBodyCode, code)
}
return nil
}
}
// bodyExpectation sets a function to check the body expectations.
// Some connectors (such as coraza-proxy-wasm) might not be able to change anymore the status code at phase:4,
// therefore, if nulledBody parameter is true, we expect a 200, but with a nulled body
type bodyExpectation func(int, []byte) error
func expectEmptyOrNulledBody(nulledBody bool) bodyExpectation {
return func(contentLength int, body []byte) error {
if nulledBody {
if contentLength == 0 {
return fmt.Errorf("expected nulled body, got content-length 0")
}
if len(body) == 0 {
return fmt.Errorf("expected nulled body, got empty body")
}
for _, b := range body {
if b != 0 {
return fmt.Errorf("expected nulled body, got %q", string(body))
}
}
return nil
}
if contentLength != 0 {
return fmt.Errorf("expected empty body, got content-length %d", contentLength)
}
if len(body) != 0 {
return fmt.Errorf("expected empty body, got %q", string(body))
}
return nil
}
}
func expectEmptyBody() bodyExpectation {
return func(contentLength int, body []byte) error {
if contentLength != 0 {
return fmt.Errorf("expected empty body, got content-length %d", contentLength)
}
if len(body) != 0 {
return fmt.Errorf("expected empty body, got %q", string(body))
}
return nil
}
}
func Run(cfg Config) error {
healthURL := setHTTPSchemeIfMissing(cfg.HttpbinEntrypoint) + "/status/200"
baseProxyURL := setHTTPSchemeIfMissing(cfg.ProxiedEntrypoint)
echoProxiedURL := setHTTPSchemeIfMissing(baseProxyURL) + "/anything"
healthChecks := []struct {
name string
url string
expectedCode int
}{
{
name: "Health check",
url: healthURL,
expectedCode: 200,
},
{
name: "Proxy check",
url: baseProxyURL,
expectedCode: 200,
},
{
name: "Header check",
url: baseProxyURL,
expectedCode: configCheckStatusCode,
},
}
tests := []struct {
name string
requestURL string
requestHeaders map[string]string
requestBody string
requestMethod string
expectedStatusCode statusCodeExpectation
expectedBody bodyExpectation
}{
{
name: "Legit request",
requestURL: baseProxyURL + "?arg=arg_1",
requestMethod: "GET",
expectedStatusCode: expectStatusCode(200),
},
{
name: "Denied request by URL",
requestURL: baseProxyURL + "/admin",
requestMethod: "GET",
expectedStatusCode: expectStatusCode(403),
expectedBody: expectEmptyBody(),
},
{
name: "Legit request with legit body",
requestURL: echoProxiedURL,
requestMethod: "POST",
// When sending a POST request, the "application/x-www-form-urlencoded" content-type header is needed
// being the only content-type for which by default Coraza enforces the request body processing.
// See https://github.com/corazawaf/coraza/issues/438
requestHeaders: map[string]string{"Content-Type": "application/x-www-form-urlencoded"},
requestBody: "This is a legit payload",
expectedStatusCode: expectStatusCode(200),
},
{
name: "Denied request with a malicious request body",
requestURL: echoProxiedURL,
requestMethod: "POST",
requestHeaders: map[string]string{"Content-Type": "application/x-www-form-urlencoded"},
requestBody: "maliciouspayload",
expectedStatusCode: expectStatusCode(403),
},
{
name: "Denied request with a malicious response header",
requestURL: baseProxyURL + "/response-headers?pass=leak",
requestMethod: "GET",
expectedStatusCode: expectStatusCode(403),
},
{
name: "Denied request with a malicious response body",
requestURL: echoProxiedURL,
requestMethod: "POST",
requestHeaders: map[string]string{"Content-Type": "application/x-www-form-urlencoded"},
requestBody: "responsebodycode",
expectedBody: expectEmptyOrNulledBody(cfg.NulledBody),
expectedStatusCode: expectNulledBodyStatusCode(cfg.NulledBody, 403, 200),
},
{
name: "Denied request with XSS query parameters",
requestURL: echoProxiedURL + "?arg=<script>alert(0)</script>",
requestMethod: "GET",
expectedStatusCode: expectStatusCode(403),
},
{
name: "Denied request with SQLi query parameters",
requestURL: echoProxiedURL,
requestMethod: "POST",
requestHeaders: map[string]string{"Content-Type": "application/x-www-form-urlencoded"},
requestBody: "1%27%20ORDER%20BY%203--%2B",
expectedStatusCode: expectStatusCode(403),
},
{
name: "CRS malicious UA test (913100-6)",
requestURL: echoProxiedURL,
requestHeaders: map[string]string{
"User-Agent": "Grabber/0.1 (X11; U; Linux i686; en-US; rv:1.7)",
},
requestMethod: "GET",
expectedStatusCode: expectStatusCode(403),
},
}
// Check health endpoint
client := http.DefaultClient
for currentCheckIndex, healthCheck := range healthChecks {
fmt.Printf("[%d/%d] Running health check: %s\n", currentCheckIndex+1, len(healthChecks), healthCheck.name)
timeout := healthCheckTimeout
ticker := time.NewTicker(time.Second)
defer ticker.Stop()
req, _ := http.NewRequest(http.MethodGet, healthCheck.url, nil)
for range ticker.C {
if healthCheck.expectedCode != configCheckStatusCode {
// The default e2e header is not added if we are checking that the expected config is loaded
req.Header.Add("coraza-e2e", "ok")
}
resp, err := client.Do(req)
fmt.Printf("[Wait] Waiting for %s. Timeout: %ds\n", healthCheck.url, timeout)
if err == nil {
_, err = io.Copy(io.Discard, resp.Body)
if err != nil {
return err
}
resp.Body.Close()
if resp.StatusCode == healthCheck.expectedCode {
fmt.Printf("[Ok] Check successful, got status code %d\n", resp.StatusCode)
break
}
if healthCheck.expectedCode == configCheckStatusCode {
return fmt.Errorf("configs check failed, got status code %d, expected %d. Please check configs used", resp.StatusCode, healthCheck.expectedCode)
}
fmt.Printf("[Wait] Unexpected status code %d\n", resp.StatusCode)
}
timeout--
if timeout == 0 {
if err != nil {
return fmt.Errorf("timeout waiting for response from %s, make sure the server is running. Last request error: %v", healthCheck.url, err)
}
return fmt.Errorf("timeout waiting for response from %s, unexpected status code", healthCheck.url)
}
}
}
// Iterate over tests
for currentTestIndex, test := range tests {
fmt.Printf("[%d/%d] Running test: %s\n", currentTestIndex+1, len(tests), test.name)
var requestBody io.Reader
if test.requestBody != "" {
requestBody = strings.NewReader(test.requestBody)
}
req, err := http.NewRequest(test.requestMethod, test.requestURL, requestBody)
if err != nil {
return fmt.Errorf("could not make http request: %v", err)
}
for k, v := range test.requestHeaders {
req.Header.Add(k, v)
}
req.Header.Add("coraza-e2e", "ok")
resp, err := http.DefaultClient.Do(req)
if err != nil {
return fmt.Errorf("could not do http request: %v", err)
}
respBody, errReadRespBody := io.ReadAll(resp.Body)
resp.Body.Close()
if test.expectedStatusCode != nil {
if err := test.expectedStatusCode(resp.StatusCode); err != nil {
return err
}
fmt.Printf("[Ok] Got expected status code %d\n", resp.StatusCode)
}
if test.expectedBody != nil {
// Some servers might abort the request before sending the body (E.g. triggering a phase 3 rule with deny action)
// Therefore, we check if we properly read the body only if we expect a body to be received.
if errReadRespBody != nil {
return fmt.Errorf("could not read response body: %v", err)
}
code, err := strconv.Atoi(resp.Header.Get("Content-Length"))
if err != nil {
return fmt.Errorf("could not convert content-length header to int: %v", err)
}
if err := test.expectedBody(code, respBody); err != nil {
return err
}
fmt.Print("[Ok] Got expected response body\n")
}
}
return nil
}
func setHTTPSchemeIfMissing(rawURL string) string {
if rawURL == "" || strings.HasPrefix(rawURL, "http") || strings.HasPrefix(rawURL, "://") {
return rawURL
}
return "http://" + rawURL
}