From e7399111b45e1de309cb5dc872b46836bf5ab175 Mon Sep 17 00:00:00 2001 From: Timothy Jacobs Date: Mon, 10 Feb 2020 16:06:58 +0000 Subject: [PATCH] REST API: Add support for the REDIRECT_HTTP_AUTHORIZATION header. Previously the REST API did not account for server configurations where the Authorization header must be added using ModRewrite. This caused major DUX issues when trying to use custom authentication mechanisms. Fixes #47077. Props dshanske, cklosows. git-svn-id: https://develop.svn.wordpress.org/trunk@47239 602fd350-edb4-49c9-b593-d223f7449a82 --- .../rest-api/class-wp-rest-server.php | 6 ++ tests/phpunit/tests/rest-api/rest-server.php | 58 +++++++++++++++++++ 2 files changed, 64 insertions(+) diff --git a/src/wp-includes/rest-api/class-wp-rest-server.php b/src/wp-includes/rest-api/class-wp-rest-server.php index d11318916e..acf23f4dda 100644 --- a/src/wp-includes/rest-api/class-wp-rest-server.php +++ b/src/wp-includes/rest-api/class-wp-rest-server.php @@ -1380,6 +1380,12 @@ class WP_REST_Server { foreach ( $server as $key => $value ) { if ( strpos( $key, 'HTTP_' ) === 0 ) { $headers[ substr( $key, 5 ) ] = $value; + } elseif ( 'REDIRECT_HTTP_AUTHORIZATION' === $key && empty( $server['HTTP_AUTHORIZATION'] ) ) { + /* + * In some server configurations, the authorization header is passed in this alternate location. + * Since it would not be passed in in both places we do not check for both headers and resolve. + */ + $headers['AUTHORIZATION'] = $value; } elseif ( isset( $additional[ $key ] ) ) { $headers[ $key ] = $value; } diff --git a/tests/phpunit/tests/rest-api/rest-server.php b/tests/phpunit/tests/rest-api/rest-server.php index 4d450f7e17..abfdefcd0b 100644 --- a/tests/phpunit/tests/rest-api/rest-server.php +++ b/tests/phpunit/tests/rest-api/rest-server.php @@ -1373,6 +1373,64 @@ class Tests_REST_Server extends WP_Test_REST_TestCase { $this->assertEquals( '', rest_get_server()->sent_body ); } + /** + * @ticket 47077 + */ + public function test_http_authorization_header_substitution() { + $headers = array( 'HTTP_AUTHORIZATION' => 'foo' ); + $parsed_headers = rest_get_server()->get_headers( $headers ); + + $this->assertSame( + array( 'AUTHORIZATION' => 'foo' ), + $parsed_headers + ); + } + + /** + * @ticket 47077 + */ + public function test_redirect_http_authorization_header_substitution() { + $headers = array( 'REDIRECT_HTTP_AUTHORIZATION' => 'foo' ); + $parsed_headers = rest_get_server()->get_headers( $headers ); + + $this->assertSame( + array( 'AUTHORIZATION' => 'foo' ), + $parsed_headers + ); + } + + /** + * @ticket 47077 + */ + public function test_redirect_http_authorization_with_http_authorization_header_substitution() { + $headers = array( + 'HTTP_AUTHORIZATION' => 'foo', + 'REDIRECT_HTTP_AUTHORIZATION' => 'bar', + ); + $parsed_headers = rest_get_server()->get_headers( $headers ); + + $this->assertSame( + array( 'AUTHORIZATION' => 'foo' ), + $parsed_headers + ); + } + + /** + * @ticket 47077 + */ + public function test_redirect_http_authorization_with_empty_http_authorization_header_substitution() { + $headers = array( + 'HTTP_AUTHORIZATION' => '', + 'REDIRECT_HTTP_AUTHORIZATION' => 'bar', + ); + $parsed_headers = rest_get_server()->get_headers( $headers ); + + $this->assertSame( + array( 'AUTHORIZATION' => 'bar' ), + $parsed_headers + ); + } + public function _validate_as_integer_123( $value, $request, $key ) { if ( ! is_int( $value ) ) { return new WP_Error( 'some-error', 'This is not valid!' );