New

Just launched: Vueblocks, a brand-new search engine for Vue packages! Explore Vueblocks →

masterro/laravel-xss-filter

Filter user input for XSS but don't touch other html
299,601 41
Install
composer require masterro/laravel-xss-filter
Latest Version:v2.2.1
PHP:^8.2.35|^8.3.36|^8.4.27|^8.5.12
Laravel:8 – 13
Older releases:5 · 6 · 7
License:MIT
Last Updated:Oct 21, 2026
Links: GitHub  ·  Packagist
Maintainer: MasterRO

Latest Stable Version Total Downloads Build Status License

StandWithUkraine

XSS Filter/Sanitizer for Laravel

Configure once and forget about XSS attacks!

It does not remove the html, it is only escaped script tags and embeds.
However, by default, it does delete inline event listeners such as onclick. Optionally they also can be escaped (set escape_inline_listeners to true in xss-filter.php config file).

For example

<html>
<head>
    <script src="app.js"></script>
    <script>window.init()</script> 
    <meta name="test" />
    <script>
    let Iframe = new Iframe('#iframe');
    </script>
<head>
<body>
    <div class="hover" onhover="show()" data-a="b"><p onclick="click"><span class="span" ondblclick="hide()"></span>Aawfawfaw f awf aw  </p></div>
    <iframe id="iframe">Not supported!</iframe>
</body>
</html>

will be transformed to

<html>
<head>
&lt;script src=&quot;app.js&quot;&gt;&lt;/script&gt;
&lt;script&gt;window.init()&lt;/script&gt; 
<meta name="test" />
&lt;script&gt;
let Iframe = new Iframe(&#039;#iframe&#039;);
&lt;/script&gt;
<head>
<body>
<div class="hover"  data-a="b"><p ><span class="span" ></span>Aawfawfaw f awf aw  </p></div>
&lt;iframe id=&quot;iframe&quot;&gt;Not supported!&lt;/iframe&gt;
</body>
</html>

This allows to render html in views based on users' input and don't be afraid of XSS attacks and embed elements.

Installation

Step 1: Composer

From command line

composer require masterro/laravel-xss-filter

Step 2: publish configs (optional)

From command line

php artisan vendor:publish --provider="MasterRO\LaravelXSSFilter\XSSFilterServiceProvider"

Step 3: Middleware

You can register \MasterRO\LaravelXSSFilter\FilterXSS::class for filtering in global middleware stack, group middleware stack or for specific routes.

Have a look at Laravel's middleware documentation, if you need any help.

Livewire

If you are using Livewire you can either register global middleware to all the update livewire requests. This special middleware will clean only required part of Livewire request payload and will not touch snapshot so the component checksum still would be valid.

// AppServiceProvider.php

public function boot(): void
{
    Livewire::setUpdateRoute(static function ($handle) {
        return Route::post('/livewire/update', $handle)
            ->middleware(['web', FilterXSSLivewire::class]);
    });
}

Or you can apply middleware to specific routes and add it to persistent list to ensure inputs are cleared on subsequent component requests:

// AppServiceProvider.php

public function boot(): void
{
    Livewire::addPersistentMiddleware([
        FilterXSSLivewire::class,
    ]);
}

[!NOTE]
If you have both Livewire components and traditional Controllers you can apply only FilterXSSLivewire::class middleware for all required routes or globally. It will fall back to base logic for non Livewire requests.

Usage

After adding middleware, every request will be filtered.

If you need to specify attributes that should not be filtered add them to xss-filter.except config. By default, filter excepts password and password_confirmation fields.

If you want to clean some value in other place (i.e. Controller) you can use XSSCleaner Facade.

$clean = XSSCleaner::clean($string);
Runtime configuration
XSSCleaner::config()
    ->allowElement('iframe')
    ->allowMediaHosts(['youtube.com', 'youtu.be'])
    ->denyElement('a');
    
$clean = XSSCleaner::clean($string);
I will be grateful if you star this project :)

Related Packages

sneek/laravel-xss-middleware

Simple Laravel XSS middlware to strip all tags and encode all entities from a us...

3,583 2
frozennode/xssinput

A simple extension of the Laravel Input facade that mimics CodeIgniter's xss fil...

71,089 39
shin1x1/laravel-force-https-url-scheme

Force https url schema middleware for Laravel 5

26,582 25
spatelsofmen/xssinput

A simple extension of the Laravel Input facade that mimics CodeIgniter's xss fil...

102 0
laravel-dev-common/union-middleware

Laravel dev middleware util

95 0

Version History

Version Released PHP Laravel License
v2.2.1 ^8.2.35|^8.3.36|^8.4.27|^8.5.12 ^8.0|^9.0|^10.0|^11.0|^12.0|^13.0 MIT
v2.2.0 >=8.1 ^8.0|^9.0|^10.0|^11.0|^12.0|^13.0 MIT
v2.1.0 >=8.1 ^8.0|^9.0|^10.0|^11.0|^12.0 MIT
v2.0.0 >=8.1 ^8.0|^9.0|^10.0|^11.0 MIT
v1.8.0 >=7.4 ^6.20.26|^7.30.6|^8.0|^9.0|^10.0|^11.0 MIT