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

Only allow custom meta of types string, number and boolean #2850

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 10 additions & 0 deletions lib/fields/class-wp-rest-meta-fields.php
Expand Up @@ -286,6 +286,12 @@ protected function get_registered_fields() {
continue;
}

// Whitelist the supported types for types, as we don't want invalid types
// to be updated with arbitrary values that we can't do decent sanitizing for.
if ( ! in_array( $args['type'], array( 'number', 'string', 'boolean' ), true ) ) {
continue;
}

if ( $rest_args['single'] ) {
$rest_args['schema']['type'] = $args['type'];
} else {
Expand All @@ -294,6 +300,10 @@ protected function get_registered_fields() {
'type' => $args['type'],
);
}
} else {
if ( ! in_array( $rest_args['schema']['type'], array( 'number', 'string', 'boolean' ), true ) ) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

elseif?

continue;
}
}

$registered[ $rest_args['name'] ] = $rest_args;
Expand Down
11 changes: 10 additions & 1 deletion tests/test-rest-post-meta-fields.php
Expand Up @@ -43,9 +43,17 @@ public function setUp() {
));
register_meta( 'post', 'test_invalid_type', array(
'single' => true,
'type' => false,
'type' => 'mycomplexobject',
'show_in_rest' => true,
));
register_meta( 'post', 'test_invalid_type_in_schema', array(
'single' => true,
'show_in_rest' => array(
'schema' => array(
'type' => 'mycomplexobject',
),
),
));

/** @var WP_REST_Server $wp_rest_server */
global $wp_rest_server;
Expand Down Expand Up @@ -607,6 +615,7 @@ public function test_get_schema() {
$this->assertArrayNotHasKey( 'test_no_rest', $meta_schema );
$this->assertArrayNotHasKey( 'test_rest_disabled', $meta_schema );
$this->assertArrayNotHasKey( 'test_invalid_type', $meta_schema );
$this->assertArrayNotHasKey( 'test_invalid_type_in_schema', $meta_schema );
}

/**
Expand Down