open-policy-agent / opa

Open Policy Agent (OPA) is an open source, general-purpose policy engine.
https://www.openpolicyagent.org
Apache License 2.0
9.76k stars 1.36k forks source link

`to_number` built-in function should reject "Inf", "Infinity" and "NaN" values #7200

Closed anderseknert closed 7 hours ago

anderseknert commented 1 day ago

While providing these will omit errors, these errors are from later JSON marshalling and not from input validation. Compare:

opa eval -f pretty --strict-builtin-errors 'to_number("Infinite")'
1 error occurred: to_number("Infinite"): eval_builtin_error: to_number: strconv.ParseFloat: parsing "Infinite": invalid syntax
opa eval -f pretty --strict-builtin-errors 'to_number("Infinity")'
json: invalid number literal "Infinity"

This since the strcov.ParseFloat function accepts these values, but OPA obviously should not.

sikehish commented 1 day ago

I'm looking into this issue :) Could you assign this to me?

sikehish commented 1 day ago

While providing these will omit errors, these errors are from later JSON marshalling and not from input validation. Compare:

opa eval -f pretty --strict-builtin-errors 'to_number("Infinite")'
1 error occurred: to_number("Infinite"): eval_builtin_error: to_number: strconv.ParseFloat: parsing "Infinite": invalid syntax
opa eval -f pretty --strict-builtin-errors 'to_number("Infinity")'
json: invalid number literal "Infinity"

This since the strcov.ParseFloat function accepts these values, but OPA obviously should not.

Hi @anderseknert . I've made the required changes. I just wanted to know where the test cases have to be added in this case(if that's required, as I've manually tested for all three inputs: "Inf", "Infinity" and "NaN").

anderseknert commented 1 day ago

Hi there!

There was a PR open on that already, but unfortunately it doesn't seem like it was linked https://github.com/open-policy-agent/opa/pull/7201 ... and also that there's quite some work left to do there.

@raj921 , do you want to keep going there?

As for test cases (no matter who adds them, the preferred way is to add "YAML tests" under test/cases/testdata/v1/casts ... you can look at some existing tests to see what they look like (you can also add attributes to test for errors returned).

The comment in this file contains some information for how to run those tests isolated (if not as part of the whole make test suite).

Sorry for having snoozed for a while, it's evening here and 2 kids are demanding my attention 😅

raj921 commented 1 day ago

no sir I will try another issue

anderseknert commented 1 day ago

Alright, I'll keep that in mind, and can assign you to one when I find one good to start with 👍 Thanks a lot!

anderseknert commented 1 day ago

@sikehish you can proceed with adding some tests then as previously mentioned. Let me know if you have any questions!

sikehish commented 17 hours ago

@sikehish you can proceed with adding some tests then as previously mentioned. Let me know if you have any questions!

Do I need to create a new file for it or can I make changes in any of the existing test case yml file?

anderseknert commented 15 hours ago

I'd probably create a new file and name it something appropriate, but either way is fine. Don't mind the naming or the contents of the old files too much. They were generated from code long ago, and new tests don't need to follow them.

sikehish commented 15 hours ago

I'd probably create a new file and name it something appropriate, but either way is fine. Don't mind the naming or the contents of the old files too much. They were generated from code long ago, and new tests don't need to follow them.

Cool. Also is there a way to generate the tests or do I have to write it manually?

anderseknert commented 15 hours ago

The YAML file? You'd write that yourself. But you can obviously copy the structure from other files.

sikehish commented 15 hours ago

The YAML file? You'd write that yourself. But you can obviously copy the structure from other files.

Yup, I was referring to the YAML file. The yml file names looked like they were generated so was a bit curious xD.

sikehish commented 7 hours ago

Thanks for the opportunity @anderseknert !

anderseknert commented 7 hours ago

Thanks for the contribution!