Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 39 additions & 36 deletions src/wp-includes/rest-api/class-wp-rest-server.php
Original file line number Diff line number Diff line change
Expand Up @@ -321,42 +321,6 @@ public function serve_request( $path = null ) {
* https://miki.it/blog/2014/7/8/abusing-jsonp-with-rosetta-flash/
*/
$this->send_header( 'X-Content-Type-Options', 'nosniff' );
$expose_headers = array( 'X-WP-Total', 'X-WP-TotalPages', 'Link' );

/**
* Filters the list of response headers that are exposed to REST API CORS requests.
*
* @since 5.5.0
*
* @param string[] $expose_headers The list of response headers to expose.
*/
$expose_headers = apply_filters( 'rest_exposed_cors_headers', $expose_headers );

$this->send_header( 'Access-Control-Expose-Headers', implode( ', ', $expose_headers ) );

$allow_headers = array(
'Authorization',
'X-WP-Nonce',
'Content-Disposition',
'Content-MD5',
'Content-Type',
);

/**
* Filters the list of request headers that are allowed for REST API CORS requests.
*
* The allowed headers are passed to the browser to specify which
* headers can be passed to the REST API. By default, we allow the
* Content-* headers needed to upload files to the media endpoints.
* As well as the Authorization and Nonce headers for allowing authentication.
*
* @since 5.5.0
*
* @param string[] $allow_headers The list of request headers to allow.
*/
$allow_headers = apply_filters( 'rest_allowed_cors_headers', $allow_headers );

$this->send_header( 'Access-Control-Allow-Headers', implode( ', ', $allow_headers ) );

/**
* Filters whether to send nocache headers on a REST API request.
Expand Down Expand Up @@ -436,6 +400,45 @@ public function serve_request( $path = null ) {
$request->set_method( $_SERVER['HTTP_X_HTTP_METHOD_OVERRIDE'] );
}

$expose_headers = array( 'X-WP-Total', 'X-WP-TotalPages', 'Link' );

/**
* Filters the list of response headers that are exposed to REST API CORS requests.
*
* @since 5.5.0
*
* @param string[] $expose_headers The list of response headers to expose.
* @param WP_REST_Request The request in context.
*/
$expose_headers = apply_filters( 'rest_exposed_cors_headers', $expose_headers, $request );

$this->send_header( 'Access-Control-Expose-Headers', implode( ', ', $expose_headers ) );

$allow_headers = array(
'Authorization',
'X-WP-Nonce',
'Content-Disposition',
'Content-MD5',
'Content-Type',
);

/**
* Filters the list of request headers that are allowed for REST API CORS requests.
*
* The allowed headers are passed to the browser to specify which
* headers can be passed to the REST API. By default, we allow the
* Content-* headers needed to upload files to the media endpoints.
* As well as the Authorization and Nonce headers for allowing authentication.
*
* @since 5.5.0
*
* @param string[] $allow_headers The list of request headers to allow.
* @param WP_REST_Request The request in context.
*/
$allow_headers = apply_filters( 'rest_allowed_cors_headers', $allow_headers, $request );

$this->send_header( 'Access-Control-Allow-Headers', implode( ', ', $allow_headers ) );

$result = $this->check_authentication();

if ( ! is_wp_error( $result ) ) {
Expand Down
30 changes: 30 additions & 0 deletions tests/phpunit/tests/rest-api/rest-server.php
Original file line number Diff line number Diff line change
Expand Up @@ -2200,6 +2200,36 @@ public function test_json_encode_error_results_in_500_status_code() {
$this->assertSame( 500, rest_get_server()->status );
}

/**
* @ticket 57752
*/
public function test_rest_exposed_cors_headers_filter_receives_request_object() {
$mock_hook = new MockAction();
add_filter( 'rest_exposed_cors_headers', array( $mock_hook, 'filter' ), 10, 2 );

rest_get_server()->serve_request( '/test-exposed-cors-headers' );

$this->assertCount( 1, $mock_hook->get_events() );
$this->assertCount( 2, $mock_hook->get_events()[0]['args'] );
$this->assertInstanceOf( 'WP_REST_Request', $mock_hook->get_events()[0]['args'][1] );
$this->assertSame( '/test-exposed-cors-headers', $mock_hook->get_events()[0]['args'][1]->get_route() );
}

/**
* @ticket 57752
*/
public function test_rest_allowed_cors_headers_filter_receives_request_object() {
$mock_hook = new MockAction();
add_filter( 'rest_allowed_cors_headers', array( $mock_hook, 'filter' ), 10, 2 );

rest_get_server()->serve_request( '/test-allowed-cors-headers' );

$this->assertCount( 1, $mock_hook->get_events() );
$this->assertCount( 2, $mock_hook->get_events()[0]['args'] );
$this->assertInstanceOf( 'WP_REST_Request', $mock_hook->get_events()[0]['args'][1] );
$this->assertSame( '/test-allowed-cors-headers', $mock_hook->get_events()[0]['args'][1]->get_route() );
}

public function _validate_as_integer_123( $value, $request, $key ) {
if ( ! is_int( $value ) ) {
return new WP_Error( 'some-error', 'This is not valid!' );
Expand Down