Skip to content
This repository was archived by the owner on Sep 24, 2018. It is now read-only.

Commit d67dd84

Browse files
committed
Adds boolean validation/sanitization to REST API
This adds relatively robust validation/sanitization for the REST API. Boolean values can be translated from booleans, strings, and integers.
1 parent c625a49 commit d67dd84

2 files changed

Lines changed: 109 additions & 5 deletions

File tree

plugin.php

Lines changed: 60 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -299,8 +299,8 @@ function rest_validate_request_arg( $value, $request, $param ) {
299299
return new WP_Error( 'rest_invalid_param', sprintf( __( '%s is not of type %s' ), $param, 'integer' ) );
300300
}
301301

302-
if ( 'boolean' === $args['type'] && ! is_bool( $value ) ) {
303-
return new WP_Error( 'rest_invalid_param', sprintf( __( '%s is not of type %s' ), $param, 'boolean' ) );
302+
if ( 'boolean' === $args['type'] && ! rest_is_boolean( $value ) ) {
303+
return new WP_Error( 'rest_invalid_param', sprintf( __( '%s is not of type %s' ), $value, 'boolean' ) );
304304
}
305305

306306
if ( 'string' === $args['type'] && ! is_string( $value ) ) {
@@ -387,6 +387,10 @@ function rest_sanitize_request_arg( $value, $request, $param ) {
387387
return (int) $value;
388388
}
389389

390+
if ( 'boolean' === $args['type'] ) {
391+
return rest_sanitize_boolean( $value );
392+
}
393+
390394
if ( isset( $args['format'] ) ) {
391395
switch ( $args['format'] ) {
392396
case 'date-time' :
@@ -408,7 +412,6 @@ function rest_sanitize_request_arg( $value, $request, $param ) {
408412

409413
return $value;
410414
}
411-
412415
}
413416

414417
if ( ! function_exists( 'rest_is_ip_address' ) ) {
@@ -430,3 +433,57 @@ function rest_is_ip_address( $ipv4 ) {
430433
return $ipv4;
431434
}
432435
}
436+
437+
/**
438+
* Changes a boolean-like value into the proper boolean value.
439+
*
440+
* @param bool|string|int $value The value being evaluated.
441+
* @return boolean Returns the proper associated boolean value.
442+
*/
443+
if ( ! function_exists( 'rest_sanitize_boolean' ) ) {
444+
function rest_sanitize_boolean( $value ) {
445+
// String values are translated to `true`; make sure 'false' is false.
446+
if ( is_string( $value ) ) {
447+
$value = strtolower( $value );
448+
if ( in_array( $value, array( 'false', '0' ), true ) ) {
449+
$value = false;
450+
}
451+
}
452+
453+
// Everything else will map nicely to boolean.
454+
return (boolean) $value;
455+
}
456+
}
457+
458+
/**
459+
* Determines if a given value is boolean-like.
460+
*
461+
* @param bool|string $maybe_bool The value being evaluated.
462+
* @return boolean True if a boolean, otherwise false.
463+
*/
464+
if ( ! function_exists( 'rest_is_boolean' ) ) {
465+
function rest_is_boolean( $maybe_bool ) {
466+
if ( is_bool( $maybe_bool ) ) {
467+
return true;
468+
}
469+
470+
if ( is_string( $maybe_bool ) ) {
471+
$maybe_bool = strtolower( $maybe_bool );
472+
473+
$valid_boolean_values = array(
474+
'false',
475+
'true',
476+
'0',
477+
'1',
478+
);
479+
480+
return in_array( $maybe_bool, $valid_boolean_values, true );
481+
}
482+
483+
if ( is_int( $maybe_bool ) ) {
484+
return in_array( $maybe_bool, array( 0, 1 ), true );
485+
}
486+
487+
return false;
488+
}
489+
}

tests/test-rest-controller.php

Lines changed: 49 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,57 @@ public function test_validate_schema_type_boolean() {
5252
rest_validate_request_arg( false, $this->request, 'someboolean' )
5353
);
5454

55-
$this->assertErrorResponse(
56-
'rest_invalid_param',
55+
$this->assertTrue(
5756
rest_validate_request_arg( 'true', $this->request, 'someboolean' )
5857
);
58+
$this->assertTrue(
59+
rest_validate_request_arg( 'TRUE', $this->request, 'someboolean' )
60+
);
61+
$this->assertTrue(
62+
rest_validate_request_arg( 'false', $this->request, 'someboolean' )
63+
);
64+
$this->assertTrue(
65+
rest_validate_request_arg( 'False', $this->request, 'someboolean' )
66+
);
67+
$this->assertTrue(
68+
rest_validate_request_arg( '1', $this->request, 'someboolean' )
69+
);
70+
$this->assertTrue(
71+
rest_validate_request_arg( '0', $this->request, 'someboolean' )
72+
);
73+
$this->assertTrue(
74+
rest_validate_request_arg( 1, $this->request, 'someboolean' )
75+
);
76+
$this->assertTrue(
77+
rest_validate_request_arg( 0, $this->request, 'someboolean' )
78+
);
79+
80+
// Check sanitize testing.
81+
$this->assertEquals( false,
82+
rest_sanitize_request_arg( 'false', $this->request, 'someboolean' )
83+
);
84+
$this->assertEquals( false,
85+
rest_sanitize_request_arg( '0', $this->request, 'someboolean' )
86+
);
87+
$this->assertEquals( false,
88+
rest_sanitize_request_arg( 0, $this->request, 'someboolean' )
89+
);
90+
$this->assertEquals( false,
91+
rest_sanitize_request_arg( 'FALSE', $this->request, 'someboolean' )
92+
);
93+
$this->assertEquals( true,
94+
rest_sanitize_request_arg( 'true', $this->request, 'someboolean' )
95+
);
96+
$this->assertEquals( true,
97+
rest_sanitize_request_arg( '1', $this->request, 'someboolean' )
98+
);
99+
$this->assertEquals( true,
100+
rest_sanitize_request_arg( 1, $this->request, 'someboolean' )
101+
);
102+
$this->assertEquals( true,
103+
rest_sanitize_request_arg( 'TRUE', $this->request, 'someboolean' )
104+
);
105+
59106
$this->assertErrorResponse(
60107
'rest_invalid_param',
61108
rest_validate_request_arg( '123', $this->request, 'someboolean' )

0 commit comments

Comments
 (0)