← PHP / Laravel · Contents

PHP-031 / Intermediate

XSS due to incorrect JSON response Content-Type

A03:2021 Injection · CWE-79 XSS · CWE-116 Improper Encoding or Escaping

Why is this a problem?

Browsers interpret JSON or user responses served as text/html.

How to fix it

php
return response()->json($data)->header('X-Content-Type-Options', 'nosniff');
// Discontinue JSONP; use CORS

Further reading