typo3/html-sanitizer
Standalone PHP HTML sanitizer from TYPO3. Define sanitization rules via Behavior, apply with Visitors, and get a ready-to-use Sanitizer via Builders/presets. Control allowed tags, attributes, and values; encode or remove invalid nodes and comments.
XSS Protection for User-Generated Content (UGC): Enables secure rendering of HTML from untrusted sources (e.g., comments, forum posts, CMS content) by whitelisting only safe tags/attributes. Critical for platforms handling dynamic content (e.g., social media, Q&A, wikis).
Compliance with Security Standards: Aligns with OWASP guidelines for input validation/sanitization, reducing legal/regulatory risks (e.g., GDPR, PCI-DSS). Justifies investment in security-focused infrastructure.
Customizable Sanitization Policies:
Supports build vs. buy by allowing granular control over allowed HTML (e.g., strict vs. permissive rules for different content types). Avoids reinventing wheel for complex use cases (e.g., rich-text editors with custom tags like <typo3>).
Roadmap for Scalable Content Moderation: Integrates with moderation workflows by enabling safe preview/editing of user-submitted HTML before publication. Example: Sanitize previews in a CMS while preserving formatting for admins.
Multi-Tenant or Role-Based HTML Rules:
Use Builder presets to create tenant-specific or role-based sanitization rules (e.g., "Editors can use <strong>; Guests cannot").
Avoid if:
bleach (Python) or sanitize-html (Node.js).Consider if:
<script> but allowing <iframe> with src restrictions)."This package is a turnkey XSS protection layer for user-generated HTML, reducing our attack surface without sacrificing functionality. It’s like a ‘whitelist firewall’ for HTML—only safe tags/attributes pass through. For example:
"This is a declarative HTML sanitizer with three key advantages:
->withTags(new Behavior\Tag('a')->addAttrs(new Behavior\Attr('href')))). Need custom rules? Extend it with Visitor classes.ENCODE_INVALID_TAG ensure even malformed HTML is safe.HTMLPurifier—it’s faster for simple use cases.
Tradeoff: Slight learning curve for custom builders, but docs/examples cover 90% of needs. Start with CommonBuilder for quick wins."*"This mitigates XSS risks by:
<script> becomes <script>) instead of silently dropping it.javascript: URLs, custom elements).
Integration tip: Use it in two layers:<iframe> with src validation).
Prove its effectiveness by testing with XSS Payloads and the demo server."*How can I help you explore Laravel packages today?