typo3/html-sanitizer
Standalone PHP HTML sanitizer from TYPO3. Define sanitizing rules via Behavior, apply multiple Visitors, and run through a Sanitizer built from reusable presets. Supports safe tag/attribute allowlists, value validation (e.g., regex), and encoding or removing invalid nodes.
Behavior + Visitor + Builder), making it highly adaptable to custom sanitization rules. This aligns well with Laravel’s dependency injection and service container patterns, allowing for seamless integration into existing Laravel services or middleware.Behavior class enforces immutability (e.g., withTags(), withoutNodes()), which aligns with Laravel’s functional programming trends and reduces side effects in sanitization logic.dom extension (enabled by default in Laravel).SanitizeHtmlMiddleware) to sanitize input/output globally.HtmlSanitizerServiceProvider) with configurable presets (e.g., CommonBuilder, StrictBuilder).Tag::ALLOW_CHILDREN, Attr::MANDATORY). Documentation is sparse but examples (e.g., CommonBuilder) provide a starting point.DOMDocument may be unfamiliar to Laravel devs accustomed to query builders (e.g., str_get_html).HTMLPurifier or DOMSanitizer.ALLOW_CUSTOM_ELEMENTS flag enables hyphenated tags (e.g., <my-component>), which may conflict with Laravel’s Blade components or Tailwind CSS.RegExpAttrValue) requires careful tuning to avoid false positives/negatives.CommonBuilder) or fully custom rules?CommonBuilder deprecations)?HTMLPurifier (more features, heavier).DOMSanitizer (simpler, less flexible).Str::of()->markdown() (limited to Markdown).request()->input('html_content')).@sanitize($unsafeHtml)).FormRequest with custom sanitization rules:
public function rules() {
return [
'bio' => ['required', new SanitizeHtmlRule($this->sanitizer)],
];
}
HttpTests to verify sanitization of malicious payloads (e.g., <script>alert(1)</script>).Sanitizer in unit tests with createMock(Sanitizer::class).CommonBuilder as a baseline, then customize.strip_tags() calls with Sanitizer.class LegacySanitizerDecorator implements SanitizerInterface {
public function sanitize(string $html): string {
return strip_tags($html, '<b><i><u>');
}
}
dom extension is enabled.spatie/laravel-html).Behavior class may shadow Laravel’s Illuminate\Support\Behavior).TEXT (not VARCHAR) to avoid truncation.composer require typo3/html-sanitizer
config/sanitizer.php:
return [
'presets' => [
'default' => \TYPO3\HtmlSanitizer\Builder\CommonBuilder::class,
'strict' => \App\Sanitizer\StrictBuilder::class,
],
];
// app/Providers/SanitizerServiceProvider.php
public function register() {
$this->app->singleton(Sanitizer::class, function ($app) {
$builder = $app['config']['sanitizer.presets.default'];
return new Sanitizer($builder->build());
});
}
// app/Http/Middleware/SanitizeHtml.php
public function handle($request, Closure $next) {
$request->merge([
'sanitized_content' => $this->sanitizer->sanitize($request->content),
]);
return $next($request);
}
// app/Facades/Sanitizer.php
public static function clean(string $html): string {
return app(Sanitizer::class)->sanitize($html);
}
// app/Providers/BladeServiceProvider.php
Blade::directive('sanitize', function ($expression) {
return "<?php echo app(\TYPO3\HtmlSanitizer\Sanitizer::class)->sanitize({$expression}); ?>";
});
Usage: @sanitize($unsafeHtml)CommonBuilder deprecations in v3.0).composer.json if stability is critical:
"typo3/html-sanitizer": "^2.3"
Behavior/Visitor implementations in a SANITIZATION_RULES.md file.public function test_s
How can I help you explore Laravel packages today?