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

Htmlpurifier Html5 Laravel Package

xemlock/htmlpurifier-html5

HTML5 definitions and tidy/sanitization rules for HTML Purifier, aligned with the WHATWG spec. Purify and normalize dirty HTML5 into valid output with an HTML5-ready config, plus flexible directives (e.g., safely allow YouTube iframes).

View on GitHub
Deep Wiki
Context7

Technical Evaluation

Architecture Fit

  • Seamless Laravel Integration: Designed as a drop-in replacement for Laravel’s built-in HTMLPurifier (via Purifier facade or direct instantiation). Leverages Composer for dependency management, aligning with Laravel’s ecosystem.
  • Modular Configuration: Extends HTMLPurifier’s existing config system with HTML5-specific directives (e.g., HTML.SafeIframe, Attr.AllowedInputTypes), enabling granular control without monolithic changes.
  • WHATWG Compliance: Aligns with modern web standards, reducing friction for features like semantic markup (<article>, <section>) or media elements (<video>, <audio>). Critical for Laravel apps targeting accessibility (WCAG) or SEO.
  • Security-First Design: Inherits HTMLPurifier’s XSS protection while adding HTML5-specific safeguards (e.g., URI.SafeIframeRegexp for iframe whitelisting). Mitigates risks like phishing via <form> elements (configurable via HTML.Forms).

Integration Feasibility

  • Low-Coupling: Replaces or augments Laravel’s HTMLPurifier instance without requiring changes to existing sanitization logic. Example:
    // Current Laravel (legacy)
    $purifier = new HTMLPurifier();
    
    // Upgraded (HTML5-compliant)
    $config = HTMLPurifier_HTML5Config::createDefault();
    $purifier = new HTMLPurifier($config);
    
  • Backward Compatibility: Maintains HTMLPurifier’s API, ensuring existing configs (e.g., HTML.Allowed) remain functional. New directives (e.g., HTML.SafeLink) are additive.
  • Dependency Synergy: Works with Laravel’s purifier package (if used) or standalone ezyang/htmlpurifier. No conflicts with other PHP sanitizers (e.g., DOMDocument).

Technical Risk

Risk Mitigation Severity
Breaking Changes Avoid v0.1.9 (buggy <a> tag). Stick to v0.1.10+ for stable HTML5 support. Medium
Performance Overhead Benchmark against base HTMLPurifier; HTML5 definitions add ~10–15% CPU overhead. Low
Config Complexity Document default configs (e.g., createDefault()) and provide Laravel-specific examples. Medium
Laravel Caching Ensure HTMLPurifier instances are not cached globally (configs may vary by context). High
Third-Party Conflicts Test with Laravel’s Str::markdown() or ckeditor packages to ensure no attribute clashes. Low

Key Questions for TPM

  1. Use Case Prioritization:

    • Which HTML5 features are critical for your MVP (e.g., <video> for media-rich apps vs. <article> for CMS)?
    • Do you need real-time sanitization (client-side) or server-side batch processing (e.g., moderation queues)?
  2. Security Trade-offs:

    • Should <form> elements be always blocked (HTML.Forms = false) or allowed only in trusted contexts?
    • What domains should be whitelisted for <iframe>/<link> tags? (Example: URI.SafeIframeRegexp for YouTube.)
  3. Performance:

    • Will this run in high-throughput environments (e.g., 10K+ requests/sec)? If yes, consider caching HTMLPurifier instances per config.
    • Does your app use Laravel’s queue system for sanitization? If so, test with batched processing.
  4. Maintenance:

    • Who will own the sanitization config? (DevOps, security team, or product?)
    • How will you audit allowed elements over time? (Example: Add <dialog> now but block <script> globally.)
  5. Alternatives:

    • If HTML5 compliance isn’t urgent, could you use base HTMLPurifier + custom definitions to reduce overhead?
    • For client-side needs, would DOMPurifier (JS) + this package (server-side) be a better fit?

Integration Approach

