← PHP / Laravel · Contents

PHP-086 / Intermediate

XSS in Blade @json or <script> contexts

A03:2021 Injection · CWE-79 XSS

Why is this a problem?

When inserting values inside a script block, </script> escapes the block.

How to fix it

blade
<script>const data = @json($user->only('name'), JSON_HEX_TAG | JSON_HEX_APOS);</script>

Further reading