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

Purify Laravel Package

stevebauman/purify

Laravel wrapper for HTMLPurifier to sanitize user HTML safely. Clean strings or arrays via the Purify facade, with optional per-call configuration. Publish a config file, tune allowed tags/attributes, and leverage caching for performance.

View on GitHub
Deep Wiki
Context7

Technical Evaluation

Architecture Fit

  • Strengths:

    • Security-First: Leverages HTMLPurifier, a battle-tested library for sanitizing HTML input, addressing XSS vulnerabilities—a critical concern for user-generated content (e.g., comments, rich-text editors).
    • Laravel-Native: Integrates seamlessly with Laravel’s service container, facades, and Eloquent models, reducing boilerplate and aligning with Laravel’s conventions.
    • Configurability: Supports multiple configuration profiles (e.g., default, comments, wysiwyg), enabling granular control over sanitization rules per use case.
    • Performance: Optional caching of HTMLPurifier’s serialized definitions reduces runtime overhead in production.
    • Extensibility: Allows custom HTML/CSS definitions (e.g., for Trix, Markdown, or domain-specific elements) via interfaces like Definition and CssDefinition.
  • Fit for Use Cases:

    • User-Generated Content: Ideal for platforms with rich-text input (e.g., CMS, forums, wikis).
    • API Input Validation: Sanitizes HTML payloads before storage/processing.
    • Legacy System Migration: Can retroactively sanitize existing HTML data (e.g., via Eloquent casts or model mutators).

Integration Feasibility

  • Dependencies:

    • PHP 7.4+: Compatible with Laravel 7+ (LTS support until 2025+).
    • HTMLPurifier: Heavy dependency (~10MB), but justified by security needs. May impact cold starts in serverless environments.
    • Laravel Core: No breaking changes; uses standard Laravel patterns (facades, config publishing, service providers).
  • Compatibility:

    • Laravel Versions: Tested up to Laravel 11; minor adjustments may be needed for future versions (e.g., cast syntax).
    • PHP Extensions: Requires dom, libxml, and filter extensions (common in Laravel deployments).
    • Database: No direct DB dependencies, but integrates with Eloquent for "sanitize-on-get" patterns.

Technical Risk

  • Performance:
    • Cold Start: HTMLPurifier’s first run may introduce latency (~100–500ms) due to definition compilation. Mitigate with caching or lazy-loading.
    • Memory: Complex configurations or large inputs could strain memory. Monitor with memory_get_usage().
  • Configuration Complexity:
    • HTMLPurifier’s config syntax is verbose (e.g., HTML.Allowed). Errors may require deep debugging.
    • Risk of over-permissive rules (e.g., allowing <script> if misconfigured). Validate with tools like OWASP XSS Tester.
  • Caching Pitfalls:
    • Cache invalidation (purify:clear) must be automated (e.g., post-deployment) to avoid stale definitions.
    • Filesystem cache paths must be unique to avoid conflicts with other Laravel caches.
  • Upgrade Path:
    • Breaking changes between major versions (e.g., v4→v5). Test thoroughly during upgrades.

Key Questions

  1. Security Requirements:
    • What’s the acceptable risk of XSS? Are there compliance mandates (e.g., PCI, HIPAA)?
    • Should we whitelist/blacklist specific elements (e.g., <iframe>)?
  2. Performance SLAs:
    • What’s the max tolerable latency for sanitization? Can we pre-compile definitions?
  3. Content Workflow:
    • Is sanitization needed on input (e.g., API validation) or output (e.g., rendering)?
    • Do we need to preserve certain attributes (e.g., data-*, aria-*)?
  4. Customization Needs:
    • Are there non-standard HTML elements (e.g., from a WYSIWYG editor like Trix or CKEditor)?
    • Do we need to support custom CSS properties (e.g., text-align: start)?
  5. Deployment:
    • How will we handle cache invalidation in CI/CD (e.g., post-deploy purify:clear)?
    • Is the cache storage (filesystem/disk) shared across instances (e.g., in containerized environments)?

Integration Approach

