symfony/html-sanitizer
Object-oriented HTML sanitizer for PHP/Symfony. Safely cleans untrusted HTML for DOM insertion with configurable allow/block/drop rules, attribute policies, forced values, and URL controls (HTTPS and scheme/host allowlists). Removes scripts and unsafe behaviors.
Security Hardening for User-Generated Content:
<script>, javascript: URLs).Roadmap for Content Moderation Tools:
<a> with href but block target="_blank" unless rel="noopener" is forced). Critical for:
AttributeSanitizer interfaces allow domain-specific rules (e.g., validating data-* attributes for analytics tools).Build vs. Buy Decision:
<custom-widget>). Extend via HtmlSanitizerConfig or AttributeSanitizerInterface.Performance-Critical Applications:
<textarea> inputs).Multi-Platform Integrations:
Adopt if:
<img> but block onerror").<head>, <textarea>, <div>).Look elsewhere if:
Str::of() or htmlspecialchars()."This package eliminates XSS risks from user-generated content—like forum posts or CMS blocks—while giving us fine-grained control over what HTML we allow. For example, we can safely embed links or images from trusted domains without exposing the app to script injection. It’s a drop-in solution that reduces security audit overhead and aligns with compliance needs (GDPR, PCI DSS). The performance boost in PHP 8.4+ also future-proofs our high-traffic features like news articles or community discussions. With minimal dev effort, we avoid reinventing sanitization wheels and leverage Symfony’s battle-tested security practices."
Key Metrics to Track:
*"This is a Swiss Army knife for HTML sanitization—it’s configurable, performant, and backed by Symfony’s security team. Here’s how we’ll use it:
- Forums/Comments: Whitelist
<b>,<i>,<a>, andhrefattributes while blocking scripts.- CMS Blocks: Allow
<div>,<img>, andsrc/altattributes with forced HTTPS.- Rich Text Editors: Context-aware sanitization (e.g.,
<textarea>inputs get entity-encoded).- SEO Tools: Safe
<meta>tag manipulation without XSS risks.Why not build it ourselves?
- Maintenance: We’d spend cycles fixing edge cases (e.g., malformed HTML, CSS injection).
- Security: Symfony’s team has already patched bugs like
srcdocremoval and URL whitespace rejection.- Performance: PHP 8.4’s native parser is faster than custom DOM libraries.
Integration:
- Drop into Laravel/Symfony via Composer (
symfony/html-sanitizer).- Extend with custom
AttributeSanitizerfor domain-specific rules.- Example: Force
rel='noopener'on all<a>tags to prevent tabnabbing.Alternatives considered:
- DOMPurify: Client-side only; we need server-side sanitization.
- HTMLPurifier: Overkill for our needs and slower.
- Custom regex: Unreliable for complex HTML edge cases."*
Engineering Asks:
/comments).AttributeSanitizerInterface.How can I help you explore Laravel packages today?