stevebauman/purify
Laravel wrapper for HTMLPurifier to sanitize user HTML safely. Clean strings or arrays via the Purify facade, with optional per-call configuration. Publish a config file, tune allowed tags/attributes, and leverage caching for performance.
Security & Compliance:
Rich-Text Editor Integration:
<figure>, <figcaption> for Trix attachments).data-trix-*).Multi-Tenant or Role-Based Sanitization:
comments, admin_posts) let PMs enforce stricter rules for sensitive content (e.g., admin dashboards) vs. public-facing areas.<iframe> in admin panels but block it in user comments.Performance Optimization:
Roadmap: AI-Generated Content:
<script> but allow <div> with data-* attributes for structured data.Build vs. Buy:
<figure> for Trix).str or sanitize-html).htmlentities for simple cases).<script> tags with CSP as the primary defense)."This package lets us safely enable user-generated content—like rich-text comments or WYSIWYG editors—without exposing our app to XSS attacks. It’s like a ‘security firewall’ for HTML, automatically stripping malicious scripts while preserving the formatting users intend. For example, we can let customers use Trix for polished content in our support portal, but block any embedded JavaScript that could hijack sessions. It’s a low-code solution that reduces our attack surface while keeping the UX rich. The caching feature also ensures it won’t slow down high-traffic areas like our blog or forum."
Key Metrics to Track:
*"This is a Laravel-native wrapper for HTMLPurifier, giving us enterprise-grade HTML sanitization with zero reinvention. Key perks:
- Seamless integration: Works with facades (
Purify::clean()), Eloquent casts (PurifyHtmlOnGet), and config-driven profiles.- Extensible: Customize allowed tags/attributes (e.g., support Trix’s
<figure>tags) via PHP classes. Need to allowdata-*attributes? Done in 10 lines.- Performance: Caches purifier definitions by default (configurable to filesystem/Laravel cache). Clear cache via
php artisan purify:clear.- Best practices baked in: Follows ‘sanitize on output’ (not input) to avoid DB bloat, with built-in Eloquent support.
- Future-proof: Handles edge cases like HTML5 doctypes and custom CSS properties (e.g.,
text-align: start).*Trade-offs:
- Adds ~1MB to your vendor dir (HTMLPurifier dependency).
- Requires config tweaks for complex use cases (e.g., multi-profile sanitization).
Alternatives considered:
sanitize-html: Lighter but less configurable for Laravel.- Custom regex/strip_tags: Risky (false positives/negatives).
- HTMLPurifier raw: More control but no Laravel integration.
Recommendation: Use this for any user-generated HTML in Laravel. Pair with CSP for defense-in-depth."*
For PMs (Implementation Notes):
PurifyHtmlOnGet cast for Eloquent models to auto-sanitize on retrieval.TrixPurifierDefinitions class if using Trix (example in README).if (strlen($cleaned) < strlen($dirty)) log->warning(...)) to catch false positives.How can I help you explore Laravel packages today?