forked from corazawaf/coraza
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathwaf.go
More file actions
183 lines (147 loc) · 4.9 KB
/
Copy pathwaf.go
File metadata and controls
183 lines (147 loc) · 4.9 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
// Copyright 2022 Juan Pablo Tosso and the OWASP Coraza contributors
// SPDX-License-Identifier: Apache-2.0
package coraza
import (
"context"
"fmt"
"strings"
"github.com/corazawaf/coraza/v3/internal/corazawaf"
"github.com/corazawaf/coraza/v3/internal/environment"
"github.com/corazawaf/coraza/v3/internal/seclang"
"github.com/corazawaf/coraza/v3/types"
)
// WAF instance is used to store configurations and rules
// Every web application should have a different WAF instance,
// but you can share an instance if you are ok with sharing
// configurations, rules and logging.
// Transactions and SecLang parser requires a WAF instance
// You can use as many WAF instances as you want, and they are
// concurrent safe
type WAF interface {
// NewTransaction Creates a new initialized transaction for this WAF instance
NewTransaction() types.Transaction
NewTransactionWithID(id string) types.Transaction
GetRuleGroup() *corazawaf.RuleGroup
}
// NewWAF creates a new WAF instance with the provided configuration.
func NewWAF(config WAFConfig) (WAF, error) {
c := config.(*wafConfig)
waf := corazawaf.NewWAF()
if environment.HasAccessToFS {
if err := environment.IsDirWritable(waf.TmpDir); err != nil {
return nil, fmt.Errorf("filesystem access check: %w. Use 'no_fs_access' build tag, if not available", err)
}
}
if c.debugLogger != nil {
waf.Logger = c.debugLogger
}
if c.ruleObserver != nil {
waf.Rules.SetObserver(c.ruleObserver)
}
parser := seclang.NewParser(waf)
if c.fsRoot != nil {
parser.SetRoot(c.fsRoot)
}
for _, r := range c.rules {
switch {
case r.rule != nil:
if err := waf.Rules.Add(r.rule); err != nil {
return nil, fmt.Errorf("invalid WAF config from rule: %w", err)
}
case r.str != "":
if err := parser.FromString(r.str); err != nil {
return nil, fmt.Errorf("invalid WAF config from string: %w", err)
}
case r.file != "":
if err := parser.FromFile(r.file); err != nil {
return nil, fmt.Errorf("invalid WAF config from file: %w", err)
}
}
}
populateAuditLog(waf, c)
if err := waf.InitAuditLogWriter(); err != nil {
return nil, fmt.Errorf("invalid WAF config from audit log: %w", err)
}
if c.requestBodyAccess {
waf.RequestBodyAccess = true
}
if c.requestBodyLimit != nil {
waf.RequestBodyLimit = int64(*c.requestBodyLimit)
}
if c.requestBodyInMemoryLimit != nil {
waf.SetRequestBodyInMemoryLimit(int64(*c.requestBodyInMemoryLimit))
}
if c.responseBodyAccess {
waf.ResponseBodyAccess = true
}
if c.responseBodyLimit != nil {
waf.ResponseBodyLimit = int64(*c.responseBodyLimit)
}
if c.responseBodyMimeTypes != nil {
waf.ResponseBodyMimeTypes = c.responseBodyMimeTypes
}
if c.errorCallback != nil {
waf.ErrorLogCb = c.errorCallback
}
// In DetectionOnly mode, set ProcessPartial for body limit actions to avoid disrupting transactions during initial deployment.
if waf.RuleEngine == types.RuleEngineDetectionOnly {
if waf.RequestBodyLimitAction != types.BodyLimitActionProcessPartial {
waf.RequestBodyLimitAction = types.BodyLimitActionProcessPartial
waf.Logger.Debug().Msg("DetectionOnly mode: SecRequestBodyLimitAction enforced to ProcessPartial")
}
if waf.ResponseBodyLimitAction != types.BodyLimitActionProcessPartial {
waf.ResponseBodyLimitAction = types.BodyLimitActionProcessPartial
waf.Logger.Debug().Msg("DetectionOnly mode: SecResponseBodyLimitAction enforced to ProcessPartial")
}
}
if err := waf.Validate(); err != nil {
return nil, err
}
return wafWrapper{waf: waf}, nil
}
func populateAuditLog(waf *corazawaf.WAF, c *wafConfig) {
if c.auditLog == nil {
return
}
if c.auditLog.relevantOnly {
waf.AuditEngine = types.AuditEngineRelevantOnly
} else {
waf.AuditEngine = types.AuditEngineOn
}
if len(c.auditLog.parts) > 0 {
waf.AuditLogParts = c.auditLog.parts
}
if c.auditLog.writer != nil {
waf.SetAuditLogWriter(c.auditLog.writer)
}
}
type wafWrapper struct {
waf *corazawaf.WAF
}
// NewTransaction implements the same method on WAF.
func (w wafWrapper) NewTransaction() types.Transaction {
return w.waf.NewTransaction()
}
// NewTransactionWithID implements the same method on WAF.
func (w wafWrapper) NewTransactionWithID(id string) types.Transaction {
id = strings.TrimSpace(id)
if len(id) == 0 {
w.waf.Logger.Warn().Msg("Empty ID passed for new transaction")
}
return w.waf.NewTransactionWithOptions(corazawaf.Options{Context: context.Background(), ID: id})
}
// NewTransactionWithOptions implements the same method on WAF.
func (w wafWrapper) NewTransactionWithOptions(opts corazawaf.Options) types.Transaction {
return w.waf.NewTransactionWithOptions(opts)
}
func (w wafWrapper) GetRuleGroup() *corazawaf.RuleGroup {
return &w.waf.Rules
}
// RulesCount returns the number of rules in this WAF.
func (w wafWrapper) RulesCount() int {
return w.waf.Rules.Count()
}
// Close releases cached resources owned by this WAF instance.
func (w wafWrapper) Close() error {
return w.waf.Close()
}