Make WordPress Core

Changeset 48437


Ignore:
Timestamp:
07/11/2020 08:32:19 PM (5 years ago)
Author:
TimothyBlynJacobs
Message:

REST API: Sanitize block renderer attributes.

In [48069] the Block Renderer was changed to register a single route for all dynamic blocks. Validation was dynamically applied based on the requested block, but sanitization was not. This commit adds the same sanitization back to the block attributes.

Props manooweb.
Fixes #50620. See #48079.

Location:
trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/package-lock.json

    r48434 r48437  
    59405940                "kind-of": {
    59415941                    "version": "6.0.2",
    5942                     "resolved": "",
     5942                    "resolved": "https://registry.npmjs.org/kind-of/-/kind-of-6.0.2.tgz",
     5943                    "integrity": "sha512-s5kLOcnH0XqDO+FvuaLX8DDjZ18CGFk7VygH40QoKPUQhW4e2rvM0rwUq0t8IQDOwYSeLK01U90OjzBTme2QqA==",
    59435944                    "dev": true
    59445945                }
     
    1056710568                "kind-of": {
    1056810569                    "version": "6.0.2",
    10569                     "resolved": "",
     10570                    "resolved": "https://registry.npmjs.org/kind-of/-/kind-of-6.0.2.tgz",
     10571                    "integrity": "sha512-s5kLOcnH0XqDO+FvuaLX8DDjZ18CGFk7VygH40QoKPUQhW4e2rvM0rwUq0t8IQDOwYSeLK01U90OjzBTme2QqA==",
    1057010572                    "dev": true
    1057110573                }
     
    2358523587                "kind-of": {
    2358623588                    "version": "6.0.2",
    23587                     "resolved": "",
     23589                    "resolved": "https://registry.npmjs.org/kind-of/-/kind-of-6.0.2.tgz",
     23590                    "integrity": "sha512-s5kLOcnH0XqDO+FvuaLX8DDjZ18CGFk7VygH40QoKPUQhW4e2rvM0rwUq0t8IQDOwYSeLK01U90OjzBTme2QqA==",
    2358823591                    "dev": true
    2358923592                }
  • trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-block-renderer-controller.php

    r48069 r48437  
    7171                                return rest_validate_value_from_schema( $value, $schema );
    7272                            },
     73                            'sanitize_callback' => static function ( $value, $request ) {
     74                                $block = WP_Block_Type_Registry::get_instance()->get_registered( $request['name'] );
     75
     76                                if ( ! $block ) {
     77                                    // This will get rejected in ::get_item().
     78                                    return true;
     79                                }
     80
     81                                $schema = array(
     82                                    'type'                 => 'object',
     83                                    'properties'           => $block->get_attributes(),
     84                                    'additionalProperties' => false,
     85                                );
     86
     87                                return rest_sanitize_value_from_schema( $value, $schema );
     88                            },
    7389                        ),
    7490                        'post_id'    => array(
  • trunk/tests/phpunit/tests/rest-api/rest-block-renderer-controller.php

    r48118 r48437  
    5757
    5858    /**
     59     * Dynamic block with boolean attributes block name.
     60     *
     61     * @since 5.5.0
     62     *
     63     * @var string
     64     */
     65    protected static $dynamic_block_with_boolean_attributes_block_name = 'core/dynamic-block-with-boolean-attributes';
     66
     67    /**
    5968     * Test API user's ID.
    6069     *
     
    128137        $this->register_post_context_test_block();
    129138        $this->register_non_dynamic_block();
     139        $this->register_dynamic_block_with_boolean_attributes();
    130140        parent::setUp();
    131141    }
     
    140150        WP_Block_Type_Registry::get_instance()->unregister( self::$context_block_name );
    141151        WP_Block_Type_Registry::get_instance()->unregister( self::$non_dynamic_block_name );
     152        WP_Block_Type_Registry::get_instance()->unregister( self::$dynamic_block_with_boolean_attributes_block_name );
    142153        parent::tearDown();
    143154    }
     
    197208
    198209    /**
     210     * Registers the dynamic with boolean attributes block name.
     211     *
     212     * @since 5.5.0
     213     */
     214    protected function register_dynamic_block_with_boolean_attributes() {
     215        register_block_type(
     216            self::$dynamic_block_with_boolean_attributes_block_name,
     217            array(
     218                'attributes'      => array(
     219                    'boolean_true_attribute'  => array(
     220                        'type'    => 'boolean',
     221                        'default' => true,
     222                    ),
     223                    'boolean_false_attribute' => array(
     224                        'type'    => 'boolean',
     225                        'default' => false,
     226                    ),
     227                ),
     228                'render_callback' => array( $this, 'render_test_block' ),
     229            )
     230        );
     231    }
     232
     233    /**
    199234     * Test render callback.
    200235     *
     
    524559
    525560    /**
     561     * @ticket 50620
     562     */
     563    public function test_get_sanitized_attributes_for_dynamic_block_with_boolean_attributes() {
     564        wp_set_current_user( self::$user_id );
     565
     566        $request = new WP_REST_Request( 'GET', self::$rest_api_route . self::$dynamic_block_with_boolean_attributes_block_name );
     567
     568        $attributes = array(
     569            'boolean_true_attribute'  => 'true',
     570            'boolean_false_attribute' => 'false',
     571        );
     572
     573        $expected = array(
     574            'boolean_true_attribute'  => true,
     575            'boolean_false_attribute' => false,
     576        );
     577
     578        $request->set_param( 'context', 'edit' );
     579        $request->set_param( 'attributes', $attributes );
     580        $response = rest_get_server()->dispatch( $request );
     581        $this->assertEquals( 200, $response->get_status() );
     582        $data = $response->get_data();
     583
     584        $this->assertSame( $expected, json_decode( $data['rendered'], true ) );
     585    }
     586
     587    /**
    526588     * Get item schema.
    527589     *
Note: See TracChangeset for help on using the changeset viewer.