forked from acouvreur/traefik-modsecurity-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodsecurity_test.go
More file actions
250 lines (227 loc) · 6.49 KB
/
modsecurity_test.go
File metadata and controls
250 lines (227 loc) · 6.49 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
// Package traefik_modsecurity_plugin a modsecurity plugin.
package traefik_modsecurity_plugin
import (
"bytes"
"io"
"log"
"net/http"
"net/http/httptest"
"regexp"
"testing"
"github.com/stretchr/testify/assert"
)
func TestModsecurity_ServeHTTP(t *testing.T) {
req, err := http.NewRequest(http.MethodGet, "http://proxy.com/test", bytes.NewBuffer([]byte("Request")))
if err != nil {
log.Fatal(err)
}
type response struct {
Body string
StatusCode int
}
serviceResponse := response{
StatusCode: 200,
Body: "Response from service",
}
tests := []struct {
name string
request http.Request
wafResponse response
serviceResponse response
expectBody string
expectStatus int
}{
{
name: "Forward request when WAF found no threats",
request: *req,
wafResponse: response{
StatusCode: 200,
Body: "Response from waf",
},
serviceResponse: serviceResponse,
expectBody: "Response from service",
expectStatus: 200,
},
{
name: "Intercepts request when WAF found threats",
request: *req,
wafResponse: response{
StatusCode: 403,
Body: "Response from waf",
},
serviceResponse: serviceResponse,
expectBody: "Response from waf",
expectStatus: 403,
},
{
name: "Does not forward Websockets",
request: http.Request{
Body: http.NoBody,
Header: http.Header{
"Upgrade": []string{"Websocket"},
},
},
wafResponse: response{
StatusCode: 200,
Body: "Response from waf",
},
serviceResponse: serviceResponse,
expectBody: "Response from service",
expectStatus: 200,
},
{
name: "Accept payloads smaller than limits",
request: http.Request{
Body: generateLargeBody(1024),
},
wafResponse: response{
StatusCode: 200,
Body: "Response from waf",
},
serviceResponse: serviceResponse,
expectBody: "Response from service",
expectStatus: http.StatusOK,
},
{
name: "Reject too big payloads",
request: http.Request{
Body: generateLargeBody(1025),
},
wafResponse: response{
StatusCode: 200,
Body: "Response from waf",
},
serviceResponse: serviceResponse,
expectBody: "\n",
expectStatus: http.StatusRequestEntityTooLarge,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
modsecurityMockServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
resp := http.Response{
Body: io.NopCloser(bytes.NewReader([]byte(tt.wafResponse.Body))),
StatusCode: tt.wafResponse.StatusCode,
Header: http.Header{},
}
forwardResponse(&resp, w)
}))
httpServiceHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
resp := http.Response{
Body: io.NopCloser(bytes.NewReader([]byte(tt.serviceResponse.Body))),
StatusCode: tt.serviceResponse.StatusCode,
Header: http.Header{},
}
forwardResponse(&resp, w)
})
middleware := &Modsecurity{
next: httpServiceHandler,
modSecurityUrl: modsecurityMockServer.URL,
maxBodySize: 1024,
name: "modsecurity-middleware",
httpClient: http.DefaultClient,
logger: log.New(io.Discard, "", log.LstdFlags),
}
rw := httptest.NewRecorder()
middleware.ServeHTTP(rw, &tt.request)
resp := rw.Result()
body, _ := io.ReadAll(resp.Body)
assert.Equal(t, tt.expectBody, string(body))
assert.Equal(t, tt.expectStatus, resp.StatusCode)
})
}
}
func generateLargeBody(size int) io.ReadCloser {
var str = make([]byte, size)
return io.NopCloser(bytes.NewReader(str))
}
func TestModsecurity_BypassRules(t *testing.T) {
serviceResponse := "Response from service"
tests := []struct {
name string
bypassRules []compiledBypassRule
method string
path string
expectWAF bool // whether the WAF mock server should be called
}{
{
name: "Bypasses WAF when method and path match",
bypassRules: []compiledBypassRule{
{method: http.MethodGet, pathRegexp: regexp.MustCompile(`/search/v1/statement/executing/`)},
},
method: http.MethodGet,
path: "/search/v1/statement/executing/abc123",
expectWAF: false,
},
{
name: "Does not bypass WAF when method does not match",
bypassRules: []compiledBypassRule{
{method: http.MethodGet, pathRegexp: regexp.MustCompile(`/search/v1/statement/executing/`)},
},
method: http.MethodPost,
path: "/search/v1/statement/executing/abc123",
expectWAF: true,
},
{
name: "Does not bypass WAF when path does not match",
bypassRules: []compiledBypassRule{
{method: http.MethodGet, pathRegexp: regexp.MustCompile(`/search/v1/statement/executing/`)},
},
method: http.MethodGet,
path: "/search/v1/statement/queued/abc123",
expectWAF: true,
},
{
name: "Bypasses WAF with method-only rule",
bypassRules: []compiledBypassRule{
{method: http.MethodGet},
},
method: http.MethodGet,
path: "/any/path",
expectWAF: false,
},
{
name: "Bypasses WAF with path-only rule",
bypassRules: []compiledBypassRule{
{pathRegexp: regexp.MustCompile(`/health`)},
},
method: http.MethodPost,
path: "/health",
expectWAF: false,
},
{
name: "No bypass rules — WAF always runs",
method: http.MethodGet,
path: "/search/v1/statement/executing/abc123",
expectWAF: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
wafCalled := false
modsecurityMockServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
wafCalled = true
w.WriteHeader(http.StatusOK)
}))
defer modsecurityMockServer.Close()
httpServiceHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
_, _ = w.Write([]byte(serviceResponse))
})
middleware := &Modsecurity{
next: httpServiceHandler,
modSecurityUrl: modsecurityMockServer.URL,
maxBodySize: 1024 * 1024,
name: "modsecurity-middleware",
httpClient: http.DefaultClient,
logger: log.New(io.Discard, "", log.LstdFlags),
bypassRules: tt.bypassRules,
}
req, _ := http.NewRequest(tt.method, "http://proxy.com"+tt.path, http.NoBody)
rw := httptest.NewRecorder()
middleware.ServeHTTP(rw, req)
assert.Equal(t, http.StatusOK, rw.Result().StatusCode)
assert.Equal(t, tt.expectWAF, wafCalled, "WAF called mismatch")
})
}
}