Skip to content

Commit

Permalink
Bug Fix: The value in the setvar should be able to start with - or +. (
Browse files Browse the repository at this point in the history
…#1125)

* Fix setvar to take the value as ---test

* Resolve comment
  • Loading branch information
soujanyanmbri authored Sep 27, 2024
1 parent b7e5857 commit 3904ccc
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 6 deletions.
19 changes: 13 additions & 6 deletions internal/actions/setvar.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Expand Down
12 changes: 12 additions & 0 deletions internal/actions/setvar_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 3904ccc

Please sign in to comment.