typo3/html-sanitizer
Standalone PHP HTML sanitizer from TYPO3. Define sanitization rules via Behavior, apply with Visitors, and get a ready-to-use Sanitizer via Builders/presets. Control allowed tags, attributes, and values; encode or remove invalid nodes and comments.
Strengths:
Behavior class, making it highly customizable for different use cases (e.g., CMS content, user-generated input, or third-party integrations).VisitorInterface enables modular sanitization logic, allowing TPMs to extend or replace sanitization rules without modifying core logic.ALLOW_INSECURE_RAW_TEXT mitigation) demonstrate proactive risk management.Behavior class enforces immutability (e.g., withFlags() returns new instances), reducing side-effect risks in shared environments.Gaps:
CommonBuilder exists, it’s not Laravel-optimized (e.g., lacks Blade template compatibility or CSRF token handling).DOMNode) may introduce latency for high-throughput APIs. Benchmarking against alternatives like HTMLPurifier or DOMDocument-based solutions is recommended.Illuminate\Validation or Illuminate\Http\Request middleware.SanitizeHtmlMiddleware).public function handle(Request $request, Closure $next) {
$request->merge(['sanitized_body' => $this->sanitizer->sanitize($request->input('body'))]);
return $next($request);
}
Sanitizer to Laravel’s IoC container for dependency injection:
$this->app->singleton(Sanitizer::class, function ($app) {
$behavior = (new Behavior())->withTags(...);
return new Sanitizer($behavior, new CommonVisitor($behavior));
});
Behavior settings (e.g., allowing unsafe tags like <script>) could reintroduce XSS vulnerabilities. Requires rigorous testing (e.g., OWASP ZAP scans).v2.3.0 deprecated CommonBuilder->srcsetAttr). Monitor Laravel’s PHP version alignment.htmlspecialchars) for critical paths.htmlspecialchars, DOMDocument) or augment it?Behavior instances) be managed across environments?HTMLPurifier (more features but heavier) or Symfony’s StringUtil (simpler but less flexible).Laravel Ecosystem:
Illuminate\Http\Middleware to sanitize input (e.g., form data, API payloads).Illuminate\View\ViewComposer or Illuminate\Routing\Controller to sanitize dynamic content before rendering.Illuminate\Validation\Rules to include sanitization:
use TYPO3\HtmlSanitizer\Sanitizer;
class Sanitize extends Rule {
public function passes($attribute, $value) {
return $this->sanitizer->sanitize($value) === $value;
}
}
Illuminate\Http\JsonResponse.Illuminate\Bus\Queueable.Third-Party Integrations:
Parsedown).config('features.html_sanitizer')) to toggle integration.strip_tags) with custom Behavior presets.htmlspecialchars for full HTML) over 2–3 releases.v1.5.x (PHP 7.0+ support).dom and libxml extensions (enabled by default in Laravel).composer require typo3/html-sanitizer.Behavior in config/sanitizer.php:
'default' => [
'tags' => [
'a' => ['attrs' => ['href' => ['values' => ['#^https?://#']]]],
'p' => [],
],
'flags' => Behavior::ENCODE_INVALID_TAG,
],
// app/Http/Middleware/SanitizeInput.php
public function handle(Request $request, Closure $next) {
$request->sanitize = function ($field) use ($request) {
return app(Sanitizer::class)->sanitize($request->input($field));
};
return $next($request);
}
// app/Providers/BladeServiceProvider.php
Blade::directive('sanitize', function ($expression) {
return "<?php echo app(\TYPO3\HtmlSanitizer\Sanitizer::class)->sanitize({$expression}); ?>";
});
Usage: @sanitize($userComment)Behavior rules:
public function testSanitizerRemovesScriptTags() {
$sanitizer =
How can I help you explore Laravel packages today?