xemlock/htmlpurifier-html5
HTML5 definitions and tidy/sanitization rules for HTML Purifier, aligned with the WHATWG spec. Purify and normalize dirty HTML5 into valid output with an HTML5-ready config, plus flexible directives (e.g., safely allow YouTube iframes).
HTMLPurifier (via Purifier facade or direct instantiation). Leverages Composer for dependency management, aligning with Laravel’s ecosystem.HTMLPurifier’s existing config system with HTML5-specific directives (e.g., HTML.SafeIframe, Attr.AllowedInputTypes), enabling granular control without monolithic changes.<article>, <section>) or media elements (<video>, <audio>). Critical for Laravel apps targeting accessibility (WCAG) or SEO.HTMLPurifier’s XSS protection while adding HTML5-specific safeguards (e.g., URI.SafeIframeRegexp for iframe whitelisting). Mitigates risks like phishing via <form> elements (configurable via HTML.Forms).HTMLPurifier instance without requiring changes to existing sanitization logic. Example:
// Current Laravel (legacy)
$purifier = new HTMLPurifier();
// Upgraded (HTML5-compliant)
$config = HTMLPurifier_HTML5Config::createDefault();
$purifier = new HTMLPurifier($config);
HTMLPurifier’s API, ensuring existing configs (e.g., HTML.Allowed) remain functional. New directives (e.g., HTML.SafeLink) are additive.purifier package (if used) or standalone ezyang/htmlpurifier. No conflicts with other PHP sanitizers (e.g., DOMDocument).| Risk | Mitigation | Severity |
|---|---|---|
| Breaking Changes | Avoid v0.1.9 (buggy <a> tag). Stick to v0.1.10+ for stable HTML5 support. |
Medium |
| Performance Overhead | Benchmark against base HTMLPurifier; HTML5 definitions add ~10–15% CPU overhead. |
Low |
| Config Complexity | Document default configs (e.g., createDefault()) and provide Laravel-specific examples. |
Medium |
| Laravel Caching | Ensure HTMLPurifier instances are not cached globally (configs may vary by context). |
High |
| Third-Party Conflicts | Test with Laravel’s Str::markdown() or ckeditor packages to ensure no attribute clashes. |
Low |
Use Case Prioritization:
<video> for media-rich apps vs. <article> for CMS)?Security Trade-offs:
<form> elements be always blocked (HTML.Forms = false) or allowed only in trusted contexts?<iframe>/<link> tags? (Example: URI.SafeIframeRegexp for YouTube.)Performance:
HTMLPurifier instances per config.Maintenance:
<dialog> now but block <script> globally.)Alternatives:
HTMLPurifier + custom definitions to reduce overhead?DOMPurifier (JS) + this package (server-side) be a better fit?ezyang/htmlpurifier (base) or Laravel’s purifier facade.allowedContent to match purifier rules).Str::markdown() → sanitize output with this package.HTMLPurifier is used (e.g., Symfony, WordPress plugins). Laravel-specific optimizations (e.g., caching) are optional.| Phase | Action | Tools |
|---|---|---|
| Assessment | Audit current sanitization logic. Identify HTML5 gaps (e.g., missing <video> support). |
php artisan tinker + var_dump() |
| Pilot | Replace HTMLPurifier in one feature (e.g., comment system). Test with real user content. |
Postman + Laravel Dusk |
| Rollout | Update configs globally. Use feature flags for gradual adoption. | Laravel Envoy (for deployments) |
| Optimize | Cache HTMLPurifier instances per config (e.g., Cache::remember). |
Laravel Cache |
HTMLPurifier v4.16+).HTMLPurifier configs will work unchanged. Example:
// Old (HTML4)
$config = HTMLPurifier_Config::createDefault();
// New (HTML5)
$config = HTMLPurifier_HTML5Config::createDefault();
$config->set('HTML.Allowed', 'p,a[href],b,i'); // Still works!
async (for <script>). Ensure your WYSIWYG editors (e.g., CKEditor) don’t inject unsupported ones.HTMLPurifier instances with HTMLPurifier_HTML5Config.<video>, <figure>) via HTML.Allowed.URI.SafeIframeRegexp, Attr.AllowedRel).HTML.Forms = false).HTMLPurifier to validate overhead.createDefault()) and use Laravel’s config caching to prevent runtime overrides.php artisan purifier:validate command to audit allowed elements against WHATWG spec.xemlock/htmlpurifier-html5 for breaking changes (e.g., new HTML5 elements in future releases).^0.1) unless major features are needed.HTMLPurifier's built-in logging ('Core.Logging' => true) to trace sanitization failures.<dialog>": Add it to HTML.Allowed.async not allowed": Update HTML5Config to include <script> attributes.HTMLPurifier core team via GitHub.xemlock/htmlpurifier-html5 or file issues.HTMLPurifier instances per config (e.g., `Cache::foreverHow can I help you explore Laravel packages today?