An object-oriented PHP library for sanitizing untrusted HTML input to prevent XSS and other injection attacks.
Symfony HtmlSanitizer is a PHP library that provides a configurable, object-oriented API to clean untrusted HTML input before it is inserted into a document's DOM. It helps prevent cross-site scripting (XSS) attacks and other security vulnerabilities by removing or neutralizing dangerous elements and attributes while preserving safe content. The component includes safe defaults based on W3C Sanitizer API standards and allows granular control over allowed HTML.
PHP developers, particularly those using the Symfony framework, who need to securely process user-generated HTML content in web applications, such as in CMS, forums, or comment systems.
Developers choose this component for its security-first design, fine-grained configurability for element and attribute control, and adherence to modern standards like the W3C Sanitizer API. It offers robust protection against XSS with flexible options for URL security, context-aware sanitization, and custom attribute sanitizers.
Provides an object-oriented API to sanitize untrusted HTML input for safe insertion into a document's DOM.
Open-Awesome is built by the community, for the community. Submit a project, suggest an awesome list, or help improve the catalog on GitHub.
Allows precise allowance, blocking, or dropping of specific HTML elements and attributes, as shown with methods like allowElement() and blockElement() in the README.
Includes preset configurations like allowSafeElements() and allowStaticElements() that align with W3C Sanitizer API standards, offering a secure starting point out of the box.
Enforces HTTPS, restricts allowed schemes and hosts for links and media, and controls relative URLs, preventing malicious URL injections through methods like forceHttpsUrls().
Provides sanitizeFor() method to tailor HTML cleaning for contexts like 'head' or 'textarea', with appropriate encoding, as demonstrated in the usage examples.
Supports custom attribute sanitizers through AttributeSanitizerInterface, allowing developers to add specialized logic beyond built-in rules.
The API requires detailed, verbose setup for advanced rules, which can be error-prone and overwhelming for developers new to HTML sanitization nuances.
Optimized for Symfony projects; using it outside this framework may require additional setup and familiarity with Symfony's architecture, adding overhead.
As noted in the README, safe defaults like allowStaticElements() may still allow dangerous behaviors like CSS injection, requiring custom configuration for full protection.