From 61803cad69bef7ac1e0ba682df504a028b521d8d Mon Sep 17 00:00:00 2001 From: Dovid Levine Date: Fri, 20 Nov 2026 14:56:27 +0200 Subject: [PATCH 1/2] fix(rest): keep REST error bodies on VIP's edge --- inc/Modules/Rest/Abstract_REST_Controller.php | 25 ++++ inc/Modules/Rest/Shared_Sites_Controller.php | 12 +- .../Rest/Abstract_REST_ControllerTest.php | 129 ++++++++++++++++++ 3 files changed, 155 insertions(+), 11 deletions(-) create mode 100644 tests/phpunit/Integration/Modules/Rest/Abstract_REST_ControllerTest.php diff --git a/inc/Modules/Rest/Abstract_REST_Controller.php b/inc/Modules/Rest/Abstract_REST_Controller.php index ee47fb2e..598a38ef 100644 --- a/inc/Modules/Rest/Abstract_REST_Controller.php +++ b/inc/Modules/Rest/Abstract_REST_Controller.php @@ -39,6 +39,7 @@ abstract class Abstract_REST_Controller extends WP_REST_Controller implements Re */ public function register_hooks(): void { add_action( 'rest_api_init', [ $this, 'register_routes' ] ); + add_filter( 'rest_post_dispatch', [ $this, 'handle_vip_reached_origin' ], 10, 3 ); } /** @@ -54,6 +55,30 @@ public function register_routes(): void { throw new \Exception( __FUNCTION__ . ' Method not implemented.' ); } + /** + * Marks the 502 and 503 errors of OneSearch's routes as coming from the site, on VIP. + * + * VIP's edge swaps a 502 or 503 body for its error page unless the response says it came from the site. + * + * @see https://docs.wpvip.com/infrastructure/edge-servers/custom-error-pages/. + * + * @param \WP_HTTP_Response $response The response. + * @param \WP_REST_Server $server The REST server. + * @param \WP_REST_Request $request The request. + * @phpstan-param \WP_REST_Request> $request + */ + public function handle_vip_reached_origin( \WP_HTTP_Response $response, \WP_REST_Server $server, \WP_REST_Request $request ): \WP_HTTP_Response { // phpcs:ignore SlevomatCodingStandard.Functions.UnusedParameter.UnusedParameter + if ( + in_array( $response->get_status(), [ 502, 503 ], true ) && + str_starts_with( $request->get_route(), '/' . self::NAMESPACE . '/' ) && + defined( 'VIP_GO_APP_ENVIRONMENT' ) + ) { + $response->header( 'x-vip-reached-origin', 'true' ); + } + + return $response; + } + /** * Whether the request is from a brand site of this governing site, signed with that brand's API key. * diff --git a/inc/Modules/Rest/Shared_Sites_Controller.php b/inc/Modules/Rest/Shared_Sites_Controller.php index cf8541be..9ccdb2ac 100644 --- a/inc/Modules/Rest/Shared_Sites_Controller.php +++ b/inc/Modules/Rest/Shared_Sites_Controller.php @@ -108,17 +108,7 @@ public function set_shared_sites( WP_REST_Request $request ): WP_REST_Response|\ $checked = Remote_Client::check_brand_site( $site['url'], $site['api_key'] ); if ( is_wp_error( $checked ) ) { - $response = rest_convert_error_to_response( $checked ); - /** - * VIP's edge swaps a 502 body for its error page unless the response says it came from the site. - * - * @see https://docs.wpvip.com/infrastructure/edge-servers/custom-error-pages/. - */ - if ( 502 === $response->get_status() && defined( 'VIP_GO_APP_ENVIRONMENT' ) ) { - $response->header( 'x-vip-reached-origin', 'true' ); - } - - return $response; + return $checked; } } diff --git a/tests/phpunit/Integration/Modules/Rest/Abstract_REST_ControllerTest.php b/tests/phpunit/Integration/Modules/Rest/Abstract_REST_ControllerTest.php new file mode 100644 index 00000000..8bb50f60 --- /dev/null +++ b/tests/phpunit/Integration/Modules/Rest/Abstract_REST_ControllerTest.php @@ -0,0 +1,129 @@ +set_up_governing_site(); + wp_set_current_user( self::factory()->user->create( [ 'role' => 'administrator' ] ) ); + + update_option( 'permalink_structure', '/%postname%/' ); + ( new \ReflectionProperty( Main::class, 'instance' ) )->setValue( null, null ); + Main::instance(); + } + + /** + * {@inheritDoc} + */ + public function tearDown(): void { + global $wp_rest_server; + $wp_rest_server = null; + + ( new \ReflectionProperty( Main::class, 'instance' ) )->setValue( null, null ); + unset( $_SERVER['REQUEST_METHOD'], $_SERVER['CONTENT_TYPE'], $GLOBALS['HTTP_RAW_POST_DATA'] ); + $_GET = []; + + parent::tearDown(); + } + + /** + * On VIP, the 502 of an unreachable brand site and a 503 from a OneSearch route carry x-vip-reached-origin. + */ + #[RunInSeparateProcess] + #[PreserveGlobalState( false )] + public function test_marks_502_and_503_errors_as_reached_origin_on_vip(): void { + define( 'VIP_GO_APP_ENVIRONMENT', 'production' ); + + add_filter( 'pre_http_request', static fn (): \WP_Error => new \WP_Error( 'http_request_failed', 'cURL error 6: Could not resolve host' ) ); + $server = $this->serve( 'POST', '/shared-sites', [ 'sites_data' => [ $this->new_brand_site() ] ] ); + + $this->assertSame( 502, $server->status ); + $this->assertSame( 'true', $server->sent_headers['x-vip-reached-origin'] ?? null ); + $this->assertSame( 'onesearch_health_check_failed', json_decode( $server->sent_body, true )['code'] ?? null ); + + add_filter( 'rest_pre_dispatch', static fn (): \WP_Error => new \WP_Error( 'onesearch_down', 'Down for maintenance.', [ 'status' => 503 ] ) ); + $server = $this->serve( 'GET', '/shared-sites', [] ); + + $this->assertSame( 503, $server->status ); + $this->assertSame( 'true', $server->sent_headers['x-vip-reached-origin'] ?? null ); + $this->assertSame( 'Down for maintenance.', json_decode( $server->sent_body, true )['message'] ?? null ); + } + + /** + * Off VIP, a 502 doesn't get the header. + */ + public function test_leaves_502_errors_unmarked_off_vip(): void { + $filter = static fn (): \WP_Error => new \WP_Error( 'http_request_failed', 'cURL error 6: Could not resolve host' ); + add_filter( 'pre_http_request', $filter ); + + $server = $this->serve( 'POST', '/shared-sites', [ 'sites_data' => [ $this->new_brand_site() ] ] ); + + remove_filter( 'pre_http_request', $filter ); + + $this->assertSame( 502, $server->status ); + $this->assertArrayNotHasKey( 'x-vip-reached-origin', $server->sent_headers ); + } + + /** + * Serves a request to a OneSearch route as WordPress serves it over HTTP. + * + * @param string $method The method. + * @param string $route The route, relative to the OneSearch namespace. + * @param array $body The JSON body, or the query of a GET request. + */ + private function serve( string $method, string $route, array $body ): \Spy_REST_Server { + global $wp_rest_server; + $wp_rest_server = null; + $server = rest_get_server(); + $this->assertInstanceOf( \Spy_REST_Server::class, $server ); + + $_SERVER['REQUEST_METHOD'] = $method; + if ( 'GET' === $method ) { + $_GET = $body; + } else { + $_SERVER['CONTENT_TYPE'] = 'application/json'; + $GLOBALS['HTTP_RAW_POST_DATA'] = (string) wp_json_encode( $body ); // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited + } + + $server->serve_request( '/' . Abstract_REST_Controller::NAMESPACE . $route ); + + return $server; + } + + /** + * A brand site that isn't stored yet, so saving it runs its health check. + * + * @return array + */ + private function new_brand_site(): array { + return [ + 'name' => 'Brand Two', + 'url' => 'https://two.example.org', + 'api_key' => 'key-two', + ]; + } +} From ace557e81225516e879da0413f95c8dff16eccf4 Mon Sep 17 00:00:00 2001 From: Dovid Levine Date: Fri, 20 Nov 2026 14:57:36 +0200 Subject: [PATCH 2/2] refactor(rest): match OneDesign's brand site request helper --- inc/Modules/Rest/Indexer_Controller.php | 2 +- inc/Modules/Rest/Remote_Client.php | 57 ++++++------ .../Indexer_Controller_GoverningSiteTest.php | 2 +- .../Modules/Rest/Remote_ClientTest.php | 90 +++++++++++++++++-- 4 files changed, 111 insertions(+), 40 deletions(-) diff --git a/inc/Modules/Rest/Indexer_Controller.php b/inc/Modules/Rest/Indexer_Controller.php index d3977256..f1d15883 100644 --- a/inc/Modules/Rest/Indexer_Controller.php +++ b/inc/Modules/Rest/Indexer_Controller.php @@ -154,7 +154,7 @@ static function ( array $reported ): array { if ( is_wp_error( $report ) || ! is_array( $site ) || ! array_key_exists( 'index', $site ) || ( null !== $site['index'] && null === $index ) ) { $sites[ $site_url ] = [ - 'error' => is_wp_error( $report ) ? $report->get_error_message() : __( 'The site returned an invalid response.', 'onesearch' ), + 'error' => is_wp_error( $report ) ? $report->get_error_message() : __( 'The brand site returned an invalid response.', 'onesearch' ), ]; continue; } diff --git a/inc/Modules/Rest/Remote_Client.php b/inc/Modules/Rest/Remote_Client.php index 931cff09..c49d5d73 100644 --- a/inc/Modules/Rest/Remote_Client.php +++ b/inc/Modules/Rest/Remote_Client.php @@ -305,7 +305,7 @@ public static function get_all_brand_post_types(): array|\WP_Error { $all_sites = []; $errors = []; foreach ( Settings::get_shared_sites() as $site_data ) { - $response = self::request_brand_site( 'all-post-types', $site_data ); + $response = self::request_brand_site( $site_data, 'all-post-types' ); if ( is_wp_error( $response ) ) { $errors[] = [ @@ -315,16 +315,7 @@ public static function get_all_brand_post_types(): array|\WP_Error { continue; } - $response_data = json_decode( wp_remote_retrieve_body( $response ), true ); - if ( ! is_array( $response_data ) ) { - $errors[] = [ - 'site_url' => $site_data['url'], - 'message' => __( 'The site returned an invalid response.', 'onesearch' ), - ]; - continue; - } - - foreach ( $response_data['sites'] as $site_url => $site_data ) { + foreach ( $response['sites'] as $site_url => $site_data ) { if ( ! is_array( $site_data ) ) { continue; } @@ -356,7 +347,7 @@ public static function get_all_brand_post_types(): array|\WP_Error { * @return true|\WP_Error True if the brand site started it. */ public static function reindex_brand_site( array $site_data ): true|\WP_Error { - $response = self::request_brand_site( 'indexer', $site_data, [ 'method' => \WP_REST_Server::CREATABLE ] ); + $response = self::request_brand_site( $site_data, 'indexer', \WP_REST_Server::CREATABLE ); return is_wp_error( $response ) ? $response : true; } @@ -369,15 +360,7 @@ public static function reindex_brand_site( array $site_data ): true|\WP_Error { * @return array|\WP_Error The report as the brand site sent it. */ public static function get_brand_status( array $site_data ): array|\WP_Error { - $response = self::request_brand_site( 'indexer', $site_data ); - - if ( is_wp_error( $response ) ) { - return $response; - } - - $report = json_decode( wp_remote_retrieve_body( $response ), true ); - - return is_array( $report ) ? $report : new \WP_Error( 'onesearch_rest_invalid_response', __( 'The site returned an invalid response.', 'onesearch' ) ); + return self::request_brand_site( $site_data, 'indexer' ); } /** @@ -424,45 +407,55 @@ public static function clear_brand_sites_config_cache( ?string $site_url = null } /** - * Sends a request to a OneSearch route on a brand site, and fails unless the brand site answers with a 200. + * Sends a JSON request to a OneSearch route on a brand site, and decodes its answer. Anything but a 200 is an error with the brand site's own message. * - * @param string $route The route, relative to the OneSearch namespace. * @param array{url: string, api_key: string} $site_data The brand site, with its stored key. - * @param array $args Extra request arguments, like the method. + * @param string $route The route, relative to the OneSearch namespace. + * @param string $method The request method. + * @param ?array $body The request body, if any. * - * @return array|\WP_Error The response, or the brand site's error. + * @return array|\WP_Error The decoded response, or the brand site's error. */ - private static function request_brand_site( string $route, array $site_data, array $args = [] ): array|\WP_Error { + public static function request_brand_site( array $site_data, string $route, string $method = \WP_REST_Server::READABLE, ?array $body = null ): array|\WP_Error { if ( empty( $site_data['url'] ) || empty( $site_data['api_key'] ) ) { return new \WP_Error( 'onesearch_missing_site_data', __( "This site's URL or API key is missing. Remove the site and add it again.", 'onesearch' ) ); } + $args = [ 'method' => $method ]; + if ( null !== $body ) { + $args['body'] = (string) wp_json_encode( $body ); + } + $response = self::request( $site_data['url'], $route, $site_data['api_key'], $args ); if ( is_wp_error( $response ) ) { + $response->add_data( [ 'status' => 502 ] ); return $response; } $code = (int) wp_remote_retrieve_response_code( $response ); + $data = json_decode( wp_remote_retrieve_body( $response ), true ); if ( 401 === $code || 403 === $code ) { - return new \WP_Error( 'onesearch_bad_key', __( 'The brand site rejected the API key. This usually happens when the brand site regenerates its key. Copy the key from the brand site\'s OneSearch settings, then edit the site under Brand Sites and paste it in.', 'onesearch' ) ); + return new \WP_Error( 'onesearch_bad_key', __( 'The brand site rejected the API key. This is usually caused when the brand site regenerates its API key. Copy the key from the brand site\'s OneSearch settings, then edit the site under Brand Sites and paste it in.', 'onesearch' ), [ 'status' => 400 ] ); } if ( 200 !== $code ) { - $site_error = json_decode( wp_remote_retrieve_body( $response ), true ); - return new \WP_Error( 'onesearch_rest_failed_to_connect', - is_string( $site_error['message'] ?? null ) - ? $site_error['message'] + is_string( $data['message'] ?? null ) + ? $data['message'] /* translators: %d: HTTP status code. */ : sprintf( __( 'The brand site answered with error %d.', 'onesearch' ), $code ), [ 'status' => $code ] ); } - return $response; + if ( ! is_array( $data ) ) { + return new \WP_Error( 'onesearch_rest_invalid_response', __( 'The brand site returned an invalid response.', 'onesearch' ), [ 'status' => 500 ] ); + } + + return $data; } /** diff --git a/tests/phpunit/Integration/Modules/Rest/Indexer_Controller_GoverningSiteTest.php b/tests/phpunit/Integration/Modules/Rest/Indexer_Controller_GoverningSiteTest.php index c4a11169..e9a23673 100644 --- a/tests/phpunit/Integration/Modules/Rest/Indexer_Controller_GoverningSiteTest.php +++ b/tests/phpunit/Integration/Modules/Rest/Indexer_Controller_GoverningSiteTest.php @@ -430,7 +430,7 @@ static function ( $preempt, array $args, string $url ) use ( $reports ) { // php ], $status['sites']['https://brand-a.example.com/']['index'] ); - $this->assertSame( [ 'error' => 'The site returned an invalid response.' ], $status['sites']['https://brand-b.example.com/'] ); + $this->assertSame( [ 'error' => 'The brand site returned an invalid response.' ], $status['sites']['https://brand-b.example.com/'] ); $this->assertSame( [ 'error' => 'cURL error 7' ], $status['sites']['https://brand-c.example.com/'] ); $this->assertSame( [ diff --git a/tests/phpunit/Integration/Modules/Rest/Remote_ClientTest.php b/tests/phpunit/Integration/Modules/Rest/Remote_ClientTest.php index db40bfda..53b00c79 100644 --- a/tests/phpunit/Integration/Modules/Rest/Remote_ClientTest.php +++ b/tests/phpunit/Integration/Modules/Rest/Remote_ClientTest.php @@ -330,7 +330,7 @@ static function ( $preempt, $args, $url ) use ( &$requested_urls ) { // phpcs:ig } /** - * A brand site is told to re-index with its key, and fails with the brand site's own error, unless it rejected the key. + * A brand site is told to re-index with its key, and fails with the brand site's own error. */ public function test_brand_site_requests_return_the_brand_sites_answer(): void { $site = [ @@ -368,10 +368,6 @@ static function ( $preempt, $args, $url ) use ( $answer, &$sent ) { // phpcs:ign $sent ); - $answer->code = 403; - $answer->body = '{"code":"rest_forbidden","message":"Sorry, you are not allowed to do that."}'; - $this->assertSame( 'The brand site rejected the API key. This usually happens when the brand site regenerates its key. Copy the key from the brand site\'s OneSearch settings, then edit the site under Brand Sites and paste it in.', Remote_Client::reindex_brand_site( $site )->get_error_message() ); - $answer->code = 500; $answer->body = '{"code":"internal_server_error","message":"There has been a critical error on this website."}'; $this->assertSame( 'There has been a critical error on this website.', Remote_Client::reindex_brand_site( $site )->get_error_message() ); @@ -693,6 +689,88 @@ public function test_deregister_from_governing_site_propagates_wp_error(): void $this->assertSame( 'http_request_failed', $result->get_error_code() ); } + /** + * A brand site's own error message reaches the governing site, whatever the status. + */ + public function test_brand_site_errors_carry_the_brand_message(): void { + $filter = static fn (): array => [ + 'response' => [ + 'code' => 404, + 'message' => 'Not Found', + ], + 'body' => '{"message":"No route was found"}', + 'headers' => [], + 'cookies' => [], + ]; + add_filter( 'pre_http_request', $filter ); + + $result = Remote_Client::request_brand_site( + [ + 'url' => 'https://brand.example.com/', + 'api_key' => 'key', + ], + 'indexer' + ); + + remove_filter( 'pre_http_request', $filter ); + + $this->assertInstanceOf( \WP_Error::class, $result ); + $this->assertSame( 'No route was found', $result->get_error_message() ); + $this->assertSame( 404, $result->get_error_data()['status'] ); + } + + /** + * A brand site that rejects the stored key, after its key was regenerated, asks the admin for the new key. + */ + public function test_brand_site_rejecting_the_stored_key_asks_for_the_new_key(): void { + $filter = static fn (): array => [ + 'response' => [ + 'code' => 403, + 'message' => 'Forbidden', + ], + 'body' => '{"code":"rest_forbidden","message":"Sorry, you are not allowed to do that."}', + 'headers' => [], + 'cookies' => [], + ]; + add_filter( 'pre_http_request', $filter ); + + $result = Remote_Client::request_brand_site( + [ + 'url' => 'https://brand.example.com/', + 'api_key' => 'old-key', + ], + 'indexer' + ); + + remove_filter( 'pre_http_request', $filter ); + + $this->assertInstanceOf( \WP_Error::class, $result ); + $this->assertSame( 'onesearch_bad_key', $result->get_error_code() ); + $this->assertSame( 400, $result->get_error_data()['status'] ); + } + + /** + * A brand site that can't be reached is a bad gateway, not an error on this site. + */ + public function test_unreachable_brand_site_is_a_bad_gateway(): void { + $filter = static fn (): \WP_Error => new \WP_Error( 'http_request_failed', 'cURL error 7' ); + add_filter( 'pre_http_request', $filter ); + + $result = Remote_Client::request_brand_site( + [ + 'url' => 'https://brand.example.com/', + 'api_key' => 'key', + ], + 'indexer' + ); + + remove_filter( 'pre_http_request', $filter ); + + $this->assertInstanceOf( \WP_Error::class, $result ); + $this->assertSame( 'http_request_failed', $result->get_error_code() ); + $this->assertSame( [ 'status' => 502 ], $result->get_error_data() ); + } + /** * Successful per-site responses are merged into the `sites` map. */ @@ -824,7 +902,7 @@ public function test_get_all_brand_post_types_reports_a_rejected_key(): void { 'errors' => [ [ 'site_url' => 'https://site-a.example.com/', - 'message' => 'The brand site rejected the API key. This usually happens when the brand site regenerates its key. Copy the key from the brand site\'s OneSearch settings, then edit the site under Brand Sites and paste it in.', + 'message' => 'The brand site rejected the API key. This is usually caused when the brand site regenerates its API key. Copy the key from the brand site\'s OneSearch settings, then edit the site under Brand Sites and paste it in.', ], ], ],