From 13855e1caa170070887fc2d861a6d75e6db67fe8 Mon Sep 17 00:00:00 2001 From: girishpanchal30 Date: Fri, 20 Nov 2026 12:13:58 +0530 Subject: [PATCH 1/6] fix: validate header and footer row text colors --- .../Core/Builder/Abstract_Builder.php | 8 +- inc/abilities/abstract_ability.php | 23 ++ inc/abilities/builder_configure.php | 23 +- inc/abilities/site_set_theme_mod.php | 12 +- tests/test-neve-row-text-color.php | 213 ++++++++++++++++++ 5 files changed, 270 insertions(+), 9 deletions(-) create mode 100644 tests/test-neve-row-text-color.php diff --git a/header-footer-grid/Core/Builder/Abstract_Builder.php b/header-footer-grid/Core/Builder/Abstract_Builder.php index 24173fec99..263503abf1 100644 --- a/header-footer-grid/Core/Builder/Abstract_Builder.php +++ b/header-footer-grid/Core/Builder/Abstract_Builder.php @@ -527,7 +527,7 @@ public function define_row_settings( $row_id ) { ], 'partial' => $row_id === 'sidebar' ? 'hfg_header_layout_partial' : $row_setting_id . '_partial', ], - 'sanitize_callback' => 'wp_filter_nohtml_kses', + 'sanitize_callback' => 'neve_sanitize_colors', 'default' => $default_colors['text'], ] ); @@ -1039,6 +1039,12 @@ private function add_row_style( $row_index, $css_array = array() ) { $rules['--color'] = [ Dynamic_Selector::META_KEY => $this->control_id . '_' . $row_index . '_' . self::TEXT_COLOR, Dynamic_Selector::META_DEFAULT => $default_colors['text'], + // Stored values can bypass the setting sanitizer, so they could still close the declaration. + Dynamic_Selector::META_FILTER => function ( $css_prop, $value ) use ( $default_colors ) { + $color = neve_sanitize_colors( $value ); + + return sprintf( ' %s: %s;', $css_prop, '' === $color ? $default_colors['text'] : $color ); + }, ]; // Exclude the following rules for the header when the global header background is active. diff --git a/inc/abilities/abstract_ability.php b/inc/abilities/abstract_ability.php index ff6c2b0598..715d79890d 100644 --- a/inc/abilities/abstract_ability.php +++ b/inc/abilities/abstract_ability.php @@ -236,6 +236,29 @@ protected function validation_issue( $code, $message, $field = '' ) { ); } + /** + * Sanitize a color or background theme mod the way its Customizer setting does. + * + * @param string $key Theme mod key. + * @param mixed $value Incoming value. + * @return mixed The sanitized value; other keys are returned unchanged. Null when a non-empty color or a background is invalid. + */ + protected function sanitize_style_mod( string $key, $value ) { + if ( '_background' === substr( $key, -11 ) ) { + $background = neve_sanitize_background( $value ); + + return is_wp_error( $background ) ? null : $background; + } + + if ( '_color' === substr( $key, -6 ) ) { + $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..aa311e2658 100644 --- a/inc/abilities/builder_configure.php +++ b/inc/abilities/builder_configure.php @@ -185,13 +185,22 @@ protected function apply_row_style( $builder_id, $row, array $style ) { set_theme_mod( $prefix . '_height', wp_json_encode( map_deep( $style['height'], 'sanitize_text_field' ) ) ); $affected[] = $prefix . '_height'; } - if ( isset( $style['background'] ) ) { - set_theme_mod( $prefix . '_background', map_deep( $style['background'], 'sanitize_text_field' ) ); - $affected[] = $prefix . '_background'; - } - if ( isset( $style['text_color'] ) ) { - set_theme_mod( $prefix . '_new_text_color', sanitize_text_field( $style['text_color'] ) ); - $affected[] = $prefix . '_new_text_color'; + $style_mods = array( + 'background' => $prefix . '_background', + 'text_color' => $prefix . '_new_text_color', + ); + foreach ( $style_mods as $field => $key ) { + if ( ! isset( $style[ $field ] ) ) { + continue; + } + + $value = $this->sanitize_style_mod( $key, map_deep( $style[ $field ], 'sanitize_text_field' ) ); + 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-row-text-color.php b/tests/test-neve-row-text-color.php new file mode 100644 index 0000000000..7566c5bd66 --- /dev/null +++ b/tests/test-neve-row-text-color.php @@ -0,0 +1,213 @@ +previous_customize = $GLOBALS['wp_customize'] ?? null; + $this->previous_styles = $GLOBALS['wp_styles'] ?? null; + + Mods::$no_cache = true; + } + + /** + * Restore the replaced global state. + */ + public function tear_down(): void { + $GLOBALS['wp_customize'] = $this->previous_customize; + $GLOBALS['wp_styles'] = $this->previous_styles; + Mods::$no_cache = false; + + foreach ( self::TOUCHED_MODS as $mod ) { + remove_theme_mod( $mod ); + } + + parent::tear_down(); + } + + /** + * The Customizer setting keeps colors and drops anything else. + */ + public function test_row_text_color_setting_accepts_only_colors(): void { + require_once ABSPATH . WPINC . '/class-wp-customize-manager.php'; + + $manager = new WP_Customize_Manager(); + Main::get_instance()->get_builder( 'footer' )->customize_register( $manager ); + + $setting = $manager->get_setting( 'hfg_footer_layout_main_new_text_color' ); + $this->assertInstanceOf( WP_Customize_Setting::class, $setting ); + + $this->assertSame( '', $setting->sanitize( self::PAYLOAD ) ); + $this->assertSame( '', $setting->sanitize( 'notacolor' ) ); + $this->assertSame( '#222222', $setting->sanitize( '#222222' ) ); + $this->assertSame( 'rgba(1, 2, 3, 0.5)', $setting->sanitize( 'rgba(1, 2, 3, 0.5)' ) ); + $this->assertSame( 'var(--nv-primary-accent)', $setting->sanitize( 'var(--nv-primary-accent)' ) ); + } + + /** + * A stored row text color is printed only when it is a color; otherwise the row default is printed. + */ + public function test_row_text_color_output_prints_only_colors(): void { + set_theme_mod( 'hfg_header_layout_main_new_text_color', self::PAYLOAD ); + $css = $this->get_frontend_css(); + + $this->assertStringNotContainsString( 'lime', $css ); + $this->assertMatchesRegularExpression( '/\.header-main\{[^}]*--color: var\(--nv-text-color\);/', $css ); + + set_theme_mod( 'hfg_header_layout_main_new_text_color', 'notacolor' ); + $css = $this->get_frontend_css(); + + $this->assertStringNotContainsString( 'notacolor', $css ); + $this->assertMatchesRegularExpression( '/\.header-main\{[^}]*--color: var\(--nv-text-color\);/', $css ); + + set_theme_mod( 'hfg_header_layout_main_new_text_color', '#123456' ); + + $this->assertMatchesRegularExpression( '/\.header-main\{[^}]*--color: #123456;/', $this->get_frontend_css() ); + } + + /** + * The configure abilities skip a row text color that is not a color and keep valid ones. + */ + public function test_configure_abilities_sanitize_row_style_colors(): void { + set_theme_mod( 'hfg_header_layout_main_new_text_color', '#111111' ); + + ( new Header_Configure() )->execute( + array( + 'row_style' => array( + array( + 'row' => 'main', + 'text_color' => self::PAYLOAD, + ), + ), + ) + ); + $this->assertSame( '#111111', get_theme_mod( 'hfg_header_layout_main_new_text_color' ) ); + + ( new Footer_Configure() )->execute( + array( + 'row_style' => array( + array( + 'row' => 'main', + 'text_color' => '#abcdef', + 'background' => array( + 'type' => 'color', + 'colorValue' => self::PAYLOAD, + ), + ), + ), + ) + ); + $this->assertSame( '#abcdef', get_theme_mod( 'hfg_footer_layout_main_new_text_color' ) ); + + $background = get_theme_mod( 'hfg_footer_layout_main_background' ); + $this->assertIsArray( $background ); + $this->assertSame( '', $background['colorValue'] ); + } + + /** + * The set-theme-mod ability rejects colors and backgrounds its Customizer setting would not save. + */ + public function test_set_theme_mod_ability_sanitizes_colors_and_backgrounds(): void { + $ability = new Site_Set_Theme_Mod(); + + set_theme_mod( 'hfg_header_layout_sidebar_new_text_color', '#111111' ); + $result = $ability->execute( + array( + 'action' => 'set', + 'key' => 'hfg_header_layout_sidebar_new_text_color', + 'value' => self::PAYLOAD, + ) + ); + $this->assertFalse( $result['success'] ); + $this->assertSame( 'invalid_value', $result['error']['code'] ); + $this->assertSame( '#111111', get_theme_mod( 'hfg_header_layout_sidebar_new_text_color' ) ); + + $result = $ability->execute( + array( + 'action' => 'set', + 'key' => 'neve_form_fields_border_color', + 'value' => 'rgba(1, 2, 3, 1)', + ) + ); + $this->assertTrue( $result['success'] ); + $this->assertSame( 'rgba(1, 2, 3, 1)', get_theme_mod( 'neve_form_fields_border_color' ) ); + + $result = $ability->execute( + array( + 'action' => 'set', + 'key' => 'hfg_header_layout_sidebar_new_text_color', + 'value' => '', + ) + ); + $this->assertTrue( $result['success'] ); + $this->assertSame( '', get_theme_mod( 'hfg_header_layout_sidebar_new_text_color' ) ); + + $result = $ability->execute( + array( + 'action' => 'set', + 'key' => 'hfg_header_layout_sidebar_background', + 'value' => self::PAYLOAD, + ) + ); + $this->assertFalse( $result['success'] ); + $this->assertFalse( get_theme_mod( 'hfg_header_layout_sidebar_background' ) ); + } + + /** + * Build the frontend inline CSS. + * + * @return string + */ + private function get_frontend_css(): string { + $GLOBALS['wp_styles'] = new WP_Styles(); + wp_register_style( Dynamic_Css::FRONTEND_HANDLE, false ); + + ( new Dynamic_Css() )->enqueue(); + + return implode( '', (array) wp_styles()->get_data( Dynamic_Css::FRONTEND_HANDLE, 'after' ) ); + } +} From cc3240a758c1dfe6878b11dbae79075dcbfd6641 Mon Sep 17 00:00:00 2001 From: girishpanchal30 Date: Fri, 20 Nov 2026 14:23:05 +0530 Subject: [PATCH 2/6] fix: improve color sanitization and validation in builder configurations --- .../Core/Builder/Abstract_Builder.php | 45 +++++-- inc/abilities/builder_configure.php | 31 +++-- inc/abilities/footer_configure.php | 2 +- inc/abilities/header_configure.php | 2 +- tests/test-neve-css-style-tags.php | 4 +- tests/test-neve-row-text-color.php | 119 ++++++++++++++++++ 6 files changed, 178 insertions(+), 25 deletions(-) diff --git a/header-footer-grid/Core/Builder/Abstract_Builder.php b/header-footer-grid/Core/Builder/Abstract_Builder.php index 263503abf1..52ea21bdcf 100644 --- a/header-footer-grid/Core/Builder/Abstract_Builder.php +++ b/header-footer-grid/Core/Builder/Abstract_Builder.php @@ -1039,12 +1039,7 @@ private function add_row_style( $row_index, $css_array = array() ) { $rules['--color'] = [ Dynamic_Selector::META_KEY => $this->control_id . '_' . $row_index . '_' . self::TEXT_COLOR, Dynamic_Selector::META_DEFAULT => $default_colors['text'], - // Stored values can bypass the setting sanitizer, so they could still close the declaration. - Dynamic_Selector::META_FILTER => function ( $css_prop, $value ) use ( $default_colors ) { - $color = neve_sanitize_colors( $value ); - - return sprintf( ' %s: %s;', $css_prop, '' === $color ? $default_colors['text'] : $color ); - }, + Dynamic_Selector::META_FILTER => $this->sanitized_color_rule( $default_colors['text'] ), ]; // Exclude the following rules for the header when the global header background is active. @@ -1067,6 +1062,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->sanitized_color_rule( $default_color ), ], ] ); @@ -1077,7 +1073,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->sanitized_color_rule( '' ), ], '--bgimage' => [ Dynamic_Selector::META_KEY => $this->control_id . '_' . $row_index . '_background', @@ -2060,7 +2057,7 @@ private function add_columns_layout_controls( $row_setting_id, $row_id ) { 'group' => $row_setting_id, 'tab' => SettingsManager::TAB_LAYOUT, 'transport' => 'postMessage', - 'sanitize_callback' => 'wp_filter_nohtml_kses', + 'sanitize_callback' => [ $this, 'sanitize_vertical_align' ], 'default' => 'flex-start', 'section' => $row_setting_id, 'label' => __( 'Vertical Alignment', 'neve' ), @@ -2080,6 +2077,35 @@ private function add_columns_layout_controls( $row_setting_id, $row_id ) { ); } + /** + * Sanitize a row vertical alignment. + * + * @param mixed $value Incoming or stored value. + * + * @return 'flex-start'|'center'|'flex-end' + */ + public function sanitize_vertical_align( $value ) { + return in_array( $value, [ 'center', 'flex-end' ], true ) ? $value : 'flex-start'; + } + + /** + * Build a CSS rule filter that prints only valid colors. + * + * Stored values can bypass the setting sanitizer, so they could still close the declaration. + * + * @param string $fallback Color printed when the stored one is invalid; '' prints no rule. + * + * @return callable(string, mixed): string + */ + private function sanitized_color_rule( $fallback ) { + return function ( $css_prop, $value ) use ( $fallback ) { + $color = neve_sanitize_colors( $value ); + $color = '' === $color ? $fallback : $color; + + return '' === $color ? '' : sprintf( ' %s: %s;', $css_prop, $color ); + }; + } + /** * Sanitize builder columns * @@ -2280,6 +2306,9 @@ private function add_new_builder_styles( $css_array, $row ) { '--valign' => [ Dynamic_Selector::META_KEY => $mods_prefix . self::VERTICAL_ALIGN, Dynamic_Selector::META_DEFAULT => 'flex-start', + Dynamic_Selector::META_FILTER => function ( $css_prop, $value ) { + return sprintf( ' %s: %s;', $css_prop, $this->sanitize_vertical_align( $value ) ); + }, ], ], ]; diff --git a/inc/abilities/builder_configure.php b/inc/abilities/builder_configure.php index aa311e2658..a217891ef1 100644 --- a/inc/abilities/builder_configure.php +++ b/inc/abilities/builder_configure.php @@ -10,6 +10,8 @@ namespace Neve\Abilities; +use HFG\Core\Builder\Abstract_Builder; + /** * Trait Builder_Configure * @@ -28,7 +30,7 @@ trait Builder_Configure { * Fetch the live builder instance. * * @param string $builder_id "header" or "footer". - * @return object|null + * @return Abstract_Builder|null */ protected function get_builder( $builder_id ) { if ( ! class_exists( '\HFG\Main' ) ) { @@ -37,7 +39,7 @@ protected function get_builder( $builder_id ) { $builder = \HFG\Main::get_instance()->get_builder( $builder_id ); - return is_object( $builder ) ? $builder : null; + return $builder instanceof Abstract_Builder ? $builder : null; } /** @@ -172,18 +174,17 @@ function ( $item ) use ( $component ) { /** * Apply per-row style settings (height / background / text color). * - * @param string $builder_id Builder id. - * @param string $row Row key. - * @param array $style Style block. - * @return string[] Affected theme_mod keys. + * @param Abstract_Builder $builder The builder instance. + * @param string $row Row key. + * @param array $style Style block. + * @return list Warnings for fields that were not saved. */ - protected function apply_row_style( $builder_id, $row, array $style ) { - $prefix = 'hfg_' . $builder_id . '_layout_' . $row; - $affected = array(); + protected function apply_row_style( Abstract_Builder $builder, $row, array $style ) { + $prefix = 'hfg_' . $builder->get_id() . '_layout_' . $row; + $warnings = array(); if ( isset( $style['height'] ) ) { - set_theme_mod( $prefix . '_height', wp_json_encode( map_deep( $style['height'], 'sanitize_text_field' ) ) ); - $affected[] = $prefix . '_height'; + set_theme_mod( $prefix . '_height', $builder->sanitize_responsive_int_json( $style['height'] ) ); } $style_mods = array( 'background' => $prefix . '_background', @@ -196,14 +197,18 @@ protected function apply_row_style( $builder_id, $row, array $style ) { $value = $this->sanitize_style_mod( $key, map_deep( $style[ $field ], 'sanitize_text_field' ) ); if ( null === $value ) { + $warnings[] = sprintf( + /* translators: %s: input field name */ + __( 'Invalid value for "%s".', 'neve' ), + 'row_style.' . $row . '.' . $field + ); continue; } set_theme_mod( $key, $value ); - $affected[] = $key; } - return $affected; + return $warnings; } /** diff --git a/inc/abilities/footer_configure.php b/inc/abilities/footer_configure.php index 0823ae157d..c38174e082 100644 --- a/inc/abilities/footer_configure.php +++ b/inc/abilities/footer_configure.php @@ -206,7 +206,7 @@ protected function run( array $input ) { if ( ! $dry_run && ! empty( $input['row_style'] ) && is_array( $input['row_style'] ) ) { foreach ( $input['row_style'] as $style ) { if ( isset( $style['row'] ) ) { - $this->apply_row_style( 'footer', $style['row'], $style ); + $warnings = array_merge( $warnings, $this->apply_row_style( $builder, $style['row'], $style ) ); } } } diff --git a/inc/abilities/header_configure.php b/inc/abilities/header_configure.php index 81c2ccd9a4..db7dfeebed 100644 --- a/inc/abilities/header_configure.php +++ b/inc/abilities/header_configure.php @@ -214,7 +214,7 @@ protected function run( array $input ) { if ( ! isset( $style['row'] ) ) { continue; } - $this->apply_row_style( 'header', $style['row'], $style ); + $warnings = array_merge( $warnings, $this->apply_row_style( $builder, $style['row'], $style ) ); } } diff --git a/tests/test-neve-css-style-tags.php b/tests/test-neve-css-style-tags.php index 944e6c6ef4..d55330b9bf 100644 --- a/tests/test-neve-css-style-tags.php +++ b/tests/test-neve-css-style-tags.php @@ -138,7 +138,7 @@ public function test_customizer_css_vars_tag_strips_html(): void { } /** - * A stored row background with HTML is printed without tags in the Customizer preview. + * A stored row background with HTML prints the default color in the Customizer preview. */ public function test_builder_css_strips_html_in_customizer_preview(): void { require_once ABSPATH . WPINC . '/class-wp-customize-manager.php'; @@ -158,7 +158,7 @@ public function test_builder_css_strips_html_in_customizer_preview(): void { Main::get_instance()->get_builder( 'header' )->render(); $output = (string) ob_get_clean(); - $this->assertStringContainsString( '--bgcolor: linear-gradient(red,blue);', $output ); + $this->assertMatchesRegularExpression( '/\.header-main\{[^}]*--bgcolor: var\(--nv-site-bg\);/', $output ); $this->assertStringNotContainsString( 'assertMatchesRegularExpression( '/\.header-main\{[^}]*--color: #123456;/', $this->get_frontend_css() ); } + /** + * A stored row background or overlay color that is not a color prints the default or nothing. + */ + public function test_row_background_colors_output_prints_only_colors(): void { + set_theme_mod( + 'hfg_header_layout_main_background', + [ + 'type' => 'color', + 'colorValue' => self::PAYLOAD, + ] + ); + $css = $this->get_frontend_css(); + + $this->assertStringNotContainsString( 'lime', $css ); + $this->assertMatchesRegularExpression( '/\.header-main\{[^}]*--bgcolor: var\(--nv-site-bg\);/', $css ); + + set_theme_mod( + 'hfg_header_layout_main_background', + [ + 'type' => 'image', + 'imageUrl' => 'https://example.com/a.png', + 'overlayColorValue' => self::PAYLOAD, + ] + ); + $css = $this->get_frontend_css(); + + $this->assertStringNotContainsString( 'lime', $css ); + $this->assertStringNotContainsString( '--overlaycolor', $css ); + } + + /** + * The footer row vertical alignment accepts and prints only its choices. + */ + public function test_row_vertical_align_accepts_only_choices(): void { + require_once ABSPATH . WPINC . '/class-wp-customize-manager.php'; + + $manager = new WP_Customize_Manager(); + Main::get_instance()->get_builder( 'footer' )->customize_register( $manager ); + + $setting = $manager->get_setting( 'hfg_footer_layout_bottom_vertical_align' ); + $this->assertInstanceOf( WP_Customize_Setting::class, $setting ); + $this->assertSame( 'flex-start', $setting->sanitize( 'top;}body{background:lime}/*' ) ); + $this->assertSame( 'center', $setting->sanitize( 'center' ) ); + + set_theme_mod( 'hfg_footer_layout_bottom_vertical_align', 'top;}body{background:lime}/*' ); + $css = $this->get_frontend_css(); + + $this->assertStringNotContainsString( 'lime', $css ); + $this->assertStringContainsString( '--valign: flex-start;', $css ); + + set_theme_mod( 'hfg_footer_layout_bottom_vertical_align', 'flex-end' ); + + $this->assertStringContainsString( '--valign: flex-end;', $this->get_frontend_css() ); + } + /** * The configure abilities skip a row text color that is not a color and keep valid ones. */ @@ -148,6 +206,67 @@ public function test_configure_abilities_sanitize_row_style_colors(): void { $this->assertSame( '', $background['colorValue'] ); } + /** + * The configure abilities report each row style field they did not save. + */ + public function test_configure_abilities_warn_about_skipped_row_style_fields(): void { + $result = ( new Header_Configure() )->execute( + array( + 'row_style' => array( + array( + 'row' => 'main', + 'text_color' => 'red', + ), + ), + ) + ); + $this->assertSame( array( 'Invalid value for "row_style.main.text_color".' ), $result['warnings'] ); + + $result = ( new Footer_Configure() )->execute( + array( + 'row_style' => array( + array( + 'row' => 'main', + 'text_color' => '#abcdef', + 'background' => array( 'type' => 'gradient' ), + ), + ), + ) + ); + $this->assertSame( array( 'Invalid value for "row_style.main.background".' ), $result['warnings'] ); + $this->assertSame( '#abcdef', get_theme_mod( 'hfg_footer_layout_main_new_text_color' ) ); + } + + /** + * The configure abilities save a row height the way its Customizer setting does. + */ + public function test_configure_abilities_sanitize_row_height(): void { + ( new Header_Configure() )->execute( + array( + 'row_style' => array( + array( + 'row' => 'main', + 'height' => array( + 'mobile' => 20, + 'desktop' => '80', + 'suffix' => array( + 'mobile' => 'px;}body{background:lime}/*', + 'desktop' => 'em', + ), + ), + ), + ), + ) + ); + + $height = json_decode( get_theme_mod( 'hfg_header_layout_main_height' ), true ); + $this->assertSame( 20, $height['mobile'] ); + $this->assertSame( 80, $height['desktop'] ); + $this->assertSame( 'px', $height['suffix']['mobile'] ); + $this->assertSame( 'em', $height['suffix']['desktop'] ); + $this->assertStringNotContainsString( 'lime', $this->get_frontend_css() ); + } + /** * The set-theme-mod ability rejects colors and backgrounds its Customizer setting would not save. */ From c70cf3be86af243d1c751f62cf97bbef3fe037fa Mon Sep 17 00:00:00 2001 From: girishpanchal30 Date: Fri, 20 Nov 2026 15:15:19 +0530 Subject: [PATCH 3/6] fix: enhance font weights and family validation --- inc/abilities/appearance_update.php | 22 ++- tests/test-neve-appearance-update-fonts.php | 149 ++++++++++++++++++++ 2 files changed, 169 insertions(+), 2 deletions(-) create mode 100644 tests/test-neve-appearance-update-fonts.php diff --git a/inc/abilities/appearance_update.php b/inc/abilities/appearance_update.php index 7c0461323c..51640098d8 100644 --- a/inc/abilities/appearance_update.php +++ b/inc/abilities/appearance_update.php @@ -34,6 +34,16 @@ class Appearance_Update extends Abstract_Ability { 'extra_2' => 'nv-c-2', ); + /** + * Font weights the typography controls accept. + */ + const FONT_WEIGHTS = array( '100', '200', '300', '400', '500', '600', '700', '800', '900' ); + + /** + * Font family names without the characters that could close their CSS declaration. + */ + const FONT_FAMILY_PATTERN = '^[^;{}()/]*$'; + /** * Container style contexts mapped to their theme_mods. */ @@ -117,10 +127,12 @@ protected function get_input_schema() { 'body_font' => array( 'type' => 'string', 'description' => 'Body font family name.', + 'pattern' => self::FONT_FAMILY_PATTERN, ), 'headings_font' => array( 'type' => 'string', 'description' => 'Headings font family name.', + 'pattern' => self::FONT_FAMILY_PATTERN, ), 'element' => array( 'type' => 'object', @@ -133,7 +145,10 @@ protected function get_input_schema() { 'fontSize' => $this->responsive_value_schema( 'Font size per device' ), 'lineHeight' => $this->responsive_value_schema( 'Line height per device', false ), 'letterSpacing' => $this->responsive_value_schema( 'Letter spacing per device', false ), - 'fontWeight' => array( 'type' => 'string' ), + 'fontWeight' => array( + 'type' => 'string', + 'enum' => self::FONT_WEIGHTS, + ), 'textTransform' => array( 'type' => 'string', 'enum' => array( 'none', 'capitalize', 'uppercase', 'lowercase' ), @@ -236,7 +251,10 @@ private function button_schema() { ), ), 'fontSize' => $this->responsive_value_schema( 'Font size per device.' ), - 'fontWeight' => array( 'type' => 'string' ), + 'fontWeight' => array( + 'type' => 'string', + 'enum' => self::FONT_WEIGHTS, + ), 'textTransform' => array( 'type' => 'string', 'enum' => array( 'none', 'capitalize', 'uppercase', 'lowercase' ), diff --git a/tests/test-neve-appearance-update-fonts.php b/tests/test-neve-appearance-update-fonts.php new file mode 100644 index 0000000000..c862397594 --- /dev/null +++ b/tests/test-neve-appearance-update-fonts.php @@ -0,0 +1,149 @@ +previous_user = get_current_user_id(); + $this->previous_styles = $GLOBALS['wp_styles'] ?? null; + Mods::$no_cache = true; + + wp_set_current_user( self::factory()->user->create( array( 'role' => 'administrator' ) ) ); + } + + /** + * Restore the replaced global state. + */ + public function tear_down(): void { + wp_set_current_user( $this->previous_user ); + $GLOBALS['wp_styles'] = $this->previous_styles; + Mods::$no_cache = false; + + foreach ( self::TOUCHED_MODS as $mod ) { + remove_theme_mod( $mod ); + } + + parent::tear_down(); + } + + /** + * Font weights outside the typography choices are rejected for every target; valid ones are saved. + */ + public function test_font_weights_accept_only_typography_choices(): void { + $inputs = array( + array( 'typography' => array( 'element' => array( 'target' => 'body', 'fontWeight' => '400' . self::PAYLOAD ) ) ), + array( 'typography' => array( 'element' => array( 'target' => 'h2', 'fontWeight' => '700' . self::PAYLOAD ) ) ), + array( 'buttons' => array( 'primary' => array( 'fontWeight' => '600' . self::PAYLOAD ) ) ), + ); + + foreach ( $inputs as $input ) { + $this->assertInstanceOf( WP_Error::class, $this->run_ability( $input ) ); + } + + foreach ( array( 'neve_typeface_general', 'neve_h2_typeface_general', 'neve_button_typeface' ) as $mod ) { + $this->assertFalse( get_theme_mod( $mod ) ); + } + $this->assertStringNotContainsString( 'lime', $this->get_frontend_css() ); + + $result = $this->run_ability( + array( + 'typography' => array( 'element' => array( 'target' => 'h2', 'fontWeight' => '700' ) ), + 'buttons' => array( 'primary' => array( 'fontWeight' => '500' ) ), + ) + ); + $this->assertIsArray( $result ); + $this->assertSame( '700', get_theme_mod( 'neve_h2_typeface_general' )['fontWeight'] ); + $this->assertSame( '500', get_theme_mod( 'neve_button_typeface' )['fontWeight'] ); + } + + /** + * Font families that could close their CSS declaration are rejected; valid ones are saved. + */ + public function test_font_families_accept_only_safe_names(): void { + $this->assertInstanceOf( WP_Error::class, $this->run_ability( array( 'typography' => array( 'body_font' => 'Arial' . self::PAYLOAD ) ) ) ); + $this->assertInstanceOf( WP_Error::class, $this->run_ability( array( 'typography' => array( 'headings_font' => 'Arial' . self::PAYLOAD ) ) ) ); + + $this->assertFalse( get_theme_mod( 'neve_body_font_family' ) ); + $this->assertFalse( get_theme_mod( 'neve_headings_font_family' ) ); + $this->assertStringNotContainsString( 'lime', $this->get_frontend_css() ); + + $result = $this->run_ability( + array( + 'typography' => array( + 'body_font' => 'Open Sans', + 'headings_font' => 'Arial, Helvetica, sans-serif', + ), + ) + ); + $this->assertIsArray( $result ); + $this->assertSame( 'Open Sans', get_theme_mod( 'neve_body_font_family' ) ); + $this->assertSame( 'Arial, Helvetica, sans-serif', get_theme_mod( 'neve_headings_font_family' ) ); + } + + /** + * Run the ability through the Abilities API, which validates the input schema. + * + * @param array $input Ability input. + * + * @return mixed + */ + private function run_ability( array $input ) { + $ability = wp_get_ability( 'neve/appearance-update' ); + $this->assertInstanceOf( WP_Ability::class, $ability ); + + return $ability->execute( $input ); + } + + /** + * Build the frontend inline CSS. + * + * @return string + */ + private function get_frontend_css(): string { + $GLOBALS['wp_styles'] = new WP_Styles(); + wp_register_style( Dynamic_Css::FRONTEND_HANDLE, false ); + + ( new Dynamic_Css() )->enqueue(); + + return implode( '', (array) wp_styles()->get_data( Dynamic_Css::FRONTEND_HANDLE, 'after' ) ); + } +} From 0a6a267bb97cb04055425a454b1af34d769aad02 Mon Sep 17 00:00:00 2001 From: girishpanchal30 Date: Fri, 20 Nov 2026 15:49:30 +0530 Subject: [PATCH 4/6] fix: enhance color sanitization and validation in theme settings --- inc/abilities/abstract_ability.php | 30 +++++++++- inc/abilities/appearance_update.php | 17 +----- inc/abilities/schema_helpers.php | 17 ++++-- inc/abilities/site_set_theme_mod.php | 10 +++- tests/test-neve-appearance-update-fonts.php | 28 +++++++++ tests/test-neve-row-text-color.php | 64 +++++++++++++++++++++ 6 files changed, 144 insertions(+), 22 deletions(-) diff --git a/inc/abilities/abstract_ability.php b/inc/abilities/abstract_ability.php index 715d79890d..8d801f4fed 100644 --- a/inc/abilities/abstract_ability.php +++ b/inc/abilities/abstract_ability.php @@ -237,11 +237,11 @@ protected function validation_issue( $code, $message, $field = '' ) { } /** - * Sanitize a color or background theme mod the way its Customizer setting does. + * Sanitize a theme mod printed as CSS: colors and backgrounds the way their Customizer settings do. * * @param string $key Theme mod key. * @param mixed $value Incoming value. - * @return mixed The sanitized value; other keys are returned unchanged. Null when a non-empty color or a background is invalid. + * @return mixed The sanitized value. Null when a non-empty color or a background is invalid, or any other value could close its CSS declaration. */ protected function sanitize_style_mod( string $key, $value ) { if ( '_background' === substr( $key, -11 ) ) { @@ -256,7 +256,31 @@ protected function sanitize_style_mod( string $key, $value ) { return '' === $color && '' !== $value ? null : $color; } - return $value; + return $this->closes_css_declaration( $value ) ? null : $value; + } + + /** + * Whether a value, or any value nested in it or in its JSON, could close a CSS declaration. + * + * @param mixed $value Incoming value. + * @return bool + */ + private function closes_css_declaration( $value ) { + if ( is_string( $value ) ) { + $decoded = json_decode( $value, true ); + + return is_array( $decoded ) ? $this->closes_css_declaration( $decoded ) : (bool) preg_match( '/[;{}<>]|\/\*/', $value ); + } + + if ( is_array( $value ) ) { + foreach ( $value as $item ) { + if ( $this->closes_css_declaration( $item ) ) { + return true; + } + } + } + + return false; } /** diff --git a/inc/abilities/appearance_update.php b/inc/abilities/appearance_update.php index 51640098d8..8cf0168f36 100644 --- a/inc/abilities/appearance_update.php +++ b/inc/abilities/appearance_update.php @@ -34,15 +34,10 @@ class Appearance_Update extends Abstract_Ability { 'extra_2' => 'nv-c-2', ); - /** - * Font weights the typography controls accept. - */ - const FONT_WEIGHTS = array( '100', '200', '300', '400', '500', '600', '700', '800', '900' ); - /** * Font family names without the characters that could close their CSS declaration. */ - const FONT_FAMILY_PATTERN = '^[^;{}()/]*$'; + const FONT_FAMILY_PATTERN = '^[^;{}()/<]*$'; /** * Container style contexts mapped to their theme_mods. @@ -145,10 +140,7 @@ protected function get_input_schema() { 'fontSize' => $this->responsive_value_schema( 'Font size per device' ), 'lineHeight' => $this->responsive_value_schema( 'Line height per device', false ), 'letterSpacing' => $this->responsive_value_schema( 'Letter spacing per device', false ), - 'fontWeight' => array( - 'type' => 'string', - 'enum' => self::FONT_WEIGHTS, - ), + 'fontWeight' => $this->font_weight_schema(), 'textTransform' => array( 'type' => 'string', 'enum' => array( 'none', 'capitalize', 'uppercase', 'lowercase' ), @@ -251,10 +243,7 @@ private function button_schema() { ), ), 'fontSize' => $this->responsive_value_schema( 'Font size per device.' ), - 'fontWeight' => array( - 'type' => 'string', - 'enum' => self::FONT_WEIGHTS, - ), + 'fontWeight' => $this->font_weight_schema(), 'textTransform' => array( 'type' => 'string', 'enum' => array( 'none', 'capitalize', 'uppercase', 'lowercase' ), diff --git a/inc/abilities/schema_helpers.php b/inc/abilities/schema_helpers.php index 911657b82d..10dd29bd8b 100644 --- a/inc/abilities/schema_helpers.php +++ b/inc/abilities/schema_helpers.php @@ -31,10 +31,7 @@ protected function typeface_schema( $description = 'Typography settings' ) { 'fontSize' => $this->responsive_value_schema( 'Font size per device', true ), 'lineHeight' => $this->responsive_value_schema( 'Line height per device', false ), 'letterSpacing' => $this->responsive_value_schema( 'Letter spacing per device', false ), - 'fontWeight' => array( - 'type' => 'string', - 'enum' => array( 'none', '100', '200', '300', '400', '500', '600', '700', '800', '900' ), - ), + 'fontWeight' => $this->font_weight_schema(), 'textTransform' => array( 'type' => 'string', 'enum' => array( 'none', 'capitalize', 'uppercase', 'lowercase' ), @@ -43,6 +40,18 @@ protected function typeface_schema( $description = 'Typography settings' ) { ); } + /** + * Font weight schema; 'none' is the typeface control's default and reset value. + * + * @return array{type: 'string', enum: list} + */ + protected function font_weight_schema() { + return array( + 'type' => 'string', + 'enum' => array( 'none', '100', '200', '300', '400', '500', '600', '700', '800', '900' ), + ); + } + /** * Responsive numeric value schema, optionally with a unit suffix. * diff --git a/inc/abilities/site_set_theme_mod.php b/inc/abilities/site_set_theme_mod.php index 8ff38f0ff9..0109d32441 100644 --- a/inc/abilities/site_set_theme_mod.php +++ b/inc/abilities/site_set_theme_mod.php @@ -51,6 +51,11 @@ class Site_Set_Theme_Mod extends Abstract_Ability { 'hfg_header_layout_sidebar_new_text_color', ); + /** + * Allowlisted keys that hold text or markup rather than CSS values. + */ + const TEXT_KEYS = array( 'nav-icon_svg_menu_icon', 'nav-icon_menu_label' ); + /** * {@inheritDoc} */ @@ -172,7 +177,10 @@ protected function run( array $input ) { ); } - $value = $this->sanitize_style_mod( $key, $this->sanitize_value( $input['value'] ) ); + $value = $this->sanitize_value( $input['value'] ); + if ( ! in_array( $key, self::TEXT_KEYS, true ) ) { + $value = $this->sanitize_style_mod( $key, $value ); + } if ( null === $value ) { return $this->validation_issue( 'invalid_value', diff --git a/tests/test-neve-appearance-update-fonts.php b/tests/test-neve-appearance-update-fonts.php index c862397594..530057cd78 100644 --- a/tests/test-neve-appearance-update-fonts.php +++ b/tests/test-neve-appearance-update-fonts.php @@ -119,6 +119,34 @@ public function test_font_families_accept_only_safe_names(): void { $this->assertSame( 'Arial, Helvetica, sans-serif', get_theme_mod( 'neve_headings_font_family' ) ); } + /** + * The typeface control's 'none' weight resets a weight and is saved. + */ + public function test_font_weight_none_is_saved(): void { + $result = $this->run_ability( + array( + 'typography' => array( 'element' => array( 'target' => 'body', 'fontWeight' => 'none' ) ), + 'buttons' => array( 'primary' => array( 'fontWeight' => 'none' ) ), + ) + ); + + $this->assertIsArray( $result ); + $this->assertSame( 'none', get_theme_mod( 'neve_typeface_general' )['fontWeight'] ); + $this->assertSame( 'none', get_theme_mod( 'neve_button_typeface' )['fontWeight'] ); + } + + /** + * A font family that saving would escape into an entity with a semicolon is rejected. + */ + public function test_font_families_reject_text_escaped_to_entities(): void { + $this->assertInstanceOf( WP_Error::class, $this->run_ability( array( 'typography' => array( 'body_font' => 'xassertInstanceOf( WP_Error::class, $this->run_ability( array( 'typography' => array( 'headings_font' => 'xassertFalse( get_theme_mod( 'neve_body_font_family' ) ); + $this->assertFalse( get_theme_mod( 'neve_headings_font_family' ) ); + $this->assertStringNotContainsString( 'lime', $this->get_frontend_css() ); + } + /** * Run the ability through the Abilities API, which validates the input schema. * diff --git a/tests/test-neve-row-text-color.php b/tests/test-neve-row-text-color.php index 8b0489f368..4b2a849c29 100644 --- a/tests/test-neve-row-text-color.php +++ b/tests/test-neve-row-text-color.php @@ -29,6 +29,15 @@ class TestNeveRowTextColor extends WP_UnitTestCase { 'hfg_header_layout_sidebar_new_text_color', 'hfg_header_layout_sidebar_background', 'neve_form_fields_border_color', + 'nav-icon_button_appearance', + 'nav-icon_menu_label', + 'neve_scroll_to_top_border_radius', + 'neve_scroll_to_top_padding', + 'neve_form_fields_padding', + 'neve_form_fields_spacing', + 'neve_form_fields_border_width', + 'neve_form_fields_border_radius', + 'hfg_header_layout_sidebar_width', ); /** @@ -316,6 +325,61 @@ public function test_set_theme_mod_ability_sanitizes_colors_and_backgrounds(): v $this->assertFalse( get_theme_mod( 'hfg_header_layout_sidebar_background' ) ); } + /** + * The set-theme-mod ability rejects nested or dimension values that could close their CSS declaration. + */ + public function test_set_theme_mod_ability_rejects_values_that_close_css(): void { + $ability = new Site_Set_Theme_Mod(); + $payloads = array( + 'nav-icon_button_appearance' => array( + 'type' => 'fill', + 'background' => self::PAYLOAD, + 'text' => '#fff', + ), + 'neve_scroll_to_top_border_radius' => '3;}body{background:lime}/*', + 'neve_scroll_to_top_padding' => array( 'top' => self::PAYLOAD ), + 'neve_form_fields_padding' => array( 'desktop' => array( 'top' => self::PAYLOAD ) ), + 'neve_form_fields_spacing' => self::PAYLOAD, + 'neve_form_fields_border_width' => array( 'top' => self::PAYLOAD ), + 'neve_form_fields_border_radius' => array( 'top' => self::PAYLOAD ), + 'hfg_header_layout_sidebar_width' => '{"mobile":"360;}body{background:lime}/*"}', + ); + + foreach ( $payloads as $key => $value ) { + $result = $ability->execute( + array( + 'action' => 'set', + 'key' => $key, + 'value' => $value, + ) + ); + $this->assertFalse( $result['success'], $key ); + $this->assertFalse( get_theme_mod( $key ), $key ); + } + $this->assertStringNotContainsString( 'lime', $this->get_frontend_css() ); + + $valid = array( + 'hfg_header_layout_sidebar_width' => '{"mobile":360,"tablet":360,"desktop":360}', + 'nav-icon_button_appearance' => array( + 'type' => 'fill', + 'background' => '#222222', + 'text' => '#ffffff', + ), + 'nav-icon_menu_label' => 'Menu; more', + ); + foreach ( $valid as $key => $value ) { + $result = $ability->execute( + array( + 'action' => 'set', + 'key' => $key, + 'value' => $value, + ) + ); + $this->assertTrue( $result['success'], $key ); + $this->assertSame( $value, get_theme_mod( $key ), $key ); + } + } + /** * Build the frontend inline CSS. * From 10422d2c04c3e3a7d8f7479685a92160f13acf86 Mon Sep 17 00:00:00 2001 From: girishpanchal30 Date: Fri, 20 Nov 2026 16:31:29 +0530 Subject: [PATCH 5/6] fix: enhance CSS value sanitization and validation across components --- globals/sanitize-functions.php | 67 +++++- .../Core/Components/Abstract_Component.php | 2 +- header-footer-grid/Traits/Core.php | 8 +- inc/abilities/schema_helpers.php | 4 + inc/core/styles/css_prop.php | 17 +- inc/customizer/options/typography.php | 8 +- tests/test-neve-appearance-update-fonts.php | 24 +- tests/test-neve-customizer-css-values.php | 219 ++++++++++++++++++ 8 files changed, 331 insertions(+), 18 deletions(-) create mode 100644 tests/test-neve-customizer-css-values.php diff --git a/globals/sanitize-functions.php b/globals/sanitize-functions.php index 9a0990f50d..8d55834793 100644 --- a/globals/sanitize-functions.php +++ b/globals/sanitize-functions.php @@ -245,14 +245,73 @@ function neve_sanitize_background( $value ) { return $value; } +/** + * Sanitize the values of a CSS dimension array: sides, responsive sizes and their units. + * + * Nested arrays are sanitized recursively; any other value is kept only when it is a number, + * a boolean, empty, or a plain word or unit (`px`, `auto`), and is otherwise cleared. + * + * @param array $value Dimension array. + * + * @return array + */ +function neve_sanitize_css_dimensions( array $value ) { + foreach ( $value as $key => $item ) { + if ( is_array( $item ) ) { + $value[ $key ] = neve_sanitize_css_dimensions( $item ); + continue; + } + + if ( is_string( $item ) && ! is_numeric( $item ) && ! preg_match( '/^[a-z%-]*$/i', $item ) ) { + $value[ $key ] = ''; + } + } + + return $value; +} + +/** + * Sanitize a font family name, rejecting anything that could close its CSS declaration. + * + * @param mixed $value Font family. + * + * @return string The sanitized font family, or '' when it is not a font family name. + */ +function neve_sanitize_font_family( $value ) { + if ( ! is_string( $value ) ) { + return ''; + } + + // Checked after sanitize_text_field(), which turns a lone `<` into `<`. + $value = sanitize_text_field( $value ); + + return preg_match( '/[;{}()<>]|\/\*/', $value ) ? '' : $value; +} + /** * Sanitize the button appearance control. * - * @param array $value the control value. + * @param mixed $value the control value. * - * @return array + * @return mixed The sanitized array; any other value is returned unchanged. */ function neve_sanitize_button_appearance( $value ) { + if ( ! is_array( $value ) ) { + return $value; + } + + foreach ( [ 'background', 'backgroundHover', 'text', 'textHover', 'shadowColor', 'shadowColorHover' ] as $color ) { + if ( isset( $value[ $color ] ) ) { + $value[ $color ] = neve_sanitize_colors( $value[ $color ] ); + } + } + + foreach ( [ 'borderRadius', 'borderWidth', 'shadowProperties', 'shadowPropertiesHover' ] as $dimension ) { + if ( isset( $value[ $dimension ] ) && is_array( $value[ $dimension ] ) ) { + $value[ $dimension ] = neve_sanitize_css_dimensions( $value[ $dimension ] ); + } + } + return $value; } @@ -290,9 +349,7 @@ function neve_sanitize_typography_control( $value ) { // Make sure we deal with arrays. foreach ( [ 'letterSpacing', 'lineHeight', 'fontSize' ] as $value_type ) { - if ( ! is_array( $value[ $value_type ] ) ) { - $value[ $value_type ] = []; - } + $value[ $value_type ] = is_array( $value[ $value_type ] ) ? neve_sanitize_css_dimensions( $value[ $value_type ] ) : []; } return $value; diff --git a/header-footer-grid/Core/Components/Abstract_Component.php b/header-footer-grid/Core/Components/Abstract_Component.php index 7777a28f13..97c110d6e5 100644 --- a/header-footer-grid/Core/Components/Abstract_Component.php +++ b/header-footer-grid/Core/Components/Abstract_Component.php @@ -829,7 +829,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/Traits/Core.php b/header-footer-grid/Traits/Core.php index 6542892595..dcf260abac 100644 --- a/header-footer-grid/Traits/Core.php +++ b/header-footer-grid/Traits/Core.php @@ -121,15 +121,15 @@ public function sanitize_responsive_int_json( $input ) { } /** - * Sanitize regular json. + * Sanitize a spacing array's sides and units. * - * @param string $input Input. + * @param mixed $input Input. * - * @return array + * @return array */ public function sanitize_spacing_array( $input ) { if ( is_array( $input ) ) { - return $input; + return neve_sanitize_css_dimensions( $input ); } return array(); diff --git a/inc/abilities/schema_helpers.php b/inc/abilities/schema_helpers.php index 10dd29bd8b..8ce0f971bd 100644 --- a/inc/abilities/schema_helpers.php +++ b/inc/abilities/schema_helpers.php @@ -140,6 +140,10 @@ protected function box_model_schema( $description ) { 'right' => array( 'type' => 'number' ), 'bottom' => array( 'type' => 'number' ), 'left' => array( 'type' => 'number' ), + 'unit' => array( + 'type' => 'string', + 'enum' => array( 'px', 'em', 'rem', '%' ), + ), ), ); } diff --git a/inc/core/styles/css_prop.php b/inc/core/styles/css_prop.php index 5c3e96659e..2a2d1e1024 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 self::css_unit( $suffix ); } /** @@ -57,7 +57,18 @@ 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' );; + return self::css_unit( isset( $all_value['suffix'][ $device ] ) ? $all_value['suffix'][ $device ] : ( isset( $all_value['suffix'] ) && is_string( $all_value['suffix'] ) ? $all_value['suffix'] : 'px' ) ); + } + + /** + * Keep a stored CSS unit only when it is letters or `%`, so it cannot close its declaration. + * + * @param mixed $unit Stored unit. + * + * @return string + */ + private static function css_unit( $unit ) { + return is_string( $unit ) && preg_match( '/^[a-z%]*$/i', $unit ) ? $unit : 'px'; } /** @@ -282,7 +293,7 @@ public static function get_suffix( $meta, $device, $value, $css_prop ) { Font_Manager::add_google_font( $font, strval( $value ) ); } - return $suffix === '—' ? '' : $suffix; + return $suffix === '—' ? '' : self::css_unit( $suffix ); } /** diff --git a/inc/customizer/options/typography.php b/inc/customizer/options/typography.php index 6f5d602512..5d382536b5 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( @@ -232,7 +232,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 +274,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 +355,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', diff --git a/tests/test-neve-appearance-update-fonts.php b/tests/test-neve-appearance-update-fonts.php index 530057cd78..0882817fc3 100644 --- a/tests/test-neve-appearance-update-fonts.php +++ b/tests/test-neve-appearance-update-fonts.php @@ -1,6 +1,6 @@ assertStringNotContainsString( 'lime', $this->get_frontend_css() ); } + /** + * A button radius unit outside the CSS units is rejected; a valid one is saved. + */ + public function test_button_radius_unit_accepts_only_css_units(): void { + $radius = array( + 'top' => 3, + 'right' => 3, + 'bottom' => 3, + 'left' => 3, + ); + + $result = $this->run_ability( array( 'buttons' => array( 'primary' => array( 'radius' => $radius + array( 'unit' => 'px' . self::PAYLOAD ) ) ) ) ); + $this->assertInstanceOf( WP_Error::class, $result ); + $this->assertFalse( get_theme_mod( 'neve_button_appearance' ) ); + $this->assertStringNotContainsString( 'lime', $this->get_frontend_css() ); + + $result = $this->run_ability( array( 'buttons' => array( 'primary' => array( 'radius' => $radius + array( 'unit' => 'em' ) ) ) ) ); + $this->assertIsArray( $result ); + $this->assertSame( 'em', get_theme_mod( 'neve_button_appearance' )['borderRadius']['unit'] ); + } + /** * Run the ability through the Abilities API, which validates the input schema. * diff --git a/tests/test-neve-customizer-css-values.php b/tests/test-neve-customizer-css-values.php new file mode 100644 index 0000000000..ec3508fc26 --- /dev/null +++ b/tests/test-neve-customizer-css-values.php @@ -0,0 +1,219 @@ +previous_styles = $GLOBALS['wp_styles'] ?? null; + Mods::$no_cache = true; + + $this->manager = new WP_Customize_Manager(); + Main::get_instance()->get_builder( 'header' )->customize_register( $this->manager ); + } + + /** + * Restore the replaced global state. + */ + public function tear_down(): void { + $GLOBALS['wp_styles'] = $this->previous_styles; + Mods::$no_cache = false; + + foreach ( self::TOUCHED_MODS as $mod ) { + remove_theme_mod( $mod ); + } + + parent::tear_down(); + } + + /** + * Button appearance colors and sizes keep only valid values. + */ + public function test_button_appearance_setting_keeps_only_valid_values(): void { + $value = $this->save( + 'nav-icon_button_appearance', + array( + 'type' => 'fill', + 'background' => '#222' . self::PAYLOAD, + 'text' => '#ffffff', + 'borderRadius' => array( + 'top' => self::PAYLOAD, + 'right' => 3, + 'bottom' => 3, + 'left' => 3, + ), + ) + ); + + $this->assertSame( '', $value['background'] ); + $this->assertSame( '#ffffff', $value['text'] ); + $this->assertSame( '', $value['borderRadius']['top'] ); + $this->assertSame( 3, $value['borderRadius']['left'] ); + $this->assertStringNotContainsString( 'lime', $this->get_frontend_css() ); + } + + /** + * Component typography sizes keep only numbers and units. + */ + public function test_component_typography_setting_keeps_only_numbers_and_units(): void { + $value = $this->save( + 'primary-menu_component_typeface', + array( + 'fontWeight' => '400', + 'textTransform' => 'none', + 'lineHeight' => array( + 'mobile' => self::PAYLOAD, + 'tablet' => '1.6', + 'suffix' => array( + 'mobile' => 'em', + 'tablet' => 'em;}', + ), + ), + 'letterSpacing' => array(), + 'fontSize' => array(), + ) + ); + + $this->assertSame( '', $value['lineHeight']['mobile'] ); + $this->assertSame( '1.6', $value['lineHeight']['tablet'] ); + $this->assertSame( 'em', $value['lineHeight']['suffix']['mobile'] ); + $this->assertSame( '', $value['lineHeight']['suffix']['tablet'] ); + $this->assertStringNotContainsString( 'lime', $this->get_frontend_css() ); + } + + /** + * Component padding and margin keep only numbers and units. + */ + public function test_component_spacing_settings_keep_only_numbers_and_units(): void { + foreach ( array( 'primary-menu_component_padding', 'primary-menu_component_margin' ) as $setting ) { + $value = $this->save( + $setting, + array( + 'desktop' => array( + 'top' => self::PAYLOAD, + 'right' => 10, + 'bottom' => '', + 'left' => 'auto', + ), + 'desktop-unit' => 'px;}', + ) + ); + + $this->assertSame( '', $value['desktop']['top'], $setting ); + $this->assertSame( 10, $value['desktop']['right'], $setting ); + $this->assertSame( 'auto', $value['desktop']['left'], $setting ); + $this->assertSame( '', $value['desktop-unit'], $setting ); + } + + $this->assertStringNotContainsString( 'lime', $this->get_frontend_css() ); + } + + /** + * Component and global font families that could close their declaration save empty. + */ + public function test_font_family_settings_reject_declaration_breaking_names(): void { + $this->assertSame( '', $this->save( 'primary-menu_component_font_family', 'Arial' . self::PAYLOAD ) ); + $this->assertSame( '', $this->save( 'primary-menu_component_font_family', 'xassertSame( 'Open Sans', $this->save( 'primary-menu_component_font_family', 'Open Sans' ) ); + + $this->assertSame( '', neve_sanitize_font_family( 'Arial;}body{background:lime}/*' ) ); + $this->assertSame( 'Arial, Helvetica, sans-serif', neve_sanitize_font_family( 'Arial, Helvetica, sans-serif' ) ); + } + + /** + * A stored unit that is not a CSS unit prints as px. + */ + public function test_stored_unit_cannot_close_declaration(): void { + set_theme_mod( + 'neve_button_appearance', + array( + 'type' => 'fill', + 'borderRadius' => array( + 'top' => 3, + 'right' => 3, + 'bottom' => 3, + 'left' => 3, + 'unit' => 'px;}body{background:lime}/*', + ), + ) + ); + + $css = $this->get_frontend_css(); + + $this->assertStringNotContainsString( 'lime', $css ); + $this->assertStringContainsString( '--primarybtnborderradius:3px;', $css ); + } + + /** + * Sanitize a value through its registered Customizer setting and save it. + * + * @param string $id Setting id. + * @param mixed $value Incoming value. + * + * @return mixed The saved value. + */ + private function save( string $id, $value ) { + $setting = $this->manager->get_setting( $id ); + $this->assertInstanceOf( WP_Customize_Setting::class, $setting, $id ); + + set_theme_mod( $id, $setting->sanitize( $value ) ); + + return get_theme_mod( $id ); + } + + /** + * Build the frontend inline CSS. + * + * @return string + */ + private function get_frontend_css(): string { + $GLOBALS['wp_styles'] = new WP_Styles(); + wp_register_style( Dynamic_Css::FRONTEND_HANDLE, false ); + + ( new Dynamic_Css() )->enqueue(); + + return implode( '', (array) wp_styles()->get_data( Dynamic_Css::FRONTEND_HANDLE, 'after' ) ); + } +} From a86a3f45b72da0008212cb3c86d8d12bf46af1fd Mon Sep 17 00:00:00 2001 From: girishpanchal30 Date: Fri, 20 Nov 2026 17:11:46 +0530 Subject: [PATCH 6/6] fix: enhance CSS unit validation to include "no unit" option --- globals/sanitize-functions.php | 4 +- inc/core/styles/css_prop.php | 4 +- tests/test-neve-customizer-css-values.php | 47 +++++++++++++++++++++++ 3 files changed, 51 insertions(+), 4 deletions(-) diff --git a/globals/sanitize-functions.php b/globals/sanitize-functions.php index 8d55834793..5636e3c7a9 100644 --- a/globals/sanitize-functions.php +++ b/globals/sanitize-functions.php @@ -249,7 +249,7 @@ function neve_sanitize_background( $value ) { * Sanitize the values of a CSS dimension array: sides, responsive sizes and their units. * * Nested arrays are sanitized recursively; any other value is kept only when it is a number, - * a boolean, empty, or a plain word or unit (`px`, `auto`), and is otherwise cleared. + * a boolean, empty, a plain word or unit (`px`, `auto`) or the controls' `—` "no unit" choice, and is otherwise cleared. * * @param array $value Dimension array. * @@ -262,7 +262,7 @@ function neve_sanitize_css_dimensions( array $value ) { continue; } - if ( is_string( $item ) && ! is_numeric( $item ) && ! preg_match( '/^[a-z%-]*$/i', $item ) ) { + if ( is_string( $item ) && ! is_numeric( $item ) && ! preg_match( '/^(?:[a-z%-]*|—)$/iu', $item ) ) { $value[ $key ] = ''; } } diff --git a/inc/core/styles/css_prop.php b/inc/core/styles/css_prop.php index 2a2d1e1024..1b649c33bb 100644 --- a/inc/core/styles/css_prop.php +++ b/inc/core/styles/css_prop.php @@ -61,14 +61,14 @@ public static function get_suffix_responsive( $meta, $device ) { } /** - * Keep a stored CSS unit only when it is letters or `%`, so it cannot close its declaration. + * Keep a stored CSS unit only when it is letters, `%` or the `—` "no unit" choice, so it cannot close its declaration. * * @param mixed $unit Stored unit. * * @return string */ private static function css_unit( $unit ) { - return is_string( $unit ) && preg_match( '/^[a-z%]*$/i', $unit ) ? $unit : 'px'; + return is_string( $unit ) && preg_match( '/^(?:[a-z%]*|—)$/iu', $unit ) ? $unit : 'px'; } /** diff --git a/tests/test-neve-customizer-css-values.php b/tests/test-neve-customizer-css-values.php index ec3508fc26..9bca2ab068 100644 --- a/tests/test-neve-customizer-css-values.php +++ b/tests/test-neve-customizer-css-values.php @@ -8,6 +8,7 @@ use HFG\Main; use Neve\Core\Dynamic_Css; use Neve\Core\Settings\Mods; +use Neve\Core\Styles\Css_Prop; /** * Class TestNeveCustomizerCssValues @@ -23,6 +24,7 @@ class TestNeveCustomizerCssValues extends WP_UnitTestCase { 'primary-menu_component_margin', 'primary-menu_component_font_family', 'neve_button_appearance', + 'neve_test_css_unit', ); /** @@ -186,6 +188,51 @@ public function test_stored_unit_cannot_close_declaration(): void { $this->assertStringContainsString( '--primarybtnborderradius:3px;', $css ); } + /** + * The typeface control's "No unit" line height choice is saved and printed without a unit. + */ + public function test_no_unit_line_height_is_kept(): void { + $value = $this->save( + 'primary-menu_component_typeface', + array( + 'fontWeight' => '400', + 'textTransform' => 'none', + 'lineHeight' => array( + 'mobile' => '1.6', + 'tablet' => '1.6', + 'desktop' => '1.6', + 'suffix' => array( + 'mobile' => '—', + 'tablet' => '—', + 'desktop' => '—', + ), + ), + 'letterSpacing' => array(), + 'fontSize' => array(), + ) + ); + + $this->assertSame( '—', $value['lineHeight']['suffix']['mobile'] ); + + $css = $this->get_frontend_css(); + $this->assertStringContainsString( '--lineheight:1.6;', str_replace( ' ', '', $css ) ); + $this->assertStringNotContainsString( '--lineheight:1.6px', str_replace( ' ', '', $css ) ); + } + + /** + * The responsive unit getters print a stored unit that is not a CSS unit as px. + */ + public function test_responsive_unit_getters_reject_stored_units(): void { + set_theme_mod( 'neve_test_css_unit', array( 'desktop-unit' => 'px;}body{background:lime}/*' ) ); + $this->assertSame( 'px', Css_Prop::get_unit_responsive( array( 'key' => 'neve_test_css_unit' ), 'desktop' ) ); + + set_theme_mod( 'neve_test_css_unit', array( 'suffix' => array( 'desktop' => 'px;}body{background:lime}/*' ) ) ); + $this->assertSame( 'px', Css_Prop::get_suffix_responsive( array( 'key' => 'neve_test_css_unit' ), 'desktop' ) ); + + set_theme_mod( 'neve_test_css_unit', array( 'suffix' => array( 'desktop' => 'em' ) ) ); + $this->assertSame( 'em', Css_Prop::get_suffix_responsive( array( 'key' => 'neve_test_css_unit' ), 'desktop' ) ); + } + /** * Sanitize a value through its registered Customizer setting and save it. *