Skip to content

Commit 3c15624

Browse files
authored
fix: BodyLimit related documented default values, default RequestBodyLimitAction, adds some tests (#895)
* fixes documented default values and default requestbodyLimit * mod tidy * comments out SecRequestBodyNoFilesLimit * Add comment about not implemented SecRequestBodyNoFilesLimit in coraza.conf-recommended * Aligns RequestBodyLimit and ResponseBodyLimit defaults values to doc * adds link to issue in TODO comment * fix comment for autogeneration
1 parent acd2040 commit 3c15624

File tree

4 files changed

+33
-13
lines changed

4 files changed

+33
-13
lines changed

coraza.conf-recommended

+2-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,8 @@ SecRequestBodyLimit 13107200
4545

4646
SecRequestBodyInMemoryLimit 131072
4747

48-
SecRequestBodyNoFilesLimit 131072
48+
# SecRequestBodyNoFilesLimit is currently not supported by Coraza
49+
# SecRequestBodyNoFilesLimit 131072
4950

5051
# What to do if the request body size is above our configured limit.
5152
# Keep in mind that this setting will automatically be set to ProcessPartial

internal/corazawaf/transaction_test.go

+22-6
Original file line numberDiff line numberDiff line change
@@ -162,10 +162,11 @@ func TestWriteRequestBody(t *testing.T) {
162162
)
163163

164164
testCases := []struct {
165-
name string
166-
requestBodyLimit int
167-
requestBodyLimitAction types.BodyLimitAction
168-
shouldInterrupt bool
165+
name string
166+
requestBodyLimit int
167+
requestBodyLimitAction types.BodyLimitAction
168+
avoidRequestBodyLimitActionInit bool
169+
shouldInterrupt bool
169170
}{
170171
{
171172
name: "LimitNotReached",
@@ -178,6 +179,14 @@ func TestWriteRequestBody(t *testing.T) {
178179
requestBodyLimitAction: types.BodyLimitActionReject,
179180
shouldInterrupt: true,
180181
},
182+
{
183+
name: "LimitReachedAndRejectsDefaultValue",
184+
requestBodyLimit: urlencodedBodyLen - 3,
185+
// Omitting requestBodyLimitAction defaults to Reject
186+
// requestBodyLimitAction: types.BodyLimitActionReject,
187+
avoidRequestBodyLimitActionInit: true,
188+
shouldInterrupt: true,
189+
},
181190
{
182191
name: "LimitReachedAndPartialProcessing",
183192
requestBodyLimit: urlencodedBodyLen - 3,
@@ -201,8 +210,9 @@ func TestWriteRequestBody(t *testing.T) {
201210
waf.RuleEngine = types.RuleEngineOn
202211
waf.RequestBodyAccess = true
203212
waf.RequestBodyLimit = int64(testCase.requestBodyLimit)
204-
waf.RequestBodyLimitAction = testCase.requestBodyLimitAction
205-
213+
if !testCase.avoidRequestBodyLimitActionInit {
214+
waf.RequestBodyLimitAction = testCase.requestBodyLimitAction
215+
}
206216
tx := waf.NewTransaction()
207217
tx.AddRequestHeader("content-type", "application/x-www-form-urlencoded")
208218

@@ -472,6 +482,12 @@ func TestWriteResponseBody(t *testing.T) {
472482
responseBodyLimit: urlencodedBodyLen - 3,
473483
responseBodyLimitAction: types.BodyLimitActionProcessPartial,
474484
},
485+
{
486+
name: "LimitReachedAndPartialProcessingDefaultValue",
487+
responseBodyLimit: urlencodedBodyLen - 3,
488+
// Omitting requestBodyLimitAction defaults to ProcessPartial
489+
// responseBodyLimitAction: types.BodyLimitActionProcessPartial,
490+
},
475491
}
476492

477493
urlencodedBodyLenThird := urlencodedBodyLen / 3

internal/corazawaf/waf.go

+5-3
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ type WAF struct {
9191
UploadDir string
9292

9393
// Request body in memory limit excluding the size of any files being transported in the request.
94+
// TODO: SecRequestBodyNoFilesLimit directive is retrieving the value, but no logic based on it is implemented. See https://github.com/corazawaf/coraza/issues/896
9495
RequestBodyNoFilesLimit int64
9596

9697
RequestBodyLimitAction types.BodyLimitAction
@@ -196,7 +197,7 @@ func (w *WAF) newTransaction(opts Options) *Transaction {
196197
// Always non-nil if buffers / collections were already initialized so we don't do any of them
197198
// based on the presence of RequestBodyBuffer.
198199
if tx.requestBodyBuffer == nil {
199-
// if no requestBodyInMemoryLimit has been set we default to the
200+
// if no requestBodyInMemoryLimit has been set we default to the requestBodyLimit
200201
var requestBodyInMemoryLimit int64 = w.RequestBodyLimit
201202
if w.requestBodyInMemoryLimit != nil {
202203
requestBodyInMemoryLimit = int64(*w.requestBodyInMemoryLimit)
@@ -291,9 +292,10 @@ func NewWAF() *WAF {
291292
// These defaults are unavoidable as they are zero values for the variables
292293
RuleEngine: types.RuleEngineOn,
293294
RequestBodyAccess: false,
294-
RequestBodyLimit: _1gb,
295+
RequestBodyLimit: 134217728, // Hard limit equal to _1gb
296+
RequestBodyLimitAction: types.BodyLimitActionReject,
295297
ResponseBodyAccess: false,
296-
ResponseBodyLimit: _1gb,
298+
ResponseBodyLimit: 524288, // Hard limit equal to _1gb
297299
auditLogWriter: logWriter,
298300
auditLogWriterInitialized: false,
299301
AuditLogWriterConfig: auditlog.NewConfig(),

internal/seclang/directives.go

+4-3
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ func directiveSecResponseBodyAccess(options *DirectiveOptions) error {
229229
}
230230

231231
// Description: Configures the maximum request body size Coraza will accept for buffering.
232-
// Default: 134217728 (131072 KB)
232+
// Default: 134217728 (128 Mib)
233233
// Syntax: SecRequestBodyLimit [LIMIT_IN_BYTES]
234234
// ---
235235
// Anything over the limit will be rejected with status code 413 (Request Entity Too Large).
@@ -423,7 +423,7 @@ func directiveSecResponseBodyLimitAction(options *DirectiveOptions) error {
423423

424424
// Description: Configures the maximum response body size that will be accepted for buffering.
425425
// Syntax: SecResponseBodyLimit [LIMIT_IN_BYTES]
426-
// Default: 524288 (512 KB)
426+
// Default: 524288 (512 Kib)
427427
// ---
428428
// Anything over this limit will be rejected with status code 500 (Internal Server Error).
429429
// This setting will not affect the responses with MIME types that are not selected for
@@ -461,7 +461,7 @@ func directiveSecRequestBodyLimitAction(options *DirectiveOptions) error {
461461
}
462462

463463
// Description: Configures the maximum request body size that Coraza will store in memory.
464-
// Default: 131072 (128 KB)
464+
// Default: defaults to RequestBodyLimit
465465
// Syntax: SecRequestBodyInMemoryLimit [LIMIT_IN_BYTES]
466466
// ---
467467
// When a `multipart/form-data` request is being processed, once the in-memory limit is reached,
@@ -903,6 +903,7 @@ func directiveSecUploadDir(options *DirectiveOptions) error {
903903
// Generally speaking, the default value is not small enough. For most applications, you
904904
// should be able to reduce it down to 128 KB or lower. Anything over the limit will be
905905
// rejected with status code 413 (Request Entity Too Large). There is a hard limit of 1 GB.
906+
// Note: not implemented yet
906907
func directiveSecRequestBodyNoFilesLimit(options *DirectiveOptions) error {
907908
if len(options.Opts) == 0 {
908909
return errEmptyOptions

0 commit comments

Comments
 (0)