Skip to content

Commit

Permalink
breaking: rename methods for a better description of what they do.
Browse files Browse the repository at this point in the history
  • Loading branch information
jcchavezs committed Nov 28, 2022
1 parent dd37821 commit 9d655dd
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 23 deletions.
8 changes: 4 additions & 4 deletions http/interceptor.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,13 @@ func (i *rwInterceptor) Write(b []byte) (int, error) {
i.WriteHeader(http.StatusOK)
}

if i.tx.Interrupted() {
if i.tx.IsInterrupted() {
// if there is an interruption it must be from phase 4 and hence
// we won't write anything to either the body or the buffer.
return 0, nil
}

if i.tx.ResponseBodyAccessible() {
if i.tx.IsResponseBodyAccessible() {
// we only buffer the response body if we are going to access
// to it, otherwise we just send it to the response writer.
return i.tx.ResponseBodyWriter().Write(b)
Expand Down Expand Up @@ -85,13 +85,13 @@ func wrap(w http.ResponseWriter, r *http.Request, tx types.Transaction) (
responseProcessor := func(tx types.Transaction, r *http.Request) error {
// We look for interruptions determined at phase 4 (response headers)
// as body hasn't being analized yet.
if tx.Interrupted() {
if tx.IsInterrupted() {
// phase 4 interruption stops execution
w.WriteHeader(i.statusCode)
return nil
}

if tx.ResponseBodyAccessible() && tx.IsProcessableResponseBody() {
if tx.IsResponseBodyAccessible() && tx.IsResponseBodyProcessable() {
if it, err := tx.ProcessResponseBody(); err != nil {
w.WriteHeader(http.StatusInternalServerError)
return err
Expand Down
2 changes: 1 addition & 1 deletion http/middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func processRequest(tx types.Transaction, req *http.Request) (*types.Interruptio
return in, nil
}

if tx.RequestBodyAccessible() {
if tx.IsRequestBodyAccessible() {
// We only do body buffering if the transaction requires request
// body inspection, otherwise we just let the request follow its
// regular flow.
Expand Down
18 changes: 9 additions & 9 deletions internal/corazawaf/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -844,12 +844,12 @@ func (tx *Transaction) ProcessResponseHeaders(code int, proto string) *types.Int
return tx.interruption
}

// IsProcessableResponseBody returns true if the response body meets the
// IsResponseBodyProcessable returns true if the response body meets the
// criteria to be processed, response headers must be set before this.
// The content-type response header must be in the SecResponseBodyMimeType
// This is used by webservers to choose whether to stream response buffers
// directly to the client or write them to Coraza's buffer.
func (tx *Transaction) IsProcessableResponseBody() bool {
func (tx *Transaction) IsResponseBodyProcessable() bool {
// TODO add more validations
ct := tx.variables.responseContentType.String()
return stringsutil.InSlice(ct, tx.WAF.ResponseBodyMimeTypes)
Expand All @@ -870,7 +870,7 @@ func (tx *Transaction) ProcessResponseBody() (*types.Interruption, error) {
if tx.RuleEngine == types.RuleEngineOff {
return tx.interruption, nil
}
if !tx.ResponseBodyAccess || !tx.IsProcessableResponseBody() {
if !tx.ResponseBodyAccess || !tx.IsResponseBodyProcessable() {
tx.WAF.Logger.Debug("[%s] Skipping response body processing (Access: %t)", tx.id, tx.ResponseBodyAccess)
tx.WAF.Rules.Eval(types.PhaseResponseBody, tx)
return tx.interruption, nil
Expand Down Expand Up @@ -944,18 +944,18 @@ func (tx *Transaction) IsRuleEngineOff() bool {
return tx.RuleEngine == types.RuleEngineOff
}

// RequestBodyAccessible will return true if RequestBody access has been enabled by RequestBodyAccess
func (tx *Transaction) RequestBodyAccessible() bool {
// IsRequestBodyAccessible will return true if RequestBody access has been enabled by RequestBodyAccess
func (tx *Transaction) IsRequestBodyAccessible() bool {
return tx.RequestBodyAccess
}

// ResponseBodyAccessible will return true if ResponseBody access has been enabled by ResponseBodyAccess
func (tx *Transaction) ResponseBodyAccessible() bool {
// IsResponseBodyAccessible will return true if ResponseBody access has been enabled by ResponseBodyAccess
func (tx *Transaction) IsResponseBodyAccessible() bool {
return tx.ResponseBodyAccess
}

// Interrupted will return true if the transaction was interrupted
func (tx *Transaction) Interrupted() bool {
func (tx *Transaction) IsInterrupted() bool {
return tx.interruption != nil
}

Expand Down Expand Up @@ -1079,7 +1079,7 @@ func (tx *Transaction) Close() error {
errs = append(errs, err)
}

tx.WAF.Logger.Debug("[%s] Transaction finished, disrupted: %t", tx.id, tx.Interrupted())
tx.WAF.Logger.Debug("[%s] Transaction finished, disrupted: %t", tx.id, tx.IsInterrupted())

switch {
case len(errs) == 0:
Expand Down
4 changes: 2 additions & 2 deletions internal/seclang/rules_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ func TestSecMarkers(t *testing.T) {
tx := waf.NewTransaction()
defer tx.ProcessLogging()
tx.ProcessRequestHeaders()
if tx.Interrupted() {
if tx.IsInterrupted() {
t.Error("transaction failed to skipAfter")
}
interruption, err := tx.ProcessRequestBody()
Expand Down Expand Up @@ -317,7 +317,7 @@ func TestTxIssue147(t *testing.T) {
tx.ResponseBodyAccess = true
// we need a content-type header
tx.AddResponseHeader("Content-Type", "text/html")
if tx.IsProcessableResponseBody() {
if tx.IsResponseBodyProcessable() {
if _, err := tx.ResponseBodyBuffer.Write([]byte("#!/usr/bin/python")); err != nil {
t.Error(err)
}
Expand Down
14 changes: 7 additions & 7 deletions types/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,33 +106,33 @@ type Transaction interface {
// IsRuleEngineOff will return true if RuleEngine is set to Off
IsRuleEngineOff() bool

// RequestBodyAccessible will return true if RequestBody access has been enabled by RequestBodyAccess
// IsRequestBodyAccessible will return true if RequestBody access has been enabled by RequestBodyAccess
//
// This can be used to perform checks just before calling request body related functions.
// In order to avoid any risk of performing wrong early assumptions, perform early checks on this value
// only if the API consumer requires them for specific server/proxy actions
// (such as avoiding proxy side buffering).
// Note: it returns the current status, later rules may still change it via ctl actions.
RequestBodyAccessible() bool
IsRequestBodyAccessible() bool

// ResponseBodyAccessible will return true if ResponseBody access has been enabled by ResponseBodyAccess
// IsResponseBodyAccessible will return true if ResponseBody access has been enabled by ResponseBodyAccess
//
// This can be used to perform checks just before calling response body related functions.
// In order to avoid any risk of performing wrong early assumptions, perform early checks on this value
// only if the API consumer requires them for specific server/proxy actions
// (such as avoiding proxy side buffering).
// Note: it returns the current status, later rules may still change it via ctl actions.
ResponseBodyAccessible() bool
IsResponseBodyAccessible() bool

// IsProcessableResponseBody returns true if the response body meets the
// IsResponseBodyProcessable returns true if the response body meets the
// criteria to be processed, response headers must be set before this.
// The content-type response header must be in the SecResponseBodyMimeType
// This is used by webservers to choose whether to stream response buffers
// directly to the client or write them to Coraza's buffer.
IsProcessableResponseBody() bool
IsResponseBodyProcessable() bool

// Interrupted will return true if the transaction was interrupted
Interrupted() bool
IsInterrupted() bool

// Interruption returns the types.Interruption if the request was interrupted,
// or nil otherwise.
Expand Down

0 comments on commit 9d655dd

Please sign in to comment.