voku/anti-xss
PHP AntiXSS library to sanitize untrusted HTML and prevent XSS attacks. Cleans input by removing dangerous tags/attributes, filters CSS/JS vectors, and supports UTF-8 and common encodings. Useful for safely handling user-generated content in apps.
Install via composer require voku/anti-xss. Instantiate the class once (e.g., in a service provider or as a singleton) and use xss_clean() on untrusted strings just before output, especially when rendering raw HTML (e.g., from rich text editors or user comments). For quick validation in Blade:
{!! $antiXss->xss_clean($comment->body) !!}
⚠️ Do not use this in place of Blade’s auto-escaping ({{ $var }}). Reserve it for known-safe contexts where HTML is expected but must be sanitized.
$antiXss->xss_clean($input, $isAttribute = true); // For src="" / href=""
$antiXss->xss_clean($input, $isScript = true); // For inline JS
// In AppServiceProvider
Blade::directive('antiXss', fn ($expr) => "<?php echo \\voku\\helper\\AntiXSS::xss_clean($expr); ?>";
Then use: @antiXss($html)& → &amp;) is common. Test with ©, , and emoji—sanitize after decoding if necessary.xss_clean() may strip legitimate content (e.g., < in math expressions becomes <). Validate output manually for edge-case content types.htmlpurifier/laravel-html-purifier) for tag/attr whitelisting, while voku/anti-xss handles protocol/attribute neutralization (e.g., javascript:).How can I help you explore Laravel packages today?