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

Purifier Laravel Package

mews/purifier

View on GitHub
Deep Wiki
Context7

Technical Evaluation

Architecture Fit

  • Pros:

    • Seamless Laravel Integration: Designed as a Laravel service provider with facade support, aligning with Laravel’s dependency injection and service container patterns. Leverages Laravel’s auto-discovery (L5.5+) for minimal setup.
    • HTML Purifier Core: Under the hood, it uses HTML Purifier, a battle-tested, standards-compliant HTML sanitizer with a robust whitelist-based security model. Ideal for mitigating XSS risks in user-generated content (UGC).
    • Configuration Flexibility: Supports dynamic configurations per use case (e.g., titles, youtube, custom definitions) via method chaining or published config files. Enables granular control over allowed HTML tags/attributes.
    • Eloquent Integration: Provides custom casts (CleanHtml, CleanHtmlInput, CleanHtmlOutput) for automatic sanitization in model attributes, reducing boilerplate in controllers/services.
    • URI Filtering: Built-in support for URI filtering (e.g., restricting embedded content like YouTube iframes to trusted domains).
  • Cons:

    • Performance Overhead: HTML Purifier is resource-intensive due to its strict validation and parsing. May impact high-throughput APIs or real-time systems (e.g., chat apps) if not cached or optimized.
    • Complexity: Custom configurations (e.g., HTML5 definitions) require deep knowledge of HTML Purifier’s schema. Misconfigurations could inadvertently block legitimate content or introduce vulnerabilities.
    • No Built-in Rate Limiting: While the package itself doesn’t handle rate limiting, purifying large volumes of HTML (e.g., bulk imports) could strain server resources without external mitigation.

Integration Feasibility

  • Laravel Ecosystem Compatibility:
    • Works out-of-the-box with Laravel 5.5–12 (auto-discovery) and older versions with manual registration. Compatible with Laravel’s request pipeline (middleware, form requests) and Eloquent models.
    • Supports both procedural (clean() helper) and object-oriented (Purifier::clean()) usage patterns.
  • Third-Party Dependencies:
    • Primary dependency: ezyang/htmlpurifier (v4.x+). Ensure compatibility with your PHP version (PHP 7.4+ recommended for Laravel 9/10/11/12).
    • No hard dependencies on other Laravel packages, reducing conflict risks.
  • Testing:
    • Includes Travis CI and codecov for test coverage. Unit/integration tests should validate edge cases (e.g., malformed HTML, nested tags, script injection attempts).

Technical Risk

  • Security Risks:
    • Misconfiguration: Default settings may be too permissive or restrictive. Validate against OWASP XSS prevention guidelines (e.g., ESAPI).
    • Cache Poisoning: If using caching (cachePath), ensure storage_path('app/purifier') is writable and protected from unauthorized access.
    • Denial of Service (DoS): Complex HTML inputs could exhaust memory. Consider adding input size limits or async processing for large payloads.
  • Performance Risks:
    • Cold Start: First-time purifications compile configurations, adding latency. Pre-warm the cache in production (e.g., via artisan purifier:warm if supported).
    • Memory Leaks: Long-running processes (e.g., queues) with repeated purifications may leak memory. Monitor with tools like Blackfire or Xdebug.
  • Deprecation Risks:
    • Laravel 12+ compatibility is recent (last release: 2026-04-15). Monitor for breaking changes in future Laravel minor versions (e.g., service provider auto-discovery updates).

Key Questions

  1. Use Case Alignment:
    • Is this primarily for UGC (e.g., comments, forum posts) or structured data (e.g., CMS content)?
    • Are there specific HTML tags/attributes that must be preserved (e.g., semantic HTML5, custom data attributes)?
  2. Performance Requirements:
    • What’s the expected volume of HTML to purify per second/minute?
    • Can purifications be batched or offloaded (e.g., queue workers)?
  3. Configuration Strategy:
    • Should configurations be centralized (published config file) or dynamic (per-request)?
    • Are there plans to extend HTML Purifier’s definitions (e.g., adding MathML, SVG)?
  4. Testing Coverage:
    • Are there existing tests for XSS vectors (e.g., <script>, onerror=, SVG exploits)?
    • Should fuzz testing be incorporated into CI/CD?
  5. Maintenance:
    • Who will own updates to HTML Purifier’s core or Laravel compatibility?
    • Is there a fallback plan if the package is abandoned (e.g., fork or migrate to spatie/laravel-html-sanitizer)?

