fix(web): replace regex HTML sanitization with sanitize-html#171
Merged
Conversation
Closes the remaining CodeQL findings in apps/web/src/lib/github.ts:
12 × js/incomplete-multi-character-sanitization, 2 × js/bad-tag-filter,
1 × js/double-escaping — all flagging the fundamental fact that
regex-based HTML stripping is bypass-able.
`sanitize-html` (v2.17) uses a real HTML parser (htmlparser2) so
malformed/nested tags and encoded entities can't escape the allowlist.
Two configurations preserve current call sites' contracts:
- sanitizeContent → allowedTags: [] (plain text, used in DiffViewer
for content fetched via GitHub API).
- sanitizeExcerpt → allowedTags: ['mark'] (Pagefind highlight wrapper,
rendered via {@html} in SearchBar).
One existing test changed expectation: the prior regex would decode
HTML entities and re-strip — sanitize-html preserves entities as
entities, which is the actually-safe behavior (entity-encoded text
renders as literal text, never as parsed HTML). Test updated to
assert no raw `<`/`>` in the output rather than the specific decoded
form, which captures the security property.
Verified: 269 → 269 tests pass (one updated assertion), pnpm build /
typecheck / lint all clean.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
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
Closes the remaining 15 CodeQL findings in
apps/web/src/lib/github.ts:js/incomplete-multi-character-sanitizationjs/bad-tag-filterjs/double-escapingAll flagged the fundamental fact that regex-based HTML stripping is bypass-able.
sanitize-html(v2.17) uses a real HTML parser (htmlparser2) so malformed/nested tags and encoded entities cannot escape the allowlist.Two configs preserve existing contracts
sanitizeContent→allowedTags: [](plain text). Used byDiffViewer.sveltefor content fetched from the us-code repo via GitHub API.sanitizeExcerpt→allowedTags: ['mark'](Pagefind highlight wrapper). Rendered via{@html ...}inSearchBar.svelte.Test change worth flagging
One test changed expectation: the prior regex would decode
<script>...then strip —sanitize-htmlpreserves entities as entities, which is the actually-safe behavior (entity-encoded text renders as literal text, never as parsed HTML). Test updated to assert no raw</>in the output rather than the specific decoded form — captures the security property without coupling to the legacy regex's "decode then strip" pattern.Test plan
pnpm installresolves cleanly (added: sanitize-html 2.17.3 + @types/sanitize-html 2.16.1)pnpm build(Astro SSG + Pagefind index) passes — sanitize-html survives the SSR/bundle pathpnpm test(269 tests) greenpnpm typecheckcleanpnpm lintclean🤖 Generated with Claude Code