-
Notifications
You must be signed in to change notification settings - Fork 43
/
coraza_test.go
207 lines (180 loc) · 4.86 KB
/
coraza_test.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
// Copyright 2023 The OWASP Coraza contributors
// SPDX-License-Identifier: Apache-2.0
package coraza
import (
"bytes"
"fmt"
"io"
"mime/multipart"
"net/http"
"os"
"strings"
"testing"
"time"
"github.com/caddyserver/caddy/v2/caddyconfig/caddyfile"
"github.com/caddyserver/caddy/v2/caddytest"
)
const baseURL = "http://127.0.0.1:8080"
func TestPlugin(t *testing.T) {
tester, err := newTester("test.init.config", t)
if err != nil {
t.Fatal(err)
}
res, _ := tester.AssertGetResponse(baseURL+"/test", 200, "test123")
// Comes from https://github.com/corazawaf/coraza-caddy/blob/5e8337/test.init.config#L17
if len(res.Header.Get("x-request-id")) == 0 {
t.Fatal("X-Request-Id header is not set")
}
time.Sleep(1 * time.Second)
}
func TestPluginReload(t *testing.T) {
tester := caddytest.NewTester(t)
configFile := "test.init.config"
configContent, err := os.ReadFile(configFile)
if err != nil {
t.Fatalf("Failed to load configuration file %s: %s", configFile, err)
}
rawConfig := string(configContent)
rawConfig = strings.ReplaceAll(rawConfig, "test123", "test456")
tester.InitServer(rawConfig, "caddyfile")
tester.AssertGetResponse(baseURL+"/test", 200, "test456")
time.Sleep(1 * time.Second)
}
func TestSimpleRule(t *testing.T) {
tester, err := newTester("test.init.config", t)
if err != nil {
t.Fatal(err)
}
req, _ := http.NewRequest("GET", baseURL+"/test5", nil)
tester.AssertResponseCode(req, 403)
time.Sleep(1 * time.Second)
req, _ = http.NewRequest("GET", baseURL+"/test_include1", nil)
tester.AssertResponseCode(req, 403)
time.Sleep(1 * time.Second)
req, _ = http.NewRequest("GET", baseURL+"/test_include2", nil)
tester.AssertResponseCode(req, 403)
time.Sleep(1 * time.Second)
}
func TestPhase3Disruption(t *testing.T) {
tester, err := newTester("test.init.config", t)
if err != nil {
t.Fatal(err)
}
req, _ := http.NewRequest("GET", baseURL+"/test6", nil)
tester.AssertResponseCode(req, 403)
time.Sleep(1 * time.Second)
}
func TestPostUrlEncoded(t *testing.T) {
tester, err := newTester("test.init.config", t)
if err != nil {
t.Fatal(err)
}
b := strings.NewReader("adsf=qwer" + strings.Repeat("a", 1000))
req, _ := http.NewRequest("POST", baseURL+"/test", b)
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
tester.AssertResponseCode(req, 200)
time.Sleep(1 * time.Second)
}
func TestPostMultipart(t *testing.T) {
tester, err := newTester("test.init.config", t)
if err != nil {
t.Fatal(err)
}
req, _ := http.NewRequest("POST", baseURL+"/", nil)
if err := multipartRequest(req); err != nil {
t.Fatal(err)
}
tester.AssertResponseCode(req, 200)
time.Sleep(1 * time.Second)
}
func multipartRequest(req *http.Request) error {
var b bytes.Buffer
w := multipart.NewWriter(&b)
tempfile, err := os.CreateTemp("/tmp", "tmpfile*")
if err != nil {
return err
}
defer os.Remove(tempfile.Name())
for i := 0; i < 1024*5; i++ {
// this should create a 5mb file
if _, err := tempfile.Write([]byte(strings.Repeat("A", 1024))); err != nil {
return err
}
}
var fw io.Writer
if fw, err = w.CreateFormFile("fupload", tempfile.Name()); err != nil {
return err
}
if _, err := tempfile.Seek(0, 0); err != nil {
return err
}
if _, err = io.Copy(fw, tempfile); err != nil {
return err
}
req.Body = io.NopCloser(&b)
req.Header.Set("Content-Type", w.FormDataContentType())
req.Method = "POST"
return nil
}
func newTester(caddyfile string, t *testing.T) (*caddytest.Tester, error) {
tester := caddytest.NewTester(t)
configContent, err := os.ReadFile(caddyfile)
if err != nil {
return nil, fmt.Errorf("failed to load configuration file %q: %s", caddyfile, err)
}
tester.InitServer(string(configContent), "caddyfile")
return tester, nil
}
func TestUnmarshalCaddyfile(t *testing.T) {
tests := map[string]struct {
config string
shouldErr bool
}{
"empty config": {
shouldErr: true,
},
"invalid config for directives without value": {
config: `coraza_waf {
directives
}`,
shouldErr: true,
},
"invalid config for directives with more than one value": {
config: `coraza_waf {
directives first_arg second_arg
}`,
shouldErr: true,
},
"invalid config for unexpected key": {
config: `coraza_waf {
unknown_key first_arg
}`,
shouldErr: true,
},
"invalid config for load_owasp_crs with value": {
config: `coraza_waf {
load_owasp_crs next_arg
}`,
shouldErr: true,
},
"valid config": {
config: `coraza_waf {
load_owasp_crs
directives ` + "`Include my-rules.conf`" + `
}`,
},
}
for name, test := range tests {
t.Run(name, func(t *testing.T) {
dispenser := caddyfile.NewTestDispenser(test.config)
m := &corazaModule{}
err := m.UnmarshalCaddyfile(dispenser)
if test.shouldErr && err == nil {
t.Fatal("Expected error but got nil")
}
if !test.shouldErr && err != nil {
t.Fatalf("Expected no error but got: %v", err)
}
})
}
}