Make WordPress Core

Opened 8 months ago

Closed 8 months ago

#64901 closed defect (bug) (fixed)

rest_convert_error_to_response() fatals when WP_Error data is a stdClass object in 7.0-beta5

Reported by: nateallen Owned by: westonruter
Priority: normal Milestone: 7.0
Component: REST API Version: 7.0
Severity: critical Keywords: has-patch has-unit-tests commit
Cc: Focuses: rest-api

Description

The rest_convert_error_to_response() function throws a PHP fatal error when a WP_Error object contains error data that is a stdClass instead of an array.

PHP Fatal error: Uncaught Error: Cannot use object of type stdClass as array
in wp-includes/rest-api.php on line 3431

Cause

In 61429 (part of the Code Modernization effort, #63430), the isset() ternary in rest_convert_error_to_response() was replaced with a null coalescing operator:

Before (6.9.4):

<?php
return is_array( $error_data ) && isset( $error_data['status'] ) ? $error_data['status'] : $status;

After (7.0-beta5):

<?php
return $error_data['status'] ?? $status;

The is_array() guard was critical here because WP_Error::get_all_error_data() can return data of any type, such as arrays, objects, strings, integers, etc. Removing it causes a fatal when error data is a stdClass.

Steps to reproduce

Register a REST route that returns a WP_Error with stdClass data:

<?php
add_action( 'rest_api_init', function () {
    register_rest_route( 'test/v1', '/trigger-bug', array(
        'methods'             => 'GET',
        'callback'            => function () {
            return new WP_Error(
                'test_error',
                'Test error',
                (object) array( 'status' => 500 )
            );
        },
        'permission_callback' => '__return_true',
    ) );
} );

Then request GET /wp-json/test/v1/trigger-bug.

Expected result

A JSON error response (as in 6.9.4).

Actual result

PHP fatal error on line 3431 of wp-includes/rest-api.php.

Suggested fix

Restore the is_array() check:

<?php
return is_array( $error_data ) ? ( $error_data['status'] ?? $status ) : $status;

Change History (9)

#1 @desrosj
8 months ago

  • Milestone Awaiting Review → 7.0

#2 @desrosj
8 months ago

  • Version → trunk

#3 @mukesh27
8 months ago

Better to revert.

return is_array( $error_data ) && isset( $error_data['status'] ) ? $error_data['status'] : $status;

This ticket was mentioned in ​PR #11307 on ​WordPress/wordpress-develop by ​@nateallen.


8 months ago
#4

  • Keywords has-patch has-unit-tests added

​61429 (part of the Code Modernization effort, #63430) replaced the isset() ternary in rest_convert_error_to_response() with a null coalescing operator, removing the is_array() guard. Since WP_Error::get_all_error_data() can return data of any type, this causes a PHP fatal error when error data is a stdClass object.

This PR reverts the line to its original form and adds a test case for stdClass error data.

Trac ticket: https://core.trac.wordpress.org/ticket/64901

## Use of AI Tools

AI assistance: Yes
Tool(s): Claude Code
Model(s): Claude Opus 4.6
Used for: Identifying the regression by diffing 6.9.4 and 7.0-beta5, and writing the test case. The fix itself is a direct revert of the changed line. All code was reviewed by me.

This ticket was mentioned in ​Slack in #core by audrasjb. ​View the logs.


8 months ago

#6 @jorbin
8 months ago

  • Owner set to jorbin
  • Status new → assigned

​@westonruter commented on ​PR #11307:


8 months ago
#7

Thank you for addressing this.

The prior change was indeed wrong because \WP_Error::get_all_error_data() returns mixed[]:

​https://github.com/WordPress/wordpress-develop/blob/6607b4df3d604d8e9e2aaf11e5c45a6725cb2a08/src/wp-includes/class-wp-error.php#L233-L241

I was curious why I wasn't seeing PHPStan flag this issue locally since where I have the PHPStan rule level 8 enforced. Only when I switched to ​rule level 9 did PHPStan catch the issue because it will:

be strict about explicit mixed type - the only allowed operation you can do with it is to pass it to another mixed

Once I did that, it was caught:

https://github.com/user-attachments/assets/58ed6c3e-fbf1-4edf-879f-b6b93cd8661e

#8 @westonruter
8 months ago

  • Keywords commit added
  • Owner changed from jorbin to westonruter
  • Status assigned → accepted

#9 @westonruter
8 months ago

  • Resolution → fixed
  • Status accepted → closed

In 62107:

Code Modernization: Fix rest_convert_error_to_response() handling of non-array error data when obtaining status.

While the error data is normally an array, this is not guaranteed. This issue would have been detected by PHPStan rule level 9, since WP_Error::get_all_error_data() returns mixed[].

Developed in ​https://github.com/WordPress/wordpress-develop/pull/11307

Follow-up to r61429.

Props nateallen, desrosj, jorbin, mukesh27, westonruter.
See #63430.
Fixes #64901.

Note: See TracTickets for help on using tickets.