rhukster/dom-sanitizer
MIT-licensed PHP 7.3+ DOM/SVG/MathML sanitizer using DOMDocument and DOMPurify-based allowlists. Remove dangerous tags/attributes, strip namespaces and PHP/HTML/XML tags, and optionally compress output. Supports HTML, SVG, and MathML modes.
DOMDocument for parsing and validation, ensuring compatibility with Laravel’s existing DOM manipulation tools (e.g., str_get_html, DOMDocument extensions).Blade escaping, htmlspecialchars).FormRequest, API payloads) or outgoing responses (e.g., SVG/XML exports).illuminate.queue.working, illuminate.auth.attempting) for pre/post-processing.&) or under-sanitization (e.g., missing edge-case XSS vectors). Mitigate via:
phpunit assertions on sanitized output).spatie/laravel-html, laravelcollective/html).simplexml_load_string) that need migration?<script> tag in user upload")?FormRequest::sanitize()).XmlSanitizerService).@sanitize) for templating.php-dom and php-xml are enabled (Laravel’s default).libxml_disable_entity_loader() is supported (PHP 7.3+).beforeSave Eloquent observer).Attribute Casting to auto-sanitize on retrieval.strip_tags) that can be replaced.DOMSanitizer.Deprecates trait for custom methods.spatie/laravel-html (use DOMSanitizer for XML/SVG, spatie for HTML).laravelcollective/html (sanitize separately if both are needed).dompdf/dompdf (sanitize inputs before PDF generation).svg/svgo, intervention/image (for SVG thumbnails).// app/Providers/AppServiceProvider.php
public function register()
{
$this->app->singleton(DOMSanitizer::class, function () {
return new DOMSanitizer(DOMSanitizer::HTML);
});
}
// app/Http/Middleware/SanitizeXml.php
public function handle($request, Closure $next)
{
if ($request->has('xml_content')) {
$request->merge([
'sanitized_xml' => app(DOMSanitizer::class)->sanitize($request->xml_content)
]);
}
return $next($request);
}
// app/Services/XmlSanitizerService.php
public function sanitizeHtml(string $input): string
{
return app(DOMSanitizer::class)->sanitize($input, [
'remove-html-tags' => false,
]);
}
public function testSanitizesXxe()
{
$input = '<!DOCTYPE foo [ <!ENTITY xxe SYSTEM "file:///etc/passwd"> ]><svg><use>&xxe;</use></svg>';
$output = $this->sanitizer->sanitize($input);
$this->assertNotContains('xxe', $output);
}
\Log::debug('Sanitized XML', ['input' => $input, 'output' => $output]);
composer update will handle upgrades.config/sanitizer.php) for allowlists to avoid duplication:How can I help you explore Laravel packages today?