diff --git a/internal/actions/setvar.go b/internal/actions/setvar.go index 2c363520..39992177 100644 --- a/internal/actions/setvar.go +++ b/internal/actions/setvar.go @@ -145,16 +145,23 @@ func (a *setvarFn) evaluateTxCollection(r plugintypes.RuleMetadata, tx plugintyp case len(value) == 0: // if nothing to input col.Set(key, []string{""}) - case value[0] == '+', value[0] == '-': // Increment or decrement, arithmetical operations + // Check if this could be an arithemetic operation. If it is followed by a number, it will be treated as an arithmetic operation. Otherwise, it will be treated as a string. + case value[0] == '+', value[0] == '-': val := 0 if len(value) > 1 { val, err = strconv.Atoi(value[1:]) if err != nil { - tx.DebugLogger().Error(). - Str("var_value", value). - Int("rule_id", r.ID()). - Err(err). - Msg("Invalid value") + // If the variable doesn't exist, we would need to raise an error. Otherwise, it should be the same value. + if strings.HasPrefix(value[1:], "tx.") { + tx.DebugLogger().Error(). + Str("var_value", value). + Int("rule_id", r.ID()). + Err(err). + Msg(value) + return + } + + col.Set(key, []string{value}) return } } diff --git a/internal/actions/setvar_test.go b/internal/actions/setvar_test.go index 591d5a85..48966e81 100644 --- a/internal/actions/setvar_test.go +++ b/internal/actions/setvar_test.go @@ -96,6 +96,18 @@ func TestSetvarEvaluate(t *testing.T) { init: "TX.newvar=-%{tx.missingvar}", expectInvalidSyntaxError: true, }, + { + name: "Non Numerical Operation - If the value starts with -", + init: "TX.newvar=----expected_value", + expectInvalidSyntaxError: false, + expectNewVarValue: "----expected_value", + }, + { + name: "Non Numerical Operation - If the value starts with +", + init: "TX.newvar=+++expected_value", + expectInvalidSyntaxError: false, + expectNewVarValue: "+++expected_value", + }, } for _, tt := range tests {