LessonPHPLaravelSecurity

XSS Prevention in Laravel Blade: {{ }}, {!! !!} and Js::from

Abstract emerald hexagon filter on a dark background; green safe lines pass through while red lines are stopped with X marks before the shield
On this page
  1. What XSS is (and why Blade defaults matter)
  2. {{ }} auto-escapes with htmlspecialchars
  3. Never put user input in {!! !!} unless sanitized
  4. Embedding JSON in <script>: use Js::from
  5. Blade and JavaScript frameworks: @{{ }}
  6. Minimal PHP demo (escaped vs raw)
  7. Practical checklist
  8. Sources

Quick answer: treat every value that reaches HTML as untrusted until escaped. Blade {{ $value }} (and Laravel’s e() helper) pass the string through PHP’s htmlspecialchars, which is the right default for HTML body and attribute text. Reserve {!! $html !!} for content you already trust or have sanitized. When you need an array or object inside a <script> block, use Js::from / Illuminate\Support\Js::from instead of raw json_encode. Prefix frontend mustache syntax with @{{ }} so Blade leaves it alone.

What XSS is (and why Blade defaults matter)

Cross-Site Scripting (XSS) happens when an application puts untrusted data into a page so the browser treats it as code — for example a comment that should be plain text but instead injects a <script> tag or an event-handler attribute. OWASP’s XSS Prevention Cheat Sheet frames defence as context-aware output encoding plus safe sinks: escape for the HTML context you are writing into, prefer APIs that treat data as text, and sanitize only when you truly need user-authored HTML.

Laravel Blade steers you toward the safe path: its double-curly echo is escaped by default. The risk is the escape hatch.

{{ }} auto-escapes with htmlspecialchars

Given a view variable:

Hello, {{ $name }}.

Blade sends $name through PHP’s htmlspecialchars before printing it. That converts &, ", ', <, and > into HTML entities so a payload like <script>…</script> renders as text instead of executing. Laravel’s e() helper does the same encoding and is what Blade uses under the hood.

By default Blade (and e) double-encode existing entities. If you need to turn that off for a special case, Laravel documents Blade::withoutDoubleEncoding() in a service provider — leave double encoding on unless you have a concrete reason.

Always surround HTML attribute values with quotes when you echo into them, and keep using the escaped echo:

<input type="text" name="title" value="{{ old('title', $post->title) }}">

Never put user input in {!! !!} unless sanitized

Unescaped output uses:

Hello, {!! $name !!}.

Laravel’s Blade docs warn explicitly: be very careful echoing content supplied by users; prefer the escaped double-curly syntax to prevent XSS. {!! !!} is for trusted markup you control (for example a static partial or HTML already cleaned by a maintained sanitizer such as DOMPurify on the client, or a server-side HTML allowlist library). If the string came from a form, URL, API, or database row written by users, do not use {!! !!}.

Embedding JSON in <script>: use Js::from

A common anti-pattern is dumping JSON into a script tag by hand:

<script>
    var app = <?php echo json_encode($array); ?>;
</script>

Laravel recommends Illuminate\Support\Js::from instead. It accepts the same arguments as json_encode, but escapes the result for inclusion inside HTML and returns a JSON.parse(...) JavaScript statement:

<script>
    var app = {{ Illuminate\Support\Js::from($array) }};
</script>

Current Laravel app skeletons also register a Js facade:

<script>
    var app = {{ Js::from($array) }};
</script>

Use Js::from only on existing variables — Blade compiles with regular expressions, and complex inline expressions can break. Do not concatenate untrusted strings into script bodies or event-handler attributes; OWASP treats those as dangerous contexts even with generic escaping.

Blade and JavaScript frameworks: @{{ }}

Vue, Angular, Alpine-style templates, and similar tools also use {{ }}. Prefix with @ so Blade emits the braces for the frontend:

Hello, @{{ name }}.

Blade strips the @ and leaves {{ name }} untouched. For large JS blocks, wrap them in @verbatim … @endverbatim so you do not need @ on every echo. The same @ escape works for Blade directives (@@if() renders @if()).

Minimal PHP demo (escaped vs raw)

This CLI snippet mirrors what Blade’s escaped echo does versus dumping raw HTML — no Laravel required:

<?php
declare(strict_types=1);

$userInput = "<script>alert('xss')</script>";

$escaped = htmlspecialchars($userInput, ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8');
$raw = $userInput; // what {!! !!} would dump unchanged

echo "RAW:\n{$raw}\n\n";
echo "ESCAPED:\n{$escaped}\n";

Run with php demo.php. The raw line still contains a live <script> tag; the escaped line shows &lt;script&gt;…&lt;/script&gt;. That difference is exactly why {{ }} is the default.

Practical checklist

  • Echo user-facing text with {{ }} or e() — never assume a value is “safe enough.”
  • Treat {!! !!} as an audited exception: trusted HTML only, or sanitized HTML from a maintained library.
  • Put JSON into scripts with Js::from / Illuminate\Support\Js::from, not bare json_encode inside HTML.
  • Use @{{ }} or @verbatim when a JS framework needs curly braces.
  • Quote HTML attributes; do not place untrusted data in event handlers, inline scripts, or CSS contexts without context-specific encoding (see OWASP).
  • Remember: fixing XSS also protects CSRF defences — a successful XSS can read tokens the browser can see.

Want a review of Blade templates, CSP headers, or output encoding across a Laravel app? See PHP Hub services (contact for quote) or run a quick pass with the PHP Security Checker.

This lesson was drafted with AI assistance and reviewed for accuracy against the official Laravel Blade documentation, the PHP htmlspecialchars manual, and the OWASP Cross Site Scripting Prevention Cheat Sheet linked below.

Sources

Check your reading

Questions are taken from published PHP Hub lessons. This does not grade a certification, and the answers are checked in your browser.

1. The XSS lesson says Blade {{ }} passes the value through which PHP function?
2. The lesson says {!! !!} is for which kind of content?
3. The lesson says to embed an array or object inside a script tag with which API?

Part 3 of 4 in the Laravel security series. Mass assignment, CSRF, XSS, then SQL injection.

Need an authorized audit?

For a PHP, Laravel or WordPress site you own or are authorized to manage, we can agree a fixed-scope review in writing first. Pricing is contact for quote.

Get new briefs by email

Short alerts when a PHP or WordPress vulnerability needs action.

Double opt-in via Buttondown; unsubscribe any time. See our privacy policy.