Repository navigation
Fix broken is_bool() check on boolean request args - #2630
rachelbaker wants to merge 2 commits into
Conversation
…with `rest_is_boolean` function
…ke strings to validate
Current coverage is 94.56% (diff: 100%)@@ develop #2630 diff @@
==========================================
Files 11 11
Lines 3611 3622 +11
Methods 172 172
Messages 0 0
Branches 0 0
==========================================
+ Hits 3414 3425 +11
Misses 197 197
Partials 0 0
|
|
@WP-API/amigos ##reviewmerge |
|
I think, we just moved the problem a bit further. If we do We will get what is not intended and finally in so basically, it would add the I think, if we loosen the I want to go these days through the plugin and see, what other booleans we have in the schemas, because we would need to go through all of them, right? |
|
I like this PR. Could we add test cases for each of the four strings, or is that not necessary? When using I would suggest strictly sanitizing the value to a true boolean, which can then be validated with
This is the main reason why I believe we would want a sanitization function to take loose values and conform them to a strict boolean value.
I think we would definitely want to do this. We also need to do this with arrays at some point. |
|
I think should be a sanitization transformation on the property rather than validity. Having it so would also address @websupporter concern about 'false' still being in the request object's data. (string) "false" is a quirk of URL params, so the sooner we can make it a "real" boolean the better. So the flow would be:
|
Right now we don't have any sanitization, and when we do add it the sanitization would run after the validation. I agree, we should correctly set the type in the sanitization function. However, if we ONLY sanitize the value folks can end up with unexpected results due to how booleans are cast: http://php.net/manual/en/function.boolval.php I think we should do both. |
|
@websupporter @BE-Webdesign I agree we need to sanitize the value as well, filed in #2633. I originally was hoping to keep that a separate (while still VERY related) issue. That does not sound like it is possible due to the inconsistencies of our conditional checks within the different endpoints. |
|
@joehoyle |
|
@websupporter ahh I hadn't noticed that - I added a comment to https://core.trac.wordpress.org/ticket/37192 with my thoughts. Personally I think that was a mistake, but further discussion on that should probably go on https://core.trac.wordpress.org/ticket/37192 |
|
Should we merge this when sanitize PR is ready? |
|
@rachelbaker do you think we should support |
|
|
||
| if ( in_array( $maybe_bool, $valid_boolean_values, true ) ) { | ||
| return true; | ||
| } |
There was a problem hiding this comment.
This should just return in_array( ... since we don't want to fall-through.
|
Labeled |
|
This was added in #2704. |
Fixes #2616
In
rest_validate_request_arg()we cannot use the! is_bool()conditional to validate if a request argument is either true or false. The query string in the url (example: https://demo.wp-api.org/wp-json/wp/v2/categories?hide_empty=true) will be parsed as a string. Which means you will always get the error:This PR introduces the new function
rest_is_booleanthat will allow us to validate boolean strings in API request parameters. It also changes the conditional in rest_validate_request_args() to userest_is_booleanfor a much less strict (and accurate) validation of boolean-type parameters.