From 27949c8e44e72619adae88db3b5baafa564fa002 Mon Sep 17 00:00:00 2001 From: girishpanchal30 Date: Fri, 6 Nov 2026 15:49:22 +0530 Subject: [PATCH 01/16] fix: sanitize ability color and background values --- inc/abilities/abstract_ability.php | 23 +++ inc/abilities/builder_configure.php | 20 ++- inc/abilities/site_set_theme_mod.php | 12 +- tests/test-neve-ability-style-mods.php | 208 +++++++++++++++++++++++++ 4 files changed, 256 insertions(+), 7 deletions(-) create mode 100644 tests/test-neve-ability-style-mods.php diff --git a/inc/abilities/abstract_ability.php b/inc/abilities/abstract_ability.php index ff6c2b0598..c55c37fab2 100644 --- a/inc/abilities/abstract_ability.php +++ b/inc/abilities/abstract_ability.php @@ -236,6 +236,29 @@ protected function validation_issue( $code, $message, $field = '' ) { ); } + /** + * Run a background or color theme mod value through Neve's sanitizer for that kind of setting. + * + * @param string $key Theme mod key. + * @param mixed $value Incoming value. + * @return mixed Sanitized value; null when it is not a valid background or color. An empty color stays empty. + */ + protected function sanitize_style_mod( string $key, $value ) { + if ( preg_match( '/_background$/', $key ) ) { + $background = neve_sanitize_background( $value ); + + return is_wp_error( $background ) ? null : $background; + } + + if ( preg_match( '/_color$/', $key ) ) { + $color = neve_sanitize_colors( $value ); + + return '' === $color && '' !== $value ? null : $color; + } + + return $value; + } + /** * Build a structured non-validation error payload for clients that collapse * WP_Error details into a generic transport error. diff --git a/inc/abilities/builder_configure.php b/inc/abilities/builder_configure.php index 894f753ce4..bd9188b167 100644 --- a/inc/abilities/builder_configure.php +++ b/inc/abilities/builder_configure.php @@ -179,19 +179,27 @@ function ( $item ) use ( $component ) { */ protected function apply_row_style( $builder_id, $row, array $style ) { $prefix = 'hfg_' . $builder_id . '_layout_' . $row; + $mods = array(); $affected = array(); if ( isset( $style['height'] ) ) { - set_theme_mod( $prefix . '_height', wp_json_encode( map_deep( $style['height'], 'sanitize_text_field' ) ) ); - $affected[] = $prefix . '_height'; + $mods[ $prefix . '_height' ] = wp_json_encode( map_deep( $style['height'], 'sanitize_text_field' ) ); } if ( isset( $style['background'] ) ) { - set_theme_mod( $prefix . '_background', map_deep( $style['background'], 'sanitize_text_field' ) ); - $affected[] = $prefix . '_background'; + $mods[ $prefix . '_background' ] = map_deep( $style['background'], 'sanitize_text_field' ); } if ( isset( $style['text_color'] ) ) { - set_theme_mod( $prefix . '_new_text_color', sanitize_text_field( $style['text_color'] ) ); - $affected[] = $prefix . '_new_text_color'; + $mods[ $prefix . '_new_text_color' ] = sanitize_text_field( $style['text_color'] ); + } + + foreach ( $mods as $key => $value ) { + $value = $this->sanitize_style_mod( $key, $value ); + if ( null === $value ) { + continue; + } + + set_theme_mod( $key, $value ); + $affected[] = $key; } return $affected; diff --git a/inc/abilities/site_set_theme_mod.php b/inc/abilities/site_set_theme_mod.php index dc7b1c3a6a..8ff38f0ff9 100644 --- a/inc/abilities/site_set_theme_mod.php +++ b/inc/abilities/site_set_theme_mod.php @@ -172,7 +172,17 @@ protected function run( array $input ) { ); } - set_theme_mod( $key, $this->sanitize_value( $input['value'] ) ); + $value = $this->sanitize_style_mod( $key, $this->sanitize_value( $input['value'] ) ); + if ( null === $value ) { + return $this->validation_issue( + 'invalid_value', + /* translators: %s: input field name */ + sprintf( __( 'Invalid value for "%s".', 'neve' ), 'value' ), + 'value' + ); + } + + set_theme_mod( $key, $value ); } return array( diff --git a/tests/test-neve-ability-style-mods.php b/tests/test-neve-ability-style-mods.php new file mode 100644 index 0000000000..ad85af3392 --- /dev/null +++ b/tests/test-neve-ability-style-mods.php @@ -0,0 +1,208 @@ +|false + */ + private $theme_mods; + + /** + * Snapshot the theme mods the abilities rewrite. + */ + public function setUp(): void { + parent::setUp(); + + wp_set_current_user( 1 ); + $this->theme_mods = get_theme_mods(); + } + + /** + * Restore the theme mods. + */ + public function tearDown(): void { + update_option( 'theme_mods_' . get_option( 'stylesheet' ), $this->theme_mods ); + + parent::tearDown(); + } + + /** + * Header row colors are sanitized and an invalid background is not saved. + */ + public function test_header_configure_sanitizes_row_style(): void { + set_theme_mod( self::HEADER_BG, 'previous' ); + + ( new Header_Configure() )->execute( + array( + 'row_style' => array( + array( + 'row' => 'main', + 'text_color' => self::INJECTED_CSS, + 'background' => 'not-a-background', + ), + ), + ) + ); + + $this->assertSame( 'rgba(1,2,3,1)', get_theme_mod( self::HEADER_TEXT ) ); + $this->assertSame( 'previous', get_theme_mod( self::HEADER_BG ) ); + } + + /** + * Header row background colors are sanitized. + */ + public function test_header_configure_sanitizes_row_background(): void { + ( new Header_Configure() )->execute( + array( + 'row_style' => array( + array( + 'row' => 'main', + 'background' => array( + 'type' => 'color', + 'colorValue' => self::INJECTED_CSS, + ), + ), + ), + ) + ); + + $background = get_theme_mod( self::HEADER_BG ); + $this->assertIsArray( $background ); + $this->assertSame( 'rgba(1,2,3,1)', $background['colorValue'] ); + } + + /** + * Footer row colors are sanitized. + */ + public function test_footer_configure_sanitizes_row_style(): void { + ( new Footer_Configure() )->execute( + array( + 'row_style' => array( + array( + 'row' => 'main', + 'text_color' => self::INJECTED_CSS, + ), + ), + ) + ); + + $this->assertSame( 'rgba(1,2,3,1)', get_theme_mod( self::FOOTER_TEXT ) ); + } + + /** + * A background that is not a background object is rejected and the stored value kept. + */ + public function test_site_set_theme_mod_rejects_invalid_background(): void { + set_theme_mod( self::SIDEBAR_BG, 'previous' ); + + $result = ( new Site_Set_Theme_Mod() )->execute( + array( + 'action' => 'set', + 'key' => self::SIDEBAR_BG, + 'value' => 'not-a-background', + ) + ); + + $this->assertIsArray( $result ); + $this->assertFalse( $result['success'] ); + $this->assertSame( 'value', $result['error']['field'] ); + $this->assertSame( 'previous', get_theme_mod( self::SIDEBAR_BG ) ); + } + + /** + * An invalid row color keeps the stored color. + */ + public function test_header_configure_keeps_color_on_invalid_value(): void { + set_theme_mod( self::HEADER_TEXT, '#ff0000' ); + + ( new Header_Configure() )->execute( + array( + 'row_style' => array( + array( + 'row' => 'main', + 'text_color' => 'notacolor', + ), + ), + ) + ); + + $this->assertSame( '#ff0000', get_theme_mod( self::HEADER_TEXT ) ); + } + + /** + * An invalid color is rejected and the stored color kept. + */ + public function test_site_set_theme_mod_rejects_invalid_color(): void { + set_theme_mod( self::SIDEBAR_TEXT, '#ff0000' ); + + $result = ( new Site_Set_Theme_Mod() )->execute( + array( + 'action' => 'set', + 'key' => self::SIDEBAR_TEXT, + 'value' => 'notacolor', + ) + ); + + $this->assertIsArray( $result ); + $this->assertFalse( $result['success'] ); + $this->assertSame( 'invalid_value', $result['error']['code'] ); + $this->assertSame( '#ff0000', get_theme_mod( self::SIDEBAR_TEXT ) ); + } + + /** + * An empty color clears the stored color. + */ + public function test_site_set_theme_mod_clears_color_with_empty_value(): void { + set_theme_mod( self::SIDEBAR_TEXT, '#ff0000' ); + + $result = ( new Site_Set_Theme_Mod() )->execute( + array( + 'action' => 'set', + 'key' => self::SIDEBAR_TEXT, + 'value' => '', + ) + ); + + $this->assertIsArray( $result ); + $this->assertTrue( $result['success'] ); + $this->assertSame( '', get_theme_mod( self::SIDEBAR_TEXT ) ); + } + + /** + * Color settings are sanitized. + */ + public function test_site_set_theme_mod_sanitizes_colors(): void { + $result = ( new Site_Set_Theme_Mod() )->execute( + array( + 'action' => 'set', + 'key' => self::SIDEBAR_TEXT, + 'value' => self::INJECTED_CSS, + ) + ); + + $this->assertIsArray( $result ); + $this->assertTrue( $result['success'] ); + $this->assertSame( 'rgba(1,2,3,1)', get_theme_mod( self::SIDEBAR_TEXT ) ); + } +} From 1b42ce89af39bd631330722a3a7decbf6d651111 Mon Sep 17 00:00:00 2001 From: girishpanchal30 Date: Fri, 6 Nov 2026 17:10:40 +0530 Subject: [PATCH 02/16] fix: sanitize row CSS and ability styles --- globals/sanitize-functions.php | 29 ++++++ .../Core/Builder/Abstract_Builder.php | 28 +++++- inc/abilities/abstract_ability.php | 29 +++++- inc/abilities/builder_configure.php | 5 +- inc/abilities/site_set_theme_mod.php | 20 +++- tests/test-neve-ability-style-mods.php | 97 ++++++++++++++++++- tests/test-neve-row-color-css.php | 77 +++++++++++++++ 7 files changed, 275 insertions(+), 10 deletions(-) create mode 100644 tests/test-neve-row-color-css.php diff --git a/globals/sanitize-functions.php b/globals/sanitize-functions.php index 9a0990f50d..76e9a3808e 100644 --- a/globals/sanitize-functions.php +++ b/globals/sanitize-functions.php @@ -59,6 +59,35 @@ function neve_is_css_function_color( string $value ): bool { return (bool) preg_match( '/^(?:rgb|rgba|hsl|hsla)\(\s*[-0-9.% ,]+\)$/', trim( $value ) ); } +/** + * Whether a value can close the CSS declaration it is printed in. + * + * JSON strings and arrays are checked value by value, so their own braces do not count. + * + * @param mixed $value Stored or incoming value. + * + * @return bool + */ +function neve_closes_css_declaration( $value ): bool { + if ( is_string( $value ) ) { + $decoded = json_decode( $value, true ); + + return is_array( $decoded ) ? neve_closes_css_declaration( $decoded ) : (bool) preg_match( '/[;{}<>]/', $value ); + } + + if ( ! is_array( $value ) ) { + return false; + } + + foreach ( $value as $item ) { + if ( neve_closes_css_declaration( $item ) ) { + return true; + } + } + + return false; +} + /** * Sanitize a color control value. * diff --git a/header-footer-grid/Core/Builder/Abstract_Builder.php b/header-footer-grid/Core/Builder/Abstract_Builder.php index 24173fec99..1ddc9833e3 100644 --- a/header-footer-grid/Core/Builder/Abstract_Builder.php +++ b/header-footer-grid/Core/Builder/Abstract_Builder.php @@ -982,6 +982,28 @@ protected function get_row_featured_image( $image_url, $use_featured, $meta ) { return apply_filters( 'nv_builder_row_image_url', $image, $use_featured, $meta ); } + /** + * Print a row color variable; a stored color that can close its declaration is sanitized first. + * + * @param string $css_prop CSS variable. + * @param mixed $value Stored color. + * + * @return string + */ + public function row_color_rule( $css_prop, $value ) { + if ( ! is_string( $value ) ) { + return ''; + } + + // The printed style is stripped of HTML as a whole; strip it here first so only CSS is checked. + $value = wp_strip_all_tags( $value ); + if ( neve_closes_css_declaration( $value ) ) { + $value = neve_sanitize_colors( $value ); + } + + return '' !== $value ? sprintf( ' %s: %s;', $css_prop, $value ) : ''; + } + /** * Method to generate css array for each row. * @@ -1033,12 +1055,14 @@ private function add_row_style( $row_index, $css_array = array() ) { $rules['--rowbcolor'] = [ Dynamic_Selector::META_KEY => $this->control_id . '_' . $row_index . '_' . self::BORDER_COLOR, Dynamic_Selector::META_DEFAULT => 'var(--nv-light-bg)', + Dynamic_Selector::META_FILTER => [ $this, 'row_color_rule' ], ]; } $rules['--color'] = [ Dynamic_Selector::META_KEY => $this->control_id . '_' . $row_index . '_' . self::TEXT_COLOR, Dynamic_Selector::META_DEFAULT => $default_colors['text'], + Dynamic_Selector::META_FILTER => [ $this, 'row_color_rule' ], ]; // Exclude the following rules for the header when the global header background is active. @@ -1061,6 +1085,7 @@ private function add_row_style( $row_index, $css_array = array() ) { '--bgcolor' => [ Dynamic_Selector::META_KEY => $this->control_id . '_' . $row_index . '_background.colorValue', Dynamic_Selector::META_DEFAULT => $default_color, + Dynamic_Selector::META_FILTER => [ $this, 'row_color_rule' ], ], ] ); @@ -1071,7 +1096,8 @@ private function add_row_style( $row_index, $css_array = array() ) { $rules, [ '--overlaycolor' => [ - Dynamic_Selector::META_KEY => $this->control_id . '_' . $row_index . '_background.overlayColorValue', + Dynamic_Selector::META_KEY => $this->control_id . '_' . $row_index . '_background.overlayColorValue', + Dynamic_Selector::META_FILTER => [ $this, 'row_color_rule' ], ], '--bgimage' => [ Dynamic_Selector::META_KEY => $this->control_id . '_' . $row_index . '_background', diff --git a/inc/abilities/abstract_ability.php b/inc/abilities/abstract_ability.php index c55c37fab2..4af17bd1b9 100644 --- a/inc/abilities/abstract_ability.php +++ b/inc/abilities/abstract_ability.php @@ -241,13 +241,11 @@ protected function validation_issue( $code, $message, $field = '' ) { * * @param string $key Theme mod key. * @param mixed $value Incoming value. - * @return mixed Sanitized value; null when it is not a valid background or color. An empty color stays empty. + * @return mixed Sanitized value; null when it is not a valid background or color, or can close its CSS declaration. An empty color stays empty. */ protected function sanitize_style_mod( string $key, $value ) { if ( preg_match( '/_background$/', $key ) ) { - $background = neve_sanitize_background( $value ); - - return is_wp_error( $background ) ? null : $background; + return $this->sanitize_background_mod( $value ); } if ( preg_match( '/_color$/', $key ) ) { @@ -256,7 +254,28 @@ protected function sanitize_style_mod( string $key, $value ) { return '' === $color && '' !== $value ? null : $color; } - return $value; + return neve_closes_css_declaration( $value ) ? null : $value; + } + + /** + * Sanitize a background theme mod value. + * + * @param mixed $value Incoming background. + * @return array|null Sanitized background; null when it, or a non-empty color in it, is invalid. + */ + private function sanitize_background_mod( $value ) { + $background = neve_sanitize_background( $value ); + if ( is_wp_error( $background ) ) { + return null; + } + + foreach ( array( 'colorValue', 'overlayColorValue' ) as $field ) { + if ( ! empty( $value[ $field ] ) && '' === $background[ $field ] ) { + return null; + } + } + + return $background; } /** diff --git a/inc/abilities/builder_configure.php b/inc/abilities/builder_configure.php index bd9188b167..a5297a0d3d 100644 --- a/inc/abilities/builder_configure.php +++ b/inc/abilities/builder_configure.php @@ -179,11 +179,12 @@ function ( $item ) use ( $component ) { */ protected function apply_row_style( $builder_id, $row, array $style ) { $prefix = 'hfg_' . $builder_id . '_layout_' . $row; + $builder = $this->get_builder( $builder_id ); $mods = array(); $affected = array(); - if ( isset( $style['height'] ) ) { - $mods[ $prefix . '_height' ] = wp_json_encode( map_deep( $style['height'], 'sanitize_text_field' ) ); + if ( isset( $style['height'] ) && null !== $builder ) { + $mods[ $prefix . '_height' ] = $builder->sanitize_responsive_int_json( map_deep( $style['height'], 'sanitize_text_field' ) ); } if ( isset( $style['background'] ) ) { $mods[ $prefix . '_background' ] = map_deep( $style['background'], 'sanitize_text_field' ); diff --git a/inc/abilities/site_set_theme_mod.php b/inc/abilities/site_set_theme_mod.php index 8ff38f0ff9..f010cf3de5 100644 --- a/inc/abilities/site_set_theme_mod.php +++ b/inc/abilities/site_set_theme_mod.php @@ -51,6 +51,21 @@ class Site_Set_Theme_Mod extends Abstract_Ability { 'hfg_header_layout_sidebar_new_text_color', ); + /** + * Sanitizers for allowlisted keys that are numbers or plain text rather than CSS values. Other keys use sanitize_style_mod(). + * + * @var array + */ + const SETTING_SANITIZERS = array( + 'neve_scroll_to_top_border_radius' => 'absint', + 'hfg_header_layout_sidebar_width' => 'neve_sanitize_range_value', + 'neve_scroll_to_top' => 'sanitize_text_field', + 'nav-icon_menu_icon' => 'sanitize_text_field', + 'nav-icon_svg_menu_icon' => 'sanitize_text_field', + 'nav-icon_menu_label' => 'sanitize_text_field', + 'hfg_header_layout_sidebar_layout' => 'sanitize_text_field', + ); + /** * {@inheritDoc} */ @@ -172,7 +187,10 @@ protected function run( array $input ) { ); } - $value = $this->sanitize_style_mod( $key, $this->sanitize_value( $input['value'] ) ); + $value = $this->sanitize_value( $input['value'] ); + $value = isset( self::SETTING_SANITIZERS[ $key ] ) + ? call_user_func( self::SETTING_SANITIZERS[ $key ], is_array( $value ) ? wp_json_encode( $value ) : $value ) + : $this->sanitize_style_mod( $key, $value ); if ( null === $value ) { return $this->validation_issue( 'invalid_value', diff --git a/tests/test-neve-ability-style-mods.php b/tests/test-neve-ability-style-mods.php index ad85af3392..ed9349ae80 100644 --- a/tests/test-neve-ability-style-mods.php +++ b/tests/test-neve-ability-style-mods.php @@ -19,7 +19,10 @@ class TestNeveAbilityStyleMods extends WP_UnitTestCase { const HEADER_BG = 'hfg_header_layout_main_background'; const FOOTER_TEXT = 'hfg_footer_layout_main_new_text_color'; const SIDEBAR_BG = 'hfg_header_layout_sidebar_background'; - const SIDEBAR_TEXT = 'hfg_header_layout_sidebar_new_text_color'; + const SIDEBAR_TEXT = 'hfg_header_layout_sidebar_new_text_color'; + const SIDEBAR_WIDTH = 'hfg_header_layout_sidebar_width'; + const HEADER_HEIGHT = 'hfg_header_layout_main_height'; + const FORM_PADDING = 'neve_form_fields_padding'; /** * Theme mods snapshot restored after each test. @@ -130,6 +133,98 @@ public function test_site_set_theme_mod_rejects_invalid_background(): void { $this->assertSame( 'previous', get_theme_mod( self::SIDEBAR_BG ) ); } + /** + * A row height suffix that would close its CSS declaration is replaced by an allowed unit. + */ + public function test_header_configure_sanitizes_row_height(): void { + ( new Header_Configure() )->execute( + array( + 'row_style' => array( + array( + 'row' => 'main', + 'height' => array( + 'desktop' => 80, + 'suffix' => array( 'desktop' => 'px;}body{background:lime}/*' ), + ), + ), + ), + ) + ); + + $height = json_decode( get_theme_mod( self::HEADER_HEIGHT ), true ); + $this->assertSame( 80, $height['desktop'] ); + $this->assertSame( 'px', $height['suffix']['desktop'] ); + } + + /** + * A background with an invalid nested color keeps the stored background. + */ + public function test_header_configure_keeps_background_on_invalid_nested_color(): void { + $background = array( + 'type' => 'color', + 'colorValue' => '#123456', + ); + set_theme_mod( self::HEADER_BG, $background ); + + ( new Header_Configure() )->execute( + array( + 'row_style' => array( + array( + 'row' => 'main', + 'background' => array( + 'type' => 'color', + 'colorValue' => 'notacolor', + ), + ), + ), + ) + ); + + $this->assertSame( $background, get_theme_mod( self::HEADER_BG ) ); + } + + /** + * The sidebar width goes through its range sanitizer. + */ + public function test_site_set_theme_mod_sanitizes_sidebar_width(): void { + $result = ( new Site_Set_Theme_Mod() )->execute( + array( + 'action' => 'set', + 'key' => self::SIDEBAR_WIDTH, + 'value' => '360;}body{background:lime}/*', + ) + ); + + $this->assertIsArray( $result ); + $this->assertTrue( $result['success'] ); + $this->assertSame( 360.0, get_theme_mod( self::SIDEBAR_WIDTH ) ); + } + + /** + * A spacing value that would close its CSS declaration is rejected and the stored value kept. + */ + public function test_site_set_theme_mod_rejects_injected_spacing(): void { + set_theme_mod( self::FORM_PADDING, 'previous' ); + + $result = ( new Site_Set_Theme_Mod() )->execute( + array( + 'action' => 'set', + 'key' => self::FORM_PADDING, + 'value' => array( + 'top' => '10;}body{background:lime}/*', + 'right' => '10', + 'bottom' => '10', + 'left' => '10', + ), + ) + ); + + $this->assertIsArray( $result ); + $this->assertFalse( $result['success'] ); + $this->assertSame( 'invalid_value', $result['error']['code'] ); + $this->assertSame( 'previous', get_theme_mod( self::FORM_PADDING ) ); + } + /** * An invalid row color keeps the stored color. */ diff --git a/tests/test-neve-row-color-css.php b/tests/test-neve-row-color-css.php new file mode 100644 index 0000000000..6c2b58eb43 --- /dev/null +++ b/tests/test-neve-row-color-css.php @@ -0,0 +1,77 @@ +|false + */ + private $theme_mods; + + /** + * Snapshot the theme mods and read them uncached. + */ + public function setUp(): void { + parent::setUp(); + + $this->theme_mods = get_theme_mods(); + Mods::$no_cache = true; + } + + /** + * Restore the theme mods. + */ + public function tearDown(): void { + update_option( 'theme_mods_' . get_option( 'stylesheet' ), $this->theme_mods ); + Mods::$no_cache = false; + + parent::tearDown(); + } + + /** + * Stored row colors that would close their declaration are printed sanitized; other colors as stored. + */ + public function test_injected_row_colors_are_printed_sanitized(): void { + set_theme_mod( 'hfg_header_layout_main_border_color', self::INJECTED_CSS ); + set_theme_mod( 'hfg_header_layout_main_new_text_color', 'red' ); + set_theme_mod( + 'hfg_header_layout_main_background', + array( + 'type' => 'color', + 'colorValue' => self::INJECTED_CSS, + ) + ); + + $css = $this->header_css(); + + $this->assertStringNotContainsString( 'background:lime', $css ); + $this->assertStringContainsString( '--rowbcolor: rgba(1,2,3,1);', $css ); + $this->assertStringContainsString( '--bgcolor: rgba(1,2,3,1);', $css ); + $this->assertStringContainsString( '--color: red;', $css ); + } + + /** + * Render the header builder's dynamic CSS. + * + * @return string + */ + private function header_css(): string { + $builder = \HFG\Main::get_instance()->get_builder( 'header' ); + + return (string) new Dynamic_Selector( $builder->add_style( array() ) ); + } +} From ed387c4bb5b1af604eeb0940e8df4c623e266289 Mon Sep 17 00:00:00 2001 From: selul Date: Tue, 10 Nov 2026 21:47:39 +0200 Subject: [PATCH 03/16] fix: sanitize button appearance CSS --- globals/sanitize-functions.php | 49 ++++++- header-footer-grid/Core/Components/Button.php | 8 +- .../Core/Components/MenuIcon.php | 10 +- .../Components/Utility/SearchIconButton.php | 20 ++- inc/core/styles/css_vars.php | 32 +++-- tests/test-neve-button-appearance-css.php | 134 ++++++++++++++++++ 6 files changed, 231 insertions(+), 22 deletions(-) create mode 100644 tests/test-neve-button-appearance-css.php diff --git a/globals/sanitize-functions.php b/globals/sanitize-functions.php index 76e9a3808e..888f836c5d 100644 --- a/globals/sanitize-functions.php +++ b/globals/sanitize-functions.php @@ -277,14 +277,59 @@ function neve_sanitize_background( $value ) { /** * Sanitize the button appearance control. * - * @param array $value the control value. + * @param mixed $value The control value. * - * @return array + * @return array */ function neve_sanitize_button_appearance( $value ) { + if ( ! is_array( $value ) ) { + return array(); + } + + $color_fields = array( 'background', 'backgroundHover', 'text', 'textHover', 'shadowColor', 'shadowColorHover' ); + foreach ( $color_fields as $field ) { + if ( array_key_exists( $field, $value ) ) { + $value[ $field ] = neve_sanitize_colors( $value[ $field ] ); + } + } + return $value; } +/** + * Format a sanitized color as a CSS custom-property rule. + * + * @param string $css_prop CSS custom property. + * @param mixed $value Stored color value. + * + * @return string + */ +function neve_sanitize_color_css_rule( $css_prop, $value ) { + $color = neve_sanitize_colors( $value ); + + return '' === $color ? '' : sprintf( ' %s: %s;', $css_prop, $color ); +} + +/** + * Format a sanitized button shadow as a CSS custom-property rule. + * + * @param string $css_prop CSS custom property. + * @param mixed $value Stored shadow color. + * @param int $width Horizontal offset. + * @param int $height Vertical offset. + * @param int $blur Blur radius. + * + * @return string + */ +function neve_sanitize_shadow_css_rule( $css_prop, $value, $width, $height, $blur ) { + $color = neve_sanitize_colors( $value ); + if ( '' === $color ) { + $color = 'none'; + } + + return sprintf( '%1$s:%2$dpx %3$dpx %4$dpx %5$s;', $css_prop, $width, $height, $blur, $color ); +} + /** * Sanitize the typography control. * diff --git a/header-footer-grid/Core/Components/Button.php b/header-footer-grid/Core/Components/Button.php index 6122547cf0..4124d10fd3 100644 --- a/header-footer-grid/Core/Components/Button.php +++ b/header-footer-grid/Core/Components/Button.php @@ -230,18 +230,22 @@ public function add_style( array $css_array = array() ) { '--primarybtnbg' => [ Dynamic_Selector::META_KEY => $id . '.background', Dynamic_Selector::META_DEFAULT => 'var(--nv-primary-accent)', + Dynamic_Selector::META_FILTER => 'neve_sanitize_color_css_rule', ], '--primarybtncolor' => [ Dynamic_Selector::META_KEY => $id . '.text', Dynamic_Selector::META_DEFAULT => '#fff', + Dynamic_Selector::META_FILTER => 'neve_sanitize_color_css_rule', ], '--primarybtnhoverbg' => [ Dynamic_Selector::META_KEY => $id . '.backgroundHover', Dynamic_Selector::META_DEFAULT => 'var(--nv-primary-accent)', + Dynamic_Selector::META_FILTER => 'neve_sanitize_color_css_rule', ], '--primarybtnhovercolor' => [ Dynamic_Selector::META_KEY => $id . '.textHover', Dynamic_Selector::META_DEFAULT => '#fff', + Dynamic_Selector::META_FILTER => 'neve_sanitize_color_css_rule', ], '--primarybtnborderradius' => [ Dynamic_Selector::META_KEY => $id . '.borderRadius', @@ -268,7 +272,7 @@ public function add_style( array $css_array = array() ) { $width = intval( $button_values['shadowProperties']['width'] ); $height = intval( $button_values['shadowProperties']['height'] ); - return sprintf( '%s:%s;', $css_prop, sprintf( '%spx %spx %spx %s;', $width, $height, $blur, $value ) ); + return neve_sanitize_shadow_css_rule( $css_prop, $value, $width, $height, $blur ); }, ]; $rules['--primarybtnhovershadow'] = [ @@ -282,7 +286,7 @@ public function add_style( array $css_array = array() ) { $width = intval( $button_values['shadowPropertiesHover']['width'] ); $height = intval( $button_values['shadowPropertiesHover']['height'] ); - return sprintf( '%s:%s;', $css_prop, sprintf( '%spx %spx %spx %s;', $width, $height, $blur, $value ) ); + return neve_sanitize_shadow_css_rule( $css_prop, $value, $width, $height, $blur ); }, ]; diff --git a/header-footer-grid/Core/Components/MenuIcon.php b/header-footer-grid/Core/Components/MenuIcon.php index 26ce6369ee..1ba043027e 100644 --- a/header-footer-grid/Core/Components/MenuIcon.php +++ b/header-footer-grid/Core/Components/MenuIcon.php @@ -680,8 +680,14 @@ public function add_style( array $css_array = array() ) { $id = $this->get_id() . '_' . self::BUTTON_APPEARANCE; $rules = [ - '--bgcolor' => $id . '.background', - '--color' => $id . '.text', + '--bgcolor' => [ + Dynamic_Selector::META_KEY => $id . '.background', + Dynamic_Selector::META_FILTER => 'neve_sanitize_color_css_rule', + ], + '--color' => [ + Dynamic_Selector::META_KEY => $id . '.text', + Dynamic_Selector::META_FILTER => 'neve_sanitize_color_css_rule', + ], '--borderradius' => [ Dynamic_Selector::META_KEY => $id . '.borderRadius', 'directional-prop' => Config::CSS_PROP_BORDER_RADIUS, diff --git a/header-footer-grid/Core/Components/Utility/SearchIconButton.php b/header-footer-grid/Core/Components/Utility/SearchIconButton.php index 34bf7dbb46..b056ec21fc 100644 --- a/header-footer-grid/Core/Components/Utility/SearchIconButton.php +++ b/header-footer-grid/Core/Components/Utility/SearchIconButton.php @@ -329,10 +329,22 @@ public function get_style( array $css_array = array() ) { $button_style_id = $this->component_id . '_' . self::BUTTON_STYLE; $rules = [ - '--primarybtnbg' => $button_style_id . '.background', - '--primarybtnhoverbg' => $button_style_id . '.background', - '--primarybtncolor' => $button_style_id . '.text', - '--primarybtnhovercolor' => $button_style_id . '.text', + '--primarybtnbg' => [ + Dynamic_Selector::META_KEY => $button_style_id . '.background', + Dynamic_Selector::META_FILTER => 'neve_sanitize_color_css_rule', + ], + '--primarybtnhoverbg' => [ + Dynamic_Selector::META_KEY => $button_style_id . '.background', + Dynamic_Selector::META_FILTER => 'neve_sanitize_color_css_rule', + ], + '--primarybtncolor' => [ + Dynamic_Selector::META_KEY => $button_style_id . '.text', + Dynamic_Selector::META_FILTER => 'neve_sanitize_color_css_rule', + ], + '--primarybtnhovercolor' => [ + Dynamic_Selector::META_KEY => $button_style_id . '.text', + Dynamic_Selector::META_FILTER => 'neve_sanitize_color_css_rule', + ], ]; $css_array[] = [ diff --git a/inc/core/styles/css_vars.php b/inc/core/styles/css_vars.php index 07c8947118..599fc5c903 100644 --- a/inc/core/styles/css_vars.php +++ b/inc/core/styles/css_vars.php @@ -46,28 +46,36 @@ public function get_button_rules() { $rules = [ '--primarybtnbg' => [ - Dynamic_Selector::META_KEY => $mod_key_primary . '.background', + Dynamic_Selector::META_KEY => $mod_key_primary . '.background', + Dynamic_Selector::META_FILTER => 'neve_sanitize_color_css_rule', ], '--secondarybtnbg' => [ - Dynamic_Selector::META_KEY => $mod_key_secondary . '.background', + Dynamic_Selector::META_KEY => $mod_key_secondary . '.background', + Dynamic_Selector::META_FILTER => 'neve_sanitize_color_css_rule', ], '--primarybtnhoverbg' => [ - Dynamic_Selector::META_KEY => $mod_key_primary . '.backgroundHover', + Dynamic_Selector::META_KEY => $mod_key_primary . '.backgroundHover', + Dynamic_Selector::META_FILTER => 'neve_sanitize_color_css_rule', ], '--secondarybtnhoverbg' => [ - Dynamic_Selector::META_KEY => $mod_key_secondary . '.backgroundHover', + Dynamic_Selector::META_KEY => $mod_key_secondary . '.backgroundHover', + Dynamic_Selector::META_FILTER => 'neve_sanitize_color_css_rule', ], '--primarybtncolor' => [ - Dynamic_Selector::META_KEY => $mod_key_primary . '.text', + Dynamic_Selector::META_KEY => $mod_key_primary . '.text', + Dynamic_Selector::META_FILTER => 'neve_sanitize_color_css_rule', ], '--secondarybtncolor' => [ - Dynamic_Selector::META_KEY => $mod_key_secondary . '.text', + Dynamic_Selector::META_KEY => $mod_key_secondary . '.text', + Dynamic_Selector::META_FILTER => 'neve_sanitize_color_css_rule', ], '--primarybtnhovercolor' => [ - Dynamic_Selector::META_KEY => $mod_key_primary . '.textHover', + Dynamic_Selector::META_KEY => $mod_key_primary . '.textHover', + Dynamic_Selector::META_FILTER => 'neve_sanitize_color_css_rule', ], '--secondarybtnhovercolor' => [ - Dynamic_Selector::META_KEY => $mod_key_secondary . '.textHover', + Dynamic_Selector::META_KEY => $mod_key_secondary . '.textHover', + Dynamic_Selector::META_FILTER => 'neve_sanitize_color_css_rule', ], '--primarybtnborderradius' => [ Dynamic_Selector::META_KEY => $mod_key_primary . '.borderRadius', @@ -95,7 +103,7 @@ public function get_button_rules() { $width = intval($primary_values['shadowProperties']['width']); $height = intval($primary_values['shadowProperties']['height']); - return sprintf( '%s:%s;', $css_prop, sprintf('%spx %spx %spx %s;', $width, $height, $blur, $value ) ); + return neve_sanitize_shadow_css_rule( $css_prop, $value, $width, $height, $blur ); } ]; } @@ -110,7 +118,7 @@ public function get_button_rules() { $width = intval($primary_values['shadowPropertiesHover']['width']); $height = intval($primary_values['shadowPropertiesHover']['height']); - return sprintf( '%s:%s;', $css_prop, sprintf('%spx %spx %spx %s;', $width, $height, $blur, $value ) ); + return neve_sanitize_shadow_css_rule( $css_prop, $value, $width, $height, $blur ); } ]; } @@ -125,7 +133,7 @@ public function get_button_rules() { $width = intval($secondary_values['shadowProperties']['width']); $height = intval($secondary_values['shadowProperties']['height']); - return sprintf( '%s:%s;', $css_prop, sprintf('%spx %spx %spx %s;', $width, $height, $blur, $value ) ); + return neve_sanitize_shadow_css_rule( $css_prop, $value, $width, $height, $blur ); } ]; } @@ -140,7 +148,7 @@ public function get_button_rules() { $width = intval($secondary_values['shadowPropertiesHover']['width']); $height = intval($secondary_values['shadowPropertiesHover']['height']); - return sprintf( '%s:%s;', $css_prop, sprintf('%spx %spx %spx %s;', $width, $height, $blur, $value ) ); + return neve_sanitize_shadow_css_rule( $css_prop, $value, $width, $height, $blur ); } ]; } diff --git a/tests/test-neve-button-appearance-css.php b/tests/test-neve-button-appearance-css.php new file mode 100644 index 0000000000..b605e6e818 --- /dev/null +++ b/tests/test-neve-button-appearance-css.php @@ -0,0 +1,134 @@ +|false + */ + private $theme_mods; + + /** + * Snapshot the theme mods and read them uncached. + */ + public function setUp(): void { + parent::setUp(); + + $this->theme_mods = get_theme_mods(); + Mods::$no_cache = true; + } + + /** + * Restore the theme mods. + */ + public function tearDown(): void { + update_option( 'theme_mods_' . get_option( 'stylesheet' ), $this->theme_mods ); + Mods::$no_cache = false; + + parent::tearDown(); + } + + /** + * Button appearance saves sanitize every field that is later printed as a color. + */ + public function test_button_appearance_sanitizes_color_fields(): void { + $appearance = neve_sanitize_button_appearance( $this->injected_appearance() ); + + foreach ( $this->color_fields() as $field ) { + $this->assertSame( 'rgba(1,2,3,1)', $appearance[ $field ] ); + } + $this->assertSame( 'fill', $appearance['type'] ); + $this->assertSame( array(), neve_sanitize_button_appearance( 'invalid' ) ); + $this->assertSame( '', neve_sanitize_color_css_rule( '--color', 'invalid' ) ); + $this->assertSame( '--shadow:0px 0px 0px none;', neve_sanitize_shadow_css_rule( '--shadow', 'invalid', 0, 0, 0 ) ); + } + + /** + * Previously stored button colors are sanitized when global CSS is generated. + */ + public function test_global_button_css_sanitizes_stored_colors(): void { + $appearance = $this->injected_appearance(); + set_theme_mod( 'neve_button_appearance', $appearance ); + set_theme_mod( 'neve_secondary_button_appearance', $appearance ); + + $rules = ( new NeveButtonRulesWrapperForTest() )->get_button_rules(); + $css = (string) new Dynamic_Selector( array( ':root' => $rules ) ); + + $this->assertStringNotContainsString( 'background:lime', $css ); + $this->assertStringContainsString( '--primarybtnbg: rgba(1,2,3,1);', $css ); + $this->assertStringContainsString( '--secondarybtnhovershadow:0px 0px 5px rgba(1,2,3,1);', $css ); + } + + /** + * Previously stored component colors are sanitized when header CSS is generated. + */ + public function test_header_component_css_sanitizes_stored_colors(): void { + $appearance = $this->injected_appearance(); + set_theme_mod( 'button_base_style_setting', $appearance ); + set_theme_mod( 'nav-icon_button_appearance', $appearance ); + set_theme_mod( 'header_search_button_appearance', 'text_button' ); + set_theme_mod( 'header_search_button_style', $appearance ); + + $builder = \HFG\Main::get_instance()->get_builder( 'header' ); + $styles = $builder->add_style( array() ); + foreach ( $builder->get_components() as $component ) { + if ( in_array( $component->get_id(), array( 'button_base', 'header_search' ), true ) ) { + $styles = $component->add_style( $styles ); + } + } + + $css = (string) new Dynamic_Selector( $styles ); + + $this->assertStringNotContainsString( 'background:lime', $css ); + $this->assertStringContainsString( '--bgcolor: rgba(1,2,3,1);', $css ); + $this->assertStringContainsString( '--primarybtnbg: rgba(1,2,3,1);', $css ); + $this->assertStringContainsString( '--primarybtnhovershadow:0px 0px 5px rgba(1,2,3,1);', $css ); + } + + /** + * Button appearance whose color fields can close their CSS declaration. + * + * @return array + */ + private function injected_appearance(): array { + $appearance = neve_get_button_appearance_default(); + foreach ( $this->color_fields() as $field ) { + $appearance[ $field ] = self::INJECTED_CSS; + } + $appearance['useShadow'] = true; + $appearance['useShadowHover'] = true; + + return $appearance; + } + + /** + * Color fields stored by the button appearance control. + * + * @return string[] + */ + private function color_fields(): array { + return array( 'background', 'backgroundHover', 'text', 'textHover', 'shadowColor', 'shadowColorHover' ); + } +} From 4ef38c2ffe72c411adc5317d69cae31a8b0cbe34 Mon Sep 17 00:00:00 2001 From: selul Date: Tue, 10 Nov 2026 22:03:03 +0200 Subject: [PATCH 04/16] fix: complete style value sanitization --- globals/sanitize-functions.php | 79 ++++++++++++++++++- header-footer-grid/Core/Components/Button.php | 10 ++- .../Core/Components/MenuIcon.php | 10 ++- inc/core/styles/css_vars.php | 30 ++++--- inc/core/styles/frontend.php | 7 ++ tests/test-neve-ability-style-mods.php | 57 +++++++++++++ tests/test-neve-button-appearance-css.php | 14 +++- 7 files changed, 184 insertions(+), 23 deletions(-) diff --git a/globals/sanitize-functions.php b/globals/sanitize-functions.php index 888f836c5d..4423e1f34d 100644 --- a/globals/sanitize-functions.php +++ b/globals/sanitize-functions.php @@ -188,9 +188,31 @@ function neve_sanitize_range_value( $input ) { $range_value['desktop'] = isset( $range_value['desktop'] ) && is_numeric( $range_value['desktop'] ) ? floatval( $range_value['desktop'] ) : ''; $range_value['tablet'] = isset( $range_value['tablet'] ) && is_numeric( $range_value['tablet'] ) ? floatval( $range_value['tablet'] ) : ''; $range_value['mobile'] = isset( $range_value['mobile'] ) && is_numeric( $range_value['mobile'] ) ? floatval( $range_value['mobile'] ) : ''; + if ( isset( $range_value['suffix'] ) ) { + if ( is_array( $range_value['suffix'] ) ) { + $range_value['suffix'] = array_map( 'neve_sanitize_css_unit', $range_value['suffix'] ); + } else { + $range_value['suffix'] = neve_sanitize_css_unit( $range_value['suffix'] ); + } + } return wp_json_encode( $range_value ); } +/** + * Sanitize a CSS unit stored by a responsive range control. + * + * @param mixed $unit CSS unit. + * + * @return string + */ +function neve_sanitize_css_unit( $unit ) { + if ( ! is_string( $unit ) || ! in_array( $unit, array( 'px', 'em', 'rem', 'vh', '%' ), true ) ) { + return 'px'; + } + + return $unit; +} + /** * Sanitize font weight values. * @@ -292,10 +314,40 @@ function neve_sanitize_button_appearance( $value ) { $value[ $field ] = neve_sanitize_colors( $value[ $field ] ); } } + foreach ( array( 'borderRadius', 'borderWidth' ) as $field ) { + if ( array_key_exists( $field, $value ) ) { + $value[ $field ] = neve_sanitize_button_dimension( $value[ $field ] ); + } + } return $value; } +/** + * Sanitize the directional dimensions stored by a button appearance control. + * + * @param mixed $value Stored directional dimensions. + * + * @return array{top: int, right: int, bottom: int, left: int} + */ +function neve_sanitize_button_dimension( $value ) { + $sanitized = array_fill_keys( array( 'top', 'right', 'bottom', 'left' ), 0 ); + if ( is_numeric( $value ) ) { + return array_fill_keys( array_keys( $sanitized ), absint( $value ) ); + } + if ( ! is_array( $value ) ) { + return $sanitized; + } + + foreach ( array_keys( $sanitized ) as $direction ) { + if ( isset( $value[ $direction ] ) && is_numeric( $value[ $direction ] ) ) { + $sanitized[ $direction ] = absint( $value[ $direction ] ); + } + } + + return $sanitized; +} + /** * Format a sanitized color as a CSS custom-property rule. * @@ -305,11 +357,36 @@ function neve_sanitize_button_appearance( $value ) { * @return string */ function neve_sanitize_color_css_rule( $css_prop, $value ) { - $color = neve_sanitize_colors( $value ); + $color = 'transparent' === $value ? $value : neve_sanitize_colors( $value ); return '' === $color ? '' : sprintf( ' %s: %s;', $css_prop, $color ); } +/** + * Format sanitized button dimensions as a CSS custom-property rule. + * + * @param string $css_prop CSS custom property. + * @param mixed $value Stored directional dimensions. + * @param array $meta Dynamic selector metadata. + * @param string $device Current responsive device. + * @phpstan-param array{directional-prop: string, override?: int|string} $meta + * + * @return string + */ +function neve_sanitize_button_dimension_css_rule( $css_prop, $value, $meta, $device ) { + if ( isset( $meta['override'] ) ) { + return sprintf( '%s:%d;', $css_prop, absint( $meta['override'] ) ); + } + + return \Neve\Core\Styles\Css_Prop::transform_directional_prop( + $meta, + $device, + neve_sanitize_button_dimension( $value ), + $css_prop, + $meta['directional-prop'] + ); +} + /** * Format a sanitized button shadow as a CSS custom-property rule. * diff --git a/header-footer-grid/Core/Components/Button.php b/header-footer-grid/Core/Components/Button.php index 4124d10fd3..4d34b3e635 100644 --- a/header-footer-grid/Core/Components/Button.php +++ b/header-footer-grid/Core/Components/Button.php @@ -248,15 +248,17 @@ public function add_style( array $css_array = array() ) { Dynamic_Selector::META_FILTER => 'neve_sanitize_color_css_rule', ], '--primarybtnborderradius' => [ - Dynamic_Selector::META_KEY => $id . '.borderRadius', - 'directional-prop' => Config::CSS_PROP_BORDER_RADIUS, + Dynamic_Selector::META_KEY => $id . '.borderRadius', + Dynamic_Selector::META_FILTER => 'neve_sanitize_button_dimension_css_rule', + 'directional-prop' => Config::CSS_PROP_BORDER_RADIUS, ], ]; if ( isset( $value['type'] ) && $value['type'] === 'outline' ) { $rules['--primarybtnborderwidth'] = [ - Dynamic_Selector::META_KEY => $id . '.borderWidth', - 'directional-prop' => Config::CSS_PROP_BORDER_WIDTH, + Dynamic_Selector::META_KEY => $id . '.borderWidth', + Dynamic_Selector::META_FILTER => 'neve_sanitize_button_dimension_css_rule', + 'directional-prop' => Config::CSS_PROP_BORDER_WIDTH, ]; } diff --git a/header-footer-grid/Core/Components/MenuIcon.php b/header-footer-grid/Core/Components/MenuIcon.php index 1ba043027e..db7cd81dd5 100644 --- a/header-footer-grid/Core/Components/MenuIcon.php +++ b/header-footer-grid/Core/Components/MenuIcon.php @@ -689,12 +689,14 @@ public function add_style( array $css_array = array() ) { Dynamic_Selector::META_FILTER => 'neve_sanitize_color_css_rule', ], '--borderradius' => [ - Dynamic_Selector::META_KEY => $id . '.borderRadius', - 'directional-prop' => Config::CSS_PROP_BORDER_RADIUS, + Dynamic_Selector::META_KEY => $id . '.borderRadius', + Dynamic_Selector::META_FILTER => 'neve_sanitize_button_dimension_css_rule', + 'directional-prop' => Config::CSS_PROP_BORDER_RADIUS, ], '--borderwidth' => [ - Dynamic_Selector::META_KEY => $id . '.borderWidth', - 'directional-prop' => Config::CSS_PROP_BORDER_WIDTH, + Dynamic_Selector::META_KEY => $id . '.borderWidth', + Dynamic_Selector::META_FILTER => 'neve_sanitize_button_dimension_css_rule', + 'directional-prop' => Config::CSS_PROP_BORDER_WIDTH, ], ]; diff --git a/inc/core/styles/css_vars.php b/inc/core/styles/css_vars.php index 599fc5c903..8b0f4e7051 100644 --- a/inc/core/styles/css_vars.php +++ b/inc/core/styles/css_vars.php @@ -78,14 +78,16 @@ public function get_button_rules() { Dynamic_Selector::META_FILTER => 'neve_sanitize_color_css_rule', ], '--primarybtnborderradius' => [ - Dynamic_Selector::META_KEY => $mod_key_primary . '.borderRadius', - Dynamic_Selector::META_SUFFIX => 'px', - 'directional-prop' => Config::CSS_PROP_BORDER_RADIUS, + Dynamic_Selector::META_KEY => $mod_key_primary . '.borderRadius', + Dynamic_Selector::META_SUFFIX => 'px', + Dynamic_Selector::META_FILTER => 'neve_sanitize_button_dimension_css_rule', + 'directional-prop' => Config::CSS_PROP_BORDER_RADIUS, ], '--secondarybtnborderradius' => [ - Dynamic_Selector::META_KEY => $mod_key_secondary . '.borderRadius', - Dynamic_Selector::META_SUFFIX => 'px', - 'directional-prop' => Config::CSS_PROP_BORDER_RADIUS, + Dynamic_Selector::META_KEY => $mod_key_secondary . '.borderRadius', + Dynamic_Selector::META_SUFFIX => 'px', + Dynamic_Selector::META_FILTER => 'neve_sanitize_button_dimension_css_rule', + 'directional-prop' => Config::CSS_PROP_BORDER_RADIUS, ], ]; @@ -156,16 +158,18 @@ public function get_button_rules() { // Border Width if ( isset( $primary_values['type'] ) && $primary_values['type'] === 'outline' ) { $rules['--primarybtnborderwidth'] = [ - Dynamic_Selector::META_KEY => $mod_key_primary . '.borderWidth', - Dynamic_Selector::META_SUFFIX => 'px', - 'directional-prop' => Config::CSS_PROP_BORDER_WIDTH, + Dynamic_Selector::META_KEY => $mod_key_primary . '.borderWidth', + Dynamic_Selector::META_SUFFIX => 'px', + Dynamic_Selector::META_FILTER => 'neve_sanitize_button_dimension_css_rule', + 'directional-prop' => Config::CSS_PROP_BORDER_WIDTH, ]; } if ( isset( $secondary_values['type'] ) && $secondary_values['type'] === 'outline' ) { $rules['--secondarybtnborderwidth'] = [ - Dynamic_Selector::META_KEY => $mod_key_secondary . '.borderWidth', - Dynamic_Selector::META_SUFFIX => 'px', - 'directional-prop' => Config::CSS_PROP_BORDER_WIDTH, + Dynamic_Selector::META_KEY => $mod_key_secondary . '.borderWidth', + Dynamic_Selector::META_SUFFIX => 'px', + Dynamic_Selector::META_FILTER => 'neve_sanitize_button_dimension_css_rule', + 'directional-prop' => Config::CSS_PROP_BORDER_WIDTH, ]; } @@ -197,7 +201,7 @@ public function get_button_rules() { if ( ! isset( $appearance_values['type'] ) || $appearance_values['type'] !== 'outline' ) { continue; } - $border_width = $appearance_values['borderWidth']; + $border_width = neve_sanitize_button_dimension( $appearance_values['borderWidth'] ); foreach ( $paddings[ $btn_type ] as $direction => $padding_value ) { if ( ! isset( $border_width[ $direction ] ) || absint( $border_width[ $direction ] ) === 0 ) { diff --git a/inc/core/styles/frontend.php b/inc/core/styles/frontend.php index caae4504c0..a834007b2c 100644 --- a/inc/core/styles/frontend.php +++ b/inc/core/styles/frontend.php @@ -592,14 +592,17 @@ private function setup_form_fields_style() { '--formfieldbgcolor' => [ Dynamic_Selector::META_KEY => Config::MODS_FORM_FIELDS_BACKGROUND_COLOR, Dynamic_Selector::META_DEFAULT => 'var(--nv-site-bg)', + Dynamic_Selector::META_FILTER => 'neve_sanitize_color_css_rule', ], '--formfieldbordercolor' => [ Dynamic_Selector::META_KEY => Config::MODS_FORM_FIELDS_BORDER_COLOR, Dynamic_Selector::META_DEFAULT => '#8c8c8c', + Dynamic_Selector::META_FILTER => 'neve_sanitize_color_css_rule', ], '--formfieldcolor' => [ Dynamic_Selector::META_KEY => Config::MODS_FORM_FIELDS_COLOR, Dynamic_Selector::META_DEFAULT => 'var(--nv-text-color)', + Dynamic_Selector::META_FILTER => 'neve_sanitize_color_css_rule', ], '--formfieldpadding' => [ Dynamic_Selector::META_KEY => Config::MODS_FORM_FIELDS_PADDING, @@ -1099,6 +1102,7 @@ private function get_scroll_to_top_rules() { '--color' => [ Dynamic_Selector::META_KEY => 'neve_scroll_to_top_icon_color', Dynamic_Selector::META_DEFAULT => empty( Mods::get( 'neve_scroll_to_top_icon_color', 'var(--nv-text-dark-bg)' ) ) ? 'transparent' : 'var(--nv-text-dark-bg)', + Dynamic_Selector::META_FILTER => 'neve_sanitize_color_css_rule', ], '--padding' => [ Dynamic_Selector::META_KEY => 'neve_scroll_to_top_padding', @@ -1137,14 +1141,17 @@ private function get_scroll_to_top_rules() { '--bgcolor' => [ Dynamic_Selector::META_KEY => 'neve_scroll_to_top_background_color', Dynamic_Selector::META_DEFAULT => empty( Mods::get( 'neve_scroll_to_top_background_color', 'var(--nv-primary-accent)' ) ) ? 'transparent' : 'var(--nv-primary-accent)', + Dynamic_Selector::META_FILTER => 'neve_sanitize_color_css_rule', ], '--hovercolor' => [ Dynamic_Selector::META_KEY => 'neve_scroll_to_top_icon_hover_color', Dynamic_Selector::META_DEFAULT => empty( Mods::get( 'neve_scroll_to_top_icon_hover_color', 'var(--nv-text-dark-bg)' ) ) ? 'transparent' : 'var(--nv-text-dark-bg)', + Dynamic_Selector::META_FILTER => 'neve_sanitize_color_css_rule', ], '--hoverbgcolor' => [ Dynamic_Selector::META_KEY => 'neve_scroll_to_top_background_hover_color', Dynamic_Selector::META_DEFAULT => empty( Mods::get( 'neve_scroll_to_top_background_hover_color', 'var(--nv-primary-accent)' ) ) ? 'transparent' : 'var(--nv-primary-accent)', + Dynamic_Selector::META_FILTER => 'neve_sanitize_color_css_rule', ], '--size' => [ Dynamic_Selector::META_KEY => 'neve_scroll_to_top_icon_size', diff --git a/tests/test-neve-ability-style-mods.php b/tests/test-neve-ability-style-mods.php index ed9349ae80..ff59331770 100644 --- a/tests/test-neve-ability-style-mods.php +++ b/tests/test-neve-ability-style-mods.php @@ -8,6 +8,8 @@ use Neve\Abilities\Footer_Configure; use Neve\Abilities\Header_Configure; use Neve\Abilities\Site_Set_Theme_Mod; +use Neve\Core\Settings\Mods; +use Neve\Core\Styles\Frontend; /** * Class TestNeveAbilityStyleMods @@ -39,6 +41,7 @@ public function setUp(): void { wp_set_current_user( 1 ); $this->theme_mods = get_theme_mods(); + Mods::$no_cache = true; } /** @@ -46,6 +49,7 @@ public function setUp(): void { */ public function tearDown(): void { update_option( 'theme_mods_' . get_option( 'stylesheet' ), $this->theme_mods ); + Mods::$no_cache = false; parent::tearDown(); } @@ -200,6 +204,32 @@ public function test_site_set_theme_mod_sanitizes_sidebar_width(): void { $this->assertSame( 360.0, get_theme_mod( self::SIDEBAR_WIDTH ) ); } + /** + * Responsive sidebar width units are restricted to safe CSS unit strings. + */ + public function test_site_set_theme_mod_sanitizes_sidebar_width_units(): void { + $width = 360; + foreach ( array( self::INJECTED_CSS, array( 'mobile' => self::INJECTED_CSS ) ) as $suffix ) { + $result = ( new Site_Set_Theme_Mod() )->execute( + array( + 'action' => 'set', + 'key' => self::SIDEBAR_WIDTH, + 'value' => array( + 'mobile' => $width, + 'tablet' => $width, + 'desktop' => $width, + 'suffix' => $suffix, + ), + ) + ); + + $this->assertIsArray( $result ); + $this->assertTrue( $result['success'] ); + $stored = json_decode( get_theme_mod( self::SIDEBAR_WIDTH ), true ); + $this->assertSame( 'px', is_array( $stored['suffix'] ) ? $stored['suffix']['mobile'] : $stored['suffix'] ); + } + } + /** * A spacing value that would close its CSS declaration is rejected and the stored value kept. */ @@ -300,4 +330,31 @@ public function test_site_set_theme_mod_sanitizes_colors(): void { $this->assertTrue( $result['success'] ); $this->assertSame( 'rgba(1,2,3,1)', get_theme_mod( self::SIDEBAR_TEXT ) ); } + + /** + * Previously stored ability color settings are sanitized when CSS is generated. + */ + public function test_stored_ability_colors_are_sanitized_when_css_is_generated(): void { + $color_mods = array( + 'neve_form_fields_background_color', + 'neve_form_fields_border_color', + 'neve_input_text_color', + 'neve_scroll_to_top_icon_color', + 'neve_scroll_to_top_icon_hover_color', + 'neve_scroll_to_top_background_color', + 'neve_scroll_to_top_background_hover_color', + ); + foreach ( $color_mods as $mod ) { + set_theme_mod( $mod, self::INJECTED_CSS ); + } + set_theme_mod( 'neve_scroll_to_top_status', '1' ); + + $css = (string) ( new Frontend() )->generate(); + + $this->assertStringNotContainsString( 'background:lime', $css ); + $this->assertStringContainsString( '--formfieldbgcolor: rgba(1,2,3,1);', $css ); + $this->assertStringContainsString( '--formfieldbordercolor: rgba(1,2,3,1);', $css ); + $this->assertStringContainsString( '--formfieldcolor: rgba(1,2,3,1);', $css ); + $this->assertStringContainsString( '--hoverbgcolor: rgba(1,2,3,1);', $css ); + } } diff --git a/tests/test-neve-button-appearance-css.php b/tests/test-neve-button-appearance-css.php index b605e6e818..175915b66c 100644 --- a/tests/test-neve-button-appearance-css.php +++ b/tests/test-neve-button-appearance-css.php @@ -59,9 +59,14 @@ public function test_button_appearance_sanitizes_color_fields(): void { foreach ( $this->color_fields() as $field ) { $this->assertSame( 'rgba(1,2,3,1)', $appearance[ $field ] ); } - $this->assertSame( 'fill', $appearance['type'] ); + foreach ( array( 'borderRadius', 'borderWidth' ) as $field ) { + $this->assertSame( array_fill_keys( array( 'top', 'right', 'bottom', 'left' ), 0 ), $appearance[ $field ] ); + } + $this->assertSame( 'outline', $appearance['type'] ); $this->assertSame( array(), neve_sanitize_button_appearance( 'invalid' ) ); $this->assertSame( '', neve_sanitize_color_css_rule( '--color', 'invalid' ) ); + $this->assertSame( ' --color: transparent;', neve_sanitize_color_css_rule( '--color', 'transparent' ) ); + $this->assertSame( array_fill_keys( array( 'top', 'right', 'bottom', 'left' ), 8 ), neve_sanitize_button_dimension( 8 ) ); $this->assertSame( '--shadow:0px 0px 0px none;', neve_sanitize_shadow_css_rule( '--shadow', 'invalid', 0, 0, 0 ) ); } @@ -79,6 +84,8 @@ public function test_global_button_css_sanitizes_stored_colors(): void { $this->assertStringNotContainsString( 'background:lime', $css ); $this->assertStringContainsString( '--primarybtnbg: rgba(1,2,3,1);', $css ); $this->assertStringContainsString( '--secondarybtnhovershadow:0px 0px 5px rgba(1,2,3,1);', $css ); + $this->assertStringContainsString( '--primarybtnborderradius:0;', $css ); + $this->assertStringContainsString( '--primarybtnborderwidth:0;', $css ); } /** @@ -105,6 +112,7 @@ public function test_header_component_css_sanitizes_stored_colors(): void { $this->assertStringContainsString( '--bgcolor: rgba(1,2,3,1);', $css ); $this->assertStringContainsString( '--primarybtnbg: rgba(1,2,3,1);', $css ); $this->assertStringContainsString( '--primarybtnhovershadow:0px 0px 5px rgba(1,2,3,1);', $css ); + $this->assertStringContainsString( '--primarybtnborderradius:0;', $css ); } /** @@ -117,6 +125,10 @@ private function injected_appearance(): array { foreach ( $this->color_fields() as $field ) { $appearance[ $field ] = self::INJECTED_CSS; } + foreach ( array( 'borderRadius', 'borderWidth' ) as $field ) { + $appearance[ $field ] = array_fill_keys( array( 'top', 'right', 'bottom', 'left' ), '10;}body{background:lime}/*' ); + } + $appearance['type'] = 'outline'; $appearance['useShadow'] = true; $appearance['useShadowHover'] = true; From e04f68d5c6e44b01b49e549fc55506f4cb849a17 Mon Sep 17 00:00:00 2001 From: selul Date: Tue, 10 Nov 2026 22:13:52 +0200 Subject: [PATCH 05/16] fix: sanitize stored style units --- globals/sanitize-functions.php | 70 ++++++++++++++++--- .../Core/Builder/Abstract_Builder.php | 3 + inc/core/styles/css_prop.php | 14 ++-- inc/core/styles/frontend.php | 5 ++ tests/test-neve-ability-style-mods.php | 61 +++++++++++++++- 5 files changed, 140 insertions(+), 13 deletions(-) diff --git a/globals/sanitize-functions.php b/globals/sanitize-functions.php index 4423e1f34d..57a3177f48 100644 --- a/globals/sanitize-functions.php +++ b/globals/sanitize-functions.php @@ -362,6 +362,66 @@ function neve_sanitize_color_css_rule( $css_prop, $value ) { return '' === $color ? '' : sprintf( ' %s: %s;', $css_prop, $color ); } +/** + * Format sanitized directional dimensions as a CSS custom-property rule. + * + * @param string $css_prop CSS custom property. + * @param mixed $value Stored directional dimensions. + * @param array $meta Dynamic selector metadata. + * @param string $device Current responsive device. + * @phpstan-param array{directional-prop: string, key: string, suffix?: string, is_responsive?: bool} $meta + * + * @return string + */ +function neve_sanitize_directional_css_rule( $css_prop, $value, $meta, $device ) { + $sanitized = array_fill_keys( array( 'top', 'right', 'bottom', 'left' ), 0 ); + if ( is_array( $value ) ) { + foreach ( array_keys( $sanitized ) as $direction ) { + if ( isset( $value[ $direction ] ) && is_numeric( $value[ $direction ] ) ) { + $sanitized[ $direction ] = (float) $value[ $direction ]; + } + } + if ( isset( $value['unit'] ) ) { + $sanitized['unit'] = neve_sanitize_css_unit( $value['unit'] ); + } + } + + return \Neve\Core\Styles\Css_Prop::transform_directional_prop( $meta, $device, $sanitized, $css_prop, $meta['directional-prop'] ); +} + +/** + * Format a sanitized numeric value as a CSS custom-property rule. + * + * @param string $css_prop CSS custom property. + * @param mixed $value Stored numeric value. + * @param array $meta Dynamic selector metadata. + * @phpstan-param array{suffix?: string} $meta + * + * @return string + */ +function neve_sanitize_numeric_css_rule( $css_prop, $value, $meta ) { + $suffix = isset( $meta['suffix'] ) ? $meta['suffix'] : ''; + + return sprintf( ' %s: %d%s;', $css_prop, absint( $value ), $suffix ); +} + +/** + * Format a sanitized responsive range as a CSS rule. + * + * @param string $css_prop CSS property. + * @param mixed $value Stored range value for the current device. + * @param array $meta Dynamic selector metadata. + * @param string $device Current responsive device. + * @phpstan-param array{key: string, default?: mixed, as_json?: mixed} $meta + * + * @return string + */ +function neve_sanitize_responsive_range_css_rule( $css_prop, $value, $meta, $device ) { + $suffix = \Neve\Core\Styles\Css_Prop::get_suffix_responsive( $meta, $device ); + + return sprintf( '%s: %s%s;', $css_prop, (float) $value, $suffix ); +} + /** * Format sanitized button dimensions as a CSS custom-property rule. * @@ -369,7 +429,7 @@ function neve_sanitize_color_css_rule( $css_prop, $value ) { * @param mixed $value Stored directional dimensions. * @param array $meta Dynamic selector metadata. * @param string $device Current responsive device. - * @phpstan-param array{directional-prop: string, override?: int|string} $meta + * @phpstan-param array{directional-prop: string, key: string, override?: int|string} $meta * * @return string */ @@ -378,13 +438,7 @@ function neve_sanitize_button_dimension_css_rule( $css_prop, $value, $meta, $dev return sprintf( '%s:%d;', $css_prop, absint( $meta['override'] ) ); } - return \Neve\Core\Styles\Css_Prop::transform_directional_prop( - $meta, - $device, - neve_sanitize_button_dimension( $value ), - $css_prop, - $meta['directional-prop'] - ); + return neve_sanitize_directional_css_rule( $css_prop, neve_sanitize_button_dimension( $value ), $meta, $device ); } /** diff --git a/header-footer-grid/Core/Builder/Abstract_Builder.php b/header-footer-grid/Core/Builder/Abstract_Builder.php index 1ddc9833e3..ec29dc2aaa 100644 --- a/header-footer-grid/Core/Builder/Abstract_Builder.php +++ b/header-footer-grid/Core/Builder/Abstract_Builder.php @@ -2144,6 +2144,7 @@ private function add_sidebar_styles( $css_array ) { Dynamic_Selector::META_DEFAULT => $default_sidebar_width, Dynamic_Selector::META_SUFFIX => 'responsive_suffix', Dynamic_Selector::META_IS_RESPONSIVE => true, + Dynamic_Selector::META_FILTER => 'neve_sanitize_responsive_range_css_rule', ], ], ]; @@ -2157,6 +2158,7 @@ private function add_sidebar_styles( $css_array ) { Dynamic_Selector::META_DEFAULT => $default_sidebar_width, Dynamic_Selector::META_SUFFIX => 'responsive_suffix', Dynamic_Selector::META_IS_RESPONSIVE => true, + Dynamic_Selector::META_FILTER => 'neve_sanitize_responsive_range_css_rule', ], ], ]; @@ -2170,6 +2172,7 @@ private function add_sidebar_styles( $css_array ) { Dynamic_Selector::META_DEFAULT => $default_sidebar_width, Dynamic_Selector::META_SUFFIX => 'responsive_suffix', Dynamic_Selector::META_IS_RESPONSIVE => true, + Dynamic_Selector::META_FILTER => 'neve_sanitize_responsive_range_css_rule', ], ], ]; diff --git a/inc/core/styles/css_prop.php b/inc/core/styles/css_prop.php index 5c3e96659e..bf27c69bcb 100644 --- a/inc/core/styles/css_prop.php +++ b/inc/core/styles/css_prop.php @@ -35,7 +35,7 @@ public static function get_unit_responsive( $meta, $device ) { $suffix = $all_value['unit']; } - return $suffix; + return neve_sanitize_css_unit( $suffix ); } /** @@ -57,7 +57,9 @@ public static function get_suffix_responsive( $meta, $device ) { } } - return isset( $all_value['suffix'][ $device ] ) ? $all_value['suffix'][ $device ] : ( isset( $all_value['suffix'] ) && is_string( $all_value['suffix'] ) ? $all_value['suffix'] : 'px' );; + $suffix = isset( $all_value['suffix'][ $device ] ) ? $all_value['suffix'][ $device ] : ( isset( $all_value['suffix'] ) && is_string( $all_value['suffix'] ) ? $all_value['suffix'] : 'px' ); + + return neve_sanitize_css_unit( $suffix ); } /** @@ -259,13 +261,17 @@ public static function get_suffix( $meta, $device, $value, $css_prop ) { // If not responsive, most controls use 'unit' key inside value. if ( ! isset( $meta['is_responsive'] ) || $meta['is_responsive'] === false ) { - $suffix = isset( $value['unit'] ) ? $value['unit'] : $suffix; + $suffix = isset( $value['unit'] ) ? neve_sanitize_css_unit( $value['unit'] ) : $suffix; } // If responsive, try to find the suffix. if ( isset( $meta[ Dynamic_Selector::META_IS_RESPONSIVE ] ) && $meta[ Dynamic_Selector::META_IS_RESPONSIVE ] ) { $all_value = Mods::get( $meta['key'] ); - $suffix = isset( $all_value['suffix'][ $device ] ) ? $all_value['suffix'][ $device ] : ( isset( $all_value['suffix'] ) ? $all_value['suffix'] : $suffix ); + if ( isset( $all_value['suffix'][ $device ] ) ) { + $suffix = neve_sanitize_css_unit( $all_value['suffix'][ $device ] ); + } elseif ( isset( $all_value['suffix'] ) ) { + $suffix = neve_sanitize_css_unit( $all_value['suffix'] ); + } } if ( $suffix === 'responsive_unit' ) { diff --git a/inc/core/styles/frontend.php b/inc/core/styles/frontend.php index a834007b2c..bf83dbf7be 100644 --- a/inc/core/styles/frontend.php +++ b/inc/core/styles/frontend.php @@ -581,12 +581,14 @@ private function setup_form_fields_style() { Dynamic_Selector::META_KEY => Config::MODS_FORM_FIELDS_BORDER_WIDTH, Dynamic_Selector::META_SUFFIX => 'px', Dynamic_Selector::META_DEFAULT => $border_width_default, + Dynamic_Selector::META_FILTER => 'neve_sanitize_directional_css_rule', 'directional-prop' => Config::CSS_PROP_BORDER_WIDTH, ], '--formfieldborderradius' => [ Dynamic_Selector::META_KEY => Config::MODS_FORM_FIELDS_BORDER_RADIUS, Dynamic_Selector::META_SUFFIX => 'px', Dynamic_Selector::META_DEFAULT => $border_radius_default, + Dynamic_Selector::META_FILTER => 'neve_sanitize_directional_css_rule', 'directional-prop' => Config::CSS_PROP_BORDER_RADIUS, ], '--formfieldbgcolor' => [ @@ -609,6 +611,7 @@ private function setup_form_fields_style() { Dynamic_Selector::META_DEFAULT => Mods::get_alternative_mod_default( Config::MODS_FORM_FIELDS_PADDING ), Dynamic_Selector::META_SUFFIX => 'px', Dynamic_Selector::META_IS_RESPONSIVE => false, + Dynamic_Selector::META_FILTER => 'neve_sanitize_directional_css_rule', 'directional-prop' => Config::CSS_PROP_PADDING, ], '--formfieldtexttransform' => [ @@ -1108,6 +1111,7 @@ private function get_scroll_to_top_rules() { Dynamic_Selector::META_KEY => 'neve_scroll_to_top_padding', Dynamic_Selector::META_IS_RESPONSIVE => true, Dynamic_Selector::META_SUFFIX => 'responsive_unit', + Dynamic_Selector::META_FILTER => 'neve_sanitize_directional_css_rule', 'directional-prop' => Config::CSS_PROP_PADDING, Dynamic_Selector::META_DEFAULT => array( 'desktop' => array( @@ -1137,6 +1141,7 @@ private function get_scroll_to_top_rules() { Dynamic_Selector::META_KEY => 'neve_scroll_to_top_border_radius', Dynamic_Selector::META_DEFAULT => 3, Dynamic_Selector::META_SUFFIX => 'px', + Dynamic_Selector::META_FILTER => 'neve_sanitize_numeric_css_rule', ], '--bgcolor' => [ Dynamic_Selector::META_KEY => 'neve_scroll_to_top_background_color', diff --git a/tests/test-neve-ability-style-mods.php b/tests/test-neve-ability-style-mods.php index ff59331770..a3af4c5774 100644 --- a/tests/test-neve-ability-style-mods.php +++ b/tests/test-neve-ability-style-mods.php @@ -9,6 +9,7 @@ use Neve\Abilities\Header_Configure; use Neve\Abilities\Site_Set_Theme_Mod; use Neve\Core\Settings\Mods; +use Neve\Core\Styles\Dynamic_Selector; use Neve\Core\Styles\Frontend; /** @@ -230,6 +231,43 @@ public function test_site_set_theme_mod_sanitizes_sidebar_width_units(): void { } } + /** + * Previously stored sidebar width values and units are sanitized when CSS is generated. + */ + public function test_stored_sidebar_width_is_sanitized_when_css_is_generated(): void { + $devices = array( 'mobile', 'tablet', 'desktop' ); + $width = '360;}body{background:lime}/*'; + foreach ( array( self::INJECTED_CSS, array_fill_keys( $devices, self::INJECTED_CSS ) ) as $suffix ) { + set_theme_mod( + self::SIDEBAR_WIDTH, + wp_json_encode( + array( + 'mobile' => $width, + 'tablet' => $width, + 'desktop' => $width, + 'suffix' => $suffix, + ) + ) + ); + + foreach ( array( 'slide_left', 'pull_left', 'pull_right' ) as $layout ) { + set_theme_mod( 'hfg_header_layout_sidebar_layout', $layout ); + $builder = \HFG\Main::get_instance()->get_builder( 'header' ); + $selector = new Dynamic_Selector( $builder->add_style( array() ) ); + $css = (string) $selector->for_mobile() . (string) $selector->for_tablet() . (string) $selector->for_desktop(); + + $this->assertStringNotContainsString( 'background:lime', $css ); + $this->assertStringContainsString( 'width: 360px;', $css ); + if ( 'pull_left' === $layout ) { + $this->assertStringContainsString( 'left: 360px;', $css ); + } + if ( 'pull_right' === $layout ) { + $this->assertStringContainsString( 'right: 360px;', $css ); + } + } + } + } + /** * A spacing value that would close its CSS declaration is rejected and the stored value kept. */ @@ -334,7 +372,7 @@ public function test_site_set_theme_mod_sanitizes_colors(): void { /** * Previously stored ability color settings are sanitized when CSS is generated. */ - public function test_stored_ability_colors_are_sanitized_when_css_is_generated(): void { + public function test_stored_ability_styles_are_sanitized_when_css_is_generated(): void { $color_mods = array( 'neve_form_fields_background_color', 'neve_form_fields_border_color', @@ -347,6 +385,23 @@ public function test_stored_ability_colors_are_sanitized_when_css_is_generated() foreach ( $color_mods as $mod ) { set_theme_mod( $mod, self::INJECTED_CSS ); } + $directional = array_fill_keys( array( 'top', 'right', 'bottom', 'left' ), 2 ); + $directional['unit'] = self::INJECTED_CSS; + $invalid_directional = array_fill_keys( array( 'top', 'right', 'bottom', 'left' ), '2;}body{background:lime}/*' ); + $invalid_directional['unit'] = 'px'; + set_theme_mod( 'neve_form_fields_border_width', $directional ); + set_theme_mod( 'neve_form_fields_border_radius', $invalid_directional ); + set_theme_mod( 'neve_form_fields_padding', $directional ); + $scroll_padding = array( + 'mobile' => $directional, + 'tablet' => $directional, + 'desktop' => $directional, + 'mobile-unit' => self::INJECTED_CSS, + 'tablet-unit' => self::INJECTED_CSS, + 'desktop-unit' => self::INJECTED_CSS, + ); + set_theme_mod( 'neve_scroll_to_top_padding', $scroll_padding ); + set_theme_mod( 'neve_scroll_to_top_border_radius', '3;}body{background:lime}/*' ); set_theme_mod( 'neve_scroll_to_top_status', '1' ); $css = (string) ( new Frontend() )->generate(); @@ -355,6 +410,10 @@ public function test_stored_ability_colors_are_sanitized_when_css_is_generated() $this->assertStringContainsString( '--formfieldbgcolor: rgba(1,2,3,1);', $css ); $this->assertStringContainsString( '--formfieldbordercolor: rgba(1,2,3,1);', $css ); $this->assertStringContainsString( '--formfieldcolor: rgba(1,2,3,1);', $css ); + $this->assertStringContainsString( '--formfieldborderwidth:2px;', $css ); + $this->assertStringContainsString( '--formfieldborderradius:0;', $css ); + $this->assertStringContainsString( '--formfieldpadding:2px;', $css ); + $this->assertStringContainsString( '--borderradius: 3px;', $css ); $this->assertStringContainsString( '--hoverbgcolor: rgba(1,2,3,1);', $css ); } } From 7b6e613fbbcb6b0503c53a3753d71516b4ab2bcb Mon Sep 17 00:00:00 2001 From: selul Date: Tue, 10 Nov 2026 22:20:27 +0200 Subject: [PATCH 06/16] fix: preserve supported style formats --- globals/sanitize-functions.php | 6 ++- inc/core/styles/css_prop.php | 8 ++- tests/test-neve-ability-style-mods.php | 67 ++++++++++++++++++++++---- 3 files changed, 68 insertions(+), 13 deletions(-) diff --git a/globals/sanitize-functions.php b/globals/sanitize-functions.php index 57a3177f48..dae87da714 100644 --- a/globals/sanitize-functions.php +++ b/globals/sanitize-functions.php @@ -206,7 +206,7 @@ function neve_sanitize_range_value( $input ) { * @return string */ function neve_sanitize_css_unit( $unit ) { - if ( ! is_string( $unit ) || ! in_array( $unit, array( 'px', 'em', 'rem', 'vh', '%' ), true ) ) { + if ( ! is_string( $unit ) || ! in_array( $unit, array( 'px', 'em', 'rem', 'vh', '%', '—' ), true ) ) { return 'px'; } @@ -375,7 +375,9 @@ function neve_sanitize_color_css_rule( $css_prop, $value ) { */ function neve_sanitize_directional_css_rule( $css_prop, $value, $meta, $device ) { $sanitized = array_fill_keys( array( 'top', 'right', 'bottom', 'left' ), 0 ); - if ( is_array( $value ) ) { + if ( is_numeric( $value ) ) { + $sanitized = array_fill_keys( array_keys( $sanitized ), (float) $value ); + } elseif ( is_array( $value ) ) { foreach ( array_keys( $sanitized ) as $direction ) { if ( isset( $value[ $direction ] ) && is_numeric( $value[ $direction ] ) ) { $sanitized[ $direction ] = (float) $value[ $direction ]; diff --git a/inc/core/styles/css_prop.php b/inc/core/styles/css_prop.php index bf27c69bcb..79188327d2 100644 --- a/inc/core/styles/css_prop.php +++ b/inc/core/styles/css_prop.php @@ -35,7 +35,9 @@ public static function get_unit_responsive( $meta, $device ) { $suffix = $all_value['unit']; } - return neve_sanitize_css_unit( $suffix ); + $suffix = neve_sanitize_css_unit( $suffix ); + + return '—' === $suffix ? '' : $suffix; } /** @@ -59,7 +61,9 @@ public static function get_suffix_responsive( $meta, $device ) { $suffix = isset( $all_value['suffix'][ $device ] ) ? $all_value['suffix'][ $device ] : ( isset( $all_value['suffix'] ) && is_string( $all_value['suffix'] ) ? $all_value['suffix'] : 'px' ); - return neve_sanitize_css_unit( $suffix ); + $suffix = neve_sanitize_css_unit( $suffix ); + + return '—' === $suffix ? '' : $suffix; } /** diff --git a/tests/test-neve-ability-style-mods.php b/tests/test-neve-ability-style-mods.php index a3af4c5774..4fe90a3f59 100644 --- a/tests/test-neve-ability-style-mods.php +++ b/tests/test-neve-ability-style-mods.php @@ -26,6 +26,7 @@ class TestNeveAbilityStyleMods extends WP_UnitTestCase { const SIDEBAR_WIDTH = 'hfg_header_layout_sidebar_width'; const HEADER_HEIGHT = 'hfg_header_layout_main_height'; const FORM_PADDING = 'neve_form_fields_padding'; + const LINE_HEIGHT = 1.5; /** * Theme mods snapshot restored after each test. @@ -254,20 +255,67 @@ public function test_stored_sidebar_width_is_sanitized_when_css_is_generated(): set_theme_mod( 'hfg_header_layout_sidebar_layout', $layout ); $builder = \HFG\Main::get_instance()->get_builder( 'header' ); $selector = new Dynamic_Selector( $builder->add_style( array() ) ); - $css = (string) $selector->for_mobile() . (string) $selector->for_tablet() . (string) $selector->for_desktop(); - - $this->assertStringNotContainsString( 'background:lime', $css ); - $this->assertStringContainsString( 'width: 360px;', $css ); - if ( 'pull_left' === $layout ) { - $this->assertStringContainsString( 'left: 360px;', $css ); - } - if ( 'pull_right' === $layout ) { - $this->assertStringContainsString( 'right: 360px;', $css ); + $css_by_device = array( + 'mobile' => (string) $selector->for_mobile(), + 'tablet' => (string) $selector->for_tablet(), + 'desktop' => (string) $selector->for_desktop(), + ); + + foreach ( $css_by_device as $css ) { + $this->assertStringNotContainsString( 'background:lime', $css ); + $this->assertStringContainsString( 'width: 360px;', $css ); + if ( 'pull_left' === $layout ) { + $this->assertStringContainsString( 'left: 360px;', $css ); + } + if ( 'pull_right' === $layout ) { + $this->assertStringContainsString( 'right: 360px;', $css ); + } } } } } + /** + * Responsive line heights remain unitless when configured without a unit. + */ + public function test_unitless_line_height_is_preserved(): void { + $devices = array( 'mobile', 'tablet', 'desktop' ); + set_theme_mod( + 'neve_typeface_general', + array( + 'lineHeight' => array( + 'mobile' => self::LINE_HEIGHT, + 'tablet' => self::LINE_HEIGHT, + 'desktop' => self::LINE_HEIGHT, + 'suffix' => array_fill_keys( $devices, '—' ), + ), + ) + ); + + $css = (string) ( new Frontend() )->generate(); + + $this->assertSame( count( $devices ), substr_count( $css, '--bodylineheight: ' . self::LINE_HEIGHT . ';' ) ); + $this->assertStringNotContainsString( '--bodylineheight: ' . self::LINE_HEIGHT . 'px;', $css ); + } + + /** + * Legacy scalar directional settings expand across all directions. + */ + public function test_scalar_directional_values_are_preserved(): void { + set_theme_mod( 'neve_form_fields_padding', 12 ); + set_theme_mod( 'neve_form_fields_border_width', 2 ); + set_theme_mod( 'neve_form_fields_border_radius', 3 ); + set_theme_mod( 'neve_scroll_to_top_padding', 4 ); + set_theme_mod( 'neve_scroll_to_top_status', '1' ); + + $css = (string) ( new Frontend() )->generate(); + + $this->assertStringContainsString( '--formfieldpadding:12px;', $css ); + $this->assertStringContainsString( '--formfieldborderwidth:2px;', $css ); + $this->assertStringContainsString( '--formfieldborderradius:3px;', $css ); + $this->assertSame( 3, substr_count( $css, '--padding:4px;' ) ); + } + /** * A spacing value that would close its CSS declaration is rejected and the stored value kept. */ @@ -413,6 +461,7 @@ public function test_stored_ability_styles_are_sanitized_when_css_is_generated() $this->assertStringContainsString( '--formfieldborderwidth:2px;', $css ); $this->assertStringContainsString( '--formfieldborderradius:0;', $css ); $this->assertStringContainsString( '--formfieldpadding:2px;', $css ); + $this->assertStringContainsString( '--padding:2px;', $css ); $this->assertStringContainsString( '--borderradius: 3px;', $css ); $this->assertStringContainsString( '--hoverbgcolor: rgba(1,2,3,1);', $css ); } From b3678277f93b770d917d0adbfb676e47e1307979 Mon Sep 17 00:00:00 2001 From: selul Date: Tue, 10 Nov 2026 23:46:13 +0200 Subject: [PATCH 07/16] fix: harden stored style values --- globals/sanitize-functions.php | 105 +++++++- .../Core/Components/Abstract_Component.php | 4 +- .../Core/Components/MenuIcon.php | 1 + header-footer-grid/Core/Components/Search.php | 2 + .../Core/Components/SearchResponsive.php | 2 + header-footer-grid/Traits/Core.php | 26 +- inc/abilities/abstract_ability.php | 11 +- inc/abilities/appearance_update.php | 19 +- inc/core/dynamic_css.php | 4 +- inc/core/styles/css_prop.php | 26 +- inc/core/styles/frontend.php | 6 + inc/customizer/options/buttons.php | 3 +- inc/customizer/options/form_fields.php | 6 +- inc/customizer/options/typography.php | 21 +- tests/test-neve-ability-style-mods.php | 247 ++++++++++++++++++ tests/test-neve-row-color-css.php | 21 ++ tests/test-neve-sanitization.php | 20 ++ 17 files changed, 490 insertions(+), 34 deletions(-) diff --git a/globals/sanitize-functions.php b/globals/sanitize-functions.php index dae87da714..a34140b1c1 100644 --- a/globals/sanitize-functions.php +++ b/globals/sanitize-functions.php @@ -56,7 +56,9 @@ function neve_is_css_gradient( string $value ): bool { * @return bool */ function neve_is_css_function_color( string $value ): bool { - return (bool) preg_match( '/^(?:rgb|rgba|hsl|hsla)\(\s*[-0-9.% ,]+\)$/', trim( $value ) ); + $component = '[+-]?(?:\d+(?:\.\d*)?|\.\d+)%?'; + + return (bool) preg_match( '/^(?:rgb|rgba|hsl|hsla)\(\s*' . $component . '(?:\s*,\s*' . $component . '){2,3}\s*\)$/', trim( $value ) ); } /** @@ -118,7 +120,12 @@ function neve_sanitize_colors( $value ) { // Is this an rgba color or a hex? $mode = ( false === strpos( $value, 'rgba' ) ) ? 'hex' : 'rgba'; if ( 'rgba' === $mode ) { - return neve_sanitize_rgba( $value ); + $component = '[+-]?(?:\d+(?:\.\d*)?|\.\d+)%?'; + if ( ! preg_match( '/^rgba\(\s*' . $component . '(?:\s*,\s*' . $component . '){2,3}\s*\)/', trim( $value ), $match ) ) { + return ''; + } + + return neve_sanitize_rgba( $match[0] ); } $hex_color = sanitize_hex_color( $value ); @@ -126,6 +133,57 @@ function neve_sanitize_colors( $value ) { return null === $hex_color ? '' : $hex_color; } +/** + * Sanitize row text colors, which historically allow CSS named colors. + * + * @param mixed $value Row text color. + * + * @return string + */ +function neve_sanitize_row_text_color( $value ) { + if ( ! is_string( $value ) ) { + return ''; + } + + if ( '' === $value ) { + return ''; + } + + $color = neve_sanitize_colors( $value ); + if ( '' !== $color ) { + return $color; + } + + if ( neve_closes_css_declaration( $value ) ) { + return ''; + } + + $named_colors = explode( + ' ', + 'aliceblue antiquewhite aqua aquamarine azure beige bisque black blanchedalmond blue blueviolet brown burlywood cadetblue chartreuse chocolate coral cornflowerblue cornsilk crimson cyan darkblue darkcyan darkgoldenrod darkgray darkgreen darkgrey darkkhaki darkmagenta darkolivegreen darkorange darkorchid darkred darksalmon darkseagreen darkslateblue darkslategray darkslategrey darkturquoise darkviolet deeppink deepskyblue dimgray dimgrey dodgerblue firebrick floralwhite forestgreen fuchsia gainsboro ghostwhite gold goldenrod gray green greenyellow grey honeydew hotpink indianred indigo ivory khaki lavender lavenderblush lawngreen lemonchiffon lightblue lightcoral lightcyan lightgoldenrodyellow lightgray lightgreen lightgrey lightpink lightsalmon lightseagreen lightskyblue lightslategray lightslategrey lightsteelblue lightyellow lime limegreen linen magenta maroon mediumaquamarine mediumblue mediumorchid mediumpurple mediumseagreen mediumslateblue mediumspringgreen mediumturquoise mediumvioletred midnightblue mintcream mistyrose moccasin navajowhite navy oldlace olive olivedrab orange orangered orchid palegoldenrod palegreen paleturquoise palevioletred papayawhip peachpuff peru pink plum powderblue purple rebeccapurple red rosybrown royalblue saddlebrown salmon sandybrown seagreen seashell sienna silver skyblue slateblue slategray slategrey snow springgreen steelblue tan teal thistle tomato turquoise violet wheat white whitesmoke yellow yellowgreen currentcolor transparent' + ); + $sanitized = strtolower( sanitize_text_field( $value ) ); + + return in_array( $sanitized, $named_colors, true ) ? $sanitized : ''; +} + +/** + * Sanitize a font family before it is stored or printed in CSS. + * + * @param mixed $value Font family value. + * + * @return string + */ +function neve_sanitize_font_family( $value ) { + if ( ! is_string( $value ) ) { + return ''; + } + + $value = sanitize_text_field( $value ); + + return neve_closes_css_declaration( $value ) ? '' : $value; +} + /** * Sanitize rgba color. * @@ -263,6 +321,20 @@ function neve_sanitize_background( $value ) { return new WP_Error(); } + foreach ( array( 'imageUrl', 'colorValue', 'overlayColorValue' ) as $string_field ) { + if ( array_key_exists( $string_field, $value ) && ! is_string( $value[ $string_field ] ) ) { + return new WP_Error(); + } + } + + if ( isset( $value['focusPoint'] ) && ! is_array( $value['focusPoint'] ) ) { + return new WP_Error(); + } + + if ( isset( $value['overlayOpacity'] ) && ! is_numeric( $value['overlayOpacity'] ) ) { + return new WP_Error(); + } + if ( ! isset( $value['focusPoint'] ) ) { $value['focusPoint'] = [ 'x' => 0.5, @@ -466,11 +538,15 @@ function neve_sanitize_shadow_css_rule( $css_prop, $value, $width, $height, $blu /** * Sanitize the typography control. * - * @param array $value the control value. + * @param mixed $value The control value. * * @return array */ function neve_sanitize_typography_control( $value ) { + if ( ! is_array( $value ) ) { + return array(); + } + $keys = [ 'lineHeight', 'letterSpacing', @@ -487,18 +563,33 @@ function neve_sanitize_typography_control( $value ) { } // Font Weight. - if ( ! in_array( $value['fontWeight'], [ '100', '200', '300', '400', '500', '600', '700', '800', '900' ], true ) ) { + if ( isset( $value['fontWeight'] ) && ! in_array( $value['fontWeight'], [ 'none', '100', '200', '300', '400', '500', '600', '700', '800', '900' ], true ) ) { $value['fontWeight'] = '300'; } // Text Transform. - if ( ! in_array( $value['textTransform'], [ 'none', 'uppercase', 'lowercase', 'capitalize' ], true ) ) { + if ( ! isset( $value['textTransform'] ) || ! in_array( $value['textTransform'], [ 'none', 'uppercase', 'lowercase', 'capitalize' ], true ) ) { $value['textTransform'] = 'none'; } - // Make sure we deal with arrays. + // Keep only numeric responsive values and allowlisted units. foreach ( [ 'letterSpacing', 'lineHeight', 'fontSize' ] as $value_type ) { - if ( ! is_array( $value[ $value_type ] ) ) { + if ( ! isset( $value[ $value_type ] ) || ! is_array( $value[ $value_type ] ) ) { $value[ $value_type ] = []; + continue; + } + + foreach ( array( 'mobile', 'tablet', 'desktop' ) as $device ) { + if ( isset( $value[ $value_type ][ $device ] ) && ! is_numeric( $value[ $value_type ][ $device ] ) ) { + unset( $value[ $value_type ][ $device ] ); + } + } + + if ( isset( $value[ $value_type ]['suffix'] ) ) { + if ( is_array( $value[ $value_type ]['suffix'] ) ) { + $value[ $value_type ]['suffix'] = array_map( 'neve_sanitize_css_unit', $value[ $value_type ]['suffix'] ); + } else { + $value[ $value_type ]['suffix'] = neve_sanitize_css_unit( $value[ $value_type ]['suffix'] ); + } } } diff --git a/header-footer-grid/Core/Components/Abstract_Component.php b/header-footer-grid/Core/Components/Abstract_Component.php index 7777a28f13..6a5bb80579 100644 --- a/header-footer-grid/Core/Components/Abstract_Component.php +++ b/header-footer-grid/Core/Components/Abstract_Component.php @@ -669,6 +669,7 @@ public function add_style( array $css_array = array() ) { Dynamic_Selector::META_DEFAULT => SettingsManager::get_instance()->get_default( $this->get_id() . '_' . self::PADDING_ID ), Dynamic_Selector::META_SUFFIX => 'responsive_unit', 'directional-prop' => Config::CSS_PROP_PADDING, + Dynamic_Selector::META_FILTER => 'neve_sanitize_directional_css_rule', ], '--margin' => [ Dynamic_Selector::META_KEY => $this->get_id() . '_' . self::MARGIN_ID, @@ -676,6 +677,7 @@ public function add_style( array $css_array = array() ) { Dynamic_Selector::META_DEFAULT => SettingsManager::get_instance()->get_default( $this->get_id() . '_' . self::MARGIN_ID ), Dynamic_Selector::META_SUFFIX => 'responsive_unit', 'directional-prop' => Config::CSS_PROP_MARGIN, + Dynamic_Selector::META_FILTER => 'neve_sanitize_directional_css_rule', ], ]; @@ -829,7 +831,7 @@ private function add_typography_controls() { 'transport' => 'postMessage', 'priority' => $priority + 1, 'type' => '\Neve\Customizer\Controls\React\Font_Family', - 'sanitize_callback' => 'sanitize_text_field', + 'sanitize_callback' => 'neve_sanitize_font_family', 'live_refresh_selector' => $this->default_typography_selector, 'live_refresh_css_prop' => array( 'cssVar' => [ diff --git a/header-footer-grid/Core/Components/MenuIcon.php b/header-footer-grid/Core/Components/MenuIcon.php index db7cd81dd5..c601f2df50 100644 --- a/header-footer-grid/Core/Components/MenuIcon.php +++ b/header-footer-grid/Core/Components/MenuIcon.php @@ -721,6 +721,7 @@ public function add_style( array $css_array = array() ) { Dynamic_Selector::META_DEFAULT => $this->default_label_margin_value, Dynamic_Selector::META_SUFFIX => 'responsive_unit', 'directional-prop' => Config::CSS_PROP_MARGIN, + Dynamic_Selector::META_FILTER => 'neve_sanitize_directional_css_rule', ], ], ]; diff --git a/header-footer-grid/Core/Components/Search.php b/header-footer-grid/Core/Components/Search.php index 8be8acea09..899ccf064d 100644 --- a/header-footer-grid/Core/Components/Search.php +++ b/header-footer-grid/Core/Components/Search.php @@ -390,6 +390,7 @@ public function add_style( array $css_array = array() ) { Dynamic_Selector::META_SUFFIX => 'px', ], '--formfieldborderwidth' => [ + Dynamic_Selector::META_FILTER => 'neve_sanitize_directional_css_rule', Dynamic_Selector::META_IS_RESPONSIVE => true, Dynamic_Selector::META_KEY => $this->get_id() . '_' . self::FIELD_BORDER_WIDTH, Dynamic_Selector::META_SUFFIX => 'px', @@ -397,6 +398,7 @@ public function add_style( array $css_array = array() ) { 'directional-prop' => Config::CSS_PROP_BORDER_WIDTH, ], '--formfieldborderradius' => [ + Dynamic_Selector::META_FILTER => 'neve_sanitize_directional_css_rule', Dynamic_Selector::META_KEY => $this->get_id() . '_' . self::FIELD_BORDER_RADIUS, Dynamic_Selector::META_IS_RESPONSIVE => true, Dynamic_Selector::META_DEFAULT => SettingsManager::get_instance()->get_default( $this->get_id() . '_' . self::FIELD_BORDER_RADIUS ), diff --git a/header-footer-grid/Core/Components/SearchResponsive.php b/header-footer-grid/Core/Components/SearchResponsive.php index 2e6dd1094d..be3ac03a39 100644 --- a/header-footer-grid/Core/Components/SearchResponsive.php +++ b/header-footer-grid/Core/Components/SearchResponsive.php @@ -571,12 +571,14 @@ public function add_style( array $css_array = array() ) { Dynamic_Selector::META_DEFAULT => SettingsManager::get_instance()->get_default( $this->get_id() . '_' . self::FIELD_FONT_SIZE ), ], '--formfieldborderwidth' => [ + Dynamic_Selector::META_FILTER => 'neve_sanitize_directional_css_rule', Dynamic_Selector::META_KEY => $this->get_id() . '_' . self::FIELD_BORDER_WIDTH, Dynamic_Selector::META_IS_RESPONSIVE => true, Dynamic_Selector::META_DEFAULT => SettingsManager::get_instance()->get_default( $this->get_id() . '_' . self::FIELD_BORDER_WIDTH ), 'directional-prop' => Config::CSS_PROP_BORDER_WIDTH, ], '--formfieldborderradius' => [ + Dynamic_Selector::META_FILTER => 'neve_sanitize_directional_css_rule', Dynamic_Selector::META_KEY => $this->get_id() . '_' . self::FIELD_BORDER_RADIUS, Dynamic_Selector::META_IS_RESPONSIVE => true, Dynamic_Selector::META_DEFAULT => SettingsManager::get_instance()->get_default( $this->get_id() . '_' . self::FIELD_BORDER_RADIUS ), diff --git a/header-footer-grid/Traits/Core.php b/header-footer-grid/Traits/Core.php index 6542892595..1f47f805a9 100644 --- a/header-footer-grid/Traits/Core.php +++ b/header-footer-grid/Traits/Core.php @@ -128,11 +128,31 @@ public function sanitize_responsive_int_json( $input ) { * @return array */ public function sanitize_spacing_array( $input ) { - if ( is_array( $input ) ) { - return $input; + if ( ! is_array( $input ) ) { + return array(); + } + + foreach ( array( 'top', 'right', 'bottom', 'left' ) as $direction ) { + if ( isset( $input[ $direction ] ) && ! is_numeric( $input[ $direction ] ) ) { + unset( $input[ $direction ] ); + } + } + + foreach ( array( 'mobile', 'tablet', 'desktop' ) as $device ) { + if ( isset( $input[ $device ] ) ) { + $input[ $device ] = $this->sanitize_spacing_array( $input[ $device ] ); + } + $unit_key = $device . '-unit'; + if ( isset( $input[ $unit_key ] ) ) { + $input[ $unit_key ] = neve_sanitize_css_unit( $input[ $unit_key ] ); + } + } + + if ( isset( $input['unit'] ) ) { + $input['unit'] = neve_sanitize_css_unit( $input['unit'] ); } - return array(); + return $input; } /** diff --git a/inc/abilities/abstract_ability.php b/inc/abilities/abstract_ability.php index 4af17bd1b9..f9cb9edd2b 100644 --- a/inc/abilities/abstract_ability.php +++ b/inc/abilities/abstract_ability.php @@ -248,6 +248,12 @@ protected function sanitize_style_mod( string $key, $value ) { return $this->sanitize_background_mod( $value ); } + if ( preg_match( '/_new_text_color$/', $key ) ) { + $color = neve_sanitize_row_text_color( $value ); + + return '' === $color && '' !== $value ? null : $color; + } + if ( preg_match( '/_color$/', $key ) ) { $color = neve_sanitize_colors( $value ); @@ -270,7 +276,10 @@ private function sanitize_background_mod( $value ) { } foreach ( array( 'colorValue', 'overlayColorValue' ) as $field ) { - if ( ! empty( $value[ $field ] ) && '' === $background[ $field ] ) { + if ( ! array_key_exists( $field, $value ) ) { + continue; + } + if ( ! is_string( $value[ $field ] ) || ( '' !== $value[ $field ] && '' === $background[ $field ] ) ) { return null; } } diff --git a/inc/abilities/appearance_update.php b/inc/abilities/appearance_update.php index 7c0461323c..1aa19cb431 100644 --- a/inc/abilities/appearance_update.php +++ b/inc/abilities/appearance_update.php @@ -417,19 +417,26 @@ private function apply_colors( array $colors, array &$updated, array &$warnings */ private function apply_typography( array $typography, array &$updated ) { if ( isset( $typography['body_font'] ) ) { - set_theme_mod( 'neve_body_font_family', sanitize_text_field( $typography['body_font'] ) ); - $updated[] = 'neve_body_font_family'; + $font = neve_sanitize_font_family( $typography['body_font'] ); + if ( '' !== $font || '' === $typography['body_font'] ) { + set_theme_mod( 'neve_body_font_family', $font ); + $updated[] = 'neve_body_font_family'; + } } if ( isset( $typography['headings_font'] ) ) { - set_theme_mod( 'neve_headings_font_family', sanitize_text_field( $typography['headings_font'] ) ); - $updated[] = 'neve_headings_font_family'; + $font = neve_sanitize_font_family( $typography['headings_font'] ); + if ( '' !== $font || '' === $typography['headings_font'] ) { + set_theme_mod( 'neve_headings_font_family', $font ); + $updated[] = 'neve_headings_font_family'; + } } if ( isset( $typography['element'] ) && is_array( $typography['element'] ) && isset( $typography['element']['target'] ) ) { $target = $typography['element']['target']; $mod = 'body' === $target ? 'neve_typeface_general' : 'neve_' . $target . '_typeface_general'; - $current = get_theme_mod( $mod, array() ); + $default = \Neve\Core\Settings\Mods::get_alternative_mod_default( $mod ); + $current = get_theme_mod( $mod, is_array( $default ) ? $default : array() ); $current = is_array( $current ) ? $current : array(); $props = $typography['element']; @@ -437,7 +444,7 @@ private function apply_typography( array $typography, array &$updated ) { // Deep-merge so a partial update (e.g. only fontSize.desktop) keeps the // other breakpoints/sub-values instead of replacing the whole sub-object. - $merged = $this->rec_wp_parse_args( $props, $current ); + $merged = neve_sanitize_typography_control( $this->rec_wp_parse_args( $props, $current ) ); set_theme_mod( $mod, $merged ); $updated[] = $mod; } diff --git a/inc/core/dynamic_css.php b/inc/core/dynamic_css.php index 60b186a3b3..590b3e6450 100644 --- a/inc/core/dynamic_css.php +++ b/inc/core/dynamic_css.php @@ -166,7 +166,9 @@ public static function get_root_css() { * @return string */ public static function get_fallback_font() { - $fallback = get_theme_mod( 'neve_fallback_font_family', 'Arial, Helvetica, sans-serif' ); + $default = 'Arial, Helvetica, sans-serif'; + $fallback = neve_sanitize_font_family( get_theme_mod( 'neve_fallback_font_family', $default ) ); + $fallback = '' === $fallback ? $default : $fallback; return '--nv-fallback-ff:' . $fallback . ';'; } diff --git a/inc/core/styles/css_prop.php b/inc/core/styles/css_prop.php index 79188327d2..14fa24a679 100644 --- a/inc/core/styles/css_prop.php +++ b/inc/core/styles/css_prop.php @@ -77,6 +77,13 @@ public static function get_suffix_responsive( $meta, $device ) { * @return string */ public static function transform( $css_prop, $value, $meta, $device ) { + if ( isset( $meta[ Dynamic_Selector::META_KEY ] ) && preg_match( '/\.(?:fontSize|lineHeight|letterSpacing)$/', $meta[ Dynamic_Selector::META_KEY ] ) ) { + if ( ! is_numeric( $value ) ) { + return ''; + } + $value = (float) $value; + } + //If we have a custom filter, let's call it. if ( isset( $meta['filter'] ) ) { if ( is_callable( $meta['filter'] ) ) { @@ -192,6 +199,9 @@ public static function transform( $css_prop, $value, $meta, $device ) { //Line height uses an awkward format saved, and we can't define it as responsive because we would need to use the suffix part. case Config::CSS_PROP_LINE_HEIGHT: case Config::CSS_PROP_FONT_SIZE: + if ( ! is_numeric( $value ) ) { + return ''; + } $suffix = isset( $meta[ Dynamic_Selector::META_SUFFIX ] ) ? $meta[ Dynamic_Selector::META_SUFFIX ] : 'em'; // We consider the provided suffix as default, in case that we have a responsive setting with responsive suffix. @@ -199,11 +209,13 @@ public static function transform( $css_prop, $value, $meta, $device ) { $all_value = Mods::get( $meta['key'] ); $suffix = isset( $all_value['suffix'][ $device ] ) ? $all_value['suffix'][ $device ] : ( isset( $all_value['suffix'] ) ? $all_value['suffix'] : $suffix ); } + $suffix = neve_sanitize_css_unit( $suffix ); + $suffix = '—' === $suffix ? '' : $suffix; - return sprintf( ' %s: %s%s;', $css_prop, $value, $suffix ); + return sprintf( ' %s: %s%s;', $css_prop, (float) $value, $suffix ); //Letter spacing has a legacy value of non-responsive which we need to take into consideration. case Config::CSS_PROP_LETTER_SPACING: - return sprintf( ' %s: %spx;', $css_prop, $value ); + return is_numeric( $value ) ? sprintf( ' %s: %spx;', $css_prop, (float) $value ) : ''; case Config::CSS_PROP_CUSTOM_BTN_TYPE: if ( $value !== 'outline' ) { return 'border:none;'; @@ -218,9 +230,13 @@ public static function transform( $css_prop, $value, $meta, $device ) { return sprintf( ' %s: %s;', $css_prop, intval( $value ) ); case Config::CSS_PROP_FONT_FAMILY: + $value = neve_sanitize_font_family( $value ); if ( $value === 'default' ) { return ''; } + if ( '' === $value ) { + return ''; + } Font_Manager::add_google_font( $value ); return sprintf( ' %s: %s, var(--nv-fallback-ff);', $css_prop, $value ); @@ -231,9 +247,13 @@ public static function transform( $css_prop, $value, $meta, $device ) { case Config::CSS_PROP_GRID_TEMPLATE_COLS: return sprintf( ' %s: %s;', $css_prop, $value ); default: - $is_font_family_var = strpos( strtolower( $css_prop ), 'fontfamily' ) > - 1; + $is_font_family_var = false !== strpos( strtolower( $css_prop ), 'fontfamily' ); if ( $is_font_family_var ) { + $value = neve_sanitize_font_family( $value ); + if ( '' === $value ) { + return ''; + } Font_Manager::add_google_font( $value ); $value = self::format_font_family_value( $value ); diff --git a/inc/core/styles/frontend.php b/inc/core/styles/frontend.php index bf83dbf7be..beb63871a0 100644 --- a/inc/core/styles/frontend.php +++ b/inc/core/styles/frontend.php @@ -788,6 +788,7 @@ private function setup_single_post_style() { Dynamic_Selector::META_KEY => Config::MODS_POST_COMMENTS_PADDING, Dynamic_Selector::META_IS_RESPONSIVE => true, Dynamic_Selector::META_DEFAULT => $this->padding_default(), + Dynamic_Selector::META_FILTER => 'neve_sanitize_directional_css_rule', 'directional-prop' => Config::CSS_PROP_PADDING, ], '--bgcolor' => [ @@ -809,6 +810,7 @@ private function setup_single_post_style() { Dynamic_Selector::META_IS_RESPONSIVE => true, Dynamic_Selector::META_SUFFIX => 'responsive_unit', Dynamic_Selector::META_DEFAULT => $this->padding_default(), + Dynamic_Selector::META_FILTER => 'neve_sanitize_directional_css_rule', 'directional-prop' => Config::CSS_PROP_PADDING, ], '--bgcolor' => [ @@ -913,6 +915,7 @@ private function setup_header_style() { Dynamic_Selector::META_IS_RESPONSIVE => true, Dynamic_Selector::META_DEFAULT => $this->padding_default( 'cover' ), Dynamic_Selector::META_SUFFIX => 'responsive_unit', + Dynamic_Selector::META_FILTER => 'neve_sanitize_directional_css_rule', 'directional-prop' => Config::CSS_PROP_PADDING, ], '--justify' => [ @@ -978,6 +981,7 @@ private function setup_header_style() { Dynamic_Selector::META_IS_RESPONSIVE => true, Dynamic_Selector::META_DEFAULT => $this->padding_default( 'cover' ), Dynamic_Selector::META_SUFFIX => 'responsive_unit', + Dynamic_Selector::META_FILTER => 'neve_sanitize_directional_css_rule', 'directional-prop' => Config::CSS_PROP_PADDING, ], '--bgcolor' => [ @@ -1045,6 +1049,7 @@ private function setup_content_vspacing() { Dynamic_Selector::META_IS_RESPONSIVE => true, Dynamic_Selector::META_SUFFIX => 'responsive_unit', Dynamic_Selector::META_DEFAULT => $default, + Dynamic_Selector::META_FILTER => 'neve_sanitize_directional_css_rule', Dynamic_Selector::META_DIRECTIONAL_PROP => Config::CSS_PROP_DIRECTIONAL_ONE_AXIS, ], ]; @@ -1064,6 +1069,7 @@ private function setup_content_vspacing() { Dynamic_Selector::META_IS_RESPONSIVE => true, Dynamic_Selector::META_SUFFIX => 'responsive_unit', Dynamic_Selector::META_DEFAULT => $this->content_vspacing_default(), + Dynamic_Selector::META_FILTER => 'neve_sanitize_directional_css_rule', 'directional-prop' => Config::CSS_PROP_DIRECTIONAL_ONE_AXIS, ], ]; diff --git a/inc/customizer/options/buttons.php b/inc/customizer/options/buttons.php index fbc40aaf09..4627a3e66e 100644 --- a/inc/customizer/options/buttons.php +++ b/inc/customizer/options/buttons.php @@ -91,7 +91,8 @@ public function add_controls() { new Control( Config::MODS_BUTTON_TYPEFACE, [ - 'transport' => $this->selective_refresh, + 'transport' => $this->selective_refresh, + 'sanitize_callback' => 'neve_sanitize_typography_control', ], [ 'label' => esc_html__( 'Button Text', 'neve' ), diff --git a/inc/customizer/options/form_fields.php b/inc/customizer/options/form_fields.php index 3cbad367d2..4f55d22376 100644 --- a/inc/customizer/options/form_fields.php +++ b/inc/customizer/options/form_fields.php @@ -389,7 +389,8 @@ private function add_input_text_controls() { new Control( Config::MODS_FORM_FIELDS_TYPEFACE, [ - 'transport' => $this->selective_refresh, + 'transport' => $this->selective_refresh, + 'sanitize_callback' => 'neve_sanitize_typography_control', ], [ 'priority' => 32, @@ -491,7 +492,8 @@ private function add_form_labels_controls() { new Control( Config::MODS_FORM_FIELDS_LABELS_TYPEFACE, [ - 'transport' => $this->selective_refresh, + 'transport' => $this->selective_refresh, + 'sanitize_callback' => 'neve_sanitize_typography_control', ], [ 'priority' => 52, diff --git a/inc/customizer/options/typography.php b/inc/customizer/options/typography.php index 7dea9cd9c6..0c548bfad8 100644 --- a/inc/customizer/options/typography.php +++ b/inc/customizer/options/typography.php @@ -138,7 +138,7 @@ private function controls_typography_general() { Config::MODS_FONT_GENERAL, [ 'transport' => $this->selective_refresh, - 'sanitize_callback' => 'sanitize_text_field', + 'sanitize_callback' => 'neve_sanitize_font_family', 'default' => Mods::get_alternative_mod_default( Config::MODS_FONT_GENERAL ), ], array( @@ -181,8 +181,9 @@ private function controls_typography_general() { new Control( Config::MODS_TYPEFACE_GENERAL, [ - 'transport' => $this->selective_refresh, - 'default' => $defaults, + 'transport' => $this->selective_refresh, + 'default' => $defaults, + 'sanitize_callback' => 'neve_sanitize_typography_control', ], [ 'priority' => 11, @@ -232,7 +233,7 @@ private function controls_typography_general() { 'neve_fallback_font_family', [ 'transport' => $this->selective_refresh, - 'sanitize_callback' => 'sanitize_text_field', + 'sanitize_callback' => 'neve_sanitize_font_family', 'default' => 'Arial, Helvetica, sans-serif', ], [ @@ -274,7 +275,7 @@ private function controls_typography_headings() { 'neve_headings_font_family', array( 'transport' => $this->selective_refresh, - 'sanitize_callback' => 'sanitize_text_field', + 'sanitize_callback' => 'neve_sanitize_font_family', ), array( 'section' => 'neve_typography_headings', @@ -355,7 +356,7 @@ private function controls_typography_headings() { $mod_key_font_family, array( 'transport' => $this->selective_refresh, - 'sanitize_callback' => 'sanitize_text_field', + 'sanitize_callback' => 'neve_sanitize_font_family', ), array( 'section' => 'neve_typography_headings', @@ -384,8 +385,9 @@ private function controls_typography_headings() { new Control( $mod_key, [ - 'transport' => $this->selective_refresh, - 'default' => $default_values, + 'transport' => $this->selective_refresh, + 'default' => $default_values, + 'sanitize_callback' => 'neve_sanitize_typography_control', ], [ 'priority' => $priority += 2, @@ -510,7 +512,8 @@ private function controls_typography_blog() { new Control( $control_id, [ - 'transport' => $this->selective_refresh, + 'transport' => $this->selective_refresh, + 'sanitize_callback' => 'neve_sanitize_typography_control', ], [ 'priority' => $control_settings['priority'] += 1, diff --git a/tests/test-neve-ability-style-mods.php b/tests/test-neve-ability-style-mods.php index 4fe90a3f59..244b515d5f 100644 --- a/tests/test-neve-ability-style-mods.php +++ b/tests/test-neve-ability-style-mods.php @@ -7,6 +7,7 @@ use Neve\Abilities\Footer_Configure; use Neve\Abilities\Header_Configure; +use Neve\Abilities\Appearance_Update; use Neve\Abilities\Site_Set_Theme_Mod; use Neve\Core\Settings\Mods; use Neve\Core\Styles\Dynamic_Selector; @@ -61,6 +62,7 @@ public function tearDown(): void { */ public function test_header_configure_sanitizes_row_style(): void { set_theme_mod( self::HEADER_BG, 'previous' ); + set_theme_mod( self::HEADER_TEXT, '#123456' ); ( new Header_Configure() )->execute( array( @@ -105,6 +107,8 @@ public function test_header_configure_sanitizes_row_background(): void { * Footer row colors are sanitized. */ public function test_footer_configure_sanitizes_row_style(): void { + set_theme_mod( self::FOOTER_TEXT, '#123456' ); + ( new Footer_Configure() )->execute( array( 'row_style' => array( @@ -189,6 +193,49 @@ public function test_header_configure_keeps_background_on_invalid_nested_color() $this->assertSame( $background, get_theme_mod( self::HEADER_BG ) ); } + /** + * Invalid nested background color types keep the stored background. + */ + public function test_header_configure_keeps_background_on_invalid_nested_color_types(): void { + $background = array( + 'type' => 'image', + 'colorValue' => '#123456', + 'overlayColorValue' => '#654321', + 'imageUrl' => '', + 'focusPoint' => array( 'x' => 0.5, 'y' => 0.5 ), + 'useFeatured' => false, + ); + + $invalid_values = array( + 'colorValue' => array( 'notacolor', array() ), + 'overlayColorValue' => array( 'notacolor', array() ), + 'imageUrl' => array( array() ), + 'focusPoint' => array( 'not-an-array' ), + 'overlayOpacity' => array( array() ), + ); + + foreach ( $invalid_values as $field => $invalid_values_for_field ) { + foreach ( $invalid_values_for_field as $invalid ) { + set_theme_mod( self::HEADER_BG, $background ); + $candidate = $background; + $candidate[ $field ] = $invalid; + + ( new Header_Configure() )->execute( + array( + 'row_style' => array( + array( + 'row' => 'main', + 'background' => $candidate, + ), + ), + ) + ); + + $this->assertSame( $background, get_theme_mod( self::HEADER_BG ) ); + } + } + } + /** * The sidebar width goes through its range sanitizer. */ @@ -361,6 +408,190 @@ public function test_header_configure_keeps_color_on_invalid_value(): void { $this->assertSame( '#ff0000', get_theme_mod( self::HEADER_TEXT ) ); } + /** + * Named row text colors remain supported by both ability paths. + */ + public function test_abilities_accept_named_row_text_colors(): void { + ( new Header_Configure() )->execute( + array( + 'row_style' => array( + array( + 'row' => 'main', + 'text_color' => 'red', + ), + ), + ) + ); + $this->assertSame( 'red', get_theme_mod( self::HEADER_TEXT ) ); + + $result = ( new Site_Set_Theme_Mod() )->execute( + array( + 'action' => 'set', + 'key' => self::SIDEBAR_TEXT, + 'value' => 'red', + ) + ); + $this->assertIsArray( $result ); + $this->assertTrue( $result['success'] ); + $this->assertSame( 'red', get_theme_mod( self::SIDEBAR_TEXT ) ); + } + + /** + * Malformed functional colors are rejected and keep the stored color. + */ + public function test_site_set_theme_mod_rejects_malformed_function_color(): void { + set_theme_mod( self::SIDEBAR_TEXT, '#123456' ); + + $result = ( new Site_Set_Theme_Mod() )->execute( + array( + 'action' => 'set', + 'key' => self::SIDEBAR_TEXT, + 'value' => 'rgb(,)', + ) + ); + + $this->assertIsArray( $result ); + $this->assertFalse( $result['success'] ); + $this->assertSame( '#123456', get_theme_mod( self::SIDEBAR_TEXT ) ); + } + + /** + * Typography and font-family values are sanitized on write and output. + */ + public function test_typography_values_are_sanitized_on_write_and_output(): void { + set_theme_mod( 'neve_body_font_family', 'Arial' ); + $result = ( new Appearance_Update() )->execute( + array( + 'typography' => array( + 'body_font' => 'Arial;}body{background:lime}/*', + 'element' => array( + 'target' => 'body', + 'fontSize' => array( + 'desktop' => '10;}body{background:lime}/*', + 'suffix' => array( 'desktop' => 'px;}body{background:lime}/*' ), + ), + ), + ), + ) + ); + + $this->assertIsArray( $result ); + $this->assertSame( 'Arial', get_theme_mod( 'neve_body_font_family' ) ); + $typeface = get_theme_mod( 'neve_typeface_general' ); + $this->assertArrayNotHasKey( 'desktop', $typeface['fontSize'] ); + $this->assertSame( 'px', $typeface['fontSize']['suffix']['desktop'] ); + + set_theme_mod( 'neve_body_font_family', 'Arial;}body{background:lime}/*' ); + set_theme_mod( + 'neve_typeface_general', + array( + 'fontSize' => array( + 'desktop' => '10;}body{background:lime}/*', + 'suffix' => array( 'desktop' => 'px;}body{background:lime}/*' ), + ), + ) + ); + $css = (string) ( new Frontend() )->generate(); + $this->assertStringNotContainsString( 'background:lime', $css ); + } + + /** + * Partial typography updates retain the target's defaults. + */ + public function test_partial_typography_updates_retain_defaults(): void { + $targets = array( + 'body' => array( 'neve_typeface_general', '400' ), + 'h1' => array( 'neve_h1_typeface_general', '700' ), + ); + + foreach ( $targets as $target => $expected ) { + remove_theme_mod( $expected[0] ); + $result = ( new Appearance_Update() )->execute( + array( + 'typography' => array( + 'element' => array( + 'target' => $target, + 'fontSize' => array( 'desktop' => 24 ), + ), + ), + ) + ); + + $this->assertIsArray( $result ); + $typeface = get_theme_mod( $expected[0] ); + $this->assertSame( $expected[1], $typeface['fontWeight'] ); + $this->assertSame( 24, $typeface['fontSize']['desktop'] ); + $this->assertArrayHasKey( 'mobile', $typeface['fontSize'] ); + } + } + + /** + * Empty font families remain valid reset values for the ability. + */ + public function test_typography_font_families_can_be_cleared(): void { + set_theme_mod( 'neve_body_font_family', 'Arial' ); + set_theme_mod( 'neve_headings_font_family', 'Georgia' ); + + $result = ( new Appearance_Update() )->execute( + array( + 'typography' => array( + 'body_font' => '', + 'headings_font' => '', + ), + ) + ); + + $this->assertIsArray( $result ); + $this->assertSame( '', get_theme_mod( 'neve_body_font_family' ) ); + $this->assertSame( '', get_theme_mod( 'neve_headings_font_family' ) ); + } + + /** + * Previously stored fallback fonts are sanitized at their CSS sink. + */ + public function test_stored_fallback_font_is_sanitized_when_css_is_generated(): void { + set_theme_mod( 'neve_fallback_font_family', 'Arial;}body{background:lime}/*' ); + + $css = \Neve\Core\Dynamic_Css::get_fallback_font(); + + $this->assertSame( '--nv-fallback-ff:Arial, Helvetica, sans-serif;', $css ); + $this->assertStringNotContainsString( 'background:lime', $css ); + } + + /** + * Stored header component spacing is sanitized at every directional sink. + */ + public function test_stored_header_component_spacing_is_sanitized(): void { + $invalid = array( + 'mobile' => array_fill_keys( array( 'top', 'right', 'bottom', 'left' ), '5;}body{background:lime}/*' ), + 'tablet' => array_fill_keys( array( 'top', 'right', 'bottom', 'left' ), '5;}body{background:lime}/*' ), + 'desktop' => array_fill_keys( array( 'top', 'right', 'bottom', 'left' ), '5;}body{background:lime}/*' ), + 'mobile-unit' => self::INJECTED_CSS, + 'tablet-unit' => self::INJECTED_CSS, + 'desktop-unit' => self::INJECTED_CSS, + ); + $mods = array( + 'primary-menu_component_padding', + 'primary-menu_component_margin', + 'header_search_field_border_width', + 'header_search_field_border_radius', + 'header_search_responsive_field_border_width', + 'header_search_responsive_field_border_radius', + 'header_menu_icon_label_margin', + ); + foreach ( $mods as $mod ) { + set_theme_mod( $mod, $invalid ); + } + + $builder = \HFG\Main::get_instance()->get_builder( 'header' ); + foreach ( array( 'mobile', 'tablet', 'desktop' ) as $device ) { + $selector = new Dynamic_Selector( $builder->add_style( array() ) ); + $method = 'for_' . $device; + $css = (string) $selector->$method(); + $this->assertStringNotContainsString( 'background:lime', $css ); + } + } + /** * An invalid color is rejected and the stored color kept. */ @@ -452,6 +683,21 @@ public function test_stored_ability_styles_are_sanitized_when_css_is_generated() set_theme_mod( 'neve_scroll_to_top_border_radius', '3;}body{background:lime}/*' ); set_theme_mod( 'neve_scroll_to_top_status', '1' ); + $responsive_directional = array( + 'mobile' => $invalid_directional, + 'tablet' => $invalid_directional, + 'desktop' => $invalid_directional, + 'mobile-unit' => self::INJECTED_CSS, + 'tablet-unit' => self::INJECTED_CSS, + 'desktop-unit' => self::INJECTED_CSS, + ); + foreach ( array( 'neve_comments_boxed_padding', 'neve_comments_form_boxed_padding', 'neve_post_cover_padding', 'neve_post_cover_title_boxed_padding', 'neve_post_content_vspacing' ) as $mod ) { + set_theme_mod( $mod, $responsive_directional ); + } + set_theme_mod( 'neve_post_inherit_vspacing', 'individual' ); + $post_id = self::factory()->post->create(); + $this->go_to( get_permalink( $post_id ) ); + $css = (string) ( new Frontend() )->generate(); $this->assertStringNotContainsString( 'background:lime', $css ); @@ -465,4 +711,5 @@ public function test_stored_ability_styles_are_sanitized_when_css_is_generated() $this->assertStringContainsString( '--borderradius: 3px;', $css ); $this->assertStringContainsString( '--hoverbgcolor: rgba(1,2,3,1);', $css ); } + } diff --git a/tests/test-neve-row-color-css.php b/tests/test-neve-row-color-css.php index 6c2b58eb43..82d945157f 100644 --- a/tests/test-neve-row-color-css.php +++ b/tests/test-neve-row-color-css.php @@ -64,6 +64,27 @@ public function test_injected_row_colors_are_printed_sanitized(): void { $this->assertStringContainsString( '--color: red;', $css ); } + /** + * Stored image overlay colors are sanitized when printed. + */ + public function test_injected_row_overlay_color_is_printed_sanitized(): void { + set_theme_mod( + 'hfg_header_layout_main_background', + array( + 'type' => 'image', + 'imageUrl' => '', + 'focusPoint' => array( 'x' => 0.5, 'y' => 0.5 ), + 'overlayColorValue' => self::INJECTED_CSS, + 'useFeatured' => false, + ) + ); + + $css = $this->header_css(); + + $this->assertStringNotContainsString( 'background:lime', $css ); + $this->assertStringContainsString( '--overlaycolor: rgba(1,2,3,1);', $css ); + } + /** * Render the header builder's dynamic CSS. * diff --git a/tests/test-neve-sanitization.php b/tests/test-neve-sanitization.php index 787f649664..54e4e7be0d 100644 --- a/tests/test-neve-sanitization.php +++ b/tests/test-neve-sanitization.php @@ -195,6 +195,24 @@ public function test_sanitize_colors_with_non_string_values() { $this->assertSame( 'var(--nv-primary-accent)', neve_sanitize_colors( 'var(--nv-primary-accent)' ) ); } + /** + * Optional typography controls retain their supported inherited weight. + */ + public function test_typography_sanitizer_keeps_inherited_font_weight() { + $value = array( + 'fontWeight' => 'none', + 'textTransform' => 'none', + 'fontSize' => array(), + 'lineHeight' => array(), + 'letterSpacing' => array(), + ); + + $this->assertSame( $value, neve_sanitize_typography_control( $value ) ); + + unset( $value['fontWeight'] ); + $this->assertArrayNotHasKey( 'fontWeight', neve_sanitize_typography_control( $value ) ); + } + /** * Logo data can already be decoded when it reaches the sanitizer. */ @@ -330,6 +348,8 @@ public function test_sanitize_colors_keeps_function_colors(): void { $this->assertSame( 'rgba(1,2,3,0.5)', neve_sanitize_colors( 'rgba(1,2,3,.5);}body{display:none' ) ); $invalid = [ + 'rgb(,)', + 'rgba(1,)', 'rgb(1,2,3);}body{display:none', 'rgb(1,2,3)),url(//example.com/t.png)', 'rgb(1,2,3)', From c35098f4ee8d24dfdce2bffa4836eadaa202252b Mon Sep 17 00:00:00 2001 From: selul Date: Wed, 11 Nov 2026 01:22:43 +0200 Subject: [PATCH 08/16] fix: cover remaining stored style paths --- globals/sanitize-functions.php | 137 ++++++++++++++---- .../Core/Components/Abstract_Component.php | 1 + header-footer-grid/Core/Components/Button.php | 10 +- inc/abilities/abstract_ability.php | 25 ++++ inc/abilities/appearance_update.php | 22 +-- inc/core/styles/css_prop.php | 12 +- inc/core/styles/css_vars.php | 73 ++++------ inc/core/styles/frontend.php | 37 ++--- inc/customizer/options/buttons.php | 12 +- inc/customizer/options/layout_container.php | 6 +- tests/test-neve-ability-style-mods.php | 58 +++++++- tests/test-neve-button-appearance-css.php | 90 ++++++++++++ tests/test-neve-sanitization.php | 28 ++++ 13 files changed, 383 insertions(+), 128 deletions(-) diff --git a/globals/sanitize-functions.php b/globals/sanitize-functions.php index a34140b1c1..1e43e8a70e 100644 --- a/globals/sanitize-functions.php +++ b/globals/sanitize-functions.php @@ -81,8 +81,8 @@ function neve_closes_css_declaration( $value ): bool { return false; } - foreach ( $value as $item ) { - if ( neve_closes_css_declaration( $item ) ) { + foreach ( $value as $key => $item ) { + if ( ( is_string( $key ) && (bool) preg_match( '/[;{}<>]/', $key ) ) || neve_closes_css_declaration( $item ) ) { return true; } } @@ -178,6 +178,9 @@ function neve_sanitize_font_family( $value ) { if ( ! is_string( $value ) ) { return ''; } + if ( (bool) preg_match( '/[;{}<>]/', $value ) ) { + return ''; + } $value = sanitize_text_field( $value ); @@ -247,11 +250,7 @@ function neve_sanitize_range_value( $input ) { $range_value['tablet'] = isset( $range_value['tablet'] ) && is_numeric( $range_value['tablet'] ) ? floatval( $range_value['tablet'] ) : ''; $range_value['mobile'] = isset( $range_value['mobile'] ) && is_numeric( $range_value['mobile'] ) ? floatval( $range_value['mobile'] ) : ''; if ( isset( $range_value['suffix'] ) ) { - if ( is_array( $range_value['suffix'] ) ) { - $range_value['suffix'] = array_map( 'neve_sanitize_css_unit', $range_value['suffix'] ); - } else { - $range_value['suffix'] = neve_sanitize_css_unit( $range_value['suffix'] ); - } + $range_value['suffix'] = neve_sanitize_css_suffix( $range_value['suffix'] ); } return wp_json_encode( $range_value ); } @@ -271,6 +270,17 @@ function neve_sanitize_css_unit( $unit ) { return $unit; } +/** + * Sanitize a scalar or responsive CSS suffix. + * + * @param mixed $suffix CSS suffix value. + * + * @return string|array + */ +function neve_sanitize_css_suffix( $suffix ) { + return is_array( $suffix ) ? array_map( 'neve_sanitize_css_unit', $suffix ) : neve_sanitize_css_unit( $suffix ); +} + /** * Sanitize font weight values. * @@ -383,7 +393,7 @@ function neve_sanitize_button_appearance( $value ) { $color_fields = array( 'background', 'backgroundHover', 'text', 'textHover', 'shadowColor', 'shadowColorHover' ); foreach ( $color_fields as $field ) { if ( array_key_exists( $field, $value ) ) { - $value[ $field ] = neve_sanitize_colors( $value[ $field ] ); + $value[ $field ] = 'transparent' === $value[ $field ] ? 'transparent' : neve_sanitize_colors( $value[ $field ] ); } } foreach ( array( 'borderRadius', 'borderWidth' ) as $field ) { @@ -391,10 +401,38 @@ function neve_sanitize_button_appearance( $value ) { $value[ $field ] = neve_sanitize_button_dimension( $value[ $field ] ); } } + foreach ( array( 'shadowProperties', 'shadowPropertiesHover' ) as $field ) { + if ( array_key_exists( $field, $value ) ) { + $value[ $field ] = neve_sanitize_button_shadow_properties( $value[ $field ] ); + } + } return $value; } +/** + * Sanitize button shadow offsets and blur. + * + * @param mixed $value Stored shadow properties. + * + * @return array{blur: int, width: int, height: int} + */ +function neve_sanitize_button_shadow_properties( $value ) { + $sanitized = array_fill_keys( array( 'blur', 'width', 'height' ), 0 ); + if ( ! is_array( $value ) ) { + return $sanitized; + } + + foreach ( array_keys( $sanitized ) as $property ) { + if ( isset( $value[ $property ] ) && is_numeric( $value[ $property ] ) ) { + $sanitized[ $property ] = (int) $value[ $property ]; + } + } + $sanitized['blur'] = abs( $sanitized['blur'] ); + + return $sanitized; +} + /** * Sanitize the directional dimensions stored by a button appearance control. * @@ -434,6 +472,25 @@ function neve_sanitize_color_css_rule( $css_prop, $value ) { return '' === $color ? '' : sprintf( ' %s: %s;', $css_prop, $color ); } +/** + * Format a sanitized font family as a CSS custom-property rule. + * + * @param string $css_prop CSS custom property. + * @param mixed $value Stored font family. + * + * @return string + */ +function neve_sanitize_font_family_css_rule( $css_prop, $value ) { + $font_family = neve_sanitize_font_family( $value ); + if ( '' === $font_family ) { + return ''; + } + + \Neve\Views\Font_Manager::add_google_font( $font_family ); + + return sprintf( ' %s: %s;', $css_prop, \Neve\Core\Styles\Css_Prop::format_font_family_value( $font_family ) ); +} + /** * Format sanitized directional dimensions as a CSS custom-property rule. * @@ -446,21 +503,40 @@ function neve_sanitize_color_css_rule( $css_prop, $value ) { * @return string */ function neve_sanitize_directional_css_rule( $css_prop, $value, $meta, $device ) { - $sanitized = array_fill_keys( array( 'top', 'right', 'bottom', 'left' ), 0 ); + $sanitized = neve_sanitize_directional_value( $value ); + if ( empty( array_filter( $sanitized, 'is_numeric' ) ) ) { + return ''; + } + + return \Neve\Core\Styles\Css_Prop::transform_directional_prop( $meta, $device, $sanitized, $css_prop, $meta['directional-prop'] ); +} + +/** + * Sanitize directional values before formatting them as CSS. + * + * @param mixed $value Stored directional dimensions. + * + * @return array + */ +function neve_sanitize_directional_value( $value ) { + $sanitized = array_fill_keys( array( 'top', 'right', 'bottom', 'left' ), '' ); if ( is_numeric( $value ) ) { - $sanitized = array_fill_keys( array_keys( $sanitized ), (float) $value ); - } elseif ( is_array( $value ) ) { - foreach ( array_keys( $sanitized ) as $direction ) { - if ( isset( $value[ $direction ] ) && is_numeric( $value[ $direction ] ) ) { - $sanitized[ $direction ] = (float) $value[ $direction ]; - } - } - if ( isset( $value['unit'] ) ) { - $sanitized['unit'] = neve_sanitize_css_unit( $value['unit'] ); + return array_fill_keys( array_keys( $sanitized ), (float) $value ); + } + if ( ! is_array( $value ) ) { + return $sanitized; + } + + foreach ( array_keys( $sanitized ) as $direction ) { + if ( isset( $value[ $direction ] ) && is_numeric( $value[ $direction ] ) ) { + $sanitized[ $direction ] = (float) $value[ $direction ]; } } + if ( isset( $value['unit'] ) ) { + $sanitized['unit'] = neve_sanitize_css_unit( $value['unit'] ); + } - return \Neve\Core\Styles\Css_Prop::transform_directional_prop( $meta, $device, $sanitized, $css_prop, $meta['directional-prop'] ); + return $sanitized; } /** @@ -535,12 +611,27 @@ function neve_sanitize_shadow_css_rule( $css_prop, $value, $width, $height, $blu return sprintf( '%1$s:%2$dpx %3$dpx %4$dpx %5$s;', $css_prop, $width, $height, $blur, $color ); } +/** + * Format a button shadow after normalizing its stored properties. + * + * @param string $css_prop CSS custom property. + * @param mixed $value Stored shadow color. + * @param mixed $properties Stored shadow properties. + * + * @return string + */ +function neve_sanitize_button_shadow_css_rule( $css_prop, $value, $properties ) { + $properties = neve_sanitize_button_shadow_properties( $properties ); + + return neve_sanitize_shadow_css_rule( $css_prop, $value, $properties['width'], $properties['height'], $properties['blur'] ); +} + /** * Sanitize the typography control. * * @param mixed $value The control value. * - * @return array + * @return array{}|array{fontWeight?: string|null, textTransform: string, fontSize: array, lineHeight: array, letterSpacing: array} */ function neve_sanitize_typography_control( $value ) { if ( ! is_array( $value ) ) { @@ -585,11 +676,7 @@ function neve_sanitize_typography_control( $value ) { } if ( isset( $value[ $value_type ]['suffix'] ) ) { - if ( is_array( $value[ $value_type ]['suffix'] ) ) { - $value[ $value_type ]['suffix'] = array_map( 'neve_sanitize_css_unit', $value[ $value_type ]['suffix'] ); - } else { - $value[ $value_type ]['suffix'] = neve_sanitize_css_unit( $value[ $value_type ]['suffix'] ); - } + $value[ $value_type ]['suffix'] = neve_sanitize_css_suffix( $value[ $value_type ]['suffix'] ); } } diff --git a/header-footer-grid/Core/Components/Abstract_Component.php b/header-footer-grid/Core/Components/Abstract_Component.php index 6a5bb80579..5a3f3eaa2a 100644 --- a/header-footer-grid/Core/Components/Abstract_Component.php +++ b/header-footer-grid/Core/Components/Abstract_Component.php @@ -731,6 +731,7 @@ public function add_style( array $css_array = array() ) { '--inheritedff' => [ Dynamic_Selector::META_KEY => $this->get_id() . '_' . self::FONT_FAMILY_ID, Dynamic_Selector::META_DEFAULT => SettingsManager::get_instance()->get_default( $this->get_id() . '_' . self::FONT_FAMILY_ID ), + Dynamic_Selector::META_FILTER => 'neve_sanitize_font_family_css_rule', ], '--inheritedfw' => [ Dynamic_Selector::META_KEY => $this->get_id() . '_' . self::TYPEFACE_ID . '.fontWeight', diff --git a/header-footer-grid/Core/Components/Button.php b/header-footer-grid/Core/Components/Button.php index 4d34b3e635..6fd967fc34 100644 --- a/header-footer-grid/Core/Components/Button.php +++ b/header-footer-grid/Core/Components/Button.php @@ -270,11 +270,8 @@ public function add_style( array $css_array = array() ) { if ( ! isset( $button_values['useShadow'] ) || empty( $button_values['useShadow'] ) ) { return sprintf( '%s:%s;', $css_prop, 'none' ); } - $blur = intval( $button_values['shadowProperties']['blur'] ); - $width = intval( $button_values['shadowProperties']['width'] ); - $height = intval( $button_values['shadowProperties']['height'] ); - return neve_sanitize_shadow_css_rule( $css_prop, $value, $width, $height, $blur ); + return neve_sanitize_button_shadow_css_rule( $css_prop, $value, $button_values['shadowProperties'] ?? array() ); }, ]; $rules['--primarybtnhovershadow'] = [ @@ -284,11 +281,8 @@ public function add_style( array $css_array = array() ) { if ( ! isset( $button_values['useShadowHover'] ) || empty( $button_values['useShadowHover'] ) ) { return sprintf( '%s:%s;', $css_prop, 'none' ); } - $blur = intval( $button_values['shadowPropertiesHover']['blur'] ); - $width = intval( $button_values['shadowPropertiesHover']['width'] ); - $height = intval( $button_values['shadowPropertiesHover']['height'] ); - return neve_sanitize_shadow_css_rule( $css_prop, $value, $width, $height, $blur ); + return neve_sanitize_button_shadow_css_rule( $css_prop, $value, $button_values['shadowPropertiesHover'] ?? array() ); }, ]; diff --git a/inc/abilities/abstract_ability.php b/inc/abilities/abstract_ability.php index f9cb9edd2b..57625b1687 100644 --- a/inc/abilities/abstract_ability.php +++ b/inc/abilities/abstract_ability.php @@ -248,6 +248,10 @@ protected function sanitize_style_mod( string $key, $value ) { return $this->sanitize_background_mod( $value ); } + if ( preg_match( '/_button_appearance$/', $key ) ) { + return $this->sanitize_button_appearance_mod( $value ); + } + if ( preg_match( '/_new_text_color$/', $key ) ) { $color = neve_sanitize_row_text_color( $value ); @@ -263,6 +267,27 @@ protected function sanitize_style_mod( string $key, $value ) { return neve_closes_css_declaration( $value ) ? null : $value; } + /** + * Sanitize a button appearance theme mod value. + * + * @param mixed $value Incoming button appearance. + * @return array|null Sanitized appearance; null when a non-empty color is invalid. + */ + private function sanitize_button_appearance_mod( $value ) { + if ( ! is_array( $value ) ) { + return null; + } + + $appearance = neve_sanitize_button_appearance( $value ); + foreach ( array( 'background', 'backgroundHover', 'text', 'textHover', 'shadowColor', 'shadowColorHover' ) as $field ) { + if ( array_key_exists( $field, $value ) && '' !== $value[ $field ] && '' === $appearance[ $field ] ) { + return null; + } + } + + return neve_closes_css_declaration( $appearance ) ? null : $appearance; + } + /** * Sanitize a background theme mod value. * diff --git a/inc/abilities/appearance_update.php b/inc/abilities/appearance_update.php index 1aa19cb431..293796031d 100644 --- a/inc/abilities/appearance_update.php +++ b/inc/abilities/appearance_update.php @@ -416,18 +416,18 @@ private function apply_colors( array $colors, array &$updated, array &$warnings * @return void */ private function apply_typography( array $typography, array &$updated ) { - if ( isset( $typography['body_font'] ) ) { - $font = neve_sanitize_font_family( $typography['body_font'] ); - if ( '' !== $font || '' === $typography['body_font'] ) { - set_theme_mod( 'neve_body_font_family', $font ); - $updated[] = 'neve_body_font_family'; + $font_mods = array( + 'body_font' => 'neve_body_font_family', + 'headings_font' => 'neve_headings_font_family', + ); + foreach ( $font_mods as $input_key => $mod ) { + if ( ! isset( $typography[ $input_key ] ) ) { + continue; } - } - if ( isset( $typography['headings_font'] ) ) { - $font = neve_sanitize_font_family( $typography['headings_font'] ); - if ( '' !== $font || '' === $typography['headings_font'] ) { - set_theme_mod( 'neve_headings_font_family', $font ); - $updated[] = 'neve_headings_font_family'; + $font = neve_sanitize_font_family( $typography[ $input_key ] ); + if ( '' !== $font || '' === $typography[ $input_key ] ) { + set_theme_mod( $mod, $font ); + $updated[] = $mod; } } diff --git a/inc/core/styles/css_prop.php b/inc/core/styles/css_prop.php index 14fa24a679..d8b9599714 100644 --- a/inc/core/styles/css_prop.php +++ b/inc/core/styles/css_prop.php @@ -77,11 +77,17 @@ public static function get_suffix_responsive( $meta, $device ) { * @return string */ public static function transform( $css_prop, $value, $meta, $device ) { - if ( isset( $meta[ Dynamic_Selector::META_KEY ] ) && preg_match( '/\.(?:fontSize|lineHeight|letterSpacing)$/', $meta[ Dynamic_Selector::META_KEY ] ) ) { - if ( ! is_numeric( $value ) ) { + if ( isset( $meta[ Dynamic_Selector::META_KEY ] ) && preg_match( '/\.(fontSize|lineHeight|letterSpacing|fontWeight|textTransform)$/', $meta[ Dynamic_Selector::META_KEY ], $matches ) ) { + if ( in_array( $matches[1], array( 'fontSize', 'lineHeight', 'letterSpacing' ), true ) ) { + if ( ! is_numeric( $value ) ) { + return ''; + } + $value = (float) $value; + } elseif ( 'fontWeight' === $matches[1] && ! in_array( $value, array( 'none', '100', '200', '300', '400', '500', '600', '700', '800', '900' ), true ) ) { + return ''; + } elseif ( 'textTransform' === $matches[1] && ! in_array( $value, array( 'none', 'uppercase', 'lowercase', 'capitalize' ), true ) ) { return ''; } - $value = (float) $value; } //If we have a custom filter, let's call it. diff --git a/inc/core/styles/css_vars.php b/inc/core/styles/css_vars.php index 8b0f4e7051..36009259ae 100644 --- a/inc/core/styles/css_vars.php +++ b/inc/core/styles/css_vars.php @@ -98,60 +98,44 @@ public function get_button_rules() { // Button Shadow Primary if ( isset( $primary_values['useShadow'] ) && ! empty( $primary_values['useShadow'] ) ) { $rules['--primarybtnshadow'] = [ - Dynamic_Selector::META_KEY => $mod_key_primary . '.shadowColor', - Dynamic_Selector::META_DEFAULT => 'none', - Dynamic_Selector::META_FILTER => function ( $css_prop, $value, $meta, $device ) use ($primary_values) { - $blur = intval($primary_values['shadowProperties']['blur']); - $width = intval($primary_values['shadowProperties']['width']); - $height = intval($primary_values['shadowProperties']['height']); - - return neve_sanitize_shadow_css_rule( $css_prop, $value, $width, $height, $blur ); - } + Dynamic_Selector::META_KEY => $mod_key_primary . '.shadowColor', + Dynamic_Selector::META_DEFAULT => 'none', + Dynamic_Selector::META_FILTER => function ( $css_prop, $value, $meta, $device ) use ( $primary_values ) { + return neve_sanitize_button_shadow_css_rule( $css_prop, $value, $primary_values['shadowProperties'] ?? array() ); + }, ]; } // Button Shadow Primary Hover if ( isset( $primary_values['useShadowHover'] ) && ! empty( $primary_values['useShadowHover'] ) ) { $rules['--primarybtnhovershadow'] = [ - Dynamic_Selector::META_KEY => $mod_key_primary . '.shadowColorHover', - Dynamic_Selector::META_DEFAULT => 'none', - Dynamic_Selector::META_FILTER => function ( $css_prop, $value, $meta, $device ) use ($primary_values) { - $blur = intval($primary_values['shadowPropertiesHover']['blur']); - $width = intval($primary_values['shadowPropertiesHover']['width']); - $height = intval($primary_values['shadowPropertiesHover']['height']); - - return neve_sanitize_shadow_css_rule( $css_prop, $value, $width, $height, $blur ); - } + Dynamic_Selector::META_KEY => $mod_key_primary . '.shadowColorHover', + Dynamic_Selector::META_DEFAULT => 'none', + Dynamic_Selector::META_FILTER => function ( $css_prop, $value, $meta, $device ) use ( $primary_values ) { + return neve_sanitize_button_shadow_css_rule( $css_prop, $value, $primary_values['shadowPropertiesHover'] ?? array() ); + }, ]; } // Button Shadow Secondary if ( isset( $secondary_values['useShadow'] ) && ! empty( $secondary_values['useShadow'] ) ) { $rules['--secondarybtnshadow'] = [ - Dynamic_Selector::META_KEY => $mod_key_secondary . '.shadowColor', - Dynamic_Selector::META_DEFAULT => 'none', - Dynamic_Selector::META_FILTER => function ( $css_prop, $value, $meta, $device ) use ($secondary_values) { - $blur = intval($secondary_values['shadowProperties']['blur']); - $width = intval($secondary_values['shadowProperties']['width']); - $height = intval($secondary_values['shadowProperties']['height']); - - return neve_sanitize_shadow_css_rule( $css_prop, $value, $width, $height, $blur ); - } + Dynamic_Selector::META_KEY => $mod_key_secondary . '.shadowColor', + Dynamic_Selector::META_DEFAULT => 'none', + Dynamic_Selector::META_FILTER => function ( $css_prop, $value, $meta, $device ) use ( $secondary_values ) { + return neve_sanitize_button_shadow_css_rule( $css_prop, $value, $secondary_values['shadowProperties'] ?? array() ); + }, ]; } // Button Shadow Secondary Hover if ( isset( $secondary_values['useShadowHover'] ) && ! empty( $secondary_values['useShadowHover'] ) ) { $rules['--secondarybtnhovershadow'] = [ - Dynamic_Selector::META_KEY => $mod_key_secondary . '.shadowColorHover', - Dynamic_Selector::META_DEFAULT => 'none', - Dynamic_Selector::META_FILTER => function ( $css_prop, $value, $meta, $device ) use ($secondary_values) { - $blur = intval($secondary_values['shadowPropertiesHover']['blur']); - $width = intval($secondary_values['shadowPropertiesHover']['width']); - $height = intval($secondary_values['shadowPropertiesHover']['height']); - - return neve_sanitize_shadow_css_rule( $css_prop, $value, $width, $height, $blur ); - } + Dynamic_Selector::META_KEY => $mod_key_secondary . '.shadowColorHover', + Dynamic_Selector::META_DEFAULT => 'none', + Dynamic_Selector::META_FILTER => function ( $css_prop, $value, $meta, $device ) use ( $secondary_values ) { + return neve_sanitize_button_shadow_css_rule( $css_prop, $value, $secondary_values['shadowPropertiesHover'] ?? array() ); + }, ]; } @@ -173,15 +157,20 @@ public function get_button_rules() { ]; } - $mod_key_primary = Config::MODS_BUTTON_PRIMARY_PADDING; - $default_primary = Mods::get_alternative_mod_default( Config::MODS_BUTTON_PRIMARY_PADDING ); + $mod_key_primary = Config::MODS_BUTTON_PRIMARY_PADDING; + $default_primary = Mods::get_alternative_mod_default( Config::MODS_BUTTON_PRIMARY_PADDING ); $rules['--btnpadding'] = [ Dynamic_Selector::META_KEY => $mod_key_primary, Dynamic_Selector::META_DEFAULT => $default_primary, Dynamic_Selector::META_IS_RESPONSIVE => true, - Dynamic_Selector::META_SUFFIX => 'responsive_unit', + Dynamic_Selector::META_SUFFIX => 'responsive_unit', Dynamic_Selector::META_FILTER => function ( $css_prop, $value, $meta, $device ) { + $value = neve_sanitize_directional_value( $value ); + if ( empty( array_filter( $value, 'is_numeric' ) ) ) { + return ''; + } + $mod_key_primary = Config::MODS_BUTTON_PRIMARY_STYLE; $default_primary = neve_get_button_appearance_default(); @@ -201,18 +190,18 @@ public function get_button_rules() { if ( ! isset( $appearance_values['type'] ) || $appearance_values['type'] !== 'outline' ) { continue; } - $border_width = neve_sanitize_button_dimension( $appearance_values['borderWidth'] ); + $border_width = neve_sanitize_button_dimension( $appearance_values['borderWidth'] ?? array() ); foreach ( $paddings[ $btn_type ] as $direction => $padding_value ) { if ( ! isset( $border_width[ $direction ] ) || absint( $border_width[ $direction ] ) === 0 ) { continue; } - if( ! is_numeric( $padding_value ) ){ + if ( ! is_numeric( $padding_value ) ) { continue; } $suffix_css_prop = $btn_type === 'primary' ? '--primarybtnpadding' : '--secondarybtnpadding'; - $suffix = Css_Prop::get_suffix( $meta, $device, $value, $suffix_css_prop ); + $suffix = Css_Prop::get_suffix( $meta, $device, $value, $suffix_css_prop ); $paddings[ $btn_type ][ 'is_outline_button_padding' ] = true; $paddings[ $btn_type ][ $direction ] = 'calc(' . $padding_value . $suffix . ' - ' . $border_width[ $direction ] . 'px)'; } diff --git a/inc/core/styles/frontend.php b/inc/core/styles/frontend.php index beb63871a0..ff402bb569 100644 --- a/inc/core/styles/frontend.php +++ b/inc/core/styles/frontend.php @@ -251,11 +251,11 @@ public function setup_blog_card_style() { $output = ''; $unit = Css_Prop::get_unit_responsive( $meta, $device ); - if ( isset( $value['right'] ) && ! empty( $value['right'] ) ) { - $output .= sprintf( 'margin-right:-%1$s%2$s;', $value['right'], $unit ); + if ( isset( $value['right'] ) && is_numeric( $value['right'] ) && ! empty( $value['right'] ) ) { + $output .= sprintf( 'margin-right:-%1$s%2$s;', (float) $value['right'], $unit ); } - if ( isset( $value['left'] ) && ! empty( $value['left'] ) ) { - $output .= sprintf( 'margin-left:-%1$s%2$s;', $value['left'], $unit ); + if ( isset( $value['left'] ) && is_numeric( $value['left'] ) && ! empty( $value['left'] ) ) { + $output .= sprintf( 'margin-left:-%1$s%2$s;', (float) $value['left'], $unit ); } return $output; @@ -275,28 +275,12 @@ public function setup_blog_card_style() { }, ], '--padding' => [ - 'key' => 'neve_blog_content_padding', - 'default' => $content_padding_default, - 'is_responsive' => true, - 'directional-prop' => 'padding', - 'suffix' => 'responsive_unit', - 'filter' => function ( $css_prop, $value, $meta, $device ) { - $output = ''; - $unit = Css_Prop::get_unit_responsive( $meta, $device ); - - $check = array_filter( - $value, - function ( $item ) { - return ! empty( $item ) || $item === '0'; - } - ); - - if ( empty( $check ) ) { - return ''; - } - - return sprintf( $css_prop . ':%1$s%5$s %2$s%5$s %3$s%5$s %4$s%5$s;', $value['top'], $value['right'], $value['bottom'], $value['left'], $unit ); - }, + Dynamic_Selector::META_KEY => 'neve_blog_content_padding', + Dynamic_Selector::META_DEFAULT => $content_padding_default, + Dynamic_Selector::META_IS_RESPONSIVE => true, + Dynamic_Selector::META_DIRECTIONAL_PROP => 'padding', + Dynamic_Selector::META_SUFFIX => 'responsive_unit', + Dynamic_Selector::META_FILTER => 'neve_sanitize_directional_css_rule', ], ]; @@ -1031,6 +1015,7 @@ private function setup_content_vspacing() { Dynamic_Selector::META_IS_RESPONSIVE => true, Dynamic_Selector::META_SUFFIX => 'responsive_unit', Dynamic_Selector::META_DEFAULT => $this->content_vspacing_default(), + Dynamic_Selector::META_FILTER => 'neve_sanitize_directional_css_rule', Dynamic_Selector::META_DIRECTIONAL_PROP => Config::CSS_PROP_DIRECTIONAL_ONE_AXIS, ], ]; diff --git a/inc/customizer/options/buttons.php b/inc/customizer/options/buttons.php index 4627a3e66e..5e1f8e3b75 100644 --- a/inc/customizer/options/buttons.php +++ b/inc/customizer/options/buttons.php @@ -71,17 +71,17 @@ public function add_controls() { new Control( $mod_key, array( - 'default' => $defaults, + 'default' => $defaults, + 'sanitize_callback' => array( $this, 'sanitize_spacing_array' ), ), array( - 'label' => __( 'Padding', 'neve' ), - 'sanitize_callback' => array( $this, 'sanitize_spacing_array' ), - 'section' => $this->section_id, - 'input_attrs' => [ + 'label' => __( 'Padding', 'neve' ), + 'section' => $this->section_id, + 'input_attrs' => [ 'units' => [ 'px', 'em', 'rem' ], 'min' => 0, ], - 'default' => $defaults, + 'default' => $defaults, ), '\Neve\Customizer\Controls\React\Spacing' ) diff --git a/inc/customizer/options/layout_container.php b/inc/customizer/options/layout_container.php index bbf9360684..519d4f90ba 100644 --- a/inc/customizer/options/layout_container.php +++ b/inc/customizer/options/layout_container.php @@ -107,12 +107,12 @@ private function control_vertical_spacing() { new Control( Config::MODS_CONTENT_VSPACING, [ - 'default' => $this->content_vspacing_default(), - 'transport' => $this->selective_refresh, + 'default' => $this->content_vspacing_default(), + 'transport' => $this->selective_refresh, + 'sanitize_callback' => [ $this, 'sanitize_spacing_array' ], ], [ 'label' => __( 'Content Vertical Spacing', 'neve' ), - 'sanitize_callback' => [ $this, 'sanitize_spacing_array' ], 'section' => 'neve_container', 'input_attrs' => [ 'units' => [ 'px', 'vh' ], diff --git a/tests/test-neve-ability-style-mods.php b/tests/test-neve-ability-style-mods.php index 244b515d5f..0405047332 100644 --- a/tests/test-neve-ability-style-mods.php +++ b/tests/test-neve-ability-style-mods.php @@ -27,6 +27,7 @@ class TestNeveAbilityStyleMods extends WP_UnitTestCase { const SIDEBAR_WIDTH = 'hfg_header_layout_sidebar_width'; const HEADER_HEIGHT = 'hfg_header_layout_main_height'; const FORM_PADDING = 'neve_form_fields_padding'; + const NAV_BUTTON = 'nav-icon_button_appearance'; const LINE_HEIGHT = 1.5; /** @@ -481,14 +482,26 @@ public function test_typography_values_are_sanitized_on_write_and_output(): void $this->assertArrayNotHasKey( 'desktop', $typeface['fontSize'] ); $this->assertSame( 'px', $typeface['fontSize']['suffix']['desktop'] ); - set_theme_mod( 'neve_body_font_family', 'Arial;}body{background:lime}/*' ); + $json_font = '{"x\\";}}body{background:lime}/*":0}'; + ( new Appearance_Update() )->execute( + array( + 'typography' => array( 'body_font' => $json_font ), + ) + ); + $this->assertSame( 'Arial', get_theme_mod( 'neve_body_font_family' ) ); + + set_theme_mod( 'neve_body_font_family', $json_font ); set_theme_mod( 'neve_typeface_general', array( - 'fontSize' => array( + 'fontSize' => array( 'desktop' => '10;}body{background:lime}/*', 'suffix' => array( 'desktop' => 'px;}body{background:lime}/*' ), ), + 'lineHeight' => array( 'desktop' => '10;}body{background:lime}/*' ), + 'letterSpacing' => array( 'desktop' => '10;}body{background:lime}/*' ), + 'fontWeight' => '10;}body{background:lime}/*', + 'textTransform' => 'none;}body{background:lime}/*', ) ); $css = (string) ( new Frontend() )->generate(); @@ -582,6 +595,7 @@ public function test_stored_header_component_spacing_is_sanitized(): void { foreach ( $mods as $mod ) { set_theme_mod( $mod, $invalid ); } + set_theme_mod( 'primary-menu_component_font_family', 'Arial;}body{background:lime}/*' ); $builder = \HFG\Main::get_instance()->get_builder( 'header' ); foreach ( array( 'mobile', 'tablet', 'desktop' ) as $device ) { @@ -612,6 +626,42 @@ public function test_site_set_theme_mod_rejects_invalid_color(): void { $this->assertSame( '#ff0000', get_theme_mod( self::SIDEBAR_TEXT ) ); } + /** + * Invalid button appearance colors keep the stored appearance. + */ + public function test_site_set_theme_mod_rejects_invalid_button_appearance_color(): void { + $stored = array( + 'background' => '#123456', + 'text' => '#ffffff', + ); + set_theme_mod( self::NAV_BUTTON, $stored ); + + $result = ( new Site_Set_Theme_Mod() )->execute( + array( + 'action' => 'set', + 'key' => self::NAV_BUTTON, + 'value' => array( 'background' => 'notacolor' ), + ) + ); + + $this->assertIsArray( $result ); + $this->assertFalse( $result['success'] ); + $this->assertSame( 'invalid_value', $result['error']['code'] ); + $this->assertSame( $stored, get_theme_mod( self::NAV_BUTTON ) ); + + $result = ( new Site_Set_Theme_Mod() )->execute( + array( + 'action' => 'set', + 'key' => self::NAV_BUTTON, + 'value' => array( 'background' => 'transparent' ), + ) + ); + + $this->assertIsArray( $result ); + $this->assertTrue( $result['success'] ); + $this->assertSame( 'transparent', get_theme_mod( self::NAV_BUTTON )['background'] ); + } + /** * An empty color clears the stored color. */ @@ -691,7 +741,7 @@ public function test_stored_ability_styles_are_sanitized_when_css_is_generated() 'tablet-unit' => self::INJECTED_CSS, 'desktop-unit' => self::INJECTED_CSS, ); - foreach ( array( 'neve_comments_boxed_padding', 'neve_comments_form_boxed_padding', 'neve_post_cover_padding', 'neve_post_cover_title_boxed_padding', 'neve_post_content_vspacing' ) as $mod ) { + foreach ( array( 'neve_content_vspacing', 'neve_blog_content_padding', 'neve_comments_boxed_padding', 'neve_comments_form_boxed_padding', 'neve_post_cover_padding', 'neve_post_cover_title_boxed_padding', 'neve_post_content_vspacing' ) as $mod ) { set_theme_mod( $mod, $responsive_directional ); } set_theme_mod( 'neve_post_inherit_vspacing', 'individual' ); @@ -705,7 +755,7 @@ public function test_stored_ability_styles_are_sanitized_when_css_is_generated() $this->assertStringContainsString( '--formfieldbordercolor: rgba(1,2,3,1);', $css ); $this->assertStringContainsString( '--formfieldcolor: rgba(1,2,3,1);', $css ); $this->assertStringContainsString( '--formfieldborderwidth:2px;', $css ); - $this->assertStringContainsString( '--formfieldborderradius:0;', $css ); + $this->assertStringNotContainsString( '--formfieldborderradius:', $css ); $this->assertStringContainsString( '--formfieldpadding:2px;', $css ); $this->assertStringContainsString( '--padding:2px;', $css ); $this->assertStringContainsString( '--borderradius: 3px;', $css ); diff --git a/tests/test-neve-button-appearance-css.php b/tests/test-neve-button-appearance-css.php index 175915b66c..576c4151ea 100644 --- a/tests/test-neve-button-appearance-css.php +++ b/tests/test-neve-button-appearance-css.php @@ -62,6 +62,9 @@ public function test_button_appearance_sanitizes_color_fields(): void { foreach ( array( 'borderRadius', 'borderWidth' ) as $field ) { $this->assertSame( array_fill_keys( array( 'top', 'right', 'bottom', 'left' ), 0 ), $appearance[ $field ] ); } + foreach ( array( 'shadowProperties', 'shadowPropertiesHover' ) as $field ) { + $this->assertSame( array( 'blur' => 5, 'width' => 0, 'height' => 0 ), $appearance[ $field ] ); + } $this->assertSame( 'outline', $appearance['type'] ); $this->assertSame( array(), neve_sanitize_button_appearance( 'invalid' ) ); $this->assertSame( '', neve_sanitize_color_css_rule( '--color', 'invalid' ) ); @@ -88,6 +91,71 @@ public function test_global_button_css_sanitizes_stored_colors(): void { $this->assertStringContainsString( '--primarybtnborderwidth:0;', $css ); } + /** + * Malformed stored button shadows and padding are normalized before output. + */ + public function test_global_button_css_handles_malformed_stored_structures(): void { + $appearance = neve_get_button_appearance_default(); + $appearance['useShadow'] = true; + $appearance['useShadowHover'] = true; + $appearance['shadowProperties'] = 'invalid'; + $appearance['shadowPropertiesHover'] = array( 'blur' => self::INJECTED_CSS ); + set_theme_mod( 'neve_button_appearance', $appearance ); + set_theme_mod( 'neve_secondary_button_appearance', $appearance ); + set_theme_mod( + 'neve_button_padding', + array( + 'mobile' => array_fill_keys( array( 'top', 'right', 'bottom', 'left' ), self::INJECTED_CSS ), + 'tablet' => array_fill_keys( array( 'top', 'right', 'bottom', 'left' ), self::INJECTED_CSS ), + 'desktop' => array_fill_keys( array( 'top', 'right', 'bottom', 'left' ), self::INJECTED_CSS ), + 'mobile-unit' => self::INJECTED_CSS, + 'tablet-unit' => self::INJECTED_CSS, + 'desktop-unit' => self::INJECTED_CSS, + ) + ); + + $rules = ( new NeveButtonRulesWrapperForTest() )->get_button_rules(); + $css = (string) new Dynamic_Selector( array( ':root' => $rules ) ); + + $this->assertStringNotContainsString( 'background:lime', $css ); + $this->assertStringContainsString( '--primarybtnshadow:0px 0px 0px', $css ); + $this->assertStringContainsString( '--secondarybtnhovershadow:0px 0px 0px', $css ); + $this->assertStringNotContainsString( '--btnpadding:', $css ); + } + + /** + * The Customizer setting sanitizes button padding before storage. + */ + public function test_button_padding_customizer_setting_sanitizes_input(): void { + require_once ABSPATH . WPINC . '/class-wp-customize-manager.php'; + + $wp_customize = new WP_Customize_Manager(); + ( new \Neve\Customizer\Options\Buttons() )->register_controls_callback( $wp_customize ); + $setting = $wp_customize->get_setting( 'neve_button_padding' ); + + $this->assertNotNull( $setting ); + $input = array( + 'mobile' => array_fill_keys( array( 'top', 'right', 'bottom', 'left' ), self::INJECTED_CSS ), + 'tablet' => array( 'top' => 12, 'right' => self::INJECTED_CSS ), + 'desktop' => array( 'top' => 10, 'right' => 20, 'bottom' => 30, 'left' => 40 ), + 'mobile-unit' => self::INJECTED_CSS, + 'tablet-unit' => 'em', + 'desktop-unit' => 'px', + ); + + $this->assertSame( + array( + 'mobile' => array(), + 'tablet' => array( 'top' => 12 ), + 'desktop' => array( 'top' => 10, 'right' => 20, 'bottom' => 30, 'left' => 40 ), + 'mobile-unit' => 'px', + 'tablet-unit' => 'em', + 'desktop-unit' => 'px', + ), + $setting->sanitize( $input ) + ); + } + /** * Previously stored component colors are sanitized when header CSS is generated. */ @@ -115,6 +183,28 @@ public function test_header_component_css_sanitizes_stored_colors(): void { $this->assertStringContainsString( '--primarybtnborderradius:0;', $css ); } + /** + * Malformed stored component shadows are normalized before output. + */ + public function test_header_component_css_handles_malformed_stored_shadow(): void { + $appearance = neve_get_button_appearance_default(); + $appearance['useShadow'] = true; + $appearance['shadowProperties'] = 'invalid'; + set_theme_mod( 'button_base_style_setting', $appearance ); + + $builder = \HFG\Main::get_instance()->get_builder( 'header' ); + $styles = $builder->add_style( array() ); + foreach ( $builder->get_components() as $component ) { + if ( 'button_base' === $component->get_id() ) { + $styles = $component->add_style( $styles ); + } + } + + $css = (string) new Dynamic_Selector( $styles ); + + $this->assertStringContainsString( '--primarybtnshadow:0px 0px 0px', $css ); + } + /** * Button appearance whose color fields can close their CSS declaration. * diff --git a/tests/test-neve-sanitization.php b/tests/test-neve-sanitization.php index 54e4e7be0d..5615104ed0 100644 --- a/tests/test-neve-sanitization.php +++ b/tests/test-neve-sanitization.php @@ -213,6 +213,34 @@ public function test_typography_sanitizer_keeps_inherited_font_weight() { $this->assertArrayNotHasKey( 'fontWeight', neve_sanitize_typography_control( $value ) ); } + /** + * Blank spacing remains blank instead of overriding inherited spacing with zero. + */ + public function test_directional_css_sanitizer_keeps_blank_spacing() { + $value = array_fill_keys( array( 'top', 'right', 'bottom', 'left' ), '' ); + $meta = array( + 'key' => 'blank_spacing', + 'suffix' => 'px', + 'directional-prop' => \Neve\Core\Settings\Config::CSS_PROP_PADDING, + ); + + $this->assertSame( '', neve_sanitize_directional_css_rule( '--padding', $value, $meta, 'desktop' ) ); + } + + /** + * Declaration checks inspect JSON object keys as well as values. + */ + public function test_css_declaration_check_inspects_json_keys() { + $this->assertTrue( neve_closes_css_declaration( '{"x\\";}}body{background:lime}/*":0}' ) ); + } + + /** + * Valid font families remain available to component custom properties. + */ + public function test_font_family_css_rule_preserves_valid_fonts() { + $this->assertSame( ' --inheritedff: Arial;', neve_sanitize_font_family_css_rule( '--inheritedff', 'Arial' ) ); + } + /** * Logo data can already be decoded when it reaches the sanitizer. */ From cd654f7d5e9dd8524a2c7462c43b94745f1a3539 Mon Sep 17 00:00:00 2001 From: selul Date: Wed, 11 Nov 2026 02:27:55 +0200 Subject: [PATCH 09/16] fix: preserve valid stored typography --- globals/sanitize-functions.php | 2 +- inc/abilities/abstract_ability.php | 7 ++- inc/core/styles/css_prop.php | 2 +- inc/core/styles/frontend.php | 4 +- tests/test-neve-ability-style-mods.php | 67 ++++++++++++++++++++++++++ tests/test-neve-sanitization.php | 4 ++ 6 files changed, 82 insertions(+), 4 deletions(-) diff --git a/globals/sanitize-functions.php b/globals/sanitize-functions.php index 1e43e8a70e..33b7983730 100644 --- a/globals/sanitize-functions.php +++ b/globals/sanitize-functions.php @@ -654,7 +654,7 @@ function neve_sanitize_typography_control( $value ) { } // Font Weight. - if ( isset( $value['fontWeight'] ) && ! in_array( $value['fontWeight'], [ 'none', '100', '200', '300', '400', '500', '600', '700', '800', '900' ], true ) ) { + if ( isset( $value['fontWeight'] ) && ! in_array( $value['fontWeight'], [ 'none', 'normal', 'bold', 'bolder', 'lighter', '100', '200', '300', '400', '500', '600', '700', '800', '900' ], true ) ) { $value['fontWeight'] = '300'; } // Text Transform. diff --git a/inc/abilities/abstract_ability.php b/inc/abilities/abstract_ability.php index 57625b1687..b49e4dc196 100644 --- a/inc/abilities/abstract_ability.php +++ b/inc/abilities/abstract_ability.php @@ -249,7 +249,12 @@ protected function sanitize_style_mod( string $key, $value ) { } if ( preg_match( '/_button_appearance$/', $key ) ) { - return $this->sanitize_button_appearance_mod( $value ); + if ( is_array( $value ) ) { + return $this->sanitize_button_appearance_mod( $value ); + } + if ( 'header_search_button_appearance' !== $key ) { + return null; + } } if ( preg_match( '/_new_text_color$/', $key ) ) { diff --git a/inc/core/styles/css_prop.php b/inc/core/styles/css_prop.php index d8b9599714..6421cfe1b1 100644 --- a/inc/core/styles/css_prop.php +++ b/inc/core/styles/css_prop.php @@ -83,7 +83,7 @@ public static function transform( $css_prop, $value, $meta, $device ) { return ''; } $value = (float) $value; - } elseif ( 'fontWeight' === $matches[1] && ! in_array( $value, array( 'none', '100', '200', '300', '400', '500', '600', '700', '800', '900' ), true ) ) { + } elseif ( 'fontWeight' === $matches[1] && ! in_array( $value, array( 'none', 'normal', 'bold', 'bolder', 'lighter', '100', '200', '300', '400', '500', '600', '700', '800', '900' ), true ) ) { return ''; } elseif ( 'textTransform' === $matches[1] && ! in_array( $value, array( 'none', 'uppercase', 'lowercase', 'capitalize' ), true ) ) { return ''; diff --git a/inc/core/styles/frontend.php b/inc/core/styles/frontend.php index ff402bb569..acb0a3c83f 100644 --- a/inc/core/styles/frontend.php +++ b/inc/core/styles/frontend.php @@ -179,7 +179,9 @@ public function setup_blog_typography() { Dynamic_Selector::META_KEY => $args['mod'] . '.fontWeight', 'font' => 'mods_' . $args['font'], ], - '--texttransform' => $args['mod'] . '.textTransform', + '--texttransform' => [ + Dynamic_Selector::META_KEY => $args['mod'] . '.textTransform', + ], ]; } } diff --git a/tests/test-neve-ability-style-mods.php b/tests/test-neve-ability-style-mods.php index 0405047332..65d08701af 100644 --- a/tests/test-neve-ability-style-mods.php +++ b/tests/test-neve-ability-style-mods.php @@ -491,6 +491,7 @@ public function test_typography_values_are_sanitized_on_write_and_output(): void $this->assertSame( 'Arial', get_theme_mod( 'neve_body_font_family' ) ); set_theme_mod( 'neve_body_font_family', $json_font ); + set_theme_mod( 'neve_archive_typography_post_title', array( 'textTransform' => self::INJECTED_CSS ) ); set_theme_mod( 'neve_typeface_general', array( @@ -508,6 +509,33 @@ public function test_typography_values_are_sanitized_on_write_and_output(): void $this->assertStringNotContainsString( 'background:lime', $css ); } + /** + * Stored keyword font weights render and survive partial updates. + */ + public function test_keyword_font_weight_survives_partial_typography_update(): void { + $typeface = Mods::get_alternative_mod_default( 'neve_typeface_general' ); + $typeface = is_array( $typeface ) ? $typeface : array(); + $typeface['fontWeight'] = 'bold'; + set_theme_mod( 'neve_typeface_general', $typeface ); + + $css = (string) ( new Frontend() )->generate(); + $this->assertStringContainsString( '--bodyfontweight: bold;', $css ); + + $result = ( new Appearance_Update() )->execute( + array( + 'typography' => array( + 'element' => array( + 'target' => 'body', + 'fontSize' => array( 'desktop' => 24 ), + ), + ), + ) + ); + + $this->assertIsArray( $result ); + $this->assertSame( 'bold', get_theme_mod( 'neve_typeface_general' )['fontWeight'] ); + } + /** * Partial typography updates retain the target's defaults. */ @@ -649,6 +677,18 @@ public function test_site_set_theme_mod_rejects_invalid_button_appearance_color( $this->assertSame( 'invalid_value', $result['error']['code'] ); $this->assertSame( $stored, get_theme_mod( self::NAV_BUTTON ) ); + $result = ( new Site_Set_Theme_Mod() )->execute( + array( + 'action' => 'set', + 'key' => self::NAV_BUTTON, + 'value' => 'text_button', + ) + ); + + $this->assertIsArray( $result ); + $this->assertFalse( $result['success'] ); + $this->assertSame( $stored, get_theme_mod( self::NAV_BUTTON ) ); + $result = ( new Site_Set_Theme_Mod() )->execute( array( 'action' => 'set', @@ -662,6 +702,33 @@ public function test_site_set_theme_mod_rejects_invalid_button_appearance_color( $this->assertSame( 'transparent', get_theme_mod( self::NAV_BUTTON )['background'] ); } + /** + * Scalar button mode settings keep the generic allowlisted write path. + */ + public function test_site_set_theme_mod_accepts_scalar_button_appearance_mode(): void { + $allow_search_button = static function ( $allowlist ) { + $allowlist[] = 'header_search_button_appearance'; + + return $allowlist; + }; + add_filter( 'neve_abilities_theme_mod_allowlist', $allow_search_button ); + try { + $result = ( new Site_Set_Theme_Mod() )->execute( + array( + 'action' => 'set', + 'key' => 'header_search_button_appearance', + 'value' => 'text_button', + ) + ); + } finally { + remove_filter( 'neve_abilities_theme_mod_allowlist', $allow_search_button ); + } + + $this->assertIsArray( $result ); + $this->assertTrue( $result['success'] ); + $this->assertSame( 'text_button', get_theme_mod( 'header_search_button_appearance' ) ); + } + /** * An empty color clears the stored color. */ diff --git a/tests/test-neve-sanitization.php b/tests/test-neve-sanitization.php index 5615104ed0..21bd499f15 100644 --- a/tests/test-neve-sanitization.php +++ b/tests/test-neve-sanitization.php @@ -208,6 +208,10 @@ public function test_typography_sanitizer_keeps_inherited_font_weight() { ); $this->assertSame( $value, neve_sanitize_typography_control( $value ) ); + foreach ( array( 'normal', 'bold', 'bolder', 'lighter' ) as $font_weight ) { + $value['fontWeight'] = $font_weight; + $this->assertSame( $font_weight, neve_sanitize_typography_control( $value )['fontWeight'] ); + } unset( $value['fontWeight'] ); $this->assertArrayNotHasKey( 'fontWeight', neve_sanitize_typography_control( $value ) ); From 21951e2f10e905018f0e6c5788812d96b30a51e9 Mon Sep 17 00:00:00 2001 From: selul Date: Wed, 11 Nov 2026 02:56:38 +0200 Subject: [PATCH 10/16] fix: validate stored gradient values --- globals/sanitize-functions.php | 118 ++++++++++++++++++++++++- tests/test-neve-ability-style-mods.php | 24 ++--- tests/test-neve-sanitization.php | 5 ++ 3 files changed, 134 insertions(+), 13 deletions(-) diff --git a/globals/sanitize-functions.php b/globals/sanitize-functions.php index 33b7983730..98bce721a2 100644 --- a/globals/sanitize-functions.php +++ b/globals/sanitize-functions.php @@ -44,8 +44,122 @@ function neve_is_css_var( $value ) { * @return bool */ function neve_is_css_gradient( string $value ): bool { - // Recursion keeps parentheses balanced, so nothing can follow the closing one. - return (bool) preg_match( '/^(?:repeating-)?(?:linear|radial|conic)-gradient(\((?:[a-z0-9#%.,\s\/_-]|(?1))*\))$/i', trim( $value ) ); + $value = trim( $value ); + if ( ! preg_match( '/^(?:repeating-)?(linear|radial|conic)-gradient\((.*)\)$/iD', $value, $matches ) ) { + return false; + } + + $body = $matches[2]; + if ( '' === trim( $body ) || ! preg_match( '/^[a-z0-9#%.,\s\/_()\-]+$/i', $body ) ) { + return false; + } + + $arguments = array(); + $current = ''; + $depth = 0; + $length = strlen( $body ); + for ( $index = 0; $index < $length; $index++ ) { + $character = $body[ $index ]; + if ( '(' === $character ) { + $depth++; + } elseif ( ')' === $character ) { + $depth--; + if ( $depth < 0 ) { + return false; + } + } + + if ( ',' === $character && 0 === $depth ) { + $arguments[] = trim( $current ); + $current = ''; + continue; + } + + $current .= $character; + } + + if ( 0 !== $depth ) { + return false; + } + $arguments[] = trim( $current ); + + if ( ! neve_gradient_stop_starts_with_color( $arguments[0] ) ) { + if ( ! neve_is_css_gradient_prelude( $matches[1], $arguments[0] ) ) { + return false; + } + array_shift( $arguments ); + } + + if ( count( $arguments ) < 2 ) { + return false; + } + + foreach ( $arguments as $argument ) { + if ( ! neve_gradient_stop_starts_with_color( $argument ) ) { + return false; + } + } + + return true; +} + +/** + * Check whether a gradient's optional first argument is a supported prelude. + * + * @param string $type Gradient type. + * @param string $value Prelude value. + * + * @return bool + */ +function neve_is_css_gradient_prelude( string $type, string $value ): bool { + $value = trim( $value ); + $number = '[+-]?(?:\d+(?:\.\d*)?|\.\d+)'; + $angle = $number . '(?:deg|grad|rad|turn)'; + $dimension = $number . '(?:%|[a-z]+)?'; + $position = '(?:center|left|right|top|bottom|' . $dimension . ')'; + + if ( 'linear' === strtolower( $type ) ) { + return (bool) preg_match( '/^(?:' . $angle . '|to\s+(?:left|right|top|bottom)(?:\s+(?:left|right|top|bottom))?)$/i', $value ); + } + + if ( 'radial' === strtolower( $type ) ) { + $size = '(?:closest-side|closest-corner|farthest-side|farthest-corner|' . $dimension . '(?:\s+' . $dimension . ')?)'; + + return (bool) preg_match( '/^(?:(?:(?:circle|ellipse)(?:\s+' . $size . ')?|' . $size . ')(?:\s+at\s+' . $position . '(?:\s+' . $position . ')?)?|at\s+' . $position . '(?:\s+' . $position . ')?)$/i', $value ); + } + + return (bool) preg_match( '/^(?:from\s+' . $angle . ')(?:\s+at\s+' . $position . '(?:\s+' . $position . ')?)?$|^at\s+' . $position . '(?:\s+' . $position . ')?$/i', $value ); +} + +/** + * Check whether a gradient stop begins with a supported color value. + * + * @param string $value Gradient stop. + * + * @return bool + */ +function neve_gradient_stop_starts_with_color( string $value ): bool { + $value = trim( $value ); + if ( preg_match( '/^#(?:[0-9a-f]{3,4}|[0-9a-f]{6}|[0-9a-f]{8})(?=\s|$)/i', $value ) ) { + return true; + } + + if ( preg_match( '/^((?:var|rgb|rgba|hsl|hsla)(\((?:[a-z0-9#%.,\s\/_-]|(?2))*\)))(?=\s|$)/i', $value, $matches ) ) { + if ( 0 === stripos( $matches[1], 'var(' ) ) { + return neve_is_css_var( $matches[1] ); + } + + $component = '[+-]?(?:\d+(?:\.\d*)?|\.\d+)%?'; + + return neve_is_css_function_color( $matches[1] ) + || (bool) preg_match( '/^(?:rgb|rgba|hsl|hsla)\(\s*' . $component . '(?:[\s,\/]+' . $component . '){2,3}\s*\)$/i', $matches[1] ); + } + + if ( ! preg_match( '/^([a-z]+(?:-[a-z]+)*)(?=\s|$)/i', $value, $matches ) ) { + return false; + } + + return '' !== neve_sanitize_row_text_color( $matches[1] ); } /** diff --git a/tests/test-neve-ability-style-mods.php b/tests/test-neve-ability-style-mods.php index 65d08701af..caca9fb1f5 100644 --- a/tests/test-neve-ability-style-mods.php +++ b/tests/test-neve-ability-style-mods.php @@ -640,18 +640,20 @@ public function test_stored_header_component_spacing_is_sanitized(): void { public function test_site_set_theme_mod_rejects_invalid_color(): void { set_theme_mod( self::SIDEBAR_TEXT, '#ff0000' ); - $result = ( new Site_Set_Theme_Mod() )->execute( - array( - 'action' => 'set', - 'key' => self::SIDEBAR_TEXT, - 'value' => 'notacolor', - ) - ); + foreach ( array( 'notacolor', 'linear-gradient()', 'linear-gradient(foo)' ) as $invalid_color ) { + $result = ( new Site_Set_Theme_Mod() )->execute( + array( + 'action' => 'set', + 'key' => self::SIDEBAR_TEXT, + 'value' => $invalid_color, + ) + ); - $this->assertIsArray( $result ); - $this->assertFalse( $result['success'] ); - $this->assertSame( 'invalid_value', $result['error']['code'] ); - $this->assertSame( '#ff0000', get_theme_mod( self::SIDEBAR_TEXT ) ); + $this->assertIsArray( $result ); + $this->assertFalse( $result['success'] ); + $this->assertSame( 'invalid_value', $result['error']['code'] ); + $this->assertSame( '#ff0000', get_theme_mod( self::SIDEBAR_TEXT ) ); + } } /** diff --git a/tests/test-neve-sanitization.php b/tests/test-neve-sanitization.php index 21bd499f15..ac36e0c0dd 100644 --- a/tests/test-neve-sanitization.php +++ b/tests/test-neve-sanitization.php @@ -328,6 +328,7 @@ public function test_sanitize_colors_gradients(): void { 'linear-gradient(135deg,rgba(6,147,227,1) 0%,rgb(155,81,224) 100%)', 'radial-gradient(rgb(6,147,227) 0%,rgb(155,81,224) 100%)', 'radial-gradient(circle at center, #fff 0%, #000 100%)', + 'radial-gradient(circle closest-side, red, blue)', 'conic-gradient(from 90deg, red, blue)', 'repeating-linear-gradient(45deg, #FFF 0 10px, transparent 10px 20px)', 'linear-gradient(var(--nv-primary-accent), var(--nv_c-1, #fff))', @@ -341,6 +342,10 @@ public function test_sanitize_colors_gradients(): void { $this->assertSame( 'linear-gradient(red, blue)', neve_sanitize_colors( ' linear-gradient(red, blue) ' ) ); $invalid = [ + 'linear-gradient()', + 'linear-gradient(foo)', + 'linear-gradient(red)', + 'linear-gradient(foo, red, blue)', 'linear-gradient(red,blue)', 'linear-gradient(red,blue);}body{display:none', 'linear-gradient(red,blue),url(//example.com/t.png)', From 0071b0e62f95fe973eb0feb71b6473379a7ce593 Mon Sep 17 00:00:00 2001 From: selul Date: Wed, 11 Nov 2026 03:02:13 +0200 Subject: [PATCH 11/16] fix: tighten stored gradient structure --- globals/sanitize-functions.php | 47 ++++++++++++++++++++------ tests/test-neve-ability-style-mods.php | 10 +++++- tests/test-neve-sanitization.php | 3 ++ 3 files changed, 48 insertions(+), 12 deletions(-) diff --git a/globals/sanitize-functions.php b/globals/sanitize-functions.php index 98bce721a2..52758eca45 100644 --- a/globals/sanitize-functions.php +++ b/globals/sanitize-functions.php @@ -114,12 +114,15 @@ function neve_is_css_gradient( string $value ): bool { function neve_is_css_gradient_prelude( string $type, string $value ): bool { $value = trim( $value ); $number = '[+-]?(?:\d+(?:\.\d*)?|\.\d+)'; - $angle = $number . '(?:deg|grad|rad|turn)'; - $dimension = $number . '(?:%|[a-z]+)?'; + $zero = '[+-]?(?:0+(?:\.0*)?|\.0+)'; + $angle = '(?:' . $zero . '|' . $number . '(?:deg|grad|rad|turn))'; + $dimension = '(?:' . $zero . '|' . $number . '(?:%|px|em|rem|vw|vh|vmin|vmax|ch|ex|cm|mm|in|pt|pc|q))'; $position = '(?:center|left|right|top|bottom|' . $dimension . ')'; if ( 'linear' === strtolower( $type ) ) { - return (bool) preg_match( '/^(?:' . $angle . '|to\s+(?:left|right|top|bottom)(?:\s+(?:left|right|top|bottom))?)$/i', $value ); + $direction = '(?:(?:left|right)(?:\s+(?:top|bottom))?|(?:top|bottom)(?:\s+(?:left|right))?)'; + + return (bool) preg_match( '/^(?:' . $angle . '|to\s+' . $direction . ')$/i', $value ); } if ( 'radial' === strtolower( $type ) ) { @@ -140,26 +143,48 @@ function neve_is_css_gradient_prelude( string $type, string $value ): bool { */ function neve_gradient_stop_starts_with_color( string $value ): bool { $value = trim( $value ); - if ( preg_match( '/^#(?:[0-9a-f]{3,4}|[0-9a-f]{6}|[0-9a-f]{8})(?=\s|$)/i', $value ) ) { - return true; + if ( preg_match( '/^#(?:[0-9a-f]{3,4}|[0-9a-f]{6}|[0-9a-f]{8})(?=\s|$)/i', $value, $matches ) ) { + return neve_gradient_stop_has_valid_positions( substr( $value, strlen( $matches[0] ) ) ); } if ( preg_match( '/^((?:var|rgb|rgba|hsl|hsla)(\((?:[a-z0-9#%.,\s\/_-]|(?2))*\)))(?=\s|$)/i', $value, $matches ) ) { if ( 0 === stripos( $matches[1], 'var(' ) ) { - return neve_is_css_var( $matches[1] ); + $is_color = neve_is_css_var( $matches[1] ); + } else { + $component = '[+-]?(?:\d+(?:\.\d*)?|\.\d+)%?'; + $is_color = neve_is_css_function_color( $matches[1] ) + || (bool) preg_match( '/^(?:rgb|rgba|hsl|hsla)\(\s*' . $component . '(?:[\s,\/]+' . $component . '){2,3}\s*\)$/i', $matches[1] ); } - $component = '[+-]?(?:\d+(?:\.\d*)?|\.\d+)%?'; - - return neve_is_css_function_color( $matches[1] ) - || (bool) preg_match( '/^(?:rgb|rgba|hsl|hsla)\(\s*' . $component . '(?:[\s,\/]+' . $component . '){2,3}\s*\)$/i', $matches[1] ); + return $is_color && neve_gradient_stop_has_valid_positions( substr( $value, strlen( $matches[1] ) ) ); } if ( ! preg_match( '/^([a-z]+(?:-[a-z]+)*)(?=\s|$)/i', $value, $matches ) ) { return false; } - return '' !== neve_sanitize_row_text_color( $matches[1] ); + return '' !== neve_sanitize_row_text_color( $matches[1] ) + && neve_gradient_stop_has_valid_positions( substr( $value, strlen( $matches[1] ) ) ); +} + +/** + * Check the optional one or two positions after a gradient stop color. + * + * @param string $value Position suffix. + * + * @return bool + */ +function neve_gradient_stop_has_valid_positions( string $value ): bool { + $value = trim( $value ); + if ( '' === $value ) { + return true; + } + + $number = '[+-]?(?:\d+(?:\.\d*)?|\.\d+)'; + $zero = '[+-]?(?:0+(?:\.0*)?|\.0+)'; + $dimension = '(?:' . $zero . '|' . $number . '(?:%|px|em|rem|vw|vh|vmin|vmax|ch|ex|cm|mm|in|pt|pc|q|deg|grad|rad|turn))'; + + return (bool) preg_match( '/^' . $dimension . '(?:\s+' . $dimension . ')?$/i', $value ); } /** diff --git a/tests/test-neve-ability-style-mods.php b/tests/test-neve-ability-style-mods.php index caca9fb1f5..5c5057f57c 100644 --- a/tests/test-neve-ability-style-mods.php +++ b/tests/test-neve-ability-style-mods.php @@ -640,7 +640,15 @@ public function test_stored_header_component_spacing_is_sanitized(): void { public function test_site_set_theme_mod_rejects_invalid_color(): void { set_theme_mod( self::SIDEBAR_TEXT, '#ff0000' ); - foreach ( array( 'notacolor', 'linear-gradient()', 'linear-gradient(foo)' ) as $invalid_color ) { + $invalid_colors = array( + 'notacolor', + 'linear-gradient()', + 'linear-gradient(foo)', + 'linear-gradient(red garbage, blue)', + 'linear-gradient(to left right, red, blue)', + 'radial-gradient(10, red, blue)', + ); + foreach ( $invalid_colors as $invalid_color ) { $result = ( new Site_Set_Theme_Mod() )->execute( array( 'action' => 'set', diff --git a/tests/test-neve-sanitization.php b/tests/test-neve-sanitization.php index ac36e0c0dd..8108130a92 100644 --- a/tests/test-neve-sanitization.php +++ b/tests/test-neve-sanitization.php @@ -346,6 +346,9 @@ public function test_sanitize_colors_gradients(): void { 'linear-gradient(foo)', 'linear-gradient(red)', 'linear-gradient(foo, red, blue)', + 'linear-gradient(red garbage, blue)', + 'linear-gradient(to left right, red, blue)', + 'radial-gradient(10, red, blue)', 'linear-gradient(red,blue)', 'linear-gradient(red,blue);}body{display:none', 'linear-gradient(red,blue),url(//example.com/t.png)', From f469957dce6f75f5ea8d0fa06d33b65aba7f619e Mon Sep 17 00:00:00 2001 From: selul Date: Wed, 11 Nov 2026 03:10:06 +0200 Subject: [PATCH 12/16] fix: validate gradient grammar by type --- globals/sanitize-functions.php | 71 ++++++++++++++++++-------- tests/test-neve-ability-style-mods.php | 4 ++ tests/test-neve-sanitization.php | 6 +++ 3 files changed, 60 insertions(+), 21 deletions(-) diff --git a/globals/sanitize-functions.php b/globals/sanitize-functions.php index 52758eca45..a72f35e6ef 100644 --- a/globals/sanitize-functions.php +++ b/globals/sanitize-functions.php @@ -83,7 +83,7 @@ function neve_is_css_gradient( string $value ): bool { } $arguments[] = trim( $current ); - if ( ! neve_gradient_stop_starts_with_color( $arguments[0] ) ) { + if ( ! neve_is_css_gradient_stop( $arguments[0], $matches[1] ) ) { if ( ! neve_is_css_gradient_prelude( $matches[1], $arguments[0] ) ) { return false; } @@ -95,7 +95,7 @@ function neve_is_css_gradient( string $value ): bool { } foreach ( $arguments as $argument ) { - if ( ! neve_gradient_stop_starts_with_color( $argument ) ) { + if ( ! neve_is_css_gradient_stop( $argument, $matches[1] ) ) { return false; } } @@ -112,12 +112,14 @@ function neve_is_css_gradient( string $value ): bool { * @return bool */ function neve_is_css_gradient_prelude( string $type, string $value ): bool { - $value = trim( $value ); - $number = '[+-]?(?:\d+(?:\.\d*)?|\.\d+)'; - $zero = '[+-]?(?:0+(?:\.0*)?|\.0+)'; - $angle = '(?:' . $zero . '|' . $number . '(?:deg|grad|rad|turn))'; - $dimension = '(?:' . $zero . '|' . $number . '(?:%|px|em|rem|vw|vh|vmin|vmax|ch|ex|cm|mm|in|pt|pc|q))'; - $position = '(?:center|left|right|top|bottom|' . $dimension . ')'; + $value = trim( $value ); + $number = '[+-]?(?:\d+(?:\.\d*)?|\.\d+)'; + $zero = '[+-]?(?:0+(?:\.0*)?|\.0+)'; + $angle = '(?:' . $zero . '|' . $number . '(?:deg|grad|rad|turn))'; + $dimension = '(?:' . $zero . '|' . $number . '(?:%|px|em|rem|vw|vh|vmin|vmax|ch|ex|cm|mm|in|pt|pc|q))'; + $horizontal = '(?:left|right)(?:\s+' . $dimension . ')?'; + $vertical = '(?:top|bottom)(?:\s+' . $dimension . ')?'; + $position = '(?:center|' . $dimension . '(?:\s+' . $dimension . ')?|' . $horizontal . '(?:\s+(?:' . $vertical . '|center))?|' . $vertical . '(?:\s+(?:' . $horizontal . '|center))?)'; if ( 'linear' === strtolower( $type ) ) { $direction = '(?:(?:left|right)(?:\s+(?:top|bottom))?|(?:top|bottom)(?:\s+(?:left|right))?)'; @@ -126,37 +128,41 @@ function neve_is_css_gradient_prelude( string $type, string $value ): bool { } if ( 'radial' === strtolower( $type ) ) { - $size = '(?:closest-side|closest-corner|farthest-side|farthest-corner|' . $dimension . '(?:\s+' . $dimension . ')?)'; + $unsigned_number = '(?:\d+(?:\.\d*)?|\.\d+)'; + $extent = '(?:closest-side|closest-corner|farthest-side|farthest-corner)'; + $circle_radius = '(?:0+(?:\.0*)?|\.0+|' . $unsigned_number . '(?:px|em|rem|vw|vh|vmin|vmax|ch|ex|cm|mm|in|pt|pc|q))'; + $ellipse_radius = '(?:0+(?:\.0*)?|\.0+|' . $unsigned_number . '(?:%|px|em|rem|vw|vh|vmin|vmax|ch|ex|cm|mm|in|pt|pc|q))'; + $size = '(?:' . $extent . '|' . $ellipse_radius . '(?:\s+' . $ellipse_radius . ')?)'; + $shape_size = '(?:circle(?:\s+(?:' . $extent . '|' . $circle_radius . '))?|ellipse(?:\s+(?:' . $extent . '|' . $ellipse_radius . '\s+' . $ellipse_radius . '))?)'; - return (bool) preg_match( '/^(?:(?:(?:circle|ellipse)(?:\s+' . $size . ')?|' . $size . ')(?:\s+at\s+' . $position . '(?:\s+' . $position . ')?)?|at\s+' . $position . '(?:\s+' . $position . ')?)$/i', $value ); + return (bool) preg_match( '/^(?:(?:' . $shape_size . '|' . $size . ')(?:\s+at\s+' . $position . ')?|at\s+' . $position . ')$/i', $value ); } - return (bool) preg_match( '/^(?:from\s+' . $angle . ')(?:\s+at\s+' . $position . '(?:\s+' . $position . ')?)?$|^at\s+' . $position . '(?:\s+' . $position . ')?$/i', $value ); + return (bool) preg_match( '/^(?:from\s+' . $angle . ')(?:\s+at\s+' . $position . ')?$|^at\s+' . $position . '$/i', $value ); } /** - * Check whether a gradient stop begins with a supported color value. + * Check whether a gradient stop contains a supported color and positions. * * @param string $value Gradient stop. + * @param string $type Gradient type. * * @return bool */ -function neve_gradient_stop_starts_with_color( string $value ): bool { +function neve_is_css_gradient_stop( string $value, string $type ): bool { $value = trim( $value ); if ( preg_match( '/^#(?:[0-9a-f]{3,4}|[0-9a-f]{6}|[0-9a-f]{8})(?=\s|$)/i', $value, $matches ) ) { - return neve_gradient_stop_has_valid_positions( substr( $value, strlen( $matches[0] ) ) ); + return neve_gradient_stop_has_valid_positions( substr( $value, strlen( $matches[0] ) ), $type ); } if ( preg_match( '/^((?:var|rgb|rgba|hsl|hsla)(\((?:[a-z0-9#%.,\s\/_-]|(?2))*\)))(?=\s|$)/i', $value, $matches ) ) { if ( 0 === stripos( $matches[1], 'var(' ) ) { $is_color = neve_is_css_var( $matches[1] ); } else { - $component = '[+-]?(?:\d+(?:\.\d*)?|\.\d+)%?'; - $is_color = neve_is_css_function_color( $matches[1] ) - || (bool) preg_match( '/^(?:rgb|rgba|hsl|hsla)\(\s*' . $component . '(?:[\s,\/]+' . $component . '){2,3}\s*\)$/i', $matches[1] ); + $is_color = neve_is_css_gradient_function_color( $matches[1] ); } - return $is_color && neve_gradient_stop_has_valid_positions( substr( $value, strlen( $matches[1] ) ) ); + return $is_color && neve_gradient_stop_has_valid_positions( substr( $value, strlen( $matches[1] ) ), $type ); } if ( ! preg_match( '/^([a-z]+(?:-[a-z]+)*)(?=\s|$)/i', $value, $matches ) ) { @@ -164,17 +170,38 @@ function neve_gradient_stop_starts_with_color( string $value ): bool { } return '' !== neve_sanitize_row_text_color( $matches[1] ) - && neve_gradient_stop_has_valid_positions( substr( $value, strlen( $matches[1] ) ) ); + && neve_gradient_stop_has_valid_positions( substr( $value, strlen( $matches[1] ) ), $type ); +} + +/** + * Check a function color used by a gradient stop. + * + * @param string $value Function color. + * + * @return bool + */ +function neve_is_css_gradient_function_color( string $value ): bool { + $number = '[+-]?(?:\d+(?:\.\d*)?|\.\d+)'; + $component = $number . '%?'; + $alpha = $number . '%?'; + $hue = $number . '(?:deg|grad|rad|turn)?'; + $rgb = '(?:rgb|rgba)\(\s*' . $component . '(?:\s*,\s*' . $component . '){2}(?:\s*,\s*' . $alpha . ')?\s*\)'; + $rgb_modern = '(?:rgb|rgba)\(\s*' . $component . '\s+' . $component . '\s+' . $component . '(?:\s*\/\s*' . $alpha . ')?\s*\)'; + $hsl = '(?:hsl|hsla)\(\s*' . $hue . '\s*,\s*' . $number . '%\s*,\s*' . $number . '%(?:\s*,\s*' . $alpha . ')?\s*\)'; + $hsl_modern = '(?:hsl|hsla)\(\s*' . $hue . '\s+' . $number . '%\s+' . $number . '%(?:\s*\/\s*' . $alpha . ')?\s*\)'; + + return (bool) preg_match( '/^(?:' . $rgb . '|' . $rgb_modern . '|' . $hsl . '|' . $hsl_modern . ')$/i', $value ); } /** * Check the optional one or two positions after a gradient stop color. * * @param string $value Position suffix. + * @param string $type Gradient type. * * @return bool */ -function neve_gradient_stop_has_valid_positions( string $value ): bool { +function neve_gradient_stop_has_valid_positions( string $value, string $type ): bool { $value = trim( $value ); if ( '' === $value ) { return true; @@ -182,7 +209,9 @@ function neve_gradient_stop_has_valid_positions( string $value ): bool { $number = '[+-]?(?:\d+(?:\.\d*)?|\.\d+)'; $zero = '[+-]?(?:0+(?:\.0*)?|\.0+)'; - $dimension = '(?:' . $zero . '|' . $number . '(?:%|px|em|rem|vw|vh|vmin|vmax|ch|ex|cm|mm|in|pt|pc|q|deg|grad|rad|turn))'; + $dimension = 'conic' === strtolower( $type ) + ? '(?:' . $zero . '|' . $number . '(?:%|deg|grad|rad|turn))' + : '(?:' . $zero . '|' . $number . '(?:%|px|em|rem|vw|vh|vmin|vmax|ch|ex|cm|mm|in|pt|pc|q))'; return (bool) preg_match( '/^' . $dimension . '(?:\s+' . $dimension . ')?$/i', $value ); } diff --git a/tests/test-neve-ability-style-mods.php b/tests/test-neve-ability-style-mods.php index 5c5057f57c..62d9e49d91 100644 --- a/tests/test-neve-ability-style-mods.php +++ b/tests/test-neve-ability-style-mods.php @@ -647,6 +647,10 @@ public function test_site_set_theme_mod_rejects_invalid_color(): void { 'linear-gradient(red garbage, blue)', 'linear-gradient(to left right, red, blue)', 'radial-gradient(10, red, blue)', + 'linear-gradient(red 10deg, blue)', + 'conic-gradient(red 10px, blue)', + 'linear-gradient(rgb(1 / 2 / 3), blue)', + 'radial-gradient(circle 10px 20px, red, blue)', ); foreach ( $invalid_colors as $invalid_color ) { $result = ( new Site_Set_Theme_Mod() )->execute( diff --git a/tests/test-neve-sanitization.php b/tests/test-neve-sanitization.php index 8108130a92..e4d58c6f81 100644 --- a/tests/test-neve-sanitization.php +++ b/tests/test-neve-sanitization.php @@ -349,6 +349,12 @@ public function test_sanitize_colors_gradients(): void { 'linear-gradient(red garbage, blue)', 'linear-gradient(to left right, red, blue)', 'radial-gradient(10, red, blue)', + 'linear-gradient(red 10deg, blue)', + 'conic-gradient(red 10px, blue)', + 'linear-gradient(rgb(1 / 2 / 3), blue)', + 'radial-gradient(circle 10px 20px, red, blue)', + 'radial-gradient(circle -10px, red, blue)', + 'radial-gradient(at left right, red, blue)', 'linear-gradient(red,blue)', 'linear-gradient(red,blue);}body{display:none', 'linear-gradient(red,blue),url(//example.com/t.png)', From 9e5d4379e31404fb7ffd55b0a6d406c7b4627f78 Mon Sep 17 00:00:00 2001 From: selul Date: Wed, 11 Nov 2026 03:19:18 +0200 Subject: [PATCH 13/16] fix: align gradient value grammar --- globals/sanitize-functions.php | 24 ++++++++++++------------ tests/test-neve-ability-style-mods.php | 3 +++ tests/test-neve-sanitization.php | 6 ++++++ 3 files changed, 21 insertions(+), 12 deletions(-) diff --git a/globals/sanitize-functions.php b/globals/sanitize-functions.php index a72f35e6ef..9201c870b3 100644 --- a/globals/sanitize-functions.php +++ b/globals/sanitize-functions.php @@ -113,13 +113,13 @@ function neve_is_css_gradient( string $value ): bool { */ function neve_is_css_gradient_prelude( string $type, string $value ): bool { $value = trim( $value ); - $number = '[+-]?(?:\d+(?:\.\d*)?|\.\d+)'; - $zero = '[+-]?(?:0+(?:\.0*)?|\.0+)'; + $number = '[+-]?(?:\d+(?:\.\d+)?|\.\d+)'; + $zero = '[+-]?(?:0+(?:\.0+)?|\.0+)'; $angle = '(?:' . $zero . '|' . $number . '(?:deg|grad|rad|turn))'; $dimension = '(?:' . $zero . '|' . $number . '(?:%|px|em|rem|vw|vh|vmin|vmax|ch|ex|cm|mm|in|pt|pc|q))'; - $horizontal = '(?:left|right)(?:\s+' . $dimension . ')?'; - $vertical = '(?:top|bottom)(?:\s+' . $dimension . ')?'; - $position = '(?:center|' . $dimension . '(?:\s+' . $dimension . ')?|' . $horizontal . '(?:\s+(?:' . $vertical . '|center))?|' . $vertical . '(?:\s+(?:' . $horizontal . '|center))?)'; + $horizontal = '(?:left|center|right)'; + $vertical = '(?:top|center|bottom)'; + $position = '(?:(?:left|center|right|top|bottom|' . $dimension . ')|(?:' . $horizontal . '\s+' . $vertical . '|' . $vertical . '\s+' . $horizontal . ')|(?:' . $horizontal . '|' . $dimension . ')\s+(?:' . $vertical . '|' . $dimension . ')|(?:left|right)\s+' . $dimension . '\s+(?:top|bottom)\s+' . $dimension . '|(?:top|bottom)\s+' . $dimension . '\s+(?:left|right)\s+' . $dimension . ')'; if ( 'linear' === strtolower( $type ) ) { $direction = '(?:(?:left|right)(?:\s+(?:top|bottom))?|(?:top|bottom)(?:\s+(?:left|right))?)'; @@ -128,11 +128,11 @@ function neve_is_css_gradient_prelude( string $type, string $value ): bool { } if ( 'radial' === strtolower( $type ) ) { - $unsigned_number = '(?:\d+(?:\.\d*)?|\.\d+)'; + $unsigned_number = '(?:\d+(?:\.\d+)?|\.\d+)'; $extent = '(?:closest-side|closest-corner|farthest-side|farthest-corner)'; - $circle_radius = '(?:0+(?:\.0*)?|\.0+|' . $unsigned_number . '(?:px|em|rem|vw|vh|vmin|vmax|ch|ex|cm|mm|in|pt|pc|q))'; - $ellipse_radius = '(?:0+(?:\.0*)?|\.0+|' . $unsigned_number . '(?:%|px|em|rem|vw|vh|vmin|vmax|ch|ex|cm|mm|in|pt|pc|q))'; - $size = '(?:' . $extent . '|' . $ellipse_radius . '(?:\s+' . $ellipse_radius . ')?)'; + $circle_radius = '(?:0+(?:\.0+)?|\.0+|' . $unsigned_number . '(?:px|em|rem|vw|vh|vmin|vmax|ch|ex|cm|mm|in|pt|pc|q))'; + $ellipse_radius = '(?:0+(?:\.0+)?|\.0+|' . $unsigned_number . '(?:%|px|em|rem|vw|vh|vmin|vmax|ch|ex|cm|mm|in|pt|pc|q))'; + $size = '(?:' . $extent . '|' . $circle_radius . '|' . $ellipse_radius . '\s+' . $ellipse_radius . ')'; $shape_size = '(?:circle(?:\s+(?:' . $extent . '|' . $circle_radius . '))?|ellipse(?:\s+(?:' . $extent . '|' . $ellipse_radius . '\s+' . $ellipse_radius . '))?)'; return (bool) preg_match( '/^(?:(?:' . $shape_size . '|' . $size . ')(?:\s+at\s+' . $position . ')?|at\s+' . $position . ')$/i', $value ); @@ -181,7 +181,7 @@ function neve_is_css_gradient_stop( string $value, string $type ): bool { * @return bool */ function neve_is_css_gradient_function_color( string $value ): bool { - $number = '[+-]?(?:\d+(?:\.\d*)?|\.\d+)'; + $number = '[+-]?(?:\d+(?:\.\d+)?|\.\d+)'; $component = $number . '%?'; $alpha = $number . '%?'; $hue = $number . '(?:deg|grad|rad|turn)?'; @@ -207,8 +207,8 @@ function neve_gradient_stop_has_valid_positions( string $value, string $type ): return true; } - $number = '[+-]?(?:\d+(?:\.\d*)?|\.\d+)'; - $zero = '[+-]?(?:0+(?:\.0*)?|\.0+)'; + $number = '[+-]?(?:\d+(?:\.\d+)?|\.\d+)'; + $zero = '[+-]?(?:0+(?:\.0+)?|\.0+)'; $dimension = 'conic' === strtolower( $type ) ? '(?:' . $zero . '|' . $number . '(?:%|deg|grad|rad|turn))' : '(?:' . $zero . '|' . $number . '(?:%|px|em|rem|vw|vh|vmin|vmax|ch|ex|cm|mm|in|pt|pc|q))'; diff --git a/tests/test-neve-ability-style-mods.php b/tests/test-neve-ability-style-mods.php index 62d9e49d91..6f6875d238 100644 --- a/tests/test-neve-ability-style-mods.php +++ b/tests/test-neve-ability-style-mods.php @@ -651,6 +651,9 @@ public function test_site_set_theme_mod_rejects_invalid_color(): void { 'conic-gradient(red 10px, blue)', 'linear-gradient(rgb(1 / 2 / 3), blue)', 'radial-gradient(circle 10px 20px, red, blue)', + 'radial-gradient(at left 10px center, red, blue)', + 'linear-gradient(1.deg, red, blue)', + 'linear-gradient(rgb(1.,2,3), blue)', ); foreach ( $invalid_colors as $invalid_color ) { $result = ( new Site_Set_Theme_Mod() )->execute( diff --git a/tests/test-neve-sanitization.php b/tests/test-neve-sanitization.php index e4d58c6f81..28b539719c 100644 --- a/tests/test-neve-sanitization.php +++ b/tests/test-neve-sanitization.php @@ -328,6 +328,8 @@ public function test_sanitize_colors_gradients(): void { 'linear-gradient(135deg,rgba(6,147,227,1) 0%,rgb(155,81,224) 100%)', 'radial-gradient(rgb(6,147,227) 0%,rgb(155,81,224) 100%)', 'radial-gradient(circle at center, #fff 0%, #000 100%)', + 'radial-gradient(at center top, red, blue)', + 'radial-gradient(at left 10px top 20px, red, blue)', 'radial-gradient(circle closest-side, red, blue)', 'conic-gradient(from 90deg, red, blue)', 'repeating-linear-gradient(45deg, #FFF 0 10px, transparent 10px 20px)', @@ -355,6 +357,10 @@ public function test_sanitize_colors_gradients(): void { 'radial-gradient(circle 10px 20px, red, blue)', 'radial-gradient(circle -10px, red, blue)', 'radial-gradient(at left right, red, blue)', + 'radial-gradient(at left 10px center, red, blue)', + 'radial-gradient(10%, red, blue)', + 'linear-gradient(1.deg, red, blue)', + 'linear-gradient(rgb(1.,2,3), blue)', 'linear-gradient(red,blue)', 'linear-gradient(red,blue);}body{display:none', 'linear-gradient(red,blue),url(//example.com/t.png)', From 459b4c12c052a007fb3a80e89abd68de49cec208 Mon Sep 17 00:00:00 2001 From: selul Date: Wed, 11 Nov 2026 03:27:23 +0200 Subject: [PATCH 14/16] fix: preserve supported gradient forms --- globals/sanitize-functions.php | 7 +++++-- tests/test-neve-ability-style-mods.php | 1 + tests/test-neve-sanitization.php | 2 ++ 3 files changed, 8 insertions(+), 2 deletions(-) diff --git a/globals/sanitize-functions.php b/globals/sanitize-functions.php index 9201c870b3..5cb2d102b1 100644 --- a/globals/sanitize-functions.php +++ b/globals/sanitize-functions.php @@ -133,7 +133,9 @@ function neve_is_css_gradient_prelude( string $type, string $value ): bool { $circle_radius = '(?:0+(?:\.0+)?|\.0+|' . $unsigned_number . '(?:px|em|rem|vw|vh|vmin|vmax|ch|ex|cm|mm|in|pt|pc|q))'; $ellipse_radius = '(?:0+(?:\.0+)?|\.0+|' . $unsigned_number . '(?:%|px|em|rem|vw|vh|vmin|vmax|ch|ex|cm|mm|in|pt|pc|q))'; $size = '(?:' . $extent . '|' . $circle_radius . '|' . $ellipse_radius . '\s+' . $ellipse_radius . ')'; - $shape_size = '(?:circle(?:\s+(?:' . $extent . '|' . $circle_radius . '))?|ellipse(?:\s+(?:' . $extent . '|' . $ellipse_radius . '\s+' . $ellipse_radius . '))?)'; + $circle = '(?:circle(?:\s+(?:' . $extent . '|' . $circle_radius . '))?|(?:' . $extent . '|' . $circle_radius . ')\s+circle)'; + $ellipse = '(?:ellipse(?:\s+(?:' . $extent . '|' . $ellipse_radius . '\s+' . $ellipse_radius . '))?|(?:' . $extent . '|' . $ellipse_radius . '\s+' . $ellipse_radius . ')\s+ellipse)'; + $shape_size = '(?:' . $circle . '|' . $ellipse . ')'; return (bool) preg_match( '/^(?:(?:' . $shape_size . '|' . $size . ')(?:\s+at\s+' . $position . ')?|at\s+' . $position . ')$/i', $value ); } @@ -185,7 +187,8 @@ function neve_is_css_gradient_function_color( string $value ): bool { $component = $number . '%?'; $alpha = $number . '%?'; $hue = $number . '(?:deg|grad|rad|turn)?'; - $rgb = '(?:rgb|rgba)\(\s*' . $component . '(?:\s*,\s*' . $component . '){2}(?:\s*,\s*' . $alpha . ')?\s*\)'; + $rgb_legacy = '(?:' . $number . '(?:\s*,\s*' . $number . '){2}|' . $number . '%(?:\s*,\s*' . $number . '%){2})'; + $rgb = '(?:rgb|rgba)\(\s*' . $rgb_legacy . '(?:\s*,\s*' . $alpha . ')?\s*\)'; $rgb_modern = '(?:rgb|rgba)\(\s*' . $component . '\s+' . $component . '\s+' . $component . '(?:\s*\/\s*' . $alpha . ')?\s*\)'; $hsl = '(?:hsl|hsla)\(\s*' . $hue . '\s*,\s*' . $number . '%\s*,\s*' . $number . '%(?:\s*,\s*' . $alpha . ')?\s*\)'; $hsl_modern = '(?:hsl|hsla)\(\s*' . $hue . '\s+' . $number . '%\s+' . $number . '%(?:\s*\/\s*' . $alpha . ')?\s*\)'; diff --git a/tests/test-neve-ability-style-mods.php b/tests/test-neve-ability-style-mods.php index 6f6875d238..5e3f483b83 100644 --- a/tests/test-neve-ability-style-mods.php +++ b/tests/test-neve-ability-style-mods.php @@ -654,6 +654,7 @@ public function test_site_set_theme_mod_rejects_invalid_color(): void { 'radial-gradient(at left 10px center, red, blue)', 'linear-gradient(1.deg, red, blue)', 'linear-gradient(rgb(1.,2,3), blue)', + 'linear-gradient(rgb(255, 0%, 0), blue)', ); foreach ( $invalid_colors as $invalid_color ) { $result = ( new Site_Set_Theme_Mod() )->execute( diff --git a/tests/test-neve-sanitization.php b/tests/test-neve-sanitization.php index 28b539719c..2899042847 100644 --- a/tests/test-neve-sanitization.php +++ b/tests/test-neve-sanitization.php @@ -330,6 +330,7 @@ public function test_sanitize_colors_gradients(): void { 'radial-gradient(circle at center, #fff 0%, #000 100%)', 'radial-gradient(at center top, red, blue)', 'radial-gradient(at left 10px top 20px, red, blue)', + 'radial-gradient(5em circle at top left, yellow, blue)', 'radial-gradient(circle closest-side, red, blue)', 'conic-gradient(from 90deg, red, blue)', 'repeating-linear-gradient(45deg, #FFF 0 10px, transparent 10px 20px)', @@ -361,6 +362,7 @@ public function test_sanitize_colors_gradients(): void { 'radial-gradient(10%, red, blue)', 'linear-gradient(1.deg, red, blue)', 'linear-gradient(rgb(1.,2,3), blue)', + 'linear-gradient(rgb(255, 0%, 0), blue)', 'linear-gradient(red,blue)', 'linear-gradient(red,blue);}body{display:none', 'linear-gradient(red,blue),url(//example.com/t.png)', From 430a1205660511e8d4582bddb308ecb37895afc5 Mon Sep 17 00:00:00 2001 From: selul Date: Wed, 11 Nov 2026 04:06:20 +0200 Subject: [PATCH 15/16] fix: validate remaining stored colors --- globals/sanitize-functions.php | 87 +++++++++++++++---- .../Core/Builder/Abstract_Builder.php | 7 +- inc/abilities/appearance_update.php | 2 +- tests/test-neve-ability-style-mods.php | 37 ++++++-- tests/test-neve-row-color-css.php | 15 ++++ tests/test-neve-sanitization.php | 8 +- 6 files changed, 125 insertions(+), 31 deletions(-) diff --git a/globals/sanitize-functions.php b/globals/sanitize-functions.php index 5cb2d102b1..d4d275332c 100644 --- a/globals/sanitize-functions.php +++ b/globals/sanitize-functions.php @@ -27,13 +27,47 @@ function neve_is_css_var( $value ) { return false; } - $hex = '#(?:[0-9a-f]{3,4}|[0-9a-f]{6}|[0-9a-f]{8})'; - $color_fn = '(?:rgb|rgba|hsl|hsla)\(\s*[0-9a-z.,%\/\s-]+\)'; - $keyword = '[a-z]+(?:-[a-z]+)*'; - $fallback = '(?:(?P>nv_var)|' . $hex . '|' . $color_fn . '|' . $keyword . ')'; - $pattern = '/^(?Pvar\(\s*--[a-z0-9_-]+\s*(?:,\s*' . $fallback . '\s*)?\))$/i'; + if ( ! preg_match( '/^var\((.*)\)$/iD', $value, $matches ) ) { + return false; + } + + $body = $matches[1]; + $depth = 0; + $comma_at = null; + $length = strlen( $body ); + for ( $index = 0; $index < $length; $index++ ) { + $character = $body[ $index ]; + if ( '(' === $character ) { + $depth++; + } elseif ( ')' === $character ) { + $depth--; + if ( $depth < 0 ) { + return false; + } + } elseif ( ',' === $character && 0 === $depth && null === $comma_at ) { + $comma_at = $index; + } + } + + if ( 0 !== $depth ) { + return false; + } - return (bool) preg_match( $pattern, $value ); + $name = trim( null === $comma_at ? $body : substr( $body, 0, $comma_at ) ); + if ( ! preg_match( '/^--[a-z0-9_-]+$/i', $name ) ) { + return false; + } + + if ( null === $comma_at ) { + return true; + } + + $fallback = trim( substr( $body, $comma_at + 1 ) ); + if ( '' === $fallback ) { + return false; + } + + return neve_is_css_var( $fallback ) || '' !== neve_sanitize_row_text_color( $fallback ); } /** @@ -103,6 +137,24 @@ function neve_is_css_gradient( string $value ): bool { return true; } +/** + * Get shared numeric patterns used by the CSS gradient validators. + * + * @return array + */ +function neve_css_gradient_patterns(): array { + $number = '[+-]?(?:\d+(?:\.\d+)?|\.\d+)'; + $unsigned_number = '(?:\d+(?:\.\d+)?|\.\d+)'; + $zero = '[+-]?(?:0+(?:\.0+)?|\.0+)'; + + return array( + 'number' => $number, + 'unsigned_number' => $unsigned_number, + 'zero' => $zero, + 'length' => '(?:' . $zero . '|' . $number . '(?:%|px|em|rem|vw|vh|vmin|vmax|ch|ex|cm|mm|in|pt|pc|q))', + ); +} + /** * Check whether a gradient's optional first argument is a supported prelude. * @@ -112,11 +164,12 @@ function neve_is_css_gradient( string $value ): bool { * @return bool */ function neve_is_css_gradient_prelude( string $type, string $value ): bool { + $patterns = neve_css_gradient_patterns(); $value = trim( $value ); - $number = '[+-]?(?:\d+(?:\.\d+)?|\.\d+)'; - $zero = '[+-]?(?:0+(?:\.0+)?|\.0+)'; + $number = $patterns['number']; + $zero = $patterns['zero']; $angle = '(?:' . $zero . '|' . $number . '(?:deg|grad|rad|turn))'; - $dimension = '(?:' . $zero . '|' . $number . '(?:%|px|em|rem|vw|vh|vmin|vmax|ch|ex|cm|mm|in|pt|pc|q))'; + $dimension = $patterns['length']; $horizontal = '(?:left|center|right)'; $vertical = '(?:top|center|bottom)'; $position = '(?:(?:left|center|right|top|bottom|' . $dimension . ')|(?:' . $horizontal . '\s+' . $vertical . '|' . $vertical . '\s+' . $horizontal . ')|(?:' . $horizontal . '|' . $dimension . ')\s+(?:' . $vertical . '|' . $dimension . ')|(?:left|right)\s+' . $dimension . '\s+(?:top|bottom)\s+' . $dimension . '|(?:top|bottom)\s+' . $dimension . '\s+(?:left|right)\s+' . $dimension . ')'; @@ -128,7 +181,7 @@ function neve_is_css_gradient_prelude( string $type, string $value ): bool { } if ( 'radial' === strtolower( $type ) ) { - $unsigned_number = '(?:\d+(?:\.\d+)?|\.\d+)'; + $unsigned_number = $patterns['unsigned_number']; $extent = '(?:closest-side|closest-corner|farthest-side|farthest-corner)'; $circle_radius = '(?:0+(?:\.0+)?|\.0+|' . $unsigned_number . '(?:px|em|rem|vw|vh|vmin|vmax|ch|ex|cm|mm|in|pt|pc|q))'; $ellipse_radius = '(?:0+(?:\.0+)?|\.0+|' . $unsigned_number . '(?:%|px|em|rem|vw|vh|vmin|vmax|ch|ex|cm|mm|in|pt|pc|q))'; @@ -183,7 +236,8 @@ function neve_is_css_gradient_stop( string $value, string $type ): bool { * @return bool */ function neve_is_css_gradient_function_color( string $value ): bool { - $number = '[+-]?(?:\d+(?:\.\d+)?|\.\d+)'; + $patterns = neve_css_gradient_patterns(); + $number = $patterns['number']; $component = $number . '%?'; $alpha = $number . '%?'; $hue = $number . '(?:deg|grad|rad|turn)?'; @@ -210,11 +264,12 @@ function neve_gradient_stop_has_valid_positions( string $value, string $type ): return true; } - $number = '[+-]?(?:\d+(?:\.\d+)?|\.\d+)'; - $zero = '[+-]?(?:0+(?:\.0+)?|\.0+)'; + $patterns = neve_css_gradient_patterns(); + $number = $patterns['number']; + $zero = $patterns['zero']; $dimension = 'conic' === strtolower( $type ) ? '(?:' . $zero . '|' . $number . '(?:%|deg|grad|rad|turn))' - : '(?:' . $zero . '|' . $number . '(?:%|px|em|rem|vw|vh|vmin|vmax|ch|ex|cm|mm|in|pt|pc|q))'; + : $patterns['length']; return (bool) preg_match( '/^' . $dimension . '(?:\s+' . $dimension . ')?$/i', $value ); } @@ -227,9 +282,7 @@ function neve_gradient_stop_has_valid_positions( string $value, string $type ): * @return bool */ function neve_is_css_function_color( string $value ): bool { - $component = '[+-]?(?:\d+(?:\.\d*)?|\.\d+)%?'; - - return (bool) preg_match( '/^(?:rgb|rgba|hsl|hsla)\(\s*' . $component . '(?:\s*,\s*' . $component . '){2,3}\s*\)$/', trim( $value ) ); + return neve_is_css_gradient_function_color( trim( $value ) ); } /** diff --git a/header-footer-grid/Core/Builder/Abstract_Builder.php b/header-footer-grid/Core/Builder/Abstract_Builder.php index ec29dc2aaa..ccf84ec533 100644 --- a/header-footer-grid/Core/Builder/Abstract_Builder.php +++ b/header-footer-grid/Core/Builder/Abstract_Builder.php @@ -983,7 +983,7 @@ protected function get_row_featured_image( $image_url, $use_featured, $meta ) { } /** - * Print a row color variable; a stored color that can close its declaration is sanitized first. + * Print a row color variable after validating the stored value. * * @param string $css_prop CSS variable. * @param mixed $value Stored color. @@ -996,10 +996,7 @@ public function row_color_rule( $css_prop, $value ) { } // The printed style is stripped of HTML as a whole; strip it here first so only CSS is checked. - $value = wp_strip_all_tags( $value ); - if ( neve_closes_css_declaration( $value ) ) { - $value = neve_sanitize_colors( $value ); - } + $value = neve_sanitize_row_text_color( wp_strip_all_tags( $value ) ); return '' !== $value ? sprintf( ' %s: %s;', $css_prop, $value ) : ''; } diff --git a/inc/abilities/appearance_update.php b/inc/abilities/appearance_update.php index 293796031d..4cd36ebdc1 100644 --- a/inc/abilities/appearance_update.php +++ b/inc/abilities/appearance_update.php @@ -588,7 +588,7 @@ private function is_supported_color_value( $value ) { return false; } - if ( preg_match( '/^#[0-9a-fA-F]{3,8}$/', $value ) ) { + if ( preg_match( '/^#(?:[0-9a-fA-F]{3,4}|[0-9a-fA-F]{6}|[0-9a-fA-F]{8})$/', $value ) ) { return true; } diff --git a/tests/test-neve-ability-style-mods.php b/tests/test-neve-ability-style-mods.php index 5e3f483b83..8b58d40e13 100644 --- a/tests/test-neve-ability-style-mods.php +++ b/tests/test-neve-ability-style-mods.php @@ -443,17 +443,38 @@ public function test_abilities_accept_named_row_text_colors(): void { public function test_site_set_theme_mod_rejects_malformed_function_color(): void { set_theme_mod( self::SIDEBAR_TEXT, '#123456' ); - $result = ( new Site_Set_Theme_Mod() )->execute( + foreach ( array( 'rgb(,)', 'rgb(255, 0%, 0)', 'rgb(10%,0,0)', 'hsl(0,0,0)', 'hsl(120, 50, 50)' ) as $invalid_color ) { + $result = ( new Site_Set_Theme_Mod() )->execute( + array( + 'action' => 'set', + 'key' => self::SIDEBAR_TEXT, + 'value' => $invalid_color, + ) + ); + + $this->assertIsArray( $result ); + $this->assertFalse( $result['success'] ); + $this->assertSame( '#123456', get_theme_mod( self::SIDEBAR_TEXT ) ); + } + } + + /** + * Invalid Appearance Update button colors keep the stored appearance. + */ + public function test_appearance_update_rejects_invalid_button_hex_color(): void { + $stored = array( 'background' => '#123456' ); + set_theme_mod( 'neve_button_appearance', $stored ); + + $result = ( new Appearance_Update() )->execute( array( - 'action' => 'set', - 'key' => self::SIDEBAR_TEXT, - 'value' => 'rgb(,)', + 'buttons' => array( + 'primary' => array( 'background' => '#12345' ), + ), ) ); - $this->assertIsArray( $result ); - $this->assertFalse( $result['success'] ); - $this->assertSame( '#123456', get_theme_mod( self::SIDEBAR_TEXT ) ); + $this->assertWPError( $result ); + $this->assertSame( $stored, get_theme_mod( 'neve_button_appearance' ) ); } /** @@ -655,6 +676,8 @@ public function test_site_set_theme_mod_rejects_invalid_color(): void { 'linear-gradient(1.deg, red, blue)', 'linear-gradient(rgb(1.,2,3), blue)', 'linear-gradient(rgb(255, 0%, 0), blue)', + 'var(--c5-missing,rgb(foo))', + 'linear-gradient(var(--c5-missing,rgb(foo)),#fff)', ); foreach ( $invalid_colors as $invalid_color ) { $result = ( new Site_Set_Theme_Mod() )->execute( diff --git a/tests/test-neve-row-color-css.php b/tests/test-neve-row-color-css.php index 82d945157f..86eaf7c62f 100644 --- a/tests/test-neve-row-color-css.php +++ b/tests/test-neve-row-color-css.php @@ -85,6 +85,21 @@ public function test_injected_row_overlay_color_is_printed_sanitized(): void { $this->assertStringContainsString( '--overlaycolor: rgba(1,2,3,1);', $css ); } + /** + * A malformed legacy row color is omitted from generated CSS. + */ + public function test_malformed_stored_row_color_is_not_printed(): void { + set_theme_mod( + 'hfg_header_layout_main_background', + array( + 'type' => 'color', + 'colorValue' => 'linear-gradient()', + ) + ); + + $this->assertSame( 0, preg_match( '/\.header-main\{[^}]*--bgcolor:/s', $this->header_css() ) ); + } + /** * Render the header builder's dynamic CSS. * diff --git a/tests/test-neve-sanitization.php b/tests/test-neve-sanitization.php index 2899042847..f3f0d8364c 100644 --- a/tests/test-neve-sanitization.php +++ b/tests/test-neve-sanitization.php @@ -293,6 +293,7 @@ public function test_is_css_var() { 'var(--x)}body{background:red}', 'var(--x, url(evil.css))', 'var(--x, "quoted")', + 'var(--c5-missing,rgb(foo))', 'linear-gradient(var(--a), var(--b))', '', [ 'var(--x)' ], @@ -363,6 +364,7 @@ public function test_sanitize_colors_gradients(): void { 'linear-gradient(1.deg, red, blue)', 'linear-gradient(rgb(1.,2,3), blue)', 'linear-gradient(rgb(255, 0%, 0), blue)', + 'linear-gradient(var(--c5-missing,rgb(foo)),#fff)', 'linear-gradient(red,blue)', 'linear-gradient(red,blue);}body{display:none', 'linear-gradient(red,blue),url(//example.com/t.png)', @@ -390,6 +392,7 @@ public function test_sanitize_colors_keeps_function_colors(): void { 'rgba(1,2,3)', 'rgba(255,0,0,.5)', 'hsl(120, 50%, 50%)', + 'hsl(120deg, 50%, 50%)', 'hsla(120, 50%, 50%, 0.5)', ]; @@ -408,9 +411,12 @@ public function test_sanitize_colors_keeps_function_colors(): void { 'rgb(1,2,3)),url(//example.com/t.png)', 'rgb(1,2,3)', 'rgb(var(--x),2,3)', + 'rgb(255, 0%, 0)', + 'rgb(10%,0,0)', + 'hsl(0,0,0)', + 'hsl(120, 50, 50)', 'rgb(1,2,3', '#rgb(1,2,3)', - 'hsl(120deg, 50%, 50%)', 'lab(50% 40 59)', ]; From 3bfebd09f00fa886b4b717f8cfb385e542707a89 Mon Sep 17 00:00:00 2001 From: selul Date: Wed, 11 Nov 2026 04:15:17 +0200 Subject: [PATCH 16/16] fix: preserve supported color syntax --- globals/sanitize-functions.php | 631 +++++++++++++++++++++++-- tests/test-neve-ability-style-mods.php | 43 +- tests/test-neve-row-color-css.php | 16 + tests/test-neve-sanitization.php | 68 ++- 4 files changed, 715 insertions(+), 43 deletions(-) diff --git a/globals/sanitize-functions.php b/globals/sanitize-functions.php index d4d275332c..ec3e8db085 100644 --- a/globals/sanitize-functions.php +++ b/globals/sanitize-functions.php @@ -67,7 +67,20 @@ function neve_is_css_var( $value ) { return false; } - return neve_is_css_var( $fallback ) || '' !== neve_sanitize_row_text_color( $fallback ); + if ( neve_closes_css_declaration( $fallback ) ) { + return false; + } + + if ( neve_is_css_var( $fallback ) || (bool) preg_match( '/^#(?:[0-9a-f]{3,4}|[0-9a-f]{6}|[0-9a-f]{8})$/i', $fallback ) ) { + return true; + } + + if ( neve_is_css_function_color( $fallback ) ) { + return true; + } + + return (bool) preg_match( '/^[a-z]+(?:-[a-z]+)*$/i', $fallback ) + && '' !== neve_sanitize_row_text_color( $fallback ); } /** @@ -84,7 +97,7 @@ function neve_is_css_gradient( string $value ): bool { } $body = $matches[2]; - if ( '' === trim( $body ) || ! preg_match( '/^[a-z0-9#%.,\s\/_()\-]+$/i', $body ) ) { + if ( '' === trim( $body ) || ! preg_match( '/^[a-z0-9#%.,\s\/_()+\-]+$/i', $body ) ) { return false; } @@ -128,10 +141,19 @@ function neve_is_css_gradient( string $value ): bool { return false; } - foreach ( $arguments as $argument ) { - if ( ! neve_is_css_gradient_stop( $argument, $matches[1] ) ) { + $previous_was_stop = false; + $last_index = count( $arguments ) - 1; + foreach ( $arguments as $index => $argument ) { + if ( neve_is_css_gradient_stop( $argument, $matches[1] ) ) { + $previous_was_stop = true; + continue; + } + + if ( ! $previous_was_stop || $index === $last_index || ! neve_is_css_gradient_hint( $argument, $matches[1] ) ) { return false; } + + $previous_was_stop = false; } return true; @@ -143,18 +165,144 @@ function neve_is_css_gradient( string $value ): bool { * @return array */ function neve_css_gradient_patterns(): array { - $number = '[+-]?(?:\d+(?:\.\d+)?|\.\d+)'; - $unsigned_number = '(?:\d+(?:\.\d+)?|\.\d+)'; - $zero = '[+-]?(?:0+(?:\.0+)?|\.0+)'; + $number = '[+-]?(?:\d+(?:\.\d+)?|\.\d+)'; + $unsigned_number = '(?:\d+(?:\.\d+)?|\.\d+)'; + $zero = '[+-]?(?:0+(?:\.0+)?|\.0+)'; + $length_units = 'px|em|rem|vw|vh|vi|vb|vmin|vmax|svw|svh|svi|svb|svmin|svmax|lvw|lvh|lvi|lvb|lvmin|lvmax|dvw|dvh|dvi|dvb|dvmin|dvmax|cqw|cqh|cqi|cqb|cqmin|cqmax|ch|ex|cap|ic|lh|rlh|cm|mm|in|pt|pc|q'; + $length_unit = '(?:' . $length_units . ')'; + $absolute_length = '(?:' . $zero . '|' . $number . $length_unit . ')'; + $length = '(?:' . $absolute_length . '|' . $number . '%)'; + $nonnegative_length = '(?:\+?(?:0+(?:\.0+)?|\.0+)|\+?' . $unsigned_number . '(?:%|' . $length_units . '))'; + $calc_length = 'calc\(\s*' . $length . '(?:\s+[+-]\s+' . $length . ')+\s*\)'; return array( - 'number' => $number, - 'unsigned_number' => $unsigned_number, - 'zero' => $zero, - 'length' => '(?:' . $zero . '|' . $number . '(?:%|px|em|rem|vw|vh|vmin|vmax|ch|ex|cm|mm|in|pt|pc|q))', + 'number' => $number, + 'unsigned_number' => $unsigned_number, + 'zero' => $zero, + 'length_unit' => $length_unit, + 'absolute_length' => $absolute_length, + 'simple_length' => $length, + 'nonnegative_length' => $nonnegative_length, + 'length' => '(?:' . $length . '|' . $calc_length . ')', ); } +/** + * Split a CSS value on a delimiter that is outside parentheses. + * + * @param string $value Value to split. + * @param string $delimiter Single-character delimiter. + * + * @return array|false + */ +function neve_css_split_top_level( string $value, string $delimiter = ',' ) { + $parts = array(); + $current = ''; + $depth = 0; + $length = strlen( $value ); + + for ( $index = 0; $index < $length; $index++ ) { + $character = $value[ $index ]; + if ( '(' === $character ) { + $depth++; + } elseif ( ')' === $character ) { + $depth--; + if ( $depth < 0 ) { + return false; + } + } + + if ( $delimiter === $character && 0 === $depth ) { + $parts[] = trim( $current ); + $current = ''; + continue; + } + + $current .= $character; + } + + if ( 0 !== $depth ) { + return false; + } + + $parts[] = trim( $current ); + + return $parts; +} + +/** + * Read a leading CSS function and the suffix after its balanced closing parenthesis. + * + * @param string $value Value beginning with a CSS function. + * + * @return array{name: string, value: string, body: string, suffix: string}|false + */ +function neve_css_leading_function( string $value ) { + if ( ! preg_match( '/^([a-z][a-z0-9-]*)\(/i', $value, $matches ) ) { + return false; + } + + $depth = 0; + $length = strlen( $value ); + $start = strlen( $matches[1] ); + for ( $index = $start; $index < $length; $index++ ) { + $character = $value[ $index ]; + if ( '(' === $character ) { + $depth++; + } elseif ( ')' === $character ) { + $depth--; + if ( 0 === $depth ) { + return array( + 'name' => strtolower( $matches[1] ), + 'value' => substr( $value, 0, $index + 1 ), + 'body' => substr( $value, $start + 1, $index - $start - 1 ), + 'suffix' => substr( $value, $index + 1 ), + ); + } + } + } + + return false; +} + +/** + * Get the supported CSS color interpolation method pattern. + * + * @return string + */ +function neve_css_gradient_interpolation_pattern(): string { + $rectangular = '(?:srgb|srgb-linear|display-p3|a98-rgb|prophoto-rgb|rec2020|lab|oklab|xyz|xyz-d50|xyz-d65)'; + $polar = '(?:hsl|hwb|lch|oklch)(?:\s+(?:shorter|longer|increasing|decreasing)\s+hue)?'; + $custom = '--[a-z0-9_-]+'; + + return 'in\s+(?:' . $rectangular . '|' . $polar . '|' . $custom . ')'; +} + +/** + * Match a gradient prelude with an optional color interpolation method. + * + * @param string $value Prelude value. + * @param string $base_pattern Direction, shape or position pattern. + * @param string $interpolation Color interpolation pattern. + * + * @return bool + */ +function neve_css_gradient_matches_prelude( string $value, string $base_pattern, string $interpolation ): bool { + if ( preg_match( '/^' . $base_pattern . '$/i', $value ) || preg_match( '/^' . $interpolation . '$/i', $value ) ) { + return true; + } + + if ( preg_match( '/^(' . $interpolation . ')\s+(.+)$/i', $value, $matches ) ) { + return (bool) preg_match( '/^' . $base_pattern . '$/i', $matches[2] ); + } + + if ( preg_match( '/^(.+)\s+(' . $interpolation . ')$/i', $value, $matches ) ) { + return (bool) preg_match( '/^' . $base_pattern . '$/i', $matches[1] ); + } + + return false; +} + /** * Check whether a gradient's optional first argument is a supported prelude. * @@ -164,36 +312,41 @@ function neve_css_gradient_patterns(): array { * @return bool */ function neve_is_css_gradient_prelude( string $type, string $value ): bool { - $patterns = neve_css_gradient_patterns(); - $value = trim( $value ); - $number = $patterns['number']; - $zero = $patterns['zero']; - $angle = '(?:' . $zero . '|' . $number . '(?:deg|grad|rad|turn))'; - $dimension = $patterns['length']; - $horizontal = '(?:left|center|right)'; - $vertical = '(?:top|center|bottom)'; - $position = '(?:(?:left|center|right|top|bottom|' . $dimension . ')|(?:' . $horizontal . '\s+' . $vertical . '|' . $vertical . '\s+' . $horizontal . ')|(?:' . $horizontal . '|' . $dimension . ')\s+(?:' . $vertical . '|' . $dimension . ')|(?:left|right)\s+' . $dimension . '\s+(?:top|bottom)\s+' . $dimension . '|(?:top|bottom)\s+' . $dimension . '\s+(?:left|right)\s+' . $dimension . ')'; + $patterns = neve_css_gradient_patterns(); + $value = trim( $value ); + $number = $patterns['number']; + $zero = $patterns['zero']; + $angle = '(?:' . $zero . '|' . $number . '(?:deg|grad|rad|turn))'; + $dimension = $patterns['simple_length']; + $horizontal = '(?:left|center|right)'; + $vertical = '(?:top|center|bottom)'; + $position = '(?:(?:left|center|right|top|bottom|' . $dimension . ')|(?:' . $horizontal . '\s+' . $vertical . '|' . $vertical . '\s+' . $horizontal . ')|(?:' . $horizontal . '|' . $dimension . ')\s+(?:' . $vertical . '|' . $dimension . ')|(?:left|right)\s+' . $dimension . '\s+' . $vertical . '|' . $vertical . '\s+(?:left|right)\s+' . $dimension . '|(?:top|bottom)\s+' . $dimension . '\s+' . $horizontal . '|' . $horizontal . '\s+(?:top|bottom)\s+' . $dimension . '|(?:left|right)\s+' . $dimension . '\s+(?:top|bottom)\s+' . $dimension . '|(?:top|bottom)\s+' . $dimension . '\s+(?:left|right)\s+' . $dimension . ')'; + $interpolation = neve_css_gradient_interpolation_pattern(); if ( 'linear' === strtolower( $type ) ) { $direction = '(?:(?:left|right)(?:\s+(?:top|bottom))?|(?:top|bottom)(?:\s+(?:left|right))?)'; + $base = '(?:' . $angle . '|to\s+' . $direction . ')'; - return (bool) preg_match( '/^(?:' . $angle . '|to\s+' . $direction . ')$/i', $value ); + return neve_css_gradient_matches_prelude( $value, $base, $interpolation ); } if ( 'radial' === strtolower( $type ) ) { $unsigned_number = $patterns['unsigned_number']; $extent = '(?:closest-side|closest-corner|farthest-side|farthest-corner)'; - $circle_radius = '(?:0+(?:\.0+)?|\.0+|' . $unsigned_number . '(?:px|em|rem|vw|vh|vmin|vmax|ch|ex|cm|mm|in|pt|pc|q))'; - $ellipse_radius = '(?:0+(?:\.0+)?|\.0+|' . $unsigned_number . '(?:%|px|em|rem|vw|vh|vmin|vmax|ch|ex|cm|mm|in|pt|pc|q))'; + $circle_radius = '(?:\+?(?:0+(?:\.0+)?|\.0+)|\+?' . $unsigned_number . $patterns['length_unit'] . ')'; + $ellipse_radius = $patterns['nonnegative_length']; $size = '(?:' . $extent . '|' . $circle_radius . '|' . $ellipse_radius . '\s+' . $ellipse_radius . ')'; $circle = '(?:circle(?:\s+(?:' . $extent . '|' . $circle_radius . '))?|(?:' . $extent . '|' . $circle_radius . ')\s+circle)'; $ellipse = '(?:ellipse(?:\s+(?:' . $extent . '|' . $ellipse_radius . '\s+' . $ellipse_radius . '))?|(?:' . $extent . '|' . $ellipse_radius . '\s+' . $ellipse_radius . ')\s+ellipse)'; $shape_size = '(?:' . $circle . '|' . $ellipse . ')'; + $base = '(?:(?:' . $shape_size . '|' . $size . ')(?:\s+at\s+' . $position . ')?|at\s+' . $position . ')'; - return (bool) preg_match( '/^(?:(?:' . $shape_size . '|' . $size . ')(?:\s+at\s+' . $position . ')?|at\s+' . $position . ')$/i', $value ); + return neve_css_gradient_matches_prelude( $value, $base, $interpolation ); } - return (bool) preg_match( '/^(?:from\s+' . $angle . ')(?:\s+at\s+' . $position . ')?$|^at\s+' . $position . '$/i', $value ); + $base = '(?:(?:from\s+' . $angle . ')(?:\s+at\s+' . $position . ')?|at\s+' . $position . ')'; + + return neve_css_gradient_matches_prelude( $value, $base, $interpolation ); } /** @@ -210,14 +363,13 @@ function neve_is_css_gradient_stop( string $value, string $type ): bool { return neve_gradient_stop_has_valid_positions( substr( $value, strlen( $matches[0] ) ), $type ); } - if ( preg_match( '/^((?:var|rgb|rgba|hsl|hsla)(\((?:[a-z0-9#%.,\s\/_-]|(?2))*\)))(?=\s|$)/i', $value, $matches ) ) { - if ( 0 === stripos( $matches[1], 'var(' ) ) { - $is_color = neve_is_css_var( $matches[1] ); - } else { - $is_color = neve_is_css_gradient_function_color( $matches[1] ); - } + $function = neve_css_leading_function( $value ); + if ( false !== $function && ( '' === $function['suffix'] || preg_match( '/^\s/', $function['suffix'] ) ) ) { + $is_color = 'var' === $function['name'] + ? neve_is_css_var( $function['value'] ) + : neve_is_css_gradient_function_color( $function['value'] ); - return $is_color && neve_gradient_stop_has_valid_positions( substr( $value, strlen( $matches[1] ) ), $type ); + return $is_color && neve_gradient_stop_has_valid_positions( $function['suffix'], $type ); } if ( ! preg_match( '/^([a-z]+(?:-[a-z]+)*)(?=\s|$)/i', $value, $matches ) ) { @@ -236,6 +388,11 @@ function neve_is_css_gradient_stop( string $value, string $type ): bool { * @return bool */ function neve_is_css_gradient_function_color( string $value ): bool { + $function = neve_css_leading_function( trim( $value ) ); + if ( false === $function || '' !== trim( $function['suffix'] ) ) { + return false; + } + $patterns = neve_css_gradient_patterns(); $number = $patterns['number']; $component = $number . '%?'; @@ -247,7 +404,102 @@ function neve_is_css_gradient_function_color( string $value ): bool { $hsl = '(?:hsl|hsla)\(\s*' . $hue . '\s*,\s*' . $number . '%\s*,\s*' . $number . '%(?:\s*,\s*' . $alpha . ')?\s*\)'; $hsl_modern = '(?:hsl|hsla)\(\s*' . $hue . '\s+' . $number . '%\s+' . $number . '%(?:\s*\/\s*' . $alpha . ')?\s*\)'; - return (bool) preg_match( '/^(?:' . $rgb . '|' . $rgb_modern . '|' . $hsl . '|' . $hsl_modern . ')$/i', $value ); + if ( (bool) preg_match( '/^(?:' . $rgb . '|' . $rgb_modern . '|' . $hsl . '|' . $hsl_modern . ')$/i', $value ) ) { + return true; + } + + $none_percentage = '(?:none|' . $number . '%)'; + $none_component = '(?:none|' . $component . ')'; + $none_hue = '(?:none|' . $hue . ')'; + $optional_alpha = '(?:\s*\/\s*' . $none_component . ')?'; + $modern_components = '\s+' . $none_component . '\s+' . $none_component . '\s+' . $none_component . $optional_alpha . '\s*'; + + switch ( $function['name'] ) { + case 'hwb': + return (bool) preg_match( '/^\s*' . $none_hue . '\s+' . $none_percentage . '\s+' . $none_percentage . $optional_alpha . '\s*$/i', $function['body'] ); + case 'lab': + case 'oklab': + return (bool) preg_match( '/^\s*' . $none_component . '\s+' . $none_component . '\s+' . $none_component . $optional_alpha . '\s*$/i', $function['body'] ); + case 'lch': + case 'oklch': + return (bool) preg_match( '/^\s*' . $none_component . '\s+' . $none_component . '\s+' . $none_hue . $optional_alpha . '\s*$/i', $function['body'] ); + case 'color': + $space = '(?:srgb|srgb-linear|display-p3|a98-rgb|prophoto-rgb|rec2020|xyz|xyz-d50|xyz-d65|--[a-z0-9_-]+)'; + + return (bool) preg_match( '/^\s*' . $space . $modern_components . '$/i', $function['body'] ); + case 'device-cmyk': + return (bool) preg_match( '/^\s*' . $none_component . '(?:\s+' . $none_component . '){3}' . $optional_alpha . '\s*$/i', $function['body'] ); + case 'color-mix': + return neve_is_css_color_mix( $function['body'] ); + case 'light-dark': + $colors = neve_css_split_top_level( $function['body'] ); + + return false !== $colors && 2 === count( $colors ) + && neve_is_css_gradient_color( $colors[0] ) + && neve_is_css_gradient_color( $colors[1] ); + default: + return false; + } +} + +/** + * Check a complete color value used by another color function. + * + * @param string $value Color value. + * + * @return bool + */ +function neve_is_css_gradient_color( string $value ): bool { + $value = trim( $value ); + if ( (bool) preg_match( '/^#(?:[0-9a-f]{3,4}|[0-9a-f]{6}|[0-9a-f]{8})$/i', $value ) ) { + return true; + } + + if ( neve_is_css_var( $value ) || neve_is_css_gradient_function_color( $value ) ) { + return true; + } + + return (bool) preg_match( '/^[a-z]+(?:-[a-z]+)*$/i', $value ) + && '' !== neve_sanitize_row_text_color( $value ); +} + +/** + * Check a color-mix() body. + * + * @param string $value Function body. + * + * @return bool + */ +function neve_is_css_color_mix( string $value ): bool { + $arguments = neve_css_split_top_level( $value ); + if ( false === $arguments || 3 !== count( $arguments ) ) { + return false; + } + + if ( ! preg_match( '/^' . neve_css_gradient_interpolation_pattern() . '$/i', $arguments[0] ) ) { + return false; + } + + $weights = array(); + foreach ( array_slice( $arguments, 1 ) as $color_stop ) { + if ( neve_is_css_gradient_color( $color_stop ) ) { + $weights[] = null; + continue; + } + + if ( ! preg_match( '/^(.*\S)\s+(' . neve_css_gradient_patterns()['number'] . '%)$/', $color_stop, $matches ) + || ! neve_is_css_gradient_color( $matches[1] ) ) { + return false; + } + + $weight = (float) substr( $matches[2], 0, -1 ); + if ( $weight < 0 || $weight > 100 ) { + return false; + } + $weights[] = $weight; + } + + return null === $weights[0] || null === $weights[1] || 0.0 !== $weights[0] + $weights[1]; } /** @@ -264,14 +516,311 @@ function neve_gradient_stop_has_valid_positions( string $value, string $type ): return true; } - $patterns = neve_css_gradient_patterns(); - $number = $patterns['number']; - $zero = $patterns['zero']; - $dimension = 'conic' === strtolower( $type ) + $positions = neve_css_split_top_level_whitespace( $value ); + if ( false === $positions || count( $positions ) > 2 ) { + return false; + } + + foreach ( $positions as $position ) { + if ( ! neve_is_css_gradient_dimension( $position, $type ) ) { + return false; + } + } + + return true; +} + +/** + * Check a color transition hint between two gradient stops. + * + * @param string $value Transition hint. + * @param string $type Gradient type. + * + * @return bool + */ +function neve_is_css_gradient_hint( string $value, string $type ): bool { + return neve_is_css_gradient_dimension( trim( $value ), $type ); +} + +/** + * Split a CSS value on whitespace that is outside parentheses. + * + * @param string $value Value to split. + * + * @return array|false + */ +function neve_css_split_top_level_whitespace( string $value ) { + $parts = array(); + $current = ''; + $depth = 0; + $length = strlen( $value ); + + for ( $index = 0; $index < $length; $index++ ) { + $character = $value[ $index ]; + if ( '(' === $character ) { + $depth++; + } elseif ( ')' === $character ) { + $depth--; + if ( $depth < 0 ) { + return false; + } + } + + if ( 0 === $depth && ctype_space( $character ) ) { + if ( '' !== $current ) { + $parts[] = $current; + $current = ''; + } + continue; + } + + $current .= $character; + } + + if ( 0 !== $depth ) { + return false; + } + + if ( '' !== $current ) { + $parts[] = $current; + } + + return $parts; +} + +/** + * Check a gradient stop dimension, including supported CSS math functions. + * + * @param string $value Dimension value. + * @param string $type Gradient type. + * + * @return bool + */ +function neve_is_css_gradient_dimension( string $value, string $type ): bool { + $patterns = neve_css_gradient_patterns(); + $number = $patterns['number']; + $zero = $patterns['zero']; + $direct = 'conic' === strtolower( $type ) + ? '(?:' . $zero . '|' . $number . '(?:%|deg|grad|rad|turn))' + : $patterns['simple_length']; + + if ( (bool) preg_match( '/^' . $direct . '$/i', trim( $value ) ) ) { + return true; + } + + return neve_is_css_gradient_math_function( trim( $value ), $type ); +} + +/** + * Check a calc(), min(), max(), clamp() or var() dimension. + * + * @param string $value CSS function. + * @param string $type Gradient type. + * + * @return bool + */ +function neve_is_css_gradient_math_function( string $value, string $type ): bool { + $value_type = neve_css_gradient_math_function_type( $value, $type ); + + return false !== $value_type && 'number' !== $value_type; +} + +/** + * Get the result type of a CSS math function. + * + * @param string $value CSS function. + * @param string $type Gradient type. + * + * @return string|false + */ +function neve_css_gradient_math_function_type( string $value, string $type ) { + $function = neve_css_leading_function( $value ); + if ( false === $function || '' !== trim( $function['suffix'] ) ) { + return false; + } + + if ( 'var' === $function['name'] ) { + $arguments = neve_css_split_top_level( $function['body'] ); + if ( false === $arguments || count( $arguments ) > 2 || ! preg_match( '/^\s*--[a-z0-9_-]+\s*$/i', $arguments[0] ) ) { + return false; + } + + if ( 2 === count( $arguments ) ) { + $fallback_type = neve_css_gradient_math_expression_type( $arguments[1], $type ); + if ( false === $fallback_type || 'number' === $fallback_type ) { + return false; + } + } + + return 'unknown'; + } + + if ( ! in_array( $function['name'], array( 'calc', 'min', 'max', 'clamp' ), true ) ) { + return false; + } + + $arguments = neve_css_split_top_level( $function['body'] ); + if ( false === $arguments || in_array( '', $arguments, true ) ) { + return false; + } + + if ( ( 'calc' === $function['name'] && 1 !== count( $arguments ) ) + || ( 'clamp' === $function['name'] && 3 !== count( $arguments ) ) ) { + return false; + } + + $result_type = false; + foreach ( $arguments as $argument ) { + $argument_type = neve_css_gradient_math_expression_type( $argument, $type ); + if ( false === $argument_type ) { + return false; + } + + if ( false === $result_type || 'unknown' === $result_type ) { + $result_type = $argument_type; + continue; + } + + if ( 'unknown' !== $argument_type && $argument_type !== $result_type ) { + return false; + } + } + + return $result_type; +} + +/** + * Check an arithmetic expression inside a CSS math function. + * + * @param string $value Expression. + * @param string $type Gradient type. + * + * @return bool + */ +function neve_is_css_gradient_math_expression( string $value, string $type ): bool { + return false !== neve_css_gradient_math_expression_type( $value, $type ); +} + +/** + * Get the result type of an arithmetic expression inside a CSS math function. + * + * @param string $value Expression. + * @param string $type Gradient type. + * + * @return string|false + */ +function neve_css_gradient_math_expression_type( string $value, string $type ) { + $value = trim( $value ); + if ( '' === $value ) { + return false; + } + + $operands = array(); + $operators = array(); + $current = ''; + $depth = 0; + $length = strlen( $value ); + for ( $index = 0; $index < $length; $index++ ) { + $character = $value[ $index ]; + if ( '(' === $character ) { + $depth++; + } elseif ( ')' === $character ) { + $depth--; + if ( $depth < 0 ) { + return false; + } + } + + if ( 0 === $depth && in_array( $character, array( '+', '-', '/' ), true ) ) { + $trimmed = trim( $current ); + if ( '' === $trimmed ) { + $current .= $character; + continue; + } + if ( in_array( $character, array( '+', '-' ), true ) + && ( ! ctype_space( $value[ $index - 1 ] ) || $index + 1 >= $length || ! ctype_space( $value[ $index + 1 ] ) ) ) { + return false; + } + $operands[] = $trimmed; + $operators[] = $character; + $current = ''; + continue; + } + + $current .= $character; + } + + if ( 0 !== $depth || '' === trim( $current ) ) { + return false; + } + $operands[] = trim( $current ); + + $result_type = neve_css_gradient_math_operand_type( $operands[0], $type ); + if ( false === $result_type ) { + return false; + } + + foreach ( $operators as $index => $operator ) { + $operand_type = neve_css_gradient_math_operand_type( $operands[ $index + 1 ], $type ); + if ( false === $operand_type ) { + return false; + } + + if ( '/' === $operator ) { + if ( 'dimension' === $operand_type ) { + if ( 'dimension' !== $result_type && 'unknown' !== $result_type ) { + return false; + } + $result_type = 'number'; + } elseif ( 'number' !== $operand_type && 'unknown' !== $operand_type ) { + return false; + } + continue; + } + + if ( 'unknown' === $result_type ) { + $result_type = $operand_type; + continue; + } + + if ( 'unknown' !== $operand_type && $operand_type !== $result_type ) { + return false; + } + } + + return $result_type; +} + +/** + * Get the result type of one operand inside a CSS math expression. + * + * @param string $value Operand. + * @param string $type Gradient type. + * + * @return string|false + */ +function neve_css_gradient_math_operand_type( string $value, string $type ) { + $value = trim( $value ); + $patterns = neve_css_gradient_patterns(); + $number = $patterns['number']; + $zero = $patterns['zero']; + $direct = 'conic' === strtolower( $type ) ? '(?:' . $zero . '|' . $number . '(?:%|deg|grad|rad|turn))' - : $patterns['length']; + : $patterns['simple_length']; + + if ( (bool) preg_match( '/^' . $number . '$/i', $value ) ) { + return 'number'; + } + + if ( (bool) preg_match( '/^' . $direct . '$/i', $value ) ) { + return 'dimension'; + } - return (bool) preg_match( '/^' . $dimension . '(?:\s+' . $dimension . ')?$/i', $value ); + if ( strlen( $value ) > 2 && '(' === $value[0] && ')' === $value[ strlen( $value ) - 1 ] ) { + return neve_css_gradient_math_expression_type( substr( $value, 1, -1 ), $type ); + } + + return neve_css_gradient_math_function_type( $value, $type ); } /** @@ -341,6 +890,10 @@ function neve_sanitize_colors( $value ) { return trim( $value ); } + if ( preg_match( '/^#(?:[0-9a-f]{4}|[0-9a-f]{8})$/i', trim( $value ), $match ) ) { + return $match[0]; + } + // Is this an rgba color or a hex? $mode = ( false === strpos( $value, 'rgba' ) ) ? 'hex' : 'rgba'; if ( 'rgba' === $mode ) { diff --git a/tests/test-neve-ability-style-mods.php b/tests/test-neve-ability-style-mods.php index 8b58d40e13..341629a49f 100644 --- a/tests/test-neve-ability-style-mods.php +++ b/tests/test-neve-ability-style-mods.php @@ -477,6 +477,29 @@ public function test_appearance_update_rejects_invalid_button_hex_color(): void $this->assertSame( $stored, get_theme_mod( 'neve_button_appearance' ) ); } + /** + * Alpha-hex button colors accepted by the ability remain available to CSS output. + */ + public function test_appearance_update_renders_alpha_hex_button_colors(): void { + $result = ( new Appearance_Update() )->execute( + array( + 'buttons' => array( + 'primary' => array( + 'background' => '#0008', + 'hover' => array( 'background' => '#11223344' ), + ), + ), + ) + ); + + $this->assertIsArray( $result ); + $stored = get_theme_mod( 'neve_button_appearance' ); + $this->assertSame( '#0008', $stored['background'] ); + $this->assertSame( '#11223344', $stored['backgroundHover'] ); + $this->assertSame( ' --primarybtnbg: #0008;', neve_sanitize_color_css_rule( '--primarybtnbg', $stored['background'] ) ); + $this->assertSame( ' --primarybtnhoverbg: #11223344;', neve_sanitize_color_css_rule( '--primarybtnhoverbg', $stored['backgroundHover'] ) ); + } + /** * Typography and font-family values are sanitized on write and output. */ @@ -672,7 +695,7 @@ public function test_site_set_theme_mod_rejects_invalid_color(): void { 'conic-gradient(red 10px, blue)', 'linear-gradient(rgb(1 / 2 / 3), blue)', 'radial-gradient(circle 10px 20px, red, blue)', - 'radial-gradient(at left 10px center, red, blue)', + 'radial-gradient(ellipse -10px 20px, red, blue)', 'linear-gradient(1.deg, red, blue)', 'linear-gradient(rgb(1.,2,3), blue)', 'linear-gradient(rgb(255, 0%, 0), blue)', @@ -695,6 +718,24 @@ public function test_site_set_theme_mod_rejects_invalid_color(): void { } } + /** + * Modern gradient colors and computed stops remain valid ability values. + */ + public function test_site_set_theme_mod_keeps_modern_gradient_color(): void { + $gradient = 'linear-gradient(hwb(30 40% 20%), color-mix(in oklab, red 40%, blue) min(20%, 100px))'; + $result = ( new Site_Set_Theme_Mod() )->execute( + array( + 'action' => 'set', + 'key' => self::SIDEBAR_TEXT, + 'value' => $gradient, + ) + ); + + $this->assertIsArray( $result ); + $this->assertTrue( $result['success'] ); + $this->assertSame( $gradient, get_theme_mod( self::SIDEBAR_TEXT ) ); + } + /** * Invalid button appearance colors keep the stored appearance. */ diff --git a/tests/test-neve-row-color-css.php b/tests/test-neve-row-color-css.php index 86eaf7c62f..429fe1bb36 100644 --- a/tests/test-neve-row-color-css.php +++ b/tests/test-neve-row-color-css.php @@ -100,6 +100,22 @@ public function test_malformed_stored_row_color_is_not_printed(): void { $this->assertSame( 0, preg_match( '/\.header-main\{[^}]*--bgcolor:/s', $this->header_css() ) ); } + /** + * Valid modern stored gradients remain present in generated CSS. + */ + public function test_modern_stored_gradient_is_printed(): void { + $gradient = 'linear-gradient(hwb(30 40% 20%), color-mix(in oklab, red 40%, blue) min(20%, 100px))'; + set_theme_mod( + 'hfg_header_layout_main_background', + array( + 'type' => 'color', + 'colorValue' => $gradient, + ) + ); + + $this->assertStringContainsString( '--bgcolor: ' . $gradient . ';', $this->header_css() ); + } + /** * Render the header builder's dynamic CSS. * diff --git a/tests/test-neve-sanitization.php b/tests/test-neve-sanitization.php index f3f0d8364c..de1156469b 100644 --- a/tests/test-neve-sanitization.php +++ b/tests/test-neve-sanitization.php @@ -191,6 +191,8 @@ public function test_sanitize_colors_with_non_string_values() { // Valid values keep working as before. $this->assertSame( '#ff0000', neve_sanitize_colors( '#ff0000' ) ); + $this->assertSame( '#0008', neve_sanitize_colors( '#0008' ) ); + $this->assertSame( '#11223344', neve_sanitize_colors( ' #11223344 ' ) ); $this->assertSame( 'rgba(255, 0, 0, 1)', neve_sanitize_colors( 'rgba(255, 0, 0, 1)' ) ); $this->assertSame( 'var(--nv-primary-accent)', neve_sanitize_colors( 'var(--nv-primary-accent)' ) ); } @@ -268,6 +270,8 @@ public function test_is_css_var() { $valid = [ 'var(--nv-primary-accent)', 'var(--nv-c-1,#E5E7EB)', + 'var(--nv-c-1, #0008)', + 'var(--nv-c-1, #11223344)', 'var(--nv-c-1, #e5e7eb)', 'var( --nv-site-bg , #fff )', 'var(--secondarybtnbg, transparent)', @@ -294,6 +298,10 @@ public function test_is_css_var() { 'var(--x, url(evil.css))', 'var(--x, "quoted")', 'var(--c5-missing,rgb(foo))', + 'var(--x, rgba(1,2,3,1)junk)', + 'var(--x, rgba(1,2,3,1);}body{background:lime}/*)', + 'var(--x, rgba(1,2,3,1)/*(*/);}body{background:lime}/*)', + 'var(--x, rgba(1,2,3,1))', 'linear-gradient(var(--a), var(--b))', '', [ 'var(--x)' ], @@ -331,12 +339,38 @@ public function test_sanitize_colors_gradients(): void { 'radial-gradient(circle at center, #fff 0%, #000 100%)', 'radial-gradient(at center top, red, blue)', 'radial-gradient(at left 10px top 20px, red, blue)', + 'radial-gradient(circle at left 10px top, red, blue)', + 'radial-gradient(circle at top left 10px, red, blue)', + 'radial-gradient(at left 10px center, red, blue)', 'radial-gradient(5em circle at top left, yellow, blue)', 'radial-gradient(circle closest-side, red, blue)', 'conic-gradient(from 90deg, red, blue)', 'repeating-linear-gradient(45deg, #FFF 0 10px, transparent 10px 20px)', 'linear-gradient(var(--nv-primary-accent), var(--nv_c-1, #fff))', 'linear-gradient(hsl(120 50% 50% / .5), transparent)', + 'linear-gradient(red 0%, 50%, blue 100%)', + 'radial-gradient(red 0%, 25px, blue 100%)', + 'conic-gradient(red 0deg, 25%, blue 180deg)', + 'linear-gradient(in oklab, red, blue)', + 'linear-gradient(in oklch longer hue to right, red, blue)', + 'linear-gradient(to right in oklab, red, blue)', + 'radial-gradient(in display-p3 circle at center, red, blue)', + 'conic-gradient(in hsl longer hue from 45deg at center, red, blue)', + 'linear-gradient(red calc(10% - 1px), blue)', + 'linear-gradient(red calc(10% + 1px), blue)', + 'linear-gradient(hwb(30 40% 20%), blue)', + 'linear-gradient(lab(50% 20 30 / .5), oklch(60% .2 120))', + 'linear-gradient(color(display-p3 1 0 0), device-cmyk(0 1 1 0))', + 'linear-gradient(color-mix(in oklab, red 40%, blue), light-dark(white, black))', + 'linear-gradient(red min(20%, 100px), blue)', + 'linear-gradient(red max(10%, 2rem), blue)', + 'linear-gradient(red clamp(10%, 20vw, 50%), blue)', + 'linear-gradient(red calc(var(--stop) - 1px), blue)', + 'linear-gradient(red min(calc(20% - 1px), var(--stop, 10%)), blue)', + 'linear-gradient(red 10dvh, blue)', + 'linear-gradient(red calc(100% - 1lh), blue)', + 'radial-gradient(ellipse 10cqw 20cqh, red, blue)', + 'radial-gradient(circle +10px, red, blue)', ]; foreach ( $valid as $value ) { @@ -359,12 +393,36 @@ public function test_sanitize_colors_gradients(): void { 'radial-gradient(circle 10px 20px, red, blue)', 'radial-gradient(circle -10px, red, blue)', 'radial-gradient(at left right, red, blue)', - 'radial-gradient(at left 10px center, red, blue)', 'radial-gradient(10%, red, blue)', + 'radial-gradient(ellipse -10px 20px, red, blue)', 'linear-gradient(1.deg, red, blue)', 'linear-gradient(rgb(1.,2,3), blue)', 'linear-gradient(rgb(255, 0%, 0), blue)', 'linear-gradient(var(--c5-missing,rgb(foo)),#fff)', + 'linear-gradient(50%, red, blue)', + 'linear-gradient(red, 50%)', + 'linear-gradient(red, 50%, 60%, blue)', + 'linear-gradient(red, 50% 60%, blue)', + 'conic-gradient(red, 10px, blue)', + 'linear-gradient(in banana, red, blue)', + 'linear-gradient(red calc(foo - 1px), blue)', + 'linear-gradient(red calc(10% - ), blue)', + 'linear-gradient(hwb(30 40%), blue)', + 'linear-gradient(lab(50% 20), blue)', + 'linear-gradient(color(display-p3 1 0), blue)', + 'linear-gradient(color-mix(in banana, red, blue), white)', + 'linear-gradient(color-mix(in oklab, red, url(foo)), white)', + 'linear-gradient(red min(20%, foo), blue)', + 'linear-gradient(red clamp(10%, 20%), blue)', + 'linear-gradient(red calc(var(bad) - 1px), blue)', + 'linear-gradient(red calc(10%-1px), blue)', + 'linear-gradient(red calc(1px + 2), blue)', + 'linear-gradient(red calc(1px + 0), blue)', + 'linear-gradient(red min(10%, 2), blue)', + 'linear-gradient(red var(--stop, 2), blue)', + 'linear-gradient(color-mix(in oklab, red -10%, blue), white)', + 'linear-gradient(color-mix(in oklab, red 110%, blue), white)', + 'linear-gradient(color-mix(in oklab, red 0%, blue 0%), white)', 'linear-gradient(red,blue)', 'linear-gradient(red,blue);}body{display:none', 'linear-gradient(red,blue),url(//example.com/t.png)', @@ -381,7 +439,7 @@ public function test_sanitize_colors_gradients(): void { } /** - * Standalone rgb(), hsl() and hsla() colors are kept; anything around them is rejected. + * Standalone CSS function colors are kept; anything around them is rejected. */ public function test_sanitize_colors_keeps_function_colors(): void { $valid = [ @@ -394,6 +452,11 @@ public function test_sanitize_colors_keeps_function_colors(): void { 'hsl(120, 50%, 50%)', 'hsl(120deg, 50%, 50%)', 'hsla(120, 50%, 50%, 0.5)', + 'hwb(30 40% 20%)', + 'lab(50% 40 59)', + 'oklch(60% .2 120)', + 'color(display-p3 1 0 0)', + 'color-mix(in oklab, red 40%, blue)', ]; foreach ( $valid as $value ) { @@ -417,7 +480,6 @@ public function test_sanitize_colors_keeps_function_colors(): void { 'hsl(120, 50, 50)', 'rgb(1,2,3', '#rgb(1,2,3)', - 'lab(50% 40 59)', ]; foreach ( $invalid as $value ) {