From 654230830a5e855273d61fda1893de9b09fe9ba4 Mon Sep 17 00:00:00 2001 From: Jon Surrell Date: Tue, 17 Nov 2026 11:33:54 +0400 Subject: [PATCH] Fonts: Escape axis tags in font-variation-settings arrays WP_Font_Face::compile_variations() writes each axis as sprintf( '"%s" %s', $tag, $value ) since r63653. Neither part is escaped or validated, so a tag holding `"` ends the string early and the rest of the tag and the value are written as CSS: array( 'wght' => 400 ) -> "wght" 400 array( 'wg"t' => 400 ) -> "wg"t" 400 The tag is written with WP_CSS_Builder::string(), and an axis is skipped unless its tag is four characters from U+0020 to U+007E, as CSS Fonts 4 requires, and its value reads as one CSS number token. Values such as `400px` or `calc(400)` are skipped; the descriptor takes a , and calc() is the only other form CSS accepts there. --- src/wp-includes/fonts/class-wp-font-face.php | 49 ++++++++++++- .../font-face/wpFontFace/generateAndPrint.php | 72 +++++++++++++++++++ 2 files changed, 120 insertions(+), 1 deletion(-) diff --git a/src/wp-includes/fonts/class-wp-font-face.php b/src/wp-includes/fonts/class-wp-font-face.php index c8d081b9557b5..e400519637745 100644 --- a/src/wp-includes/fonts/class-wp-font-face.php +++ b/src/wp-includes/fonts/class-wp-font-face.php @@ -364,6 +364,9 @@ private function compile_src( array $value ) { * * @since 6.4.0 * @since 7.2.0 Quotes each axis tag and separates the axes with commas. + * @since 7.3.0 Escapes each axis tag and skips an axis whose tag or value is invalid. + * + * @link https://www.w3.org/TR/css-fonts-4/#font-variation-settings-def * * @param array $font_variation_settings Array of font variation settings, keyed by axis tag. * @return string The CSS. @@ -372,9 +375,53 @@ private function compile_variations( array $font_variation_settings ) { $variations = array(); foreach ( $font_variation_settings as $tag => $value ) { - $variations[] = sprintf( '"%s" %s', $tag, $value ); + $tag = (string) $tag; + $number = self::get_css_number( $value ); + + // An axis tag is four ASCII characters from U+0020 to U+007E. + if ( null === $number || 1 !== preg_match( '/\A[\x20-\x7E]{4}\z/', $tag ) ) { + continue; + } + + $variations[] = WP_CSS_Builder::string( $tag ) . ' ' . $number; } return implode( ', ', $variations ); } + + /** + * Returns the CSS text of a value that is one CSS number. + * + * Whitespace around the number is removed. Any other token, such as a + * unit, a function or a second number, makes the value invalid. + * + * @since 7.3.0 + * + * @param mixed $value Value to read. + * @return string|null The number as CSS text, or null if the value is not one number. + */ + private static function get_css_number( $value ) { + if ( ! is_int( $value ) && ! is_float( $value ) && ! is_string( $value ) ) { + return null; + } + + $processor = new WP_CSS_Token_Processor( (string) $value ); + $number = null; + + while ( $processor->next_token() ) { + $type = $processor->get_token_type(); + + if ( WP_CSS_Token_Processor::TOKEN_WHITESPACE === $type ) { + continue; + } + + if ( null !== $number || WP_CSS_Token_Processor::TOKEN_NUMBER !== $type ) { + return null; + } + + $number = $processor->get_token()->get_value(); + } + + return $number; + } } diff --git a/tests/phpunit/tests/fonts/font-face/wpFontFace/generateAndPrint.php b/tests/phpunit/tests/fonts/font-face/wpFontFace/generateAndPrint.php index c0d314d0ab4d8..f754cdf2cdbf8 100644 --- a/tests/phpunit/tests/fonts/font-face/wpFontFace/generateAndPrint.php +++ b/tests/phpunit/tests/fonts/font-face/wpFontFace/generateAndPrint.php @@ -37,4 +37,76 @@ public function test_should_generate_and_print_given_fonts( array $fonts, $expec $output = get_echo( array( $font_face, 'generate_and_print' ), array( $fonts ) ); $this->assertEqualHTML( $expected_output, $output ); } + + /** + * @ticket 66103 + * + * @dataProvider data_should_print_font_variation_settings + * + * @param array $settings Font variation settings, keyed by axis tag. + * @param string|null $expected Expected descriptor value, or null if the descriptor is omitted. + */ + public function test_should_print_font_variation_settings( array $settings, $expected ) { + $font_face = new WP_Font_Face(); + $fonts = array( + array( + array( + 'font-family' => 'Inter', + 'src' => array( 'https://example.org/font.woff2' ), + 'font-variation-settings' => $settings, + ), + ), + ); + + $output = get_echo( array( $font_face, 'generate_and_print' ), array( $fonts ) ); + + if ( null === $expected ) { + $this->assertStringNotContainsString( 'font-variation-settings', $output ); + } else { + $this->assertStringContainsString( 'font-variation-settings:' . $expected . ';}', $output ); + } + } + + /** + * Data provider. + * + * @return array[] + */ + public static function data_should_print_font_variation_settings() { + return array( + 'registered axes' => array( + array( + 'wght' => 400, + 'slnt' => -10, + ), + '"wght" 400, "slnt" -10', + ), + 'a custom axis' => array( array( 'GRAD' => 0 ), '"GRAD" 0' ), + 'a float value' => array( array( 'wght' => 412.5 ), '"wght" 412.5' ), + 'a numeric string with whitespace' => array( array( 'wght' => ' 400 ' ), '"wght" 400' ), + 'a numeric tag' => array( array( '1234' => 1 ), '"1234" 1' ), + 'a tag with a quotation mark is escaped' => array( array( 'a"b;' => 1 ), '"a\\22 b\\3b " 1' ), + 'a tag with an angle bracket is escaped' => array( array( '' => 1 ), '"\\3c /a\\3e " 1' ), + 'a tag that is not four characters' => array( + array( + 'wdth' => 100, + 'weight' => 400, + ), + '"wdth" 100', + ), + 'a tag with a non-ASCII character' => array( array( 'wgh€' => 400 ), null ), + 'a value with a unit' => array( array( 'wght' => '400px' ), null ), + 'a value with a second declaration' => array( array( 'wght' => '400;color:red' ), null ), + 'a value with a function' => array( array( 'wght' => 'calc(400)' ), null ), + 'a value that is not a scalar' => array( array( 'wght' => array( 400 ) ), null ), + 'a value that is not a number' => array( + array( + 'wght' => 'bold', + 'wdth' => 100, + ), + '"wdth" 100', + ), + 'a value that is infinite' => array( array( 'wght' => INF ), null ), + ); + } }