jeffersongoncalves / laravel-html-sanitizer
This Laravel package provides a simple wrapper around the Symfony HTML Sanitizer to safely clean untrusted HTML. It strips scripts, inline event handlers, and Alpine attributes while keeping the presentational subset (headings, lists, tables, code blocks, images, links) that rendered Markdown and RE
Package info
github.com/jeffersongoncalves/laravel-html-sanitizer
pkg:composer/jeffersongoncalves/laravel-html-sanitizer
Fund package maintenance!
Requires
- php: ^8.2
- illuminate/support: ^11.0|^12.0|^13.0
- spatie/laravel-package-tools: ^1.14
- symfony/html-sanitizer: ^7.0|^8.0
Requires (Dev)
- larastan/larastan: ^3.0
- laravel/pint: ^1.0
- orchestra/testbench: ^9.0|^10.0|^11.0
- pestphp/pest: ^3.7.4|^4.0
- pestphp/pest-plugin-laravel: ^3.0|^4.0
This package is auto-updated.
Last update: 2026-06-20 21:26:19 UTC
README
Laravel HTML Sanitizer
This Laravel package provides a simple wrapper around the Symfony HTML Sanitizer to safely clean untrusted HTML. It strips scripts, inline event handlers, and Alpine attributes while keeping the presentational subset (headings, lists, tables, code blocks, images, links) that rendered Markdown and READMEs need. The package is easy to install and configure, seamlessly integrating with your existing Laravel application.
Installation
You can install the package via composer:
composer require jeffersongoncalves/laravel-html-sanitizer
Usage
Pass any untrusted HTML through HtmlSanitizer::clean() before rendering it:
use JeffersonGoncalves\HtmlSanitizer\HtmlSanitizer; $dirty = '<p>Hello</p><script>alert("xss")</script><img src="x" onerror="steal()">'; $clean = HtmlSanitizer::clean($dirty); // <p>Hello</p><img src="x">
The sanitizer:
- drops
<script>,<style>and every event-handler attribute (onerror,onclick, ...); - strips Alpine
x-*attributes; - keeps the safe presentational subset: headings, lists, tables, code blocks, images and links;
- allows relative links/medias and the
https,http,mailtolink schemes (plusdatafor media); - preserves
class/idattributes (for heading permalinks, code-language hints and table wrappers) andwidth/heighton<img>.
It is intended for rendered HTML that originated from untrusted sources — GitHub READMEs of third-party repos and the Markdown body of imported articles — where raw HTML is enabled during rendering.
Testing
composer test
Changelog
Please see CHANGELOG for more information on what has changed recently.
Contributing
Please see CONTRIBUTING for details.
Security Vulnerabilities
Please review our security policy on how to report security vulnerabilities.
Credits
License
The MIT License (MIT). Please see License File for more information.
