PaesslerAG / gval

Expression evaluation in golang
BSD 3-Clause "New" or "Revised" License
713 stars 83 forks source link

Evaluating strings with backslashes raises parsing errors #89

Open tjerman opened 1 year ago

tjerman commented 1 year ago

Env

This is on github.com/PaesslerAG/gval v1.2.1

Issue

We have a custom handler for regular expressions (match(value, regex)) where the regex can be configured from the front-end (web application).

For example, inputting match(variable, "[a-z]\d") would be presented as (when dumping the value) "match(variable, \"[a-z]\\d\")" (which seems correct to me), but when passed to gval, it raises parsing error: match(variable, "[a-z]\d") :1:11 - 1:20 could not parse string: invalid syntax.

If I bypass all of the surrounding code and run it directly as so x, err := gval.Evaluate("match(variable, \"[a-z]\\d\")", map[string]any{"a": "b"}) the error is the same.

Another example would be running this x, err := gval.Evaluate("\"\\\"", map[string]any{"a": "b"}) -- same error in regards to the invalid string (parsing error: "\" :1:1 - 1:4 could not parse string: invalid syntax).

But then, if I do this x, err := gval.Evaluate("\"apple\\banana\"", map[string]any{"a": "b"}), it passes and outputs (when running spew.Dump(x, err))

(string) (len=11) "apple\banana"
(interface {}) <nil>

Conclusion

Am I doing something wrong with my strings, or is this a (un)intentional edge case?

If this needs more investigation I can take a look through the code and propose a fix as well, but I'd appreciate some notes on where would be a good place to start/your suspicions about what's wrong

generikvault commented 1 year ago

gval handles strings exactly as go. So you need to write the regex string like this match(variable, "[a-z]\\d") or put the regex in a variable.