Make WordPress Core

Ticket #28523: 28523.2.diff

File 28523.2.diff, 2.7 KB (added by rmccue, 9 years ago)

Add unit tests

  • src/wp-includes/functions.php

     
    30913091}
    30923092
    30933093/**
     3094 * Check that a JSONP callback is a valid JavaScript callback.
     3095 *
     3096 * This only allows alphanumeric characters and the dot character. This helps to
     3097 * mitigate XSS attacks caused by directly outputting user input.
     3098 *
     3099 * @since 4.5.0
     3100 * @param string $callback Supplied JSONP callback function.
     3101 * @return bool True if valid callback, false otherwise.
     3102 */
     3103function wp_check_jsonp_callback( $callback ) {
     3104        if ( ! is_string( $callback ) ) {
     3105                return false;
     3106        }
     3107
     3108        $jsonp_callback = preg_replace( '/[^\w\.]/', '', $callback, -1, $illegal_char_count );
     3109        return 0 === $illegal_char_count;
     3110}
     3111
     3112/**
    30943113 * Retrieve the WordPress home page URL.
    30953114 *
    30963115 * If the constant named 'WP_HOME' exists, then it will be used and returned
  • src/wp-includes/rest-api/class-wp-rest-server.php

     
    280280                                return false;
    281281                        }
    282282
    283                         // Check for invalid characters (only alphanumeric allowed).
    284                         if ( is_string( $_GET['_jsonp'] ) ) {
    285                                 $jsonp_callback = preg_replace( '/[^\w\.]/', '', wp_unslash( $_GET['_jsonp'] ), -1, $illegal_char_count );
    286                                 if ( 0 !== $illegal_char_count ) {
    287                                         $jsonp_callback = null;
    288                                 }
    289                         }
    290                         if ( null === $jsonp_callback ) {
     283                        $jsonp_callback = $_GET['_jsonp'];
     284                        if ( ! wp_check_jsonp_callback( $jsonp_callback ) ) {
    291285                                echo $this->json_error( 'rest_callback_invalid', __( 'The JSONP callback function is invalid.' ), 400 );
    292286                                return false;
    293287                        }
  • tests/phpunit/tests/rest-api.php

     
    322322
    323323        }
    324324
     325        public function jsonp_callback_provider() {
     326                return array(
     327                        // Standard names
     328                        array( 'Springfield', true ),
     329                        array( 'shelby.ville', true ),
     330                        array( 'cypress_creek', true ),
     331                        array( 'KampKrusty1', true ),
     332
     333                        // Invalid names
     334                        array( 'ogden-ville', false ),
     335                        array( 'north haverbrook', false ),
     336                        array( "Terror['Lake']", false ),
     337                        array( 'Cape[Feare]', false ),
     338                        array( '"NewHorrorfield"', false ),
     339                        array( 'Scream\\ville', false ),
     340                );
     341        }
     342
     343        /**
     344         * @dataProvider jsonp_callback_provider
     345         */
     346        public function test_jsonp_callback_check( $callback, $valid ) {
     347                $this->assertEquals( $valid, wp_check_jsonp_callback( $callback ) );
     348        }
     349
    325350}