Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Exports IsRuleEngineOff #504

Merged
merged 8 commits into from
Nov 21, 2022
12 changes: 12 additions & 0 deletions http/middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,18 @@ func processRequest(tx types.Transaction, req *http.Request) (*types.Interruptio
}
}

// Calling tx.RuleEngineStatus() and tx.RequestBodyAccessible() is possible to
// anticipate some checks performed inside ProcessRequestBody(), avoiding
// to call the latter if no inspections are going to happen.
// It is performed here as a matter of example. It is recommended to avoid doing it
// if not strictly needed for server/proxy side actions.
switch {
case tx.RuleEngineStatus() == types.RuleEngineOff:
fzipi marked this conversation as resolved.
Show resolved Hide resolved
return nil, nil
case !tx.RequestBodyAccessible():
return tx.Interruption(), nil
}

return tx.ProcessRequestBody()
}

Expand Down
16 changes: 16 additions & 0 deletions http/middleware_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,22 @@ func TestProcessRequest(t *testing.T) {
}
}

func TestProcessRequestEngineOff(t *testing.T) {
req, _ := http.NewRequest("POST", "https://www.coraza.io/test", strings.NewReader("test=456"))
waf := corazawaf.NewWAF()
waf.RuleEngine = types.RuleEngineOff
tx := waf.NewTransaction()
if _, err := processRequest(tx, req); err != nil {
t.Fatal(err)
}
if tx.Variables().RequestMethod().String() != "POST" {
t.Fatal("failed to set request from request object")
}
if err := tx.Close(); err != nil {
t.Fatal(err)
}
}

func TestProcessRequestMultipart(t *testing.T) {
req, _ := http.NewRequest("POST", "/some", nil)
if err := multipartRequest(t, req); err != nil {
Expand Down
28 changes: 28 additions & 0 deletions internal/corazawaf/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -939,12 +939,40 @@ func (tx *Transaction) ProcessLogging() {
}
}

// RuleEngineStatus returns the status of the rule engine for the transaction
//
// It is suggested to perform early checks on the status only if the API consumer
// requires it for specific server/proxy actions (such as avoiding proxy side buffering).
// Otherwise do not use this method in order to avoid any risk of performing wrong early assumptions.
//
// Three values can be returned:
// types.RuleEngineOn: no early assumptions have to be made at all
// types.RuleEngineDetectionOnly: it may be possible to assume that no disruptive actions will be performed
// types.RuleEngineOff: it is safe to assume that no rules will be processed
//
// Note that it returns the current status of the engine, later rules may still change it via ctl actions
func (tx *Transaction) RuleEngineStatus() types.RuleEngineStatus {
return tx.RuleEngine
}

// RequestBodyAccessible will return true if RequestBody access has been enabled by RequestBodyAccess
//
// It is suggested to perform early checks only if the API consumer requires them for specific
// server/proxy actions (such as avoiding proxy side buffering).
// Otherwise do not use this method in order to avoid any risk of performing wrong early assumptions.
//
// Note that it returns the current status, later rules may still change it via ctl actions
func (tx *Transaction) RequestBodyAccessible() bool {
return tx.RequestBodyAccess
}

// ResponseBodyAccessible will return true if ResponseBody access has been enabled by ResponseBodyAccess
//
// It is suggested to perform early checks only if the API consumer requires them for specific
// server/proxy actions (such as avoiding proxy side buffering).
// Otherwise do not use this method in order to avoid any risk of performing wrong early assumptions.
//
// Note that it returns the current status, later rules may still change it via ctl actions.
func (tx *Transaction) ResponseBodyAccessible() bool {
return tx.ResponseBodyAccess
}
Expand Down
3 changes: 3 additions & 0 deletions types/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,9 @@ type Transaction interface {
// delivered prior to the execution of this method.
ProcessLogging()

// RuleEngineStatus returns the status of the rule engine for the transaction
RuleEngineStatus() RuleEngineStatus

// RequestBodyAccessible will return true if RequestBody access has been enabled by RequestBodyAccess
RequestBodyAccessible() bool

Expand Down