-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcoraza.go
58 lines (49 loc) · 1.14 KB
/
coraza.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
package coraza
import (
"context"
"io"
"net/http"
"strings"
"github.com/jptosso/coraza-waf/v2"
)
// Config the plugin configuration.
type Config struct {
Include string `yaml:"include"`
Directives string `yaml:"include"`
}
// CreateConfig creates the default plugin configuration.
func CreateConfig() *Config {
return &Config{}
}
// Coraza a plugin.
type Coraza struct {
next http.Handler
waf *coraza.Waf
// ...
}
// New created a new plugin.
func New(ctx context.Context, next http.Handler, config *Config, name string) (http.Handler, error) {
return &Coraza{
next: next,
waf: coraza.NewWaf(),
}, nil
}
func (e *Coraza) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
tx := e.waf.NewTransaction()
it, err := tx.ProcessRequest(req)
//TODO TEMPORARY FIX FOR A CORAZA BUG
if req.Body == nil {
req.Body = io.NopCloser(strings.NewReader(""))
}
if it != nil || err != nil {
errorPage(rw, tx)
return
}
rw = NewHTTPResponseInterceptor(rw, tx)
e.next.ServeHTTP(rw, req)
}
func errorPage(rw http.ResponseWriter, tx *coraza.Transaction) {
//TODO write som error page
rw.WriteHeader(403)
rw.Write([]byte("Access Denied!"))
}