From 0ea651a73ea14a85db5bcfac765083e168c61352 Mon Sep 17 00:00:00 2001 From: Weston Ruter Date: Sun, 19 Oct 2025 00:21:26 +0000 Subject: [PATCH] General: Improve parsing of sent HTTP `Content-Type` header to detect HTML response. This improves adherence to the HTTP spec in extracting the header name and value. Developed in https://github.com/WordPress/wordpress-develop/pull/10293 Follow-up to [60936]. Props dmsnell, westonruter. See #43258. Built from https://develop.svn.wordpress.org/trunk@60973 git-svn-id: http://core.svn.wordpress.org/trunk@60309 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-includes/template.php | 19 +++++++++++++++---- wp-includes/version.php | 2 +- 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/wp-includes/template.php b/wp-includes/template.php index f799113e9c..f17307e19d 100644 --- a/wp-includes/template.php +++ b/wp-includes/template.php @@ -926,13 +926,24 @@ function wp_finalize_template_enhancement_output_buffer( string $output, int $ph $is_html_content_type = null; $html_content_types = array( 'text/html', 'application/xhtml+xml' ); foreach ( headers_list() as $header ) { - $header_parts = preg_split( '/\s*[:;]\s*/', strtolower( $header ) ); + $header_parts = explode( ':', strtolower( $header ), 2 ); if ( - is_array( $header_parts ) && - count( $header_parts ) >= 2 && + count( $header_parts ) === 2 && 'content-type' === $header_parts[0] ) { - $is_html_content_type = in_array( $header_parts[1], $html_content_types, true ); + /* + * This is looking for very specific content types, therefore it + * doesn’t need to fully parse the header’s value. Instead, it needs + * only assert that the content type is one of the static HTML types. + * + * Example: + * + * Content-Type: text/html; charset=utf8 + * Content-Type: text/html ;charset=latin4 + * Content-Type:application/xhtml+xml + */ + $media_type = trim( strtok( $header_parts[1], ';' ), " \t" ); + $is_html_content_type = in_array( $media_type, $html_content_types, true ); break; // PHP only sends the first Content-Type header in the list. } } diff --git a/wp-includes/version.php b/wp-includes/version.php index 38e78755e5..1209fd5797 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -16,7 +16,7 @@ * * @global string $wp_version */ -$wp_version = '6.9-alpha-60972'; +$wp_version = '6.9-alpha-60973'; /** * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.