Skip to content

Commit

Permalink
flatters config removing RequestBodyConfig and ResponseBodyConfig
Browse files Browse the repository at this point in the history
  • Loading branch information
M4tteoP committed Nov 12, 2022
1 parent 947e7eb commit 0494ee2
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 86 deletions.
121 changes: 41 additions & 80 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,24 @@ type WAFConfig interface {
// WithContentInjection enables content injection.
WithContentInjection() WAFConfig

// WithRequestBodyAccess configures access to the request body.
WithRequestBodyAccess(config RequestBodyConfig) WAFConfig
// WithRequestBodyAccess enables access to the request body.
WithRequestBodyAccess() WAFConfig

// WithResponseBodyAccess configures access to the response body.
WithResponseBodyAccess(config ResponseBodyConfig) WAFConfig
// WithRequestBodyLimit sets the maximum number of bytes that can be read from the request body. Bytes beyond that set
// in WithInMemoryLimit will be buffered to disk.
WithRequestBodyLimit(limit int) WAFConfig

// WithRequestBodyInMemoryLimit sets the maximum number of bytes that can be read from the request body and buffered in memory.
WithRequestBodyInMemoryLimit(limit int) WAFConfig

// WithResponseBodyAccess enables access to the response body.
WithResponseBodyAccess() WAFConfig

// WithResponseBodyLimit sets the maximum number of bytes that can be read from the response body and buffered in memory.
WithResponseBodyLimit(limit int) WAFConfig

// WithResponseBodyMimeTypes sets the mime types of responses that will be processed.
WithResponseBodyMimeTypes(mimeTypes []string) WAFConfig

// WithDebugLogger configures a debug logger.
WithDebugLogger(logger loggers.DebugLogger) WAFConfig
Expand All @@ -51,35 +64,6 @@ func NewWAFConfig() WAFConfig {
return &wafConfig{}
}

// RequestBodyConfig controls access to the request body.
type RequestBodyConfig interface {
// WithLimit sets the maximum number of bytes that can be read from the request body. Bytes beyond that set
// in WithInMemoryLimit will be buffered to disk.
WithLimit(limit int) RequestBodyConfig

// WithInMemoryLimit sets the maximum number of bytes that can be read from the request body and buffered in memory.
WithInMemoryLimit(limit int) RequestBodyConfig
}

// NewRequestBodyConfig returns a new RequestBodyConfig with the default settings.
func NewRequestBodyConfig() RequestBodyConfig {
return &requestBodyConfig{}
}

// ResponseBodyConfig controls access to the response body.
type ResponseBodyConfig interface {
// WithLimit sets the maximum number of bytes that can be read from the response body and buffered in memory.
WithLimit(limit int) ResponseBodyConfig

// WithMimeTypes sets the mime types of responses that will be processed.
WithMimeTypes(mimeTypes []string) ResponseBodyConfig
}

// NewResponseBodyConfig returns a new ResponseBodyConfig with the default settings.
func NewResponseBodyConfig() ResponseBodyConfig {
return &responseBodyConfig{}
}

// AuditLogConfig controls audit logging.
type AuditLogConfig interface {
// LogRelevantOnly enables audit logging only for relevant events.
Expand All @@ -104,14 +88,18 @@ type wafRule struct {
}

type wafConfig struct {
rules []wafRule
auditLog *auditLogConfig
contentInjection bool
requestBody *requestBodyConfig
responseBody *responseBodyConfig
debugLogger loggers.DebugLogger
errorLogger corazawaf.ErrorLogCallback
fsRoot fs.FS
rules []wafRule
auditLog *auditLogConfig
contentInjection bool
requestBodyAccess bool
requestBodyLimit int
requestBodyInMemoryLimit int
responseBodyAccess bool
responseBodyLimit int
responseBodyMimeTypes []string
debugLogger loggers.DebugLogger
errorLogger corazawaf.ErrorLogCallback
fsRoot fs.FS
}

func (c *wafConfig) WithRule(rule *corazawaf.Rule) WAFConfig {
Expand Down Expand Up @@ -144,15 +132,15 @@ func (c *wafConfig) WithContentInjection() WAFConfig {
return ret
}

func (c *wafConfig) WithRequestBodyAccess(config RequestBodyConfig) WAFConfig {
func (c *wafConfig) WithRequestBodyAccess() WAFConfig {
ret := c.clone()
ret.requestBody = config.(*requestBodyConfig)
ret.requestBodyAccess = true
return ret
}

func (c *wafConfig) WithResponseBodyAccess(config ResponseBodyConfig) WAFConfig {
func (c *wafConfig) WithResponseBodyAccess() WAFConfig {
ret := c.clone()
ret.responseBody = config.(*responseBodyConfig)
ret.responseBodyAccess = true
return ret
}

Expand Down Expand Up @@ -182,57 +170,30 @@ func (c *wafConfig) clone() *wafConfig {
return &ret
}

type requestBodyConfig struct {
limit int
inMemoryLimit int
}

func (c *requestBodyConfig) WithLimit(limit int) RequestBodyConfig {
ret := c.clone()
ret.limit = limit
return ret
}

func (c *requestBodyConfig) WithInMemoryLimit(limit int) RequestBodyConfig {
func (c *wafConfig) WithRequestBodyLimit(limit int) WAFConfig {
ret := c.clone()
ret.inMemoryLimit = limit
ret.requestBodyLimit = limit
return ret
}

func (c *requestBodyConfig) clone() *requestBodyConfig {
ret := *c // copy
return &ret
}

type responseBodyConfig struct {
limit int
inMemoryLimit int
mimeTypes []string
}

func (c *responseBodyConfig) WithLimit(limit int) ResponseBodyConfig {
func (c *wafConfig) WithRequestBodyInMemoryLimit(limit int) WAFConfig {
ret := c.clone()
ret.limit = limit
ret.requestBodyInMemoryLimit = limit
return ret
}

func (c *responseBodyConfig) WithInMemoryLimit(limit int) ResponseBodyConfig {
func (c *wafConfig) WithResponseBodyLimit(limit int) WAFConfig {
ret := c.clone()
ret.inMemoryLimit = limit
ret.responseBodyLimit = limit
return ret
}

func (c *responseBodyConfig) WithMimeTypes(mimeTypes []string) ResponseBodyConfig {
func (c *wafConfig) WithResponseBodyMimeTypes(mimeTypes []string) WAFConfig {
ret := c.clone()
ret.mimeTypes = mimeTypes
ret.responseBodyMimeTypes = mimeTypes
return ret
}

func (c *responseBodyConfig) clone() *responseBodyConfig {
ret := *c // copy
return &ret
}

type auditLogConfig struct {
relevantOnly bool
parts types.AuditLogParts
Expand Down
2 changes: 1 addition & 1 deletion testing/engine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func TestRequest(t *testing.T) {

func TestResponse(t *testing.T) {
waf, _ := coraza.NewWAF(coraza.NewWAFConfig().
WithResponseBodyAccess(coraza.NewResponseBodyConfig()))
WithResponseBodyAccess())
test := NewTest("test", waf)
req := buildRequest("POST", "/test")
if err := test.SetRawRequest([]byte(req)); err != nil {
Expand Down
21 changes: 16 additions & 5 deletions waf.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,13 +76,24 @@ func NewWAF(config WAFConfig) (WAF, error) {
waf.ContentInjection = true
}

if r := c.requestBody; r != nil {
waf.RequestBodyLimit = int64(r.limit)
waf.RequestBodyInMemoryLimit = int64(r.inMemoryLimit)
if c.requestBodyAccess {
waf.RequestBodyAccess = true
}

if r := c.responseBody; r != nil {
waf.ResponseBodyLimit = int64(r.limit)
if c.requestBodyLimit != 0 {
waf.RequestBodyLimit = int64(c.requestBodyLimit)
}

if c.requestBodyInMemoryLimit != 0 {
waf.RequestBodyInMemoryLimit = int64(c.requestBodyInMemoryLimit)
}

if c.requestBodyAccess {
waf.RequestBodyAccess = true
}

if c.requestBodyLimit != 0 {
waf.ResponseBodyLimit = int64(c.requestBodyLimit)
}

if c.errorLogger != nil {
Expand Down

0 comments on commit 0494ee2

Please sign in to comment.