Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 62 additions & 5 deletions globals/sanitize-functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -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, a plain word or unit (`px`, `auto`) or the controls' `—` "no unit" choice, and is otherwise cleared.
*
* @param array<mixed> $value Dimension array.
*
* @return array<mixed>
*/
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%-]*|—)$/iu', $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 `&lt;`.
$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;
}

Expand Down Expand Up @@ -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;
Expand Down
41 changes: 38 additions & 3 deletions header-footer-grid/Core/Builder/Abstract_Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'],
]
);
Expand Down Expand Up @@ -1039,6 +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'],
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.
Expand All @@ -1061,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 ),
],
]
);
Expand All @@ -1071,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',
Expand Down Expand Up @@ -2054,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' ),
Expand All @@ -2074,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
*
Expand Down Expand Up @@ -2274,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 ) );
},
],
],
];
Expand Down
2 changes: 1 addition & 1 deletion header-footer-grid/Core/Components/Abstract_Component.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' => [
Expand Down
8 changes: 4 additions & 4 deletions header-footer-grid/Traits/Core.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<mixed>
*/
public function sanitize_spacing_array( $input ) {
if ( is_array( $input ) ) {
return $input;
return neve_sanitize_css_dimensions( $input );
}

return array();
Expand Down
47 changes: 47 additions & 0 deletions inc/abilities/abstract_ability.php
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,53 @@ protected function validation_issue( $code, $message, $field = '' ) {
);
}

/**
* 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. 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 ) ) {
$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 $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;
}

/**
* Build a structured non-validation error payload for clients that collapse
* WP_Error details into a generic transport error.
Expand Down
11 changes: 9 additions & 2 deletions inc/abilities/appearance_update.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ class Appearance_Update extends Abstract_Ability {
'extra_2' => 'nv-c-2',
);

/**
* Font family names without the characters that could close their CSS declaration.
*/
const FONT_FAMILY_PATTERN = '^[^;{}()/<]*$';

/**
* Container style contexts mapped to their theme_mods.
*/
Expand Down Expand Up @@ -117,10 +122,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',
Expand All @@ -133,7 +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' ),
'fontWeight' => $this->font_weight_schema(),
'textTransform' => array(
'type' => 'string',
'enum' => array( 'none', 'capitalize', 'uppercase', 'lowercase' ),
Expand Down Expand Up @@ -236,7 +243,7 @@ private function button_schema() {
),
),
'fontSize' => $this->responsive_value_schema( 'Font size per device.' ),
'fontWeight' => array( 'type' => 'string' ),
'fontWeight' => $this->font_weight_schema(),
'textTransform' => array(
'type' => 'string',
'enum' => array( 'none', 'capitalize', 'uppercase', 'lowercase' ),
Expand Down
52 changes: 33 additions & 19 deletions inc/abilities/builder_configure.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

namespace Neve\Abilities;

use HFG\Core\Builder\Abstract_Builder;

/**
* Trait Builder_Configure
*
Expand All @@ -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' ) ) {
Expand All @@ -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;
}

/**
Expand Down Expand Up @@ -172,29 +174,41 @@ 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<mixed> $style Style block.
* @return string[] Affected theme_mod keys.
* @param Abstract_Builder $builder The builder instance.
* @param string $row Row key.
* @param array<mixed> $style Style block.
* @return list<string> 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';
}
if ( isset( $style['background'] ) ) {
set_theme_mod( $prefix . '_background', map_deep( $style['background'], 'sanitize_text_field' ) );
$affected[] = $prefix . '_background';
set_theme_mod( $prefix . '_height', $builder->sanitize_responsive_int_json( $style['height'] ) );
}
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 ) {
$warnings[] = sprintf(
/* translators: %s: input field name */
__( 'Invalid value for "%s".', 'neve' ),
'row_style.' . $row . '.' . $field
);
continue;
}
Comment thread
pirate-bot marked this conversation as resolved.
Resolved

set_theme_mod( $key, $value );
}

return $affected;
return $warnings;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion inc/abilities/footer_configure.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 ) );
}
}
}
Expand Down
Loading
Loading