Make WordPress Core

#65740 assigned defect (bug)

Interactivity API can fatal error when attempting to bind a non-scalar value to an HTML attribute

Reported by: westonruter Owned by: westonruter
Priority: normal Milestone: 7.1
Component: Interactivity API Version: 6.5
Severity: normal Keywords:
Cc: Focuses:

Description

This is split out from the PHPStan improvements in #64898.

In looking at WP_Interactivity_API::data_wp_bind_processor() for PHPStan issues, I found that a mixed value was being passed into WP_HTML_Tag_Processor::set_attribute(). When the Interactivity state has an array value for some state foo, and if foo is bound to an href attribute, then this causes a fatal error because the array is passed into esc_url() and then into ltrim().

Attempting to bind a non-scalar or non-null value to an attribute should be ignored and emit a _doing_it_wrong().

If you try using the following at post_content on Playground, for example setting an array into an href attribute:

```html
<!-- wp:accordion -->
<div class="wp-block-accordion"><span data-wp-interactive="myPlugin" data-wp-context='{"list":[1,2,3]}'><a data-wp-bind--href="context.list">Boom</a></span></div>
<!-- /wp:accordion -->
```

You'll see a fatal error in the console:

PHP Fatal error: Uncaught TypeError: ltrim(): Argument #1 ($string) must be of type string, array given in /wordpress/wp-includes/formatting.php

```html
<!-- wp:accordion -->
<div class="wp-block-accordion"><span data-wp-interactive="myPlugin" data-wp-context='{"list":[1,2,3]}'><a data-wp-bind--href="context.list">Boom</a></span></div>
<!-- /wp:accordion -->
```

And if you do the same for a non-href attribute:

```html
<!-- wp:accordion -->
<div class="wp-block-accordion"><span data-wp-interactive="myPlugin" data-wp-context='{"list":[1,2,3]}' data-wp-bind--id="context.list">Boom</span></div>
<!-- /wp:accordion -->
```

You'll get similar:

PHP Fatal error: Uncaught TypeError: strtr(): Argument #1 ($string) must be of type string, array given in /wordpress/wp-includes/html-api/class-wp-html-tag-processor.php

Change History (0)

Note: See TracTickets for help on using tickets.