WordPress.org

Make WordPress Core


Ignore:
Timestamp:
01/10/2019 09:05:50 PM (3 years ago)
Author:
flixos90
Message:

General: Fix problematic string to array parsing.

WordPress has historically often used code like preg_split( '/[\s,]+/', $var ) to parse a string of comma-separated values into an array. However, this approach was causing an empty string to not be parsed into an empty array as expected, but rather into an array with the empty string as its sole element.

This was among other areas causing problems in the REST API where passing an empty request parameter could cause that request to fail because, instead of it being ignored, that parameter would be compared against the valid values for it, which typically do not include an empty string.

Props david.binda, sstoqnov.
Fixes #43977.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/rest-api/rest-schema-validation.php

    r43571 r44546  
    121121        $this->assertTrue( rest_validate_value_from_schema( array( 1 ), $schema ) );
    122122        $this->assertWPError( rest_validate_value_from_schema( array( true ), $schema ) );
     123        $this->assertWPError( rest_validate_value_from_schema( null, $schema ) );
    123124    }
    124125
     
    146147        $this->assertTrue( rest_validate_value_from_schema( '1,2,3', $schema ) );
    147148        $this->assertWPError( rest_validate_value_from_schema( 'lol', $schema ) );
     149        $this->assertTrue( rest_validate_value_from_schema( '1,,', $schema ) );
     150        $this->assertTrue( rest_validate_value_from_schema( '', $schema ) );
    148151    }
    149152
     
    170173        $this->assertTrue( rest_validate_value_from_schema( 'ribs,chicken', $schema ) );
    171174        $this->assertWPError( rest_validate_value_from_schema( 'chicken,coleslaw', $schema ) );
     175        $this->assertTrue( rest_validate_value_from_schema( 'ribs,chicken,', $schema ) );
     176        $this->assertTrue( rest_validate_value_from_schema( '', $schema ) );
    172177    }
    173178
Note: See TracChangeset for help on using the changeset viewer.