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
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
90 changes: 90 additions & 0 deletions test/cases/testdata/v1/casts/test-casts-0828.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
---
cases:
- note: "casts/to_number: nan input"
query: data.generated.p = b
modules:
- |
package generated

p if {
to_number("nan", b)
}
want_error_code: eval_type_error
strict_error: true
- note: "casts/to_number: inf input"
query: data.generated.p = b
modules:
- |
package generated

p if {
to_number("inf", b)
}
want_error_code: eval_type_error
strict_error: true
- note: "casts/to_number: -inf input"
query: data.generated.p = b
modules:
- |
package generated

p if {
to_number("-inf", b)
}
want_error_code: eval_type_error
strict_error: true
- note: "casts/to_number: Infinity input"
query: data.generated.p = b
modules:
- |
package generated

p if {
to_number("Infinity", b)
}
want_error_code: eval_type_error
strict_error: true
- note: "casts/to_number: -Infinity input"
query: data.generated.p = b
modules:
- |
package generated

p if {
to_number("-Infinity", b)
}
want_error_code: eval_type_error
strict_error: true
- note: "casts/to_number: -nan input"
query: data.generated.p = b
modules:
- |
package generated

p if {
to_number("-nan", b)
}
want_error_code: eval_type_error
strict_error: true
- note: "casts/to_number: -NaN input"
query: data.generated.p = b
modules:
- |
package generated

p if {
to_number("-NaN", b)
}
want_error_code: eval_type_error
strict_error: true
- note: "casts/to_number: iNf input"
query: data.generated.p = b
modules:
- |
package generated

p if {
to_number("iNf", b)
}
want_error_code: eval_type_error
strict_error: true
12 changes: 11 additions & 1 deletion topdown/casts.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package topdown

import (
"strconv"
"strings"

"github.com/open-policy-agent/opa/ast"
"github.com/open-policy-agent/opa/topdown/builtins"
Expand All @@ -23,7 +24,16 @@ func builtinToNumber(_ BuiltinContext, operands []*ast.Term, iter func(*ast.Term
case ast.Number:
return iter(ast.NewTerm(a))
case ast.String:
_, err := strconv.ParseFloat(string(a), 64)
strValue := string(a)

trimmedVal := strings.TrimLeft(strValue, "+-")
lowerCaseVal := strings.ToLower(trimmedVal)

if lowerCaseVal == "inf" || lowerCaseVal == "infinity" || lowerCaseVal == "nan" {
return builtins.NewOperandTypeErr(1, operands[0].Value, "valid number string")
}

_, err := strconv.ParseFloat(strValue, 64)
if err != nil {
return err
}
Expand Down