Browse Source

fix Remove{in,out}bandRuleBy{name,tag} for pre_eval

Sebastien Blot 1 year ago
parent
commit
b01901b04e
3 changed files with 50 additions and 9 deletions
  1. 5 0
      pkg/waf/tx.go
  2. 32 0
      pkg/waf/waap.go
  3. 13 9
      pkg/waf/waf_helpers.go

+ 5 - 0
pkg/waf/tx.go

@@ -27,6 +27,11 @@ func (t *ExtendedTransaction) RemoveRuleByIDWithError(id int) error {
 	return nil
 }
 
+func (t *ExtendedTransaction) RemoveRuleByTagWithError(tag string) error {
+	t.Tx.RemoveRuleByTag(tag)
+	return nil
+}
+
 func (t *ExtendedTransaction) IsRuleEngineOff() bool {
 	return t.Tx.IsRuleEngineOff()
 }

+ 32 - 0
pkg/waf/waap.go

@@ -425,6 +425,38 @@ func (w *WaapRuntimeConfig) RemoveOutbandRuleByID(params ...any) (any, error) {
 	return nil, nil
 }
 
+// func (w *WaapRuntimeConfig) RemoveInbandRuleByTag(tag string) error {
+func (w *WaapRuntimeConfig) RemoveInbandRuleByTag(params ...any) (any, error) {
+	tag := params[0].(string)
+	w.Logger.Debugf("removing inband rule with tag %s", tag)
+	_ = w.InBandTx.RemoveRuleByTagWithError(tag)
+	return nil, nil
+}
+
+// func (w *WaapRuntimeConfig) RemoveOutbandRuleByTag(tag string) error {
+func (w *WaapRuntimeConfig) RemoveOutbandRuleByTag(params ...any) (any, error) {
+	tag := params[0].(string)
+	w.Logger.Debugf("removing outband rule with tag %s", tag)
+	_ = w.OutOfBandTx.RemoveRuleByTagWithError(tag)
+	return nil, nil
+}
+
+// func (w *WaapRuntimeConfig) RemoveInbandRuleByName(name string) error {
+func (w *WaapRuntimeConfig) RemoveInbandRuleByName(params ...any) (any, error) {
+	tag := fmt.Sprintf("crowdsec-%s", params[0].(string))
+	w.Logger.Debugf("removing inband rule %s", tag)
+	_ = w.InBandTx.RemoveRuleByTagWithError(tag)
+	return nil, nil
+}
+
+// func (w *WaapRuntimeConfig) RemoveOutbandRuleByName(name string) error {
+func (w *WaapRuntimeConfig) RemoveOutbandRuleByName(params ...any) (any, error) {
+	tag := fmt.Sprintf("crowdsec-%s", params[0].(string))
+	w.Logger.Debugf("removing outband rule %s", tag)
+	_ = w.OutOfBandTx.RemoveRuleByTagWithError(tag)
+	return nil, nil
+}
+
 func (w *WaapRuntimeConfig) CancelEvent(params ...any) (any, error) {
 	w.Logger.Debugf("canceling event")
 	w.Response.SendEvent = false

+ 13 - 9
pkg/waf/waf_helpers.go

@@ -23,9 +23,9 @@ func GetOnLoadEnv(w *WaapRuntimeConfig) map[string]interface{} {
 	//FIXME: use expr.Function instead of this
 	return map[string]interface{}{
 		"RemoveInBandRuleByID":    w.DisableInBandRuleByID,
-		"RemoveOutBandRuleByID":   w.DisableOutBandRuleByID,
-		"RemoveInBandRuleByName":  w.DisableInBandRuleByName,
 		"RemoveInBandRuleByTag":   w.DisableInBandRuleByTag,
+		"RemoveInBandRuleByName":  w.DisableInBandRuleByName,
+		"RemoveOutBandRuleByID":   w.DisableOutBandRuleByID,
 		"RemoveOutBandRuleByTag":  w.DisableOutBandRuleByTag,
 		"RemoveOutBandRuleByName": w.DisableOutBandRuleByName,
 		"SetRemediationByTag":     w.SetActionByTag,
@@ -37,13 +37,17 @@ func GetOnLoadEnv(w *WaapRuntimeConfig) map[string]interface{} {
 func GetPreEvalEnv(w *WaapRuntimeConfig, request *ParsedRequest) map[string]interface{} {
 	//FIXME: use expr.Function instead of this
 	return map[string]interface{}{
-		"IsInBand":              request.IsInBand,
-		"IsOutBand":             request.IsOutBand,
-		"RemoveInBandRuleByID":  w.RemoveInbandRuleByID,
-		"RemoveOutBandRuleByID": w.RemoveOutbandRuleByID,
-		"SetRemediationByTag":   w.SetActionByTag,
-		"SetRemediationByID":    w.SetActionByID,
-		"SetRemediationByName":  w.SetActionByName,
+		"IsInBand":                request.IsInBand,
+		"IsOutBand":               request.IsOutBand,
+		"RemoveInBandRuleByID":    w.RemoveInbandRuleByID,
+		"RemoveInBandRuleByName":  w.RemoveInbandRuleByName,
+		"RemoveInBandRuleByTag":   w.RemoveInbandRuleByTag,
+		"RemoveOutBandRuleByID":   w.RemoveOutbandRuleByID,
+		"RemoveOutBandRuleByTag":  w.RemoveOutbandRuleByTag,
+		"RemoveOutBandRuleByName": w.RemoveOutbandRuleByName,
+		"SetRemediationByTag":     w.SetActionByTag,
+		"SetRemediationByID":      w.SetActionByID,
+		"SetRemediationByName":    w.SetActionByName,
 	}
 }