Skip to content

Escape props bound to child attributes in folded components - #244

Merged
ganyicz merged 4 commits into
mainfrom
escape-props-bound-to-child-attributes
Nov 3, 2026
Merged

ganyicz merged 4 commits into
mainfrom
escape-props-bound-to-child-attributes

Conversation

@ganyicz

@ganyicz ganyicz commented Nov 3, 2026 •

Copy link
Copy Markdown
Collaborator

The scenario

A folded wrapper passes a prop to a Flux input's error attribute, and the quotes reach the HTML unescaped.

{{-- components/email-field.blade.php --}}
@blaze(fold: true, safe: ['errorLabel'])
@props(['errorLabel'])

<flux:input label="Email" :error:aria-label="$errorLabel" />
<x-email-field :error-label="$value" />
<!-- $value = 'Use "work" email' -->

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

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

The wrapper looks harmless. Marking a prop as safe when it's only passed through to another component is a natural thing to do.

The problem

Placeholders record how a value enters the folded component: a PROP placeholder is restored raw and an ATTR placeholder is restored escaped. Blade changes this along the way. A prop bound to a child component's attribute gets escaped, so it's no longer raw.

While folding, that escaping does nothing to a placeholder string:

  1. The wrapper's $errorLabel holds a PROP placeholder.
  2. :error:aria-label="$errorLabel" passes it through sanitizeComponentAttribute(), which leaves it unchanged.
  3. Flux's with-field passes the error attributes through @unblaze(scope: ...).
  4. Folding restores the PROP placeholder in the scope as the raw $errorLabel.

Placeholders that end up in HTML are always restored escaped, so only @unblaze scopes are affected. In Flux these are the error attributes of fields and the avatar attributes of Flux Pro select options.

The solution

While folding, escaping an attribute turns its PROP placeholder into the ATTR placeholder of the same attribute. The existing restoration then outputs it escaped, like Blade.

<?php
public static function sanitizeFoldedAttribute(mixed $value): mixed
{
    $sanitized = BladeCompiler::sanitizeComponentAttribute($value);

    // Like Blade, trusted HTML stays unescaped, so its placeholder stays raw
    if ($value instanceof Htmlable || ! is_string($sanitized)) {
        return $sanitized;
    }

    return str_replace('BLAZE_PLACEHOLDER_PROP_', 'BLAZE_PLACEHOLDER_ATTR_', $sanitized);
}

A value wrapped in HtmlString keeps its PROP placeholder, since Blade doesn't escape trusted HTML either.

  • Foldable also registers an ATTR placeholder with the same index for every PROP placeholder, so a converted placeholder always has an attribute to restore.
  • While folding, a precompiler replaces calls to Blade's sanitizeComponentAttribute() with this method. It runs after component tags are compiled, so it covers attributes of Blaze components, their slots, and plain Blade components. mergeAttributes() uses the method as well when a non-bag value is passed as :attributes.

Restoration, the HTML output and existing test expectations don't change.

@github-actions

github-actions Bot commented Nov 3, 2026 •

Copy link
Copy Markdown
Contributor

Benchmark Result: Default

Attempt Blade Blaze Change
#1 * 99.59ms 12.67ms 87.3%
#2 100.82ms 12.19ms 87.9%
#3 98.87ms 12.19ms 87.7%
#4 99.79ms 12.23ms 87.7%
#5 98.80ms 12.13ms 87.7%
#6 * 95.15ms 12.15ms 87.2%
#7 100.70ms 12.29ms 87.8%
#8 99.36ms 12.29ms 87.6%
#9 99.16ms 12.19ms 87.7%
#10 * 99.66ms 12.51ms 87.4%
Snapshot 97.50ms 12.30ms 87.4%
Result 99.36ms (~) 12.19ms (~) 87.7% (+0.3%)

Median of 10 attempts (* = outlier, excluded from result), 5000 iterations x 10 rounds, 17.91s 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 fb1292c into main Nov 3, 2026
4 checks passed
@ganyicz
ganyicz deleted the escape-props-bound-to-child-attributes branch November 3, 2026 14:25
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