From 5b1a5b53ab1c2bd176005ed2d403c6325f1169fe Mon Sep 17 00:00:00 2001 From: audrasjb Date: Mon, 24 Jun 2024 15:04:11 +0000 Subject: [PATCH] Grouped Backports to the 6.5 branch. - Editor: Fix Path Traversal issue on Windows in Template-Part Block. - Editor: Sanitize Template Part HTML tag on save. - HTML API: Run URL attributes through `esc_url()`. Merges [58470], [58471], [58472] and [58473] to the 6.5 branch. Props xknown, peterwilsoncc, jorbin, bernhard-reiter, azaozz, dmsnell, gziolo. Built from https://develop.svn.wordpress.org/branches/6.5@58474 git-svn-id: http://core.svn.wordpress.org/branches/6.5@57923 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-includes/blocks.php | 36 ++++++++++++++++--- wp-includes/formatting.php | 3 +- wp-includes/functions.php | 3 ++ .../html-api/class-wp-html-tag-processor.php | 9 ++++- wp-includes/version.php | 2 +- 5 files changed, 46 insertions(+), 7 deletions(-) diff --git a/wp-includes/blocks.php b/wp-includes/blocks.php index 854992de57..5b452c69c4 100644 --- a/wp-includes/blocks.php +++ b/wp-includes/blocks.php @@ -1447,7 +1447,7 @@ function _filter_block_content_callback( $matches ) { * @return array The filtered and sanitized block object result. */ function filter_block_kses( $block, $allowed_html, $allowed_protocols = array() ) { - $block['attrs'] = filter_block_kses_value( $block['attrs'], $allowed_html, $allowed_protocols ); + $block['attrs'] = filter_block_kses_value( $block['attrs'], $allowed_html, $allowed_protocols, $block ); if ( is_array( $block['innerBlocks'] ) ) { foreach ( $block['innerBlocks'] as $i => $inner_block ) { @@ -1463,6 +1463,7 @@ function filter_block_kses( $block, $allowed_html, $allowed_protocols = array() * non-allowable HTML. * * @since 5.3.1 + * @since 6.5.5 Added the `$block_context` parameter. * * @param string[]|string $value The attribute value to filter. * @param array[]|string $allowed_html An array of allowed HTML elements and attributes, @@ -1470,13 +1471,18 @@ function filter_block_kses( $block, $allowed_html, $allowed_protocols = array() * for the list of accepted context names. * @param string[] $allowed_protocols Optional. Array of allowed URL protocols. * Defaults to the result of wp_allowed_protocols(). + * @param array $block_context Optional. The block the attribute belongs to, in parsed block array format. * @return string[]|string The filtered and sanitized result. */ -function filter_block_kses_value( $value, $allowed_html, $allowed_protocols = array() ) { +function filter_block_kses_value( $value, $allowed_html, $allowed_protocols = array(), $block_context = null ) { if ( is_array( $value ) ) { foreach ( $value as $key => $inner_value ) { - $filtered_key = filter_block_kses_value( $key, $allowed_html, $allowed_protocols ); - $filtered_value = filter_block_kses_value( $inner_value, $allowed_html, $allowed_protocols ); + $filtered_key = filter_block_kses_value( $key, $allowed_html, $allowed_protocols, $block_context ); + $filtered_value = filter_block_kses_value( $inner_value, $allowed_html, $allowed_protocols, $block_context ); + + if ( isset( $block_context['blockName'] ) && 'core/template-part' === $block_context['blockName'] ) { + $filtered_value = filter_block_core_template_part_attributes( $filtered_value, $filtered_key, $allowed_html ); + } if ( $filtered_key !== $key ) { unset( $value[ $key ] ); @@ -1491,6 +1497,28 @@ function filter_block_kses_value( $value, $allowed_html, $allowed_protocols = ar return $value; } +/** + * Sanitizes the value of the Template Part block's `tagName` attribute. + * + * @since 6.5.5 + * + * @param string $attribute_value The attribute value to filter. + * @param string $attribute_name The attribute name. + * @param array[]|string $allowed_html An array of allowed HTML elements and attributes, + * or a context name such as 'post'. See wp_kses_allowed_html() + * for the list of accepted context names. + * @return string The sanitized attribute value. + */ +function filter_block_core_template_part_attributes( $attribute_value, $attribute_name, $allowed_html ) { + if ( empty( $attribute_value ) || 'tagName' !== $attribute_name ) { + return $attribute_value; + } + if ( ! is_array( $allowed_html ) ) { + $allowed_html = wp_kses_allowed_html( $allowed_html ); + } + return isset( $allowed_html[ $attribute_value ] ) ? $attribute_value : ''; +} + /** * Parses blocks out of a content string, and renders those appropriate for the excerpt. * diff --git a/wp-includes/formatting.php b/wp-includes/formatting.php index a756f6d12a..b097d000ef 100644 --- a/wp-includes/formatting.php +++ b/wp-includes/formatting.php @@ -4802,12 +4802,13 @@ EOF; * Escapes an HTML tag name. * * @since 2.5.0 + * @since 6.5.5 Allow hyphens in tag names (i.e. custom elements). * * @param string $tag_name * @return string */ function tag_escape( $tag_name ) { - $safe_tag = strtolower( preg_replace( '/[^a-zA-Z0-9_:]/', '', $tag_name ) ); + $safe_tag = strtolower( preg_replace( '/[^a-zA-Z0-9-_:]/', '', $tag_name ) ); /** * Filters a string cleaned and escaped for output as an HTML tag. * diff --git a/wp-includes/functions.php b/wp-includes/functions.php index fd305be20b..f9d617660a 100644 --- a/wp-includes/functions.php +++ b/wp-includes/functions.php @@ -6192,6 +6192,9 @@ function validate_file( $file, $allowed_files = array() ) { return 0; } + // Normalize path for Windows servers + $file = wp_normalize_path( $file ); + // `../` on its own is not allowed: if ( '../' === $file ) { return 1; diff --git a/wp-includes/html-api/class-wp-html-tag-processor.php b/wp-includes/html-api/class-wp-html-tag-processor.php index c540ea96c1..bf7e026bce 100644 --- a/wp-includes/html-api/class-wp-html-tag-processor.php +++ b/wp-includes/html-api/class-wp-html-tag-processor.php @@ -2968,7 +2968,14 @@ class WP_HTML_Tag_Processor { if ( true === $value ) { $updated_attribute = $name; } else { - $escaped_new_value = esc_attr( $value ); + $comparable_name = strtolower( $name ); + + /* + * Escape URL attributes. + * + * @see https://html.spec.whatwg.org/#attributes-3 + */ + $escaped_new_value = in_array( $comparable_name, wp_kses_uri_attributes() ) ? esc_url( $value ) : esc_attr( $value ); $updated_attribute = "{$name}=\"{$escaped_new_value}\""; } diff --git a/wp-includes/version.php b/wp-includes/version.php index 61f4db61d7..d43cecce57 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -16,7 +16,7 @@ * * @global string $wp_version */ -$wp_version = '6.5.5-alpha-58448'; +$wp_version = '6.5.5-alpha-58474'; /** * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.