Integration Approach

Stack Fit

  • Laravel Core:
    • Service Provider: Auto-discovered in Laravel 5.5+, reducing boilerplate. For older versions, manual registration is straightforward.
    • Facade: Purifier facade provides clean syntax (Purifier::clean()) and integrates with Laravel’s IoC container.
    • Middleware: Ideal for sanitizing incoming requests (e.g., SanitizeInputMiddleware for API endpoints).
    • Eloquent: Custom casts (CleanHtml) enable automatic sanitization in models, aligning with Laravel’s active record pattern.
  • PHP Extensions:
    • Requires dom, filter, and json extensions (standard in most Laravel deployments).
    • PHP 7.4+ recommended for Laravel 9/10/11/12 compatibility.
  • Frontend Integration:
    • Works with any frontend (Blade, Vue, React) since it sanitizes server-side. Useful for:
      • WYSIWYG editors (e.g., TinyMCE, CKEditor) where client-side sanitization is insufficient.
      • User uploads (e.g., Markdown, rich text fields).

Migration Path

  1. Assessment Phase:
    • Audit existing HTML handling (e.g., strip_tags, custom regex, or no sanitization).
    • Identify critical paths (e.g., public-facing UGC, admin dashboards).
  2. Pilot Integration:
    • Start with a non-critical endpoint (e.g., a blog comment system).
    • Use the clean() helper or facade in controllers:
      $cleanedHtml = Purifier::clean(request()->input('content'), 'default');
      
    • Test with malicious inputs (e.g., XSS payloads) and edge cases (e.g., nested tags, empty strings).
  3. Configuration Rollout:
    • Publish the config file (php artisan vendor:publish --provider="Mews\Purifier\PurifierServiceProvider").
    • Customize settings for different use cases (e.g., youtube for embedded media, custom_definition for HTML5).
  4. Eloquent Integration:
    • Add casts to models:
      protected $casts = [
          'description' => CleanHtml::class,
      ];
      
    • Verify that get()/set() methods automatically sanitize.
  5. Middleware Integration:
    • Create middleware to sanitize all incoming HTML:
      namespace App\Http\Middleware;
      use Closure;
      class SanitizeInput
      {
          public function handle($request, Closure $next)
          {
              $request->merge(array_map(
                  fn($value) => Purifier::clean($value, 'default'),
                  $request->only(['content', 'bio', 'description'])
              ));
              return $next($request);
          }
      }
      
    • Register in app/Http/Kernel.php.
  6. Performance Optimization:
    • Enable caching (cachePath in config) for repeated purifications.
    • Consider pre-compiling configurations for high-traffic routes.

Compatibility

  • Laravel Versions: Officially supports 5.5–12. Test thoroughly in your target version (e.g., Laravel 11).
  • PHP Versions: PHP 7.4+ for Laravel 9/10/11/12. PHP 8.0+ recommended for performance.
  • HTML Purifier Version: Underlying ezyang/htmlpurifier (v4.x+) may have breaking changes. Pin the version in composer.json:
    "mews/purifier": "^3.2",
    "ezyang/htmlpurifier": "^4.16"
    
  • Database/ORM: No direct dependencies, but Eloquent casts require Laravel’s model system.

Sequencing

  1. Phase 1: Core Integration (1–2 sprints)
    • Install package, configure defaults, test basic sanitization.
    • Add to critical models/controllers.
  2. Phase 2: Advanced Features (1 sprint)
    • Implement custom configurations (e.g., HTML5, YouTube).
    • Add middleware for global sanitization.
  3. Phase 3: Optimization (Ongoing)
    • Enable caching, monitor performance.
    • Add fuzz testing to CI/CD.
  4. Phase 4: Rollout (Parallel
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.
jayeshmepani/jpl-moshier-ephemeris-php
elnasnato/laraliveui
labrodev/rest-sdk
sampaui/sampaui
babelqueue/php-sdk
facebook/capi-param-builder-php
babelqueue/symfony
hamzi/corewatch
minionfactory/raw-hydrator
hexters/coinpayment
rjcodes/rjcms
act-training/laravel-permissions-manager
alimarchal/laravel-chart-of-accounts
babenkoivan/elastic-scout-driver
mkwebdesign/filament-watchdog-v5
renatomarinho/laravel-page-speed
zedmagdy/filament-business-hours
renatovdemoura/blade-elements-ui
devgeek/beacon-admin
benjamin-rqt/data-watcher-bundle