Fix incomplete multi-character HTML sanitization in AI tool result previews - #4260
Merged
Conversation
…eviews A single-pass tag-stripping regex could let a crafted rendered field recombine into a live tag after removal. Strip tags iteratively until the output is stable. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Collaborator
📊 Performance Test ResultsComparing f2ecde1 vs trunk app-size
site-editor
site-startup
Results are median values from multiple test runs. Legend: 🟢 Improvement (faster) | 🔴 Regression (slower) | ⚪ No change (<50ms diff) |
youknowriad
reviewed
Jul 23, 2026
| if ( value && typeof value === 'object' && 'rendered' in value ) { | ||
| const rendered = ( value as { rendered?: unknown } ).rendered; | ||
| return typeof rendered === 'string' ? rendered.replace( /<[^>]*>/g, '' ) : ''; | ||
| return typeof rendered === 'string' ? stripHtmlTags( rendered ) : ''; |
Contributor
There was a problem hiding this comment.
There's probably a @wordpress/ package that offers a utility like that no? html-entities maybe?
Contributor
Author
There was a problem hiding this comment.
Sure, it makes sense to reuse the existing library. However, html-entities does completely different thing as it decodes HTML entities. There is also remove-invalid-html but it operates on DOM nodes. We could use a library like https://www.npmjs.com/package/sanitize-html, but it seems to be overkill in this case.
Contributor
There was a problem hiding this comment.
You're right, thanks for looking.
youknowriad
approved these changes
Jul 23, 2026
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.
Related issues
js/incomplete-multi-character-sanitization, CWE-20/80/116, high severity)How AI was used in this PR
Claude Code investigated the CodeQL alert, applied the fix, added a regression test, and ran lint / typecheck / the affected test suite. I reviewed the change.
Proposed Changes
When rendering previews of WordPress.com API tool results in the AI chat, Studio strips HTML tags out of
renderedfields (post titles, etc.) so the summary shows plain text. That stripping used a single-pass regex replacement, which is unsafe: for a crafted value such as<scr<script>ipt>, removing the inner match leaves the outer fragments to recombine back into a live<script>tag. A malicious or compromised API response could therefore smuggle a tag through the sanitizer.The tag-stripping now runs iteratively until the output stops changing, so no tag can reappear after removal. This is display-only sanitization for the tool-result summary line; behavior for normal (non-adversarial) content is unchanged.
Testing Instructions
npm test -- packages/common/ai/tests/tools.test.ts— passes, including a new test that feeds a recombining<scr<script>ipt>…payload throughgetToolResultPreviewand asserts no<script(nor any<) survives in the summary.npm run typecheck— passes.Pre-merge Checklist