Opened 2 weeks ago
Last modified 12 days ago
#66384 reviewing defect (bug)
REST batch requests can pass WP_Error to rest_post_dispatch, causing TypeError
| Reported by: | macrorxplus | Owned by: | westonruter |
|---|---|---|---|
| Priority: | normal | Milestone: | 7.2 |
| Component: | REST API | Version: | 7.1.3 |
| Severity: | normal | Keywords: | has-patch has-unit-tests has-test-info |
| Cc: | Focuses: | tests, rest-api |
Description
WordPress 7.1.3 can produce an HTTP 500 response when a REST API batch subrequest is short-circuited by a rest_pre_dispatch filter returning a WP_Error.
The REST batch handler in WP_REST_Server::serve_batch_request_v1() does not appear to normalize this error into a WP_REST_Response before invoking rest_post_dispatch.
Because rest_ensure_response() intentionally preserves WP_Error objects, callbacks expecting a REST response can receive an unexpected object type and trigger a PHP TypeError.
This appears to be a batch-processing equivalent of the issue previously addressed in #56566.
Environment
- WordPress: 7.1.3
- PHP: 8.2.34 and 8.2.35
- Web server: Apache 2.4 with PHP-FPM
- Operating system: Debian 12
Actual Behavior
Production logs recorded the following fatal error:
PHP Fatal error: Uncaught TypeError: _wp_connectors_rest_settings_dispatch(): Argument #1 ($response) must be of type WP_REST_Response, WP_Error given wp-includes/connectors.php:697
The stack trace includes:
WP_REST_Server->serve_batch_request_v1() WP_REST_Server->respond_to_request() WP_REST_Server->dispatch()
The error occurred three times on October 13, 2026, and three additional times on October 25, 2026.
Each occurrence corresponded closely to an HTTP 500 response.
The issue was observed before and after upgrading PHP from 8.2.34 to 8.2.35.
Root Cause Analysis
Source inspection indicates that serve_batch_request_v1() handles rest_pre_dispatch results differently from the normal REST dispatch path.
When a rest_pre_dispatch filter returns a WP_Error, the batch handler can bypass normal dispatch processing.
The batch handler subsequently executes logic equivalent to:
$result = rest_ensure_response( $result ); $result = apply_filters( ‘rest_post_dispatch’, $result, $this, $single_request );
However, rest_ensure_response() returns WP_Error objects unchanged.
Consequently, rest_post_dispatch can receive a WP_Error rather than the expected response object.
In WordPress 7.1.3, _wp_connectors_rest_settings_dispatch() requires a WP_REST_Response, producing the observed fatal error.
Expected Behavior
When a REST batch subrequest returns a WP_Error through rest_pre_dispatch, WordPress should normalize that error into an appropriate REST response before invoking rest_post_dispatch.
For example, a batch subrequest producing a WP_Error with HTTP status 403 should result in:
- An outer batch response with HTTP status 207.
- A nested subrequest response with HTTP status 403.
- The original REST error code and message preserved.
- No PHP fatal error.
Suggested Fix
Normalize WP_Error objects before applying rest_post_dispatch within serve_batch_request_v1().
For example:
$result = rest_ensure_response( $result );
if ( is_wp_error( $result ) ) {
$result = $this->error_to_response( $result );
}
$result = apply_filters(
‘rest_post_dispatch’,
$result,
$this,
$single_request
);
This would align the batch handler with the error normalization already implemented in the normal REST dispatch path.
The proposed correction is based on source inspection and has not yet been validated by automated tests.
Suggested Regression Test
A PHPUnit test could:
- Register a valid REST route supporting batch requests.
- Add a rest_pre_dispatch filter returning a 403 WP_Error for that route.
- Dispatch a request through /batch/v1.
- Assert that the outer response returns HTTP 207.
- Assert that the nested response returns HTTP 403 with the expected error code.
- Verify that rest_post_dispatch receives a valid response object.
Source inspection suggests that a valid batch subrequest can trigger this condition when a rest_pre_dispatch filter returns WP_Error. This has not yet been independently verified with a regression test.
Related Tickets
‘’’#56566 — Fatal error when WP_REST_Server::dispatch() returns WP_Error’’’
https://core.trac.wordpress.org/ticket/56566
This ticket corrected a similar error-normalization problem in the normal REST dispatch path. The batch handler appears to have an equivalent unhandled path.
‘’’#66136 — REST batch request handling issue’’’
https://core.trac.wordpress.org/ticket/66136
This concerns malformed batch subrequest paths and appears distinct from the error-normalization issue described here.
Testing Status
The issue has been observed repeatedly in production logs, and the relevant WordPress source code has been inspected.
A standalone PHPUnit reproduction has not yet been executed.
The identity of the rest_pre_dispatch callback responsible for producing the original WP_Error in production has not been established.
No WordPress core files were modified on the affected installation.
This report is being submitted to document the failure and proposed correction for further investigation and testing by WordPress core contributors.
Change History (4)
This ticket was mentioned in PR #14886 on WordPress/wordpress-develop by bluefairydevelopment.
2 weeks ago
#1
- Keywords has-patch has-unit-tests added; needs-patch removed
This ticket was mentioned in PR #15115 on WordPress/wordpress-develop by @umesh.nevase.
12 days ago
#3
Summary
WP_REST_Server::serve_batch_request_v1() runs each sub-request through the rest_pre_dispatch filter. If a callback short-circuits with a WP_Error, the batch handler only passed the result through rest_ensure_response(), which leaves a WP_Error untouched. That raw WP_Error then reached the rest_post_dispatch filter.
WP_REST_Server::dispatch() does not behave this way: it converts the WP_Error with error_to_response() first. As a result, any rest_post_dispatch callback type-hinted WP_REST_Response hit a fatal TypeError during batch requests. One example in core is _wp_connectors_rest_settings_dispatch().
Changes
- In
serve_batch_request_v1(), convert aWP_Errorresult witherror_to_response()beforerest_post_dispatchruns, asdispatch()does. - Apply the matched handler's
data_processingsetting only when a handler matched, and only to aWP_REST_Response. - Add a PHPUnit test. A
rest_pre_dispatchfilter returns aWP_Error, and the test asserts that:rest_post_dispatchreceives aWP_REST_Response.- The batch returns 207 overall.
- The sub-request keeps its status (403), error code and message.
Testing
- Register a route with
allow_batch. - Add a
rest_pre_dispatchfilter that returns aWP_Error. - POST to
/batch/v1with a request to that route.
Before the change this is a fatal error whenever a rest_post_dispatch callback requires WP_REST_Response. After it, the sub-response contains the error, as it does for a non-batch request.
`bash
vendor/bin/phpunit --filter test_batch_v1_pre_dispatch_wp_error_is_normalized_to_a_response
vendor/bin/phpunit --group restapi
Use of AI Tools
AI assistance: Yes
Tool(s): Claude Code
Model(s): Claude Sonnet 5.5
Used for: reproducing the failure, locating the cause in WP_REST_Server::serve_batch_request_v1(), drafting the fix, and drafting the regression test, including confirming that the test fails without the fix and passes with it. I reviewed and tested the final change and am responsible for it.
#4
@
12 days ago
- Keywords has-test-info added
Reproduction and Test Report
Description
This report confirms the issue on trunk and validates that the indicated patch fixes it.
Patch tested: https://github.com/WordPress/wordpress-develop/pull/15115
Environment
- WordPress: trunk (wordpress-develop Docker environment)
- PHP: 8.3.35
- Method: WP-CLI
wp eval-file - Plugins: none
Steps
- Add a
rest_pre_dispatchfilter that returns aWP_Errorfor/wp/v2/posts. - Send a batch request:
rest_do_request()withPOST /batch/v1containing one/wp/v2/postssub-request.
Actual Results
- ❌ Trunk: fatal error, before any plugin callback runs:
TypeError: _wp_connectors_rest_settings_dispatch(): Argument #1 ($response) must be of type WP_REST_Response, WP_Error given
- ✅ With PR #15115: the batch returns
207 Multi-Statusand the sub-request is a normal error response with status 403:{"code":"blocked","message":"Blocked by test","data":{"status":403}} - ✅
Tests_REST_Server: OK (196 tests). REST tests matching "batch": OK (18 tests).
Supplemental Artifacts
Reproduction script (run with wp eval-file):
<?php wp_set_current_user( 1 ); add_filter( 'rest_pre_dispatch', static function ( $result, $server, $request ) { return '/wp/v2/posts' === $request->get_route() ? new WP_Error( 'blocked', 'Blocked by test', array( 'status' => 403 ) ) : $result; }, 10, 3 ); $batch = new WP_REST_Request( 'POST', '/batch/v1' ); $batch->set_body_params( array( 'requests' => array( array( 'path' => '/wp/v2/posts', 'body' => array( 'title' => 'x' ) ) ) ) ); $response = rest_do_request( $batch );
![(please configure the [header_logo] section in trac.ini)](/chrome/site/your_project_logo.png)
In a batch request, a
WP_Errorreturned by arest_pre_dispatchfilter reachesrest_post_dispatchandenvelope_response()unconverted, becauserest_ensure_response()intentionally preservesWP_Error. Typed callbacks onrest_post_dispatch, including core's own_wp_connectors_rest_settings_dispatch(), then throw aTypeError, and the whole batch returns a 500.This converts the error with
error_to_response()beforerest_post_dispatch, asWP_REST_Server::dispatch()already does for single requests. It also covers a batched path that matches no route: there the oldelseifwas skipped entirely, so the suggested fix on the ticket would not reach it.Tests: new
test_batch_v1_pre_dispatch_wp_error_is_converted_to_response, run for a matched route and an unmatched route. It asserts a 207 outer response, a 403 sub-response with the original error code, and thatrest_post_dispatchreceives aWP_REST_Response.TypeErrorfrom_wp_connectors_rest_settings_dispatch().--group restapipasses (3,868 tests, 0 failures, 0 errors) on PHP 8.5 / MariaDB 10.11; phpcs clean on both changed files.Use of AI Tools
AI assistance: Yes
Tool(s): Claude Code
Model(s): Claude Opus 5.5
Used for: root-cause analysis, the fix, and the regression test, including confirming that the test fails without the fix and passes with it. Submitted by Blue Fairy Development, who are responsible for the change.