-
-
Notifications
You must be signed in to change notification settings - Fork 229
/
magefile.go
298 lines (255 loc) · 7.43 KB
/
magefile.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
// Copyright 2022 Juan Pablo Tosso and the OWASP Coraza contributors
// SPDX-License-Identifier: Apache-2.0
//go:build mage
// +build mage
package main
import (
"encoding/json"
"errors"
"fmt"
"io"
"os"
"os/exec"
"path/filepath"
"regexp"
"strings"
"github.com/magefile/mage/mg"
"github.com/magefile/mage/sh"
)
var addLicenseVersion = "v1.1.1" // https://github.com/google/addlicense/releases
var gosImportsVer = "v0.3.7" // https://github.com/rinchsan/gosimports/releases
var golangCILintVer = "v1.60.3" // https://github.com/golangci/golangci-lint/releases
var errNoGitDir = errors.New("no .git directory found")
var errUpdateGeneratedFiles = errors.New("generated files need to be updated")
// Format formats code in this repository.
func Format() error {
if err := sh.RunV("go", "generate", "./..."); err != nil {
return err
}
if err := sh.RunV("go", "mod", "tidy"); err != nil {
return err
}
if err := sh.RunV("go", "work", "sync"); err != nil {
return err
}
// addlicense strangely logs skipped files to stderr despite not being erroneous, so use the long sh.Exec form to
// discard stderr too.
if _, err := sh.Exec(map[string]string{}, io.Discard, io.Discard, "go", "run", fmt.Sprintf("github.com/google/addlicense@%s", addLicenseVersion),
"-c", "Juan Pablo Tosso and the OWASP Coraza contributors",
"-s=only",
"-ignore", "**/*.yml",
"-ignore", "**/*.yaml",
"-ignore", "examples/**", "."); err != nil {
return err
}
return sh.RunV("go", "run", fmt.Sprintf("github.com/rinchsan/gosimports/cmd/gosimports@%s", gosImportsVer),
"-w",
"-local",
"github.com/corazawaf/coraza",
".")
}
// Lint verifies code quality.
func Lint() error {
if err := sh.RunV("go", "generate", "./..."); err != nil {
return err
}
if sh.Run("git", "diff", "--exit-code", "--", "'*.gen.go'") != nil {
return errUpdateGeneratedFiles
}
if err := sh.RunV("go", "run", fmt.Sprintf("github.com/golangci/golangci-lint/cmd/golangci-lint@%s", golangCILintVer), "run"); err != nil {
return err
}
if err := sh.RunV("go", "work", "sync"); err != nil {
return err
}
if err := filepath.WalkDir(".", func(path string, d os.DirEntry, err error) error {
if err != nil {
return err
}
if !d.IsDir() {
return nil
}
if _, err := os.Stat(filepath.Join(path, "go.mod")); err == nil {
cmd := exec.Command("go", "mod", "tidy")
cmd.Dir = path
out, err := cmd.CombinedOutput()
fmt.Printf(string(out))
if err != nil {
return err
}
}
return nil
}); err != nil {
return err
}
return nil
}
// Test runs all tests.
func Test() error {
if err := sh.RunV("go", "test", "./..."); err != nil {
return err
}
if err := sh.RunV("go", "test", "-tags=memoize_builders", "./..."); err != nil {
return err
}
if err := sh.RunV("go", "test", "./examples/http-server", "-race"); err != nil {
return err
}
if err := sh.RunV("go", "test", "./testing/coreruleset"); err != nil {
return err
}
if err := sh.RunV("go", "test", "-tags=memoize_builders", "./testing/coreruleset"); err != nil {
return err
}
// Execute FTW tests with multiphase evaluation enabled as well
if err := sh.RunV("go", "test", "-tags=coraza.rule.multiphase_evaluation", "./testing/coreruleset"); err != nil {
return err
}
// Execute FTW tests with coraza.rule.no_regex_multiline as well
if err := sh.RunV("go", "test", "-tags=coraza.rule.no_regex_multiline", "./testing/coreruleset"); err != nil {
return err
}
if err := sh.RunV("go", "test", "-tags=coraza.rule.no_regex_multiline", "-run=^TestRx", "./..."); err != nil {
return err
}
if err := sh.RunV("go", "test", "-tags=coraza.rule.case_sensitive_args_keys", "-run=^TestCaseSensitive", "./..."); err != nil {
return err
}
return nil
}
func buildTagsFlags(tags string) string {
if tags == "" {
return ""
}
// we remove all non alphanumeric _,-
rx := regexp.MustCompile("^[\\w_,\\.]+$")
if !rx.MatchString(tags) {
panic("Invalid build tags")
}
return tags
}
// Coverage runs tests with coverage and race detector enabled.
// Usage: mage coverage [buildTags]
func Coverage() error {
buildTags := os.Getenv("BUILD_TAGS")
tags := buildTagsFlags(buildTags)
if err := os.MkdirAll("build", 0755); err != nil {
return err
}
fmt.Println("Running tests with coverage")
fmt.Println("Tags:", tags)
tagsCmd := ""
if tags != "" {
tagsCmd = "-tags=" + tags
}
if err := sh.RunV("go", "test", "-race", tagsCmd, "-coverprofile=build/coverage.txt", "-covermode=atomic", "-coverpkg=./...", "./..."); err != nil {
return err
}
// Execute http-server tests with coverage
if err := sh.RunV("go", "test", "-race", tagsCmd, "-coverprofile=build/coverage-examples.txt", "-covermode=atomic", "-coverpkg=./...", "./examples/http-server"); err != nil {
return err
}
// Execute FTW tests with coverage as well
if err := sh.RunV("go", "test", tagsCmd, "-coverprofile=build/coverage-ftw.txt", "-covermode=atomic", "-coverpkg=./...", "./testing/coreruleset"); err != nil {
return err
}
// we run tinygo tag only if memoize_builders is not enabled
if !strings.Contains(tags, "memoize_builders") {
if tagsCmd != "" {
tagsCmd += ",tinygo"
}
if err := sh.RunV("go", "test", "-race", tagsCmd, "-coverprofile=build/coverage-tinygo.txt", "-covermode=atomic", "-coverpkg=./...", "./..."); err != nil {
return err
}
}
return sh.RunV("go", "tool", "cover", "-html=build/coverage.txt", "-o", "build/coverage.html")
}
// Fuzz runs fuzz tests
func Fuzz() error {
// Go must be run once per test when fuzzing
tests := []struct {
pkg string
tests []string
}{
{
pkg: "./internal/operators",
tests: []string{
"FuzzSQLi",
"FuzzXSS",
},
},
{
pkg: "./internal/transformations",
tests: []string{
"FuzzB64Decode$",
"FuzzB64DecodeExt",
"FuzzCMDLine",
},
},
}
for _, pkgTests := range tests {
for _, test := range pkgTests.tests {
fmt.Println("Running", test)
if err := sh.RunV("go", "test", "-fuzz="+test, "-fuzztime=2m", pkgTests.pkg); err != nil {
return err
}
}
}
return nil
}
// Doc runs godoc, access at http://localhost:6060
func Doc() error {
return sh.RunV("go", "run", "golang.org/x/tools/cmd/godoc@latest", "-http=:6060")
}
// Precommit installs a git hook to run check when committing
func Precommit() error {
if _, err := os.Stat(filepath.Join(".git", "hooks")); os.IsNotExist(err) {
return errNoGitDir
}
f, err := os.ReadFile(".pre-commit.hook")
if err != nil {
return err
}
return os.WriteFile(filepath.Join(".git", "hooks", "pre-commit"), f, 0755)
}
// Check runs lint and tests.
func Check() {
mg.SerialDeps(Lint, Test)
}
// combinations generates all possible combinations of build tags
func combinations(tags []string) []string {
var result []string
n := len(tags)
for i := 0; i < (1 << n); i++ {
var combo []string
for j := 0; j < n; j++ {
if i&(1<<j) != 0 {
combo = append(combo, tags[j])
}
}
if len(combo) > 0 {
result = append(result, strings.Join(combo, ","))
} else {
result = append(result, "")
}
}
return result
}
// Generates a JSON output to stdout which contains all permutations of build tags for the project.
func TagsMatrix() error {
tags := []string{
"coraza.rule.case_sensitive_args_keys",
"coraza.rule.no_regex_multiline",
"memoize_builders",
"coraza.rule.multiphase_valuation",
"no_fs_access",
}
combos := combinations(tags)
jsonData, err := json.Marshal(combos)
if err != nil {
fmt.Println("Error generating JSON:", err)
return nil
}
fmt.Println(string(jsonData))
return nil
}