Skip to content

Commit

Permalink
HTML API: Guard against non-string attribute values.
Browse files Browse the repository at this point in the history
Because there are three ways for an attribute in HTML to exist, the HTML API
reports three kinds of values for `get_attribute()` calls:
 - `null` means that no attribute exists of the given name
 - `true` means that the attribute exists but there is no value, e.g. '<input checked>'.
 - a string value means that the attribute exists and has a value, e.g. '<img src="test">'.

When operating on the value returned by `get_attribute()` then it's important to
ensure that it's a string value before treating it as one. A call to `empty()` is not
enough because a boolean attribute, being `true`, does not return `false` for `empty()`.

In this patch blocks that read and then use attribute values as strings have been updated
in order to guard against cases where the attribute might not be the string the code
expects.
  • Loading branch information
dmsnell committed Oct 15, 2023
1 parent 8fa0d97 commit 79a58a1
Show file tree
Hide file tree
Showing 7 changed files with 9 additions and 4 deletions.
4 changes: 2 additions & 2 deletions lib/experimental/interactivity-api/directive-processing.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,11 +136,11 @@ function gutenberg_interactivity_evaluate_reference( $path, array $context = arr
);

/*
* Check first if the directive path is preceded by a negator operator (!),
* Check first if the directive path is preceded by a negation operator (!),
* indicating that the value obtained from the Interactivity Store (or the
* passed context) using the subsequent path should be negated.
*/
$should_negate_value = '!' === $path[0];
$should_negate_value = strlen( $path ) > 0 && '!' === $path[0];

$path = $should_negate_value ? substr( $path, 1 ) : $path;
$path_segments = explode( '.', $path );
Expand Down
1 change: 1 addition & 0 deletions lib/experimental/interactivity-api/directives/wp-bind.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ function gutenberg_interactivity_process_wp_bind( $tags, $context ) {
}

$expr = $tags->get_attribute( $attr );
$expr = is_string( $expr ) ? $expr : '';
$value = gutenberg_interactivity_evaluate_reference( $expr, $context->get_context() );
$tags->set_attribute( $bound_attr, $value );
}
Expand Down
1 change: 1 addition & 0 deletions lib/experimental/interactivity-api/directives/wp-class.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ function gutenberg_interactivity_process_wp_class( $tags, $context ) {
}

$expr = $tags->get_attribute( $attr );
$expr = is_string( $expr ) ? $expr : '';
$add_class = gutenberg_interactivity_evaluate_reference( $expr, $context->get_context() );
if ( $add_class ) {
$tags->add_class( $class_name );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ function gutenberg_interactivity_process_wp_context( $tags, $context ) {
}

$value = $tags->get_attribute( 'data-wp-context' );
if ( null === $value ) {
if ( ! is_string( $value ) || empty( $value ) ) {
// No data-wp-context directive.
return;
}
Expand Down
2 changes: 2 additions & 0 deletions lib/experimental/interactivity-api/directives/wp-style.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,11 @@ function gutenberg_interactivity_process_wp_style( $tags, $context ) {
}

$expr = $tags->get_attribute( $attr );
$expr = is_string( $expr ) ? $expr : '';
$style_value = gutenberg_interactivity_evaluate_reference( $expr, $context->get_context() );
if ( $style_value ) {
$style_attr = $tags->get_attribute( 'style' );
$style_attr = is_string( $style_value ) ? $style_attr : '';
$style_attr = gutenberg_interactivity_set_style( $style_attr, $style_name, $style_value );
$tags->set_attribute( 'style', $style_attr );
} else {
Expand Down
2 changes: 1 addition & 1 deletion lib/experimental/interactivity-api/directives/wp-text.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ function gutenberg_interactivity_process_wp_text( $tags, $context ) {
}

$value = $tags->get_attribute( 'data-wp-text' );
if ( null === $value ) {
if ( ! is_string( $value ) || empty( $value ) ) {
return;
}

Expand Down
1 change: 1 addition & 0 deletions packages/block-library/src/cover/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ function render_block_core_cover( $attributes, $content ) {
$processor->next_tag();

$styles = $processor->get_attribute( 'style' );
$styles = is_string( $styles ) ? $styles : '';
$merged_styles = ! empty( $styles ) ? $styles . ';' : '';
$merged_styles .= 'background-image:url(' . esc_url( $current_featured_image ) . ');';

Expand Down

0 comments on commit 79a58a1

Please sign in to comment.