Stack Fit

  • Laravel Ecosystem:
    • Replaces: ezyang/htmlpurifier (base) or Laravel’s purifier facade.
    • Complements:
      • Rich Text Editors: TinyMCE/CKEditor (configure allowedContent to match purifier rules).
      • Markdown Parsers: Str::markdown() → sanitize output with this package.
      • APIs: Validate HTML payloads (e.g., from third-party embeds) before rendering.
  • Non-Laravel PHP:
    • Works anywhere HTMLPurifier is used (e.g., Symfony, WordPress plugins). Laravel-specific optimizations (e.g., caching) are optional.

Migration Path

Phase Action Tools
Assessment Audit current sanitization logic. Identify HTML5 gaps (e.g., missing <video> support). php artisan tinker + var_dump()
Pilot Replace HTMLPurifier in one feature (e.g., comment system). Test with real user content. Postman + Laravel Dusk
Rollout Update configs globally. Use feature flags for gradual adoption. Laravel Envoy (for deployments)
Optimize Cache HTMLPurifier instances per config (e.g., Cache::remember). Laravel Cache

Compatibility

  • Laravel Versions: Compatible with LTS versions (8.x–10.x). Test with PHP 8.1+ (required for HTMLPurifier v4.16+).
  • Existing Configs: Most HTMLPurifier configs will work unchanged. Example:
    // Old (HTML4)
    $config = HTMLPurifier_Config::createDefault();
    
    // New (HTML5)
    $config = HTMLPurifier_HTML5Config::createDefault();
    $config->set('HTML.Allowed', 'p,a[href],b,i'); // Still works!
    
  • Attribute Conflicts: HTML5 adds attributes like async (for <script>). Ensure your WYSIWYG editors (e.g., CKEditor) don’t inject unsupported ones.

Sequencing

  1. Phase 1: Core Sanitization
    • Replace HTMLPurifier instances with HTMLPurifier_HTML5Config.
    • Enable critical HTML5 elements (e.g., <video>, <figure>) via HTML.Allowed.
  2. Phase 2: Security Hardening
    • Configure whitelists (URI.SafeIframeRegexp, Attr.AllowedRel).
    • Disable risky features (HTML.Forms = false).
  3. Phase 3: Performance
    • Cache purifier instances for repeated configs.
    • Benchmark against base HTMLPurifier to validate overhead.
  4. Phase 4: Expansion
    • Add context-specific configs (e.g., stricter rules for admin panels).
    • Integrate with Laravel policies to dynamically adjust allowed elements.

Operational Impact

Maintenance

  • Config Drift Risk:
    • Mitigation: Document default configs (e.g., createDefault()) and use Laravel’s config caching to prevent runtime overrides.
    • Tooling: Add a php artisan purifier:validate command to audit allowed elements against WHATWG spec.
  • Dependency Updates:
    • Monitor xemlock/htmlpurifier-html5 for breaking changes (e.g., new HTML5 elements in future releases).
    • Strategy: Pin to minor versions (e.g., ^0.1) unless major features are needed.

Support

  • Debugging:
    • Use HTMLPurifier's built-in logging ('Core.Logging' => true) to trace sanitization failures.
    • Common Issues:
      • "Unexpected element <dialog>": Add it to HTML.Allowed.
      • "Attribute async not allowed": Update HTML5Config to include <script> attributes.
  • Escalation Path:
    • For security issues (e.g., XSS bypass), engage the HTMLPurifier core team via GitHub.
    • For HTML5 compliance gaps, contribute to xemlock/htmlpurifier-html5 or file issues.

Scaling

  • Horizontal Scaling:
    • Stateless by design; no shared memory between instances.
    • Caching: Cache HTMLPurifier instances per config (e.g., `Cache::forever
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.
emuniq/filament-browser-notifications
syriable/filament-translator
hungnm28/livewire-form
wenprise/eloquent
crudly/encrypted
fadion/bouncy
cuci/prototurk-sdk
gos/pubsub-router-bundle
cuci/prototurk-sdk-symfony
clementtalleu/easyadmin-markdown-bundle
codeflextech/permission-manager
karnoweb/livewire-datepicker
sayedenam/sayed-dashboard
milito/query-filter
apiboxsym/user-bundle
apiboxsym/health-check-bundle
jayeshmepani/jpl-moshier-ephemeris-php
elnasnato/laraliveui
labrodev/rest-sdk
sampaui/sampaui