Skip to content

[Turbo] Reject invalid attribute names in TurboStream::action() - #4375

Open
Kocal wants to merge 1 commit into
symfony:2.xfrom
Kocal:turbo/stream-attribute-names
Open

Kocal wants to merge 1 commit into
symfony:2.xfrom
Kocal:turbo/stream-attribute-names

Conversation

@Kocal

@Kocal Kocal commented Nov 26, 2026

Copy link
Copy Markdown
Member
Q A
Bug fix? yes
New feature? no
Deprecations? no
Documentation? no
Issues
License MIT

TurboStream::action() escaped attribute names with htmlspecialchars(), which keeps spaces and =. A name such as a=b c was rendered as several attributes on the <turbo-stream> element.

Each name is now checked against an HTML attribute name pattern (letters, digits, _, :, ., @ and -, not starting with a digit or -), and an \InvalidArgumentException is thrown otherwise. Names like data-foo, aria-hidden, x:y and @click still render. Values are escaped as before.

Before After
['a=b c' => 'x'] renders a=b c="x" throws \InvalidArgumentException

Merge-up to 3.x: the same foreach body applies unchanged (replace the $key = htmlspecialchars($key); line). In the test, use : void on the new test methods and #[DataProvider('customThrowsExceptionOnInvalidAttributeNameDataProvider')] instead of the @dataProvider annotation. The CHANGELOG entry goes under the unreleased 3.x version heading. Not run on 3.x.

| Q              | A
| -------------- | ---
| Bug fix?       | yes
| New feature?   | no
| Deprecations?  | no
| Documentation? | no
| Issues         |
| License        | MIT

`TurboStream::action()` escaped attribute names with `htmlspecialchars()`, which keeps spaces and `=`. A name such as `a=b c` was rendered as several attributes on the `<turbo-stream>` element.

Each name is now checked against an HTML attribute name pattern (letters, digits, `_`, `:`, `.`, `@` and `-`, not starting with a digit or `-`), and an `\InvalidArgumentException` is thrown otherwise. Names like `data-foo`, `aria-hidden`, `x:y` and `@click` still render. Values are escaped as before.

| Before | After |
| ------ | ----- |
| `['a=b c' => 'x']` renders `a=b c="x"` | throws `\InvalidArgumentException` |

Merge-up to 3.x: the same `foreach` body applies unchanged (replace the `$key = htmlspecialchars($key);` line). In the test, use `: void` on the new test methods and `#[DataProvider('customThrowsExceptionOnInvalidAttributeNameDataProvider')]` instead of the `@dataProvider` annotation. The CHANGELOG entry goes under the unreleased 3.x version heading. Not run on 3.x.
@carsonbot carsonbot added Bug Bug Fix Turbo Status: Needs Review Needs to be reviewed labels Nov 26, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Bug Bug Fix Status: Needs Review Needs to be reviewed Turbo

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants