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

Inconsistent runtime type checks in *.is_valid functions #4760

Closed
anderseknert opened this issue Jun 9, 2022 · 6 comments · Fixed by #4844
Closed

Inconsistent runtime type checks in *.is_valid functions #4760

anderseknert opened this issue Jun 9, 2022 · 6 comments · Fixed by #4844
Labels

Comments

@anderseknert
Copy link
Member

IIRC, @philipaconrad mentioned this when working on the GraphQL built-in functions — creating an issue for completness sake.

package play

jv := json.is_valid(input.x)
yv := yaml.is_valid(input.x)
sv := semver.is_valid(input.x)
rv := regex.is_valid(input.x)
bv := base64.is_valid(input.x)
gv := graphql.is_valid(input.x, input.x)

With input as {"x": 5} (which is an invalid type for all of the functions) this package evaluates to:

{
    "rv": false,
    "sv": false
}

The documentation reports all of the above to return a boolean result, and while it also declares the required type of the args top be of type string, it's not documented what to expect back if that isn't the case.

I think we should either:

  1. Be consistent and "return" undefined on other argument types in all of the above built-in functions
  2. Be consistent and return false on other argument types in all of the above built-in functions
  3. Document behavior for all of the above built-in functions when other argument type is provided

I personally like option 2, as I'd expect a function named "is_valid" to return false on any invalid input, but I'd be happy with any of the above.

@philipaconrad
Copy link
Contributor

Thanks for filing this issue, @anderseknert!

My vote is for for a combination of 2 and 3: we ensure consistent behavior everywhere, and then document that behavior.

@anderseknert
Copy link
Member Author

Changing this would be a "breaking" change to some extent, but I definitely doubt there's people relying on the difference. I agree with you though @philipaconrad — I do think that the docs are fine as they are if we go with this, as they're all documented as returning boolean?

@philipaconrad
Copy link
Contributor

So, if I understand this correctly, the only change that needs to occur is that someone will need to sweep through the builtins and ensure that 2 (returning false consistently) is implemented across all the *.is_valid builtins? That seems quite doable. 😄

@anderseknert
Copy link
Member Author

Sounds about right, @philipaconrad 👍

@philipaconrad
Copy link
Contributor

philipaconrad commented Jul 5, 2022

The full set of *.is_valid builtins appears to be (as of v0.42.0):

Encoding:

  • base64.is_valid
  • json.is_valid
  • yaml.is_valid

GraphQL:

  • graphql.is_valid

Regex:

  • regex.is_valid

Semver:

  • semver.is_valid

Questionable additions to the list:

  • io.jwt.verifyX functions?

@philipaconrad
Copy link
Contributor

philipaconrad commented Jul 5, 2022

@anderseknert This actually brings up an interesting issue: Internally, the wrappers we use around the encoding-related builtins are deprecated, and different than the newer builtins. I think there's an opportunity for a refactoring here! 😃

philipaconrad added a commit to philipaconrad/opa that referenced this issue Jul 6, 2022
This commit updates the `*.is_valid` functions to no longer produce
errors when providing wrong-typed arguments. Instead, they will now
return true/false for all inputs. Tests and WASM versions of these
builtins have been updated to match the new behavior.

Fixes open-policy-agent#4760.

Signed-off-by: Philip Conrad <philipaconrad@gmail.com>
philipaconrad added a commit that referenced this issue Jul 6, 2022
This commit updates the `*.is_valid` functions to no longer produce
errors when providing wrong-typed arguments. Instead, they will now
return true/false for all inputs. Tests and WASM versions of these
builtins have been updated to match the new behavior.

Fixes #4760.

Signed-off-by: Philip Conrad <philipaconrad@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants