zendframework/zendxml
ZendXml provides secure XML scanning/loading for PHP to help prevent XXE and XML entity expansion (XEE) attacks. It disables external entity loading and rejects documents using ENTITY declarations, returning SimpleXMLElement or DOMDocument. Repository abandoned; moved to laminas/laminas-xml.
Security::scanHtml() method expands the package’s utility beyond generic XML to include HTML-specific security scanning, making it viable for projects requiring:
SanitizeHtmlMiddleware).scanHtml() method can be wrapped in a Laravel service class (e.g., HtmlSanitizer::secure($html)) and integrated into:
SanitizesHtml trait for Illuminate\Foundation\Http\FormRequest).app/Http/Middleware/SanitizeHtmlPayload).ext-dom and ext-libxml (enabled by default in Laravel).Illuminate\Validation\Rule to add HTML-specific rules (e.g., Rule::custom('secure_html')).scanHtml() method mitigates risks like XSS in XML-embedded HTML (e.g., SOAP fault messages).readonly property handling).DOMException/SimpleXMLElement edge cases.replace to alias the package and isolate it from core dependencies.masterminds/html5 for HTML5-specific sanitization.spatie/laravel-html for Laravel-native HTML handling.scanHtml() replace existing XSS protections (e.g., Purifier, HTMLPurifier)? Audit overlap.scanHtml() run? Large HTML fragments may impact memory (test with memory_get_usage()).spatie/laravel-html or masterminds/html5.php-xml (Symfony) or ext-simplexml for modern PHP support.// app/Http/Middleware/SanitizePayload.php
public function handle($request, Closure $next) {
if ($request->isHtml()) {
$request->merge(['sanitized_html' => Security::scanHtml($request->html)]);
}
return $next($request);
}
// app/Rules/SecureHtml.php
public function passes($attribute, $value) {
return is_bool(Security::scanHtml($value));
}
JsonResource to sanitize HTML fields before XML serialization.innerHTML in favor of textContent for security.Security::scanHtml() in a SanitizeHtmlJob for async processing of large payloads.Security::scanHtml() in a Laravel service (e.g., app/Services/HtmlSanitizer).XmlParser to use scanHtml() for HTML fragments in XML.scanHtml().strip_tags() calls).scanHtml() uses scalar types (string, int) compatible with PHP 8.1+.libXmlConstants (e.g., [Attribute] public int $flags).Stringable).DOMDocument/SimpleXMLElement return types align with Laravel’s type system.LONGTEXT columns with doctrine/dbal for migrations.spatie/laravel-activitylog to audit HTML/XML changes.| Step | Task | Dependencies |
|---|---|---|
| 1 | Audit HTML/XML usage | Identify all HTML fields in XML payloads. |
| 2 | Create HtmlSanitizer service |
None |
| 3 | Add middleware for request sanitization | Route groups. |
| 4 | Integrate with XML parsing/generation | XmlParser/XmlGenerator classes. |
| 5 | Test with malicious HTML payloads | Fuzz testing (e.g., <script>alert(1)</script>). |
| 6 | Deprecate legacy sanitization methods | API versioning. |
| 7 | Monitor performance | Laravel Debugbar. |
scanHtml() reduces XSS risks but adds new attack surface (e.g., libXmlConstants misconfiguration).scanHtml() (e.g., legitimate <script> tags in XML comments).roave/security-advisories to scan for LibXML risks.phpstan/extension-installer to analyze DOMDocument usage.libxml_get_errors() for scanHtml() failures.scanHtml() false positives/negatives.<img> tags in XML-embedded HTML."scanHtml() can be slow for large HTML (>1MB). Mitigate with:
Redis::remember('sanitized-html-$hash', ...)).SimpleXMLElement for lightweight parsing.scanHtml() jobs for high-throughput APIs.Illuminate\Cache\Lock for shared sanitization tasks.| Scenario | Impact | Mitigation |
|---|---|---|
| Malicious HTML | XSS in XML responses | Use scanHtml() with LIBXML_NOENT flag. |
| Large HTML | OOM killer, timeouts | Stream-process with `Simple |
How can I help you explore Laravel packages today?