Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Html Sanitizer Laravel Package

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.

View on GitHub
Deep Wiki
Context7

Product Decisions This Supports

  • Security Hardening for User-Generated Content:

    • Mitigates XSS vulnerabilities in features like forums, CMS content blocks, or rich-text editors by defaulting to a whitelist-based sanitization model. Reduces attack surface for high-risk inputs (e.g., <script>, javascript: URLs).
    • Compliance: Aligns with OWASP Top 10 (A03:2021) and PCI DSS requirements for sanitizing dynamic content.
  • Roadmap for Content Moderation Tools:

    • Enables granular policy enforcement (e.g., allow <a> with href but block target="_blank" unless rel="noopener" is forced). Critical for:
      • Community platforms (e.g., Discord-like apps where users embed links).
      • Marketing tools (e.g., email templates with safe HTML snippets).
    • Extensibility: Custom AttributeSanitizer interfaces allow domain-specific rules (e.g., validating data-* attributes for analytics tools).
  • Build vs. Buy Decision:

    • Buy: Avoids reinventing sanitization logic (e.g., for startups or teams without security expertise). MIT license and Symfony-backed ensures long-term viability.
    • Build: Ideal for teams needing custom sanitization (e.g., allowing proprietary tags like <custom-widget>). Extend via HtmlSanitizerConfig or AttributeSanitizerInterface.
  • Performance-Critical Applications:

    • PHP 8.4+ optimization: Uses native HTML5 parser for ~30% faster sanitization vs. legacy DOM libraries (benchmarked in Symfony’s release notes). Targets high-traffic apps (e.g., news sites, social media).
    • Context-aware sanitization: Reduces overhead by skipping full parsing for non-HTML contexts (e.g., <textarea> inputs).
  • Multi-Platform Integrations:

    • W3C Sanitizer API compliance ensures consistency across browsers/headless CMS (e.g., Strapi, Directus). Simplifies cross-platform content pipelines (e.g., mobile apps consuming sanitized HTML).

When to Consider This Package

  • Adopt if:

    • Your app renders untrusted HTML (e.g., user comments, CMS pages, wikis) and requires XSS protection.
    • You need fine-grained control over allowed tags/attributes (e.g., "allow <img> but block onerror").
    • Your stack is Symfony/Laravel/PHP 8.1+ (native integration with Symfony’s ecosystem).
    • You prioritize maintainability over custom code (active development, MIT license, 279+ stars).
    • Your use case involves context-specific sanitization (e.g., <head>, <textarea>, <div>).
  • Look elsewhere if:

    • You need client-side sanitization (e.g., browser extensions) → Use DOMPurify (JavaScript).
    • Your app doesn’t process HTML (e.g., sanitizing plain text or URLs) → Use Laravel’s Str::of() or htmlspecialchars().
    • You require legacy PHP (<8.1) support → Consider HTMLPurifier (slower but broader compatibility).
    • Your team lacks PHP/Symfony expertise → Evaluate managed services (e.g., Cloudflare Turnstile for comment moderation).
    • You need real-time sanitization (e.g., live preview editors) → Pair with a client-side library (e.g., DOMPurify + this package for server-side fallback).

How to Pitch It (Stakeholders)

For Executives (Business Impact)

"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:

  • Reduction in XSS vulnerabilities (post-deployment).
  • Decrease in security audit findings (e.g., fewer "untrusted HTML" flags).
  • Performance improvement in content-heavy endpoints (e.g., 20–30% faster parsing).

For Engineers (Technical Value)

*"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>, and href attributes while blocking scripts.
  • CMS Blocks: Allow <div>, <img>, and src/alt attributes 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 srcdoc removal 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 AttributeSanitizer for 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:

  • Spike: Validate performance impact on high-traffic endpoints (e.g., /comments).
  • Security Review: Audit the package’s W3C compliance against our threat model.
  • Customization: Identify 1–2 edge cases where we’d need to extend the AttributeSanitizerInterface.
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
davejamesmiller/laravel-breadcrumbs
artisanry/parsedown
christhompsontldr/phpsdk
enqueue/dsn
bunny/bunny
enqueue/test
enqueue/null
enqueue/amqp-tools
milesj/emojibase
bower-asset/punycode
bower-asset/inputmask
bower-asset/jquery
bower-asset/yii2-pjax
laravel/nova
spatie/laravel-mailcoach
spatie/laravel-superseeder
laravel/liferaft
nst/json-test-suite
danielmiessler/sec-lists
jackalope/jackalope-transport