ReviewOS

stacks/stx

@else before a builtin component emits a raw NUL byte into the response, making the page invalid UTF-8 (and silencing grep)

#1816
Closed glennmichael123 opened this 22 days ago · 1 comment
22 days ago

Verified against the fork at main (dc048673a3), rendered through renderView — no dev server, no app.

Symptom

A signal-driven @if/@else chain around a builtin component writes stx's internal sentinel, including a literal NUL byte, into the response:

<a @else="&#0;stx-boolean-attribute" href="/pricing" class="b" data-stx-link ...>

The response stops being valid UTF-8. That matters more than the stray attribute:

  • file reports the page as data rather than HTML document text.
  • grep goes silent on it. It treats the file as binary, so grep -c prints nothing at all — not 0, nothing. Any verification step that greps rendered HTML returns an empty string and reads as a pass. I only noticed because a count column in my own check came back blank instead of 0.

Minimal reproduction

Two files, no app:

{{-- page.stx --}}
<script client>
const pro = state(false)
</script>
<div>
@if (pro())
  <StxLink to="/pricing" class="a">Manage</StxLink>
@else
  <StxLink to="/pricing" class="b">Upgrade</StxLink>
@endif
</div>
const { renderView } = await import('./dist/build-views.js')
const html = await renderView('page.stx', {}, { componentsDir: './components' })
console.log('NUL bytes:', (html.match(/\0/g) || []).length)
console.log(html.match(/<a [^>]*@else[^>]*>/g))
NUL bytes: 1
anchors carrying @else: 1
  "<a @else=\"stx-boolean-attribute\" href=\"/pricing\" class=\"b\" data-stx-link ...>"

Both conditions are required, and each one alone renders clean:

variantNUL bytes
@if (pro()) signal-driven + <StxLink> builtin1
@if (pro) plain server value + <StxLink>0
@if (pro()) signal-driven + a user component (<Btn>)0
@if/@else around plain <a>0

The server-value case never reaches this code — the chain is resolved during render and the branch is simply dropped. Only a signal-driven chain is rewritten into element-level directives, which is where the sentinel gets a chance to escape.

Cause

signal-processing.ts turns a signal-driven chain into element attributes, documented at signal-processing.ts:428-433:

@if(a()) <p>A</p> @elseif(b()) <p>B</p> @else <p>C</p> @endif
  ->  <p @if="a()">A</p>
      <p @else-if="b()">B</p>
      <p @else>C</p>

@else is correctly valueless there. When that element is a component, parseAllAttributes records a valueless attribute as the sentinel (component-renderer.ts:371):

else {
  // Boolean attribute
  props[name] = BOOLEAN_ATTRIBUTE_SENTINEL   // '\0stx-boolean-attribute'
}

Then component-renderer.ts:154 classifies every @-prefixed attribute as an event handler and stores the value verbatim:

// --- Event attributes: @click, @submit.prevent, etc. ---
if (attrName.startsWith('@')) {
  resolved.events[attrName] = attrValue
  continue
}

The comment names the intent — @click, @submit.prevent — but the structural directives @if / @else-if / @else land here too. They are not events. A builtin that forwards unconsumed props onto real markup then emits the sentinel as an attribute value.

Two sibling branches already handle the sentinel (:173 for events resolved as props, :283 mapping it to 'true' for plain static attributes). The path that forwards @-attributes to markup does not.

Why @if survives and only @else breaks

@if carries a real expression, so events['@if'] = 'pro()' and it emits @if="pro()" — which is exactly what the runtime wants, so it works by luck rather than by design. @else carries no value, so it gets the sentinel. From the same rendered page:

<a @if="pro()" href="/pricing" class="a" data-stx-link ...>          <!-- fine -->
<a @else="&#0;stx-boolean-attribute" href="/pricing" class="b" ...>  <!-- broken -->

There is an ALPINE_PASSTHROUGH set (component-renderer.ts:103) listing x-if, x-for, x-show … so the x- structural family is explicitly protected. There is no equivalent for the @ structural family.

BOOLEAN_ATTRIBUTE_SENTINEL has no test referencing it — grep -rn 'BOOLEAN_ATTRIBUTE_SENTINEL\|stx-boolean-attribute' packages/stx/test returns nothing.

How it shows up in practice

The authored source is idiomatic and gives no hint:

@if (pro())
  <StxLink to="/pricing" class="btn btn-ghost">Manage plan</StxLink>
@else
  <StxLink to="/pricing" class="btn btn-primary">Upgrade to Pro</StxLink>
@endif

I hit this converting plain anchors to StxLink across an app. The same conditional was correct for as long as it wrapped <a>, and became a malformed, non-UTF-8 response the moment the branches became a builtin component — with no error, no warning, and a page that still looks right.

Workaround

Avoid @else immediately before a component; two @if blocks render the same states:

@if (pro())
  <StxLink to="/pricing" class="btn btn-ghost">Manage plan</StxLink>
@endif
@if (!pro())
  <StxLink to="/pricing" class="btn btn-primary">Upgrade to Pro</StxLink>
@endif

Verified: 0 NUL bytes and 0 stray @else across all 14 routes of the app, both states still rendered, both links still routed.

Suggested fix

I have not written a patch, so treating this as a report rather than asserting the shape. The narrow fix is to stop classifying structural directives as events at :154 — route @if / @else-if / @else to passthrough the way ALPINE_PASSTHROUGH handles the x- family, and emit a valueless attribute rather than the sentinel.

Worth considering separately: the sentinel is a raw NUL, so any path that fails to strip it produces an invalid response and silences downstream text tooling. A sentinel that cannot appear verbatim in valid output — or an assertion at emit time that no \0 reaches the wire — would turn this class of bug from silent into loud.

22 days ago

Fixed in ae82914e9a.

Root cause is one step further along than the issue traces it. Every @-prefixed attribute is classified as an event, so when the signal-driven chain is rewritten into element-level directives, @else arrives at the builtin as a bare attribute and the event-forwarding path emits it verbatim — sentinel and NUL included. (forwardStaticAttrs had the identical trap independently: the sentinel is a string, so its typeof value === 'boolean' check missed it. Both fixed.)

Forwarded @-attributes now render bare when they carry the sentinel, so @else survives as the directive the runtime binds off — dropping it would have been the wrong fix.

Your point about grep going silent is the reason I also added a stripControlChars guard on forwarded values rather than just fixing the one path. One escaped NUL costs the entire document's validity and makes verification steps read as passing; the check costs nothing.

Your variant table was exactly right and is now the test matrix — signal-driven + builtin is the only combination that reached it, and all four cases are pinned. 4 of 10 tests fail without the fix.

Sign in to comment on this issue.