From 3280d5b7d56e1d2dacf3f25b371a5cfeaba94274 Mon Sep 17 00:00:00 2001 From: shivamdev-lgtm Date: Tue, 2 Dec 2025 12:51:37 +0530 Subject: [PATCH 1/8] fix: send 500 response code on critical errors --- src/wp-includes/functions.php | 29 +++++++++++++++++++++++------ 1 file changed, 23 insertions(+), 6 deletions(-) diff --git a/src/wp-includes/functions.php b/src/wp-includes/functions.php index 68a29f444fd48..f2a888d460340 100644 --- a/src/wp-includes/functions.php +++ b/src/wp-includes/functions.php @@ -3886,6 +3886,23 @@ function _default_wp_die_handler( $message, $title = '', $args = array() ) { $message .= "\n

$back_text

"; } + // Ensure HTTP 500 status code for critical errors, even if headers were already sent. + $is_critical_error = false; + if ( function_exists( 'is_wp_error' ) && is_wp_error( $message ) ) { + $error_code = $message->get_error_code(); + $is_critical_error = ( 'internal_server_error' === $error_code ); + } elseif ( 'internal_server_error' === $parsed_args['code'] ) { + $is_critical_error = true; + } + + // Also ensure HTTP 500 if response is already set to 500 (from fatal error handler). + if ( $is_critical_error || 500 === $parsed_args['response'] ) { + $parsed_args['response'] = 500; + if ( function_exists( 'http_response_code' ) ) { + http_response_code( 500 ); + } + } + if ( ! did_action( 'admin_head' ) ) : if ( ! headers_sent() ) { header( "Content-Type: text/html; charset={$parsed_args['charset']}" ); @@ -4224,12 +4241,12 @@ function _xml_wp_die_handler( $message, $title = '', $args = array() ) { $xml = << - {$parsed_args['code']} - <![CDATA[{$title}]]> - - - {$parsed_args['response']} - + {$parsed_args['code']} + <![CDATA[{$title}]]> + + + {$parsed_args['response']} + EOD; From a246dd0cd5c12e7a0591d2b95426275aa6cfab31 Mon Sep 17 00:00:00 2001 From: shivamdev-lgtm Date: Thu, 4 Dec 2025 11:21:43 +0530 Subject: [PATCH 2/8] fix: pr reviews --- src/wp-includes/functions.php | 21 +++++++++------------ 1 file changed, 9 insertions(+), 12 deletions(-) diff --git a/src/wp-includes/functions.php b/src/wp-includes/functions.php index f2a888d460340..da001dae94a49 100644 --- a/src/wp-includes/functions.php +++ b/src/wp-includes/functions.php @@ -3891,16 +3891,13 @@ function _default_wp_die_handler( $message, $title = '', $args = array() ) { if ( function_exists( 'is_wp_error' ) && is_wp_error( $message ) ) { $error_code = $message->get_error_code(); $is_critical_error = ( 'internal_server_error' === $error_code ); - } elseif ( 'internal_server_error' === $parsed_args['code'] ) { + } elseif ( 'internal_server_error' === $parsed_args['code'] || 500 === $parsed_args['response'] ) { $is_critical_error = true; } - // Also ensure HTTP 500 if response is already set to 500 (from fatal error handler). - if ( $is_critical_error || 500 === $parsed_args['response'] ) { + if ( $is_critical_error ) { $parsed_args['response'] = 500; - if ( function_exists( 'http_response_code' ) ) { - http_response_code( 500 ); - } + http_response_code( 500 ); } if ( ! did_action( 'admin_head' ) ) : @@ -4241,12 +4238,12 @@ function _xml_wp_die_handler( $message, $title = '', $args = array() ) { $xml = << - {$parsed_args['code']} - <![CDATA[{$title}]]> - - - {$parsed_args['response']} - + {$parsed_args['code']} + <![CDATA[{$title}]]> + + + {$parsed_args['response']} + EOD; From de14cfe13125cee3fda74211e54e2546b158cd55 Mon Sep 17 00:00:00 2001 From: shivamdev-lgtm Date: Tue, 9 Dec 2025 12:24:15 +0530 Subject: [PATCH 3/8] fix: remove redundant function_exists check --- src/wp-includes/functions.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/wp-includes/functions.php b/src/wp-includes/functions.php index da001dae94a49..e0693d223b390 100644 --- a/src/wp-includes/functions.php +++ b/src/wp-includes/functions.php @@ -3888,7 +3888,7 @@ function _default_wp_die_handler( $message, $title = '', $args = array() ) { // Ensure HTTP 500 status code for critical errors, even if headers were already sent. $is_critical_error = false; - if ( function_exists( 'is_wp_error' ) && is_wp_error( $message ) ) { + if ( is_wp_error( $message ) ) { $error_code = $message->get_error_code(); $is_critical_error = ( 'internal_server_error' === $error_code ); } elseif ( 'internal_server_error' === $parsed_args['code'] || 500 === $parsed_args['response'] ) { From d19b7a3ed0cf36e7cad60da3c75d5b6f16c469fb Mon Sep 17 00:00:00 2001 From: shivamdev-lgtm Date: Thu, 11 Dec 2025 12:02:52 +0530 Subject: [PATCH 4/8] fix: more PR reviews --- src/wp-includes/functions.php | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/wp-includes/functions.php b/src/wp-includes/functions.php index e0693d223b390..a393d96a44872 100644 --- a/src/wp-includes/functions.php +++ b/src/wp-includes/functions.php @@ -3888,9 +3888,8 @@ function _default_wp_die_handler( $message, $title = '', $args = array() ) { // Ensure HTTP 500 status code for critical errors, even if headers were already sent. $is_critical_error = false; - if ( is_wp_error( $message ) ) { - $error_code = $message->get_error_code(); - $is_critical_error = ( 'internal_server_error' === $error_code ); + if ( $message instanceof WP_Error ) { + $is_critical_error = $message->get_error_code(); } elseif ( 'internal_server_error' === $parsed_args['code'] || 500 === $parsed_args['response'] ) { $is_critical_error = true; } From 43913857224771942245236002b99eb4f97a483e Mon Sep 17 00:00:00 2001 From: Shivam Jha Date: Thu, 5 Feb 2026 13:17:15 +0530 Subject: [PATCH 5/8] Update src/wp-includes/functions.php Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --- src/wp-includes/functions.php | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/src/wp-includes/functions.php b/src/wp-includes/functions.php index 97f065e9e198c..43937843f4c74 100644 --- a/src/wp-includes/functions.php +++ b/src/wp-includes/functions.php @@ -3889,9 +3889,16 @@ function _default_wp_die_handler( $message, $title = '', $args = array() ) { // Ensure HTTP 500 status code for critical errors, even if headers were already sent. $is_critical_error = false; if ( $message instanceof WP_Error ) { - $is_critical_error = $message->get_error_code(); - } elseif ( 'internal_server_error' === $parsed_args['code'] || 500 === $parsed_args['response'] ) { + $error_code = $message->get_error_code(); + $error_data = $message->get_error_data(); + + if ( 'internal_server_error' === $error_code ) { + $is_critical_error = true; + } elseif ( is_array( $error_data ) && isset( $error_data['status'] ) && 500 === (int) $error_data['status'] ) { $is_critical_error = true; + } + } elseif ( 'internal_server_error' === $parsed_args['code'] || 500 === $parsed_args['response'] ) { + $is_critical_error = true; } if ( $is_critical_error ) { From 756e1d67fd188e35f6b6b04c8df61a0a25d420e5 Mon Sep 17 00:00:00 2001 From: shivamdev-lgtm Date: Thu, 5 Feb 2026 13:36:50 +0530 Subject: [PATCH 6/8] chore: remove redundant WP_Error check --- src/wp-includes/functions.php | 16 +--------------- 1 file changed, 1 insertion(+), 15 deletions(-) diff --git a/src/wp-includes/functions.php b/src/wp-includes/functions.php index 43937843f4c74..e7f5e0a13436e 100644 --- a/src/wp-includes/functions.php +++ b/src/wp-includes/functions.php @@ -3887,21 +3887,7 @@ function _default_wp_die_handler( $message, $title = '', $args = array() ) { } // Ensure HTTP 500 status code for critical errors, even if headers were already sent. - $is_critical_error = false; - if ( $message instanceof WP_Error ) { - $error_code = $message->get_error_code(); - $error_data = $message->get_error_data(); - - if ( 'internal_server_error' === $error_code ) { - $is_critical_error = true; - } elseif ( is_array( $error_data ) && isset( $error_data['status'] ) && 500 === (int) $error_data['status'] ) { - $is_critical_error = true; - } - } elseif ( 'internal_server_error' === $parsed_args['code'] || 500 === $parsed_args['response'] ) { - $is_critical_error = true; - } - - if ( $is_critical_error ) { + if ( 'internal_server_error' === $parsed_args['code'] || 500 === $parsed_args['response'] ) { $parsed_args['response'] = 500; http_response_code( 500 ); } From ece64ebb512c621086a73a70ce15d148b4b9bfb9 Mon Sep 17 00:00:00 2001 From: shivamdev-lgtm Date: Thu, 5 Feb 2026 13:42:34 +0530 Subject: [PATCH 7/8] fix: do not send response code if header already sent --- src/wp-includes/functions.php | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/wp-includes/functions.php b/src/wp-includes/functions.php index e7f5e0a13436e..95626536044c5 100644 --- a/src/wp-includes/functions.php +++ b/src/wp-includes/functions.php @@ -3886,10 +3886,12 @@ function _default_wp_die_handler( $message, $title = '', $args = array() ) { $message .= "\n

$back_text

"; } - // Ensure HTTP 500 status code for critical errors, even if headers were already sent. + // Ensure HTTP 500 status code for critical errors. if ( 'internal_server_error' === $parsed_args['code'] || 500 === $parsed_args['response'] ) { $parsed_args['response'] = 500; - http_response_code( 500 ); + if ( ! headers_sent() ) { + http_response_code( 500 ); + } } if ( ! did_action( 'admin_head' ) ) : From 3bfafa7e2bf3b24059655dfe9fdf21102b0e6ea7 Mon Sep 17 00:00:00 2001 From: shivamdev-lgtm Date: Wed, 18 Nov 2026 18:17:04 +0530 Subject: [PATCH 8/8] test: add tests for HTTP 500 response code in _default_wp_die_handler() --- .../tests/functions/defaultWpDieHandler.php | 176 ++++++++++++++++++ 1 file changed, 176 insertions(+) create mode 100644 tests/phpunit/tests/functions/defaultWpDieHandler.php diff --git a/tests/phpunit/tests/functions/defaultWpDieHandler.php b/tests/phpunit/tests/functions/defaultWpDieHandler.php new file mode 100644 index 0000000000000..1f3669a39df1f --- /dev/null +++ b/tests/phpunit/tests/functions/defaultWpDieHandler.php @@ -0,0 +1,176 @@ +assertSame( 500, $this->get_response_code_from_wp_die( 'Error message' ) ); + } + + /** + * @ticket 64256 + */ + public function test_should_send_500_response_code_when_response_is_500() { + $this->assertSame( 500, $this->get_response_code_from_wp_die( 'Error message', array( 'response' => 500 ) ) ); + } + + /** + * @ticket 64256 + */ + public function test_should_send_500_response_code_for_internal_server_error_code() { + $response_code = $this->get_response_code_from_wp_die( + 'Error message', + array( + 'code' => 'internal_server_error', + 'response' => 403, + ) + ); + + $this->assertSame( 500, $response_code ); + } + + /** + * @ticket 64256 + */ + public function test_should_send_500_response_code_for_internal_server_error_wp_error() { + $error = new WP_Error( 'internal_server_error', 'Error message', array( 'status' => 403 ) ); + + $this->assertSame( 500, $this->get_response_code_from_wp_die( $error ) ); + } + + /** + * Tests that a critical error sends a 500 response code even after the admin header was output. + * + * @ticket 64256 + */ + public function test_should_send_500_response_code_for_critical_error_after_admin_head() { + $this->mark_admin_head_as_done(); + + $error = new WP_Error( 'internal_server_error', 'There has been a critical error on this website.' ); + + $this->assertSame( 500, $this->get_response_code_from_wp_die( $error, array( 'response' => 500 ) ) ); + } + + /** + * Tests that non-critical errors still do not send a response code after the admin header was output. + * + * @ticket 64256 + */ + public function test_should_not_send_response_code_for_non_critical_error_after_admin_head() { + $this->mark_admin_head_as_done(); + + $response_code = $this->get_response_code_from_wp_die( + 'Error message', + array( + 'code' => 'not_found', + 'response' => 404, + ) + ); + + $this->assertFalse( $response_code ); + } + + /** + * Tests the default critical error template of the fatal error handler, after the admin header was output. + * + * @ticket 64256 + * + * @covers WP_Fatal_Error_Handler::display_default_error_template + */ + public function test_fatal_error_handler_default_template_should_send_500_response_code() { + $this->mark_admin_head_as_done(); + + $error = array( + 'type' => E_ERROR, + 'message' => 'Fatal error', + 'file' => __FILE__, + 'line' => __LINE__, + ); + + $method = new ReflectionMethod( 'WP_Fatal_Error_Handler', 'display_default_error_template' ); + if ( PHP_VERSION_ID < 80100 ) { + $method->setAccessible( true ); + } + + ob_start(); + $method->invoke( new WP_Fatal_Error_Handler(), $error, false ); + ob_end_clean(); + + $this->assertSame( 500, http_response_code() ); + } + + /** + * @ticket 64256 + */ + public function test_should_send_custom_response_code_for_non_critical_errors() { + $response_code = $this->get_response_code_from_wp_die( + 'Error message', + array( + 'code' => 'not_found', + 'response' => 404, + ) + ); + + $this->assertSame( 404, $response_code ); + } +}