Stack Fit

  • Laravel Ecosystem:
    • Facades: Purify::clean() integrates natively with Laravel’s facade pattern, reducing coupling.
    • Eloquent: PurifyHtmlOnGet cast enables declarative sanitization in models (e.g., Post::content).
    • Request Handling: Can be used in middleware (App\Http\Middleware\SanitizeInput) or form requests.
    • APIs: Ideal for sanitizing JSON payloads (e.g., request()->input('description')).
  • Frontend Integration:
    • Works with any frontend that outputs HTML (e.g., Blade, React, Vue). For SPAs, sanitize on the backend to avoid CORS/XSS risks.
  • Third-Party Tools:
    • Complements libraries like Laravel Nova, Filament, or Livewire for rich-text fields.
    • Pairs with Telescope for debugging sanitization failures.

Migration Path

  1. Assessment Phase:
    • Audit existing HTML storage (e.g., database columns, API responses) for malicious content.
    • Identify high-risk areas (e.g., user comments, admin panels).
  2. Pilot Integration:
    • Start with a single model (e.g., Post) using PurifyHtmlOnGet.
    • Test with edge cases (e.g., malformed HTML, nested scripts).
  3. Gradual Rollout:
    • Phase 1: Sanitize on output (low risk, no data loss).
    • Phase 2: Add input sanitization (e.g., API validation).
    • Phase 3: Replace custom regex/string sanitization with Purify.
  4. Configuration Hardening:
    • Define strict HTML.Allowed rules early (e.g., div,p,a[href]).
    • Use multiple configs (e.g., default, wysiwyg) to avoid over-permissive defaults.

Compatibility

  • Laravel Versions:
    • Laravel 7–10: Use protected $casts syntax.
    • Laravel 11+: Use casts() method in models.
    • Laravel 12+: Monitor for changes in facades or service provider booting.
  • PHP Versions:
    • Test on PHP 8.0+ for performance gains (e.g., JIT compilation).
    • Avoid PHP 7.4 if possible (end-of-life in 2024).
  • Database:
    • No schema changes required, but ensure text columns can handle escaped HTML (e.g., TEXT in MySQL).
  • Caching:
    • Prefer CacheDefinitionCache over FilesystemDefinitionCache for shared environments (e.g., Kubernetes).
    • Configure a dedicated cache key (e.g., purify_definitions) to avoid conflicts.

Sequencing

  1. Setup:
    • Install: composer require stevebauman/purify.
    • Publish config: php artisan vendor:publish --provider="Stevebauman\Purify\PurifyServiceProvider".
    • Configure config/purify.php with strict defaults (e.g., minimal HTML.Allowed).
  2. Testing:
    • Write unit tests for Purify::clean() with malicious inputs (e.g., <script>, onerror=).
    • Test edge cases: empty strings, nested tags, malformed HTML.
  3. Deployment:
    • Run php artisan purify:clear post-deploy to invalidate cache.
    • Monitor logs for sanitization errors (e.g., HTMLPurifier_Exception).
  4. Optimization:
    • Enable caching in production (serializer config).
    • Profile performance with tideways/xhprof or Laravel Debugbar.

Operational Impact

Maintenance

  • Configuration Drift:
    • Changes to HTML.Allowed or CSS.AllowedProperties require cache clearing (purify:clear).
    • Document configuration profiles (e.g., comments, wysiwyg) in a CONFIGURATION.md.
  • Dependency Updates:
    • Monitor HTMLPurifier for security patches (e.g., CVE fixes).
    • Test upgrades in staging before production (e.g., v6→v7).
  • Custom Definitions:
    • Maintain custom Definition/CssDefinition classes in a separate module (e.g., app/Definitions/).
    • Version-control custom configs (e.g., config/purify.php) in Git.

Support

  • Debugging:
    • Enable HTMLPurifier’s debug mode in config:
      'debug' => env('PURIFY_DEBUG', false),
      
    • Log sanitized/unsanitized pairs for auditing:
      \Log::debug('Sanitized input', ['raw' => $rawInput, 'clean' => $cleaned]);
      
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
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
twbs/bootstrap4