Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

to_number : built-in function now rejects "Inf", "Infinity" and "NaN" values #7203

Merged

Conversation

sikehish
Copy link
Contributor

@sikehish sikehish commented Dec 2, 2024

closes #7200

Why the changes in this PR are needed?

The to_number built-in function in OPA currently allows inputs such as "Inf", "Infinity", and "NaN" because these are accepted by Go's strconv.ParseFloat function. However, these values are not valid JSON numbers and can cause errors during JSON marshaling or undefined behavior. This change ensures that to_number explicitly rejects these inputs, aligning with OPA's expected behavior for numeric conversions.

What are the changes in this PR?

  • Added validation to the to_number function to explicitly reject string inputs "Inf", "Infinity", and "NaN".
  • Updated the logic to raise a clear NewOperandTypeErr when these invalid strings are encountered.

@sikehish sikehish marked this pull request as draft December 2, 2024 18:20
topdown/casts.go Outdated
_, err := strconv.ParseFloat(string(a), 64)
strValue := string(a)

if strValue == "Inf" || strValue == "Infinity" || strValue == "NaN" {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From the docs linked in the issue:

ParseFloat recognizes the string "NaN", and the (possibly signed) strings "Inf" and "Infinity" as their respective special floating point values. It ignores case when matching.

So I think we'll need to match ignoring case here

Copy link
Contributor Author

@sikehish sikehish Dec 3, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My bad; didn't see the link there.I've made the change. Let me know if any other change needs to be made

@sikehish sikehish marked this pull request as ready for review December 3, 2024 04:37
Copy link
Member

@anderseknert anderseknert left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me!

If you could please squash your commits into one and sign that:

git commit --amend --signoff

And then push that, we'll have it merged. Thanks!

@anderseknert
Copy link
Member

(technically, just a signoff is enough for the DCO check, and I can squash when merging if you want.. but if you do it, you can author the commit message yourself)

@sikehish sikehish force-pushed the to_number_modification branch from 5e518be to 73e9509 Compare December 3, 2024 14:22
sikehish added a commit to sikehish/opa that referenced this pull request Dec 3, 2024
@sikehish sikehish force-pushed the to_number_modification branch from 73e9509 to 5c266d9 Compare December 3, 2024 14:26
@sikehish
Copy link
Contributor Author

sikehish commented Dec 3, 2024

(technically, just a signoff is enough for the DCO check, and I can squash when merging if you want.. but if you do it, you can author the commit message yourself)

Hi @anderseknert . I've squashed the commits.

@anderseknert anderseknert force-pushed the to_number_modification branch from 5c266d9 to cae5738 Compare December 3, 2024 14:29
@anderseknert anderseknert merged commit 03f6479 into open-policy-agent:main Dec 3, 2024
28 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

to_number built-in function should reject "Inf", "Infinity" and "NaN" values
2 participants