Skip to content

Keep bound attributes escaped in @unblaze scopes of folded components - #243

Merged
ganyicz merged 6 commits into
mainfrom
escape-unblaze-scope-attributes
Nov 3, 2026
Merged

ganyicz merged 6 commits into
mainfrom
escape-unblaze-scope-attributes

Conversation

@ganyicz

@ganyicz ganyicz commented Nov 3, 2026 •

Copy link
Copy Markdown
Collaborator

The scenario

A bound attribute that flux:input passes on to its error message is printed unescaped, so user input can break out of the attribute:

<flux:input label="Email" :error:aria-label="$value" />

With $value = 'Use "work" email':

<!-- Blade -->
<div role="alert" ... aria-label="Use &quot;work&quot; email" data-flux-error>

<!-- Blaze -->
<div role="alert" ... aria-label="Use \"work\" email" data-flux-error>

The same happens to avatar:* attributes on flux:select.option in Flux Pro, e.g. :avatar:aria-label="$value".

The problem

Folding puts every dynamic value in an @unblaze scope back as the raw expression, even values from $attributes, which Blade escapes.

While folding, Blaze renders the component once with placeholders instead of dynamic values, then swaps the original expressions back in. In HTML it swaps in {{ $value }}, which escapes. But an @unblaze scope is written into the compiled view as PHP, and there it swaps in the raw expression:

{{-- Rendered with a placeholder --}}
<div aria-label="BLAZE_PLACEHOLDER_0_">
<?php $scope = array ('aria-label' => 'BLAZE_PLACEHOLDER_0_'); ?>

{{-- Swapped back --}}
<div aria-label="{{ $value }}">
<?php $scope = array ('aria-label' => $value); ?>

flux:with-field passes the error:* attributes from its $attributes to flux:error through a scope:

@unblaze(scope: ['attributes' => $errorAttributes->getAttributes()])
<flux:error :attributes="new \Illuminate\View\ComponentAttributeBag($scope['attributes'])" />
@endunblaze

When flux:input is folded, that scope compiles to:

<?php $scope = array ('attributes' => array ('name' => NULL, 'aria-label' => $value)); ?>

In Blade, $value would reach flux:error already escaped, but because folding swapped the placeholder for the raw expression, the escaping was lost.

The solution

Folding now gives props and $attributes separate placeholders. For the input above, the component is rendered with:

<?php
$data = ['label' => 'Email', 'error:ariaLabel' => 'BLAZE_PLACEHOLDER_PROP_0_'];
$attributes = ['label' => 'Email', 'error:aria-label' => 'BLAZE_PLACEHOLDER_ATTR_1_'];

Inside PHP code, a bound ATTR placeholder is restored escaped, like Blade does it:

<?php
// Like Blade, the attribute bag holds bound attributes escaped
if ($attribute->bound() && str_starts_with($placeholder, 'BLAZE_PLACEHOLDER_ATTR_')) {
    $value = '\Illuminate\View\Compilers\BladeCompiler::sanitizeComponentAttribute(' . $value . ')';
}

So the scope from flux:with-field now compiles to:

<?php $scope = array ('attributes' => array ('name' => NULL, 'aria-label' => \Illuminate\View\Compilers\BladeCompiler::sanitizeComponentAttribute($value))); ?>

Props stay raw, and placeholders in HTML still become {{ $value }}.

Added Flux and Flux Pro tests that compare both cases above with Blade.

Related: #222.

@github-actions

github-actions Bot commented Nov 3, 2026 •

Copy link
Copy Markdown
Contributor

Benchmark Result: Default

Attempt Blade Blaze Change
#1 42.19ms 6.99ms 83.4%
#2 42.59ms 6.98ms 83.6%
#3 43.10ms 6.88ms 84%
#4 * 42.28ms 7.19ms 83%
#5 43.60ms 7.16ms 83.6%
#6 43.87ms 6.96ms 84.1%
#7 42.05ms 7.00ms 83.4%
#8 41.31ms 6.80ms 83.5%
#9 42.09ms 6.85ms 83.7%
#10 42.30ms 6.95ms 83.6%
Snapshot 43.58ms 7.11ms 83.7%
Result 42.30ms (-2.9%) 6.96ms (-2.1%) 83.5% (~)

Median of 10 attempts (* = outlier, excluded from result), 5000 iterations x 10 rounds, 9.53s total

To run a specific benchmark, comment /benchmark <name>
attributes, aware, class, default, forwarding, forwarding-merge, merge, named-slots, no-attributes, slot, compilation

@ganyicz
ganyicz merged commit f1d5e78 into main Nov 3, 2026
4 checks passed
@ganyicz
ganyicz deleted the escape-unblaze-scope-attributes branch November 3, 2026 12:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant