tgalopin/html-sanitizer
tgalopin/html-sanitizer is a PHP library for cleaning untrusted HTML safely. It removes dangerous markup to prevent XSS while letting you configure allowed tags, attributes, and extensions, making it easy to display user-generated content securely.
strip_tags filtering or letting engineers spend sprint time on a bespoke sanitizer. Both are worse outcomes for a non-differentiating, security-critical concern; a vetted library converts an open-ended engineering risk into a bounded dependency decision.Consider the capability (whitelist-based HTML5 sanitization in PHP) whenever:
Consider this specific package only in narrow, time-boxed circumstances:
Look elsewhere — and treat this as the default recommendation — because:
symfony/html-sanitizer, which is actively developed, framework-agnostic, and usable from Laravel via Composer (no Symfony app required). For new work, that is the correct buy.mews/purifier-style HTMLPurifier wrapper) if you prefer an established Laravel-facing integration and stricter-than-HTML5 policy defaults; HTMLPurifier is slower but battle-tested and still maintained.{{ }} escaping plus a Markdown renderer with HTML disabled already covers you — do not add a sanitizer dependency you don't need.Note this package ships a Symfony bundle, not a Laravel service provider; in Laravel you'd wire it yourself (singleton binding, a validation rule, a Blade directive). Trivial work, but it is your code to own — and more code to own around a dependency nobody else is patching.
For executives (one paragraph): We need to render customer-authored rich text — comments, listings, templates — and that is a well-known cross-site-scripting surface. The right answer is a policy-driven HTML sanitizer rather than custom code; hand-rolling it is slow, hard to review, and chronically under-tested. The specific library reviewed here is well designed and was the reference implementation for this problem in PHP, but it is archived with no releases since 2021, which is unacceptable for a security control. Recommendation: endorse the capability, reject this package for new work, and standardize on its actively maintained successor (symfony/html-sanitizer). Cost is one dependency plus roughly 1–2 engineering days of integration and policy configuration; the alternative — a bespoke sanitizer or an unpatched dependency — carries materially higher incident and audit risk than the integration cost.
For engineering: Treat this as a capability decision with an implementation correction. The architecture worth copying is sound: HTML5-compliant parse tree, whitelist of elements/attributes/URL schemes, extension points for project policy, URL and inline-handler filtering. Build to that shape — declarative, version-controlled policy config reviewed like code, a single sanitization service at the render boundary (not scattered strip_tags calls), applied on output as well as input, plus tests with a corpus of known XSS payloads. But implement it on symfony/html-sanitizer: same lineage, actively maintained, Composer-installable in Laravel, no Symfony application required — bind it as a singleton, expose a validation rule and a Blade directive, and translate the whitelist into one config file. If any code currently depends on tgalopin/html-sanitizer, pin it, open a migration ticket with a named owner, and budget a day or two; the APIs are close enough that this is a mechanical port, not a rewrite. Do not fork the archived library unless someone is explicitly funded to own security patches — and if that's the plan, say so out loud, because "we forked the security library" is a decision leadership should hear.
How can I help you explore Laravel packages today?