Skip to content

The browser and XSS ​

Already in place ​

PathDefense
MiniJinja templates.html templates auto-escape {{ }}; only `
Component buildersText always goes through escape (the same five characters as PHP's e())
attrs / AttributesAttribute values are escaped; names must match ^[A-Za-z_:][A-Za-z0-9:._-]*$
IconOnly registered names are accepted, and registration takes only path d values, so arbitrary SVG can't get in
Field error messagesWritten with textContent; messages are treated as data
data-state-texttextContent
dom.repeatNever goes through the HTML parser; placeholder positions are allow-listed; URLs allow only http(s)/mailto/tel/relative
Linker error toastsFixed text (with APP_DEBUG=false); server exceptions aren't shown
ActionsNo eval; action names must be registered (checked by Action::new on the server, and again in the browser)

What you must watch ​

1. Don't use |safe or Html::raw on user data ​

jinja
{{ customer.name }}              {# ✅ escaped #}
{{ customer.bio|safe }}          {# ❌ HTML the user wrote, output as is #}
rust
Badge::new(customer.name.as_str())                       // ✅ &str is escaped
Badge::new(Html::raw(customer.name.clone()))             // ❌
Html::raw(format!("<p>{}</p>", cg_component::escape(&name)))  // ✅ if you build it yourself, escape it yourself

Fragment functions return Html, and the same rules apply as for ordinary pages: when you build HTML with format!, every interpolated value must be escaped.

2. Don't open command attributes to dom.repeat ​

html
<!-- ✅ plain-text attributes -->
<tr data-linker-ref-dom="orders" data-linker-ref-allow="data-status aria-busy">

<!-- ❌ the runtime refuses this, but don't try -->
<tr data-linker-ref-dom="orders" data-linker-ref-allow="data-linker-call">

Data deciding which method a button calls = data controlling behavior. data-linker-*, data-state-*, data-render-*, data-cg-* and data-param-* can never be opened. See Table rows and dom.repeat.

3. Seed data must be JSON that can't close its tag ​

Rust has no @linkerRefSeed. When you output <script type="application/json" data-linker-ref-seed> yourself, replace <, > and & with <, > and &, or a </script> in the data closes the tag. The searchable list tutorial has a helper you can use as is.

4. Inline scripts in fragments ​

After a fragment is inserted, the runtime runs the <script> tags in it. So fragments must never contain unescaped user data. Put logic in a JS module in the resources/js/ bundle, or register it as a custom action.

5. No sensitive data in client state ​

It's memory only and never written to storage, but any code on the same page can read it (anyone can listen to cg:state). No passwords, tokens or personal data.

6. Check permit() in your own JS ​

Code inside the bundle that imports state directly isn't checked. The convention is to check the render object's allow list first:

js
import permit from './cg/renderObject.js';
import state from './cg/state.js';

const { ok, name } = permit(element, 'state:write');
if (!ok) return;
state.scope(name).set('x', 1);

That keeps "the template is the truth" true.

CSP ​

With no eval path, you can use a CSP without unsafe-eval. If page fragments contain inline scripts, you need a nonce, or rewrite them as modules in the bundle.

Laravel for Rust doesn't send a CSP header by default; if you need one, write a web middleware that adds Content-Security-Policy.