stevebauman/purify
Laravel wrapper for HTMLPurifier to sanitize user-submitted HTML and prevent XSS. Clean strings or arrays via the Purify facade, with support for per-call (dynamic) configuration and published config for app-wide rules.
Purify) and service provider (PurifyServiceProvider) align with Laravel’s architectural patterns.default, comments), enabling granular control over HTML sanitization rules across different parts of the application (e.g., user-generated content vs. admin panels).Definition and CssDefinition classes, enabling teams to tailor HTML/CSS rules to domain-specific needs (e.g., Trix editor support, custom elements/attributes).CacheDefinitionCache) or filesystem caching, reducing performance overhead in production by avoiding repeated serialization of HTMLPurifier definitions.composer require, vendor:publish), and the package requires minimal setup (PHP 7.4+, Laravel 7.0+).PurifyHtmlOnGet cast integrates natively with Eloquent models, reducing boilerplate.ezyang/htmlpurifier (a battle-tested library), ensuring reliability. No conflicting dependencies with Laravel’s core or popular packages (e.g., Blade, Livewire).Purify facade for isolated tests).purify:clear) could disrupt unrelated cached data. Mitigation: Use a dedicated cache store/disk for Purify.serializer: null) in production could degrade performance due to repeated definition serialization. Monitor CPU/memory usage post-deployment.Definition/CssDefinition classes require familiarity with HTMLPurifier’s API. Risk of misconfiguration (e.g., allowing unsafe elements) if not validated thoroughly.default, comments) and gradually extend. Use the HTMLPurifier ConfigDoc as a reference.settings to configs.default).<script> tags) could expose XSS vulnerabilities. Validate all custom definitions against OWASP guidelines.HTML.ForbiddenElements and CSS.AllowedProperties arrays to restrict allowed content strictly.Configuration Strategy:
comments, rich_text_editor) will the application need? Will dynamic configurations (e.g., Purify::config($name)) be used frequently?Caching Strategy:
purify:clear.Custom Definitions:
Performance Baseline:
Integration with Frontend:
Upgrade Cadence:
Purify facade aligns with Laravel’s Hash, Cache, and Str facades, reducing learning curves for developers.PurifyHtmlOnGet/PurifyHtmlOnSet casts integrate natively with Eloquent, enabling seamless sanitization for model attributes (e.g., content, description).config/purify.php and dependency injection.dom, mbstring are typically enabled for HTML processing).Assessment Phase:
Pilot Integration:
composer require stevebauman/purify
php artisan vendor:publish --provider="Stevebauman\Purify\PurifyServiceProvider"
default config and test sanitization on a non-critical endpoint (e.g., a blog comment system).strip_tags, regex) with Purify::clean().Configuration Rollout:
comments, rich_text) in config/purify.php based on use cases.'configs' => [
'comments' => [
'HTML.Allowed' => 'p,b,i,a[href],ul,ol,li,br',
'AutoFormat.RemoveEmpty' => true,
],
'rich_text' => [
'HTML.Allowed' => 'div,p,h1,h2,h3,b,i,u,a[href|target],img[src|alt],table,tr,td',
'CSS.AllowedProperties' => 'font-size,color,text-align',
],
],
Purify::config('comments')->clean($input) for context-specific sanitization.Eloquent Integration:
PurifyHtmlOnGet casts to models where HTML is rendered:
class Post extends Model {
protected $casts = [
'body' => PurifyHtmlOnGet::class, // Uses 'default' config
'description' => PurifyHtmlOnGet::class.':rich_text', // Uses 'rich_text' config
];
}
casts() method.Custom Definitions (Optional):
Definition class:
namespace App\Purify;
use Stevebauman\Purify\Definitions\Definition;
use HTMLPurifier_HTMLDefinition;
class TrixDefinition implements Definition {
public static function apply(HTMLPurifier_HTMLDefinition $definition) {
// Add Trix-specific elements/attributes
}
}
config/purify.php:
'definitions' => \App\Purify\TrixDefinition::class,
Caching Setup:
config/purify.php:
'serializer' => storage_path('app/purify-cache'),
php artisan purify:clear
Testing:
use Stevebauman\Purify\Facades\Purify;
use Tests\TestCase;
class PurifyTest extends TestCase {
public
How can I help you explore Laravel packages today?