Add html-safe plugin for sanitized HTML rendering - #4918
Open
JeroenBoersma wants to merge 2 commits into
Open
JeroenBoersma wants to merge 2 commits into
JeroenBoersma wants to merge 2 commits into
Conversation
JeroenBoersma
force-pushed
the
feat/html-safe-plugin
branch
from
September 22, 2026 16:01
ce03338 to
8cb97b6
Compare
Introduces @alpinejs/html-safe, an x-html-safe directive that renders HTML through a fixed tag/attribute allowlist instead of setting innerHTML directly. Works on both the standard and CSP builds, and is intended as a safer alternative to x-html for untrusted content, as well as the only sanitized-HTML option on the CSP build (which drops x-html entirely).
JeroenBoersma
force-pushed
the
feat/html-safe-plugin
branch
from
September 22, 2026 19:43
0fc11ea to
85b7110
Compare
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Adds
@alpinejs/html-safe, a new plugin registering thex-html-safedirective: renders HTML through a fixed tag/attribute allowlist instead of
setting
innerHTMLdirectly. Works on both the standard and CSP builds — theplugin itself contains no CSP-specific code, it only calls
evaluateLater,which routes through whichever evaluator core Alpine provides.
This exists because Alpine's CSP build has no
x-htmldirective at all(rendering arbitrary HTML is exactly what a strict CSP is meant to prevent),
which leaves CSP users with no sanitized-HTML option.
x-html-safeis adistinct directive, not an
x-htmloverwrite, sox-html's own documentedbehavior is unaffected on any build.
What's in this MR
packages/html-safe/— the plugin (package.json,builds/,src/index.js)scripts/build.js— registered in the build listtests/cypress/integration/plugins/html-safe.spec.js— 29 tests, all green,including two
test.csp(...)cases proving it works under CSP and thatplain
x-htmlis untouchedpackages/docs/src/en/plugins/html-safe.md— new plugin docs pagedirectives/html.mdandadvanced/csp.md's CSPlimitations section
README.md— added to the package tableAllowlist
Tags:
a, b, i, em, strong, s, u, span, small, mark, abbr, cite, q, sup, sub, br, code, kbd, samp, var, wbr, p, div, h1-h6, ul, ol, li, dl, dt, dd, blockquote, pre, hr, table, thead, tbody, tfoot, tr, th, td, caption, colgroup, col.id,class,name,style, and all Alpine directives (x-*/:*/@*)are never allowed on any tag — DOM clobbering, layout attacks, and
attacker-controlled resource loading via Tailwind's arbitrary-value class
syntax, respectively. Full attribute-by-attribute reference in the docs page.
Why we're proposing this
We maintain Hyvä, a Magento frontend built on Alpine, and we're already
shipping this plugin in our own theme to fill the gap left by the CSP
build dropping
x-html. We're offering it upstream as a courtesy, in caseit's useful to the Alpine project more broadly, not just to us — sanitized
HTML rendering seems like a generally useful thing for a CSP-focused build
to offer. Not blocking on our end either way: happy to maintain it as a
third-party plugin if it's not a fit for the core project.