Fix grapheme-vs-UTF-16 range bug in RichContentFormatter#25833
Draft
jkmassel wants to merge 1 commit into
Draft
Conversation
Collaborator
Generated by 🚫 Danger |
Contributor
|
| App Name | WordPress | |
| Configuration | Release-Alpha | |
| Build Number | 33471 | |
| Version | PR #25833 | |
| Bundle ID | org.wordpress.alpha | |
| Commit | ea3df4b | |
| Installation URL | 399rqo9jjpgqo |
Contributor
|
| App Name | Jetpack | |
| Configuration | Release-Alpha | |
| Build Number | 33471 | |
| Version | PR #25833 | |
| Bundle ID | com.jetpack.alpha | |
| Commit | ea3df4b | |
| Installation URL | 6lrbd8cocbo9g |
jkmassel
force-pushed
the
jkmassel/richcontentformatter-utf16-range
branch
5 times, most recently
from
July 24, 2026 18:25
ffbff45 to
af67b42
Compare
RichContentFormatter built its NSRanges from `content.count` (Swift grapheme count), but NSRegularExpression matches over UTF-16. With multi-code-unit characters (emoji, flags, combining sequences) the grapheme count is shorter than the UTF-16 length, so the search range was truncated and any tag or style near the end silently escaped stripping. removeTrailingBreakTags also fed a UTF-16 match offset to String.index(_:offsetBy:), which counts graphemes — right for ASCII, a crash once the range was corrected. Range over UTF-16 via `String.utf16.count`, and convert the trailing-BR match with Range(_:in:). Adds one isolated test per fix site — each forbidden-tag, div/paragraph, filterNewLines, inline-style, and trailing-break site, plus the trailing-break index-offset cut — using astral emoji, ZWJ sequences, flags, keycaps, skin-tone modifiers, and an NFD combining mark, so reverting any single site breaks exactly one test (verified by reverting each). Two further tests pin the exact off-by-one boundary and confirm the corrected range strips the intended tag rather than everything. Each fails on the old code and passes now, and the exact-output assertions confirm the clusters survive byte-for-byte.
jkmassel
force-pushed
the
jkmassel/richcontentformatter-utf16-range
branch
from
July 24, 2026 18:34
af67b42 to
ea3df4b
Compare
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.


Description
RichContentFormatterbuilds theNSRangeit hands toNSRegularExpressionfromcontent.count— Swift's grapheme cluster count — butNSRegularExpressionmatches over UTF‑16. When content contains multi-code-unit characters (emoji, flags, ZWJ/combining sequences) the grapheme count is shorter than the UTF‑16 length, so the search range is truncated at the end and any forbidden tag or inline style near the end silently escapes stripping — the unsanitized markup then reaches the rendered Reader post or comment.Concretely: a single family emoji
👨👩👧👦is one grapheme but eleven UTF‑16 units, so a<script>placed right after it starts ten units past where the grapheme-count range ends — the whole tag falls outside the search and survives.removeTrailingBreakTagscarried a second form of the same confusion — it fed a UTF‑16 match offset toString.index(_:offsetBy:), which counts graphemes. That was already wrong for multi-code-unit content, and would have become a hard crash (String index is out of bounds) the moment the range was widened, so the two are fixed together.Fix
content.utf16.countinstead ofcontent.count— 12 sites acrossremoveForbiddenTags,normalizeParagraphs,filterNewLines,removeInlineStyles, andremoveTrailingBreakTags.Stringrange withRange(match.range, in: content)rather than a grapheme offset.No behavior change on ASCII content — there the grapheme count equals the UTF‑16 length, which is why this went unnoticed.
parseValueForAttribute,formatGutenbergGallery, andformatVideoTagsalready ranged overNSString.lengthand are untouched.Tests
One isolated test per fix site — each forbidden-tag regex, each
normalizeParagraphssub-site, the threefilterNewLinespaths, the inline-style site, and the trailing-break range plus its index-offset cut — using a spread of multi-code-unit clusters (astral emoji, a ZWJ family, a flag, a keycap, a skin-tone modifier, and an NFD combining mark) positioned so the target token lands in the tail the grapheme-count range truncated. Each input holds only one tag type, so reverting any single site breaks exactly one test (confirmed by reverting each of the 13 sites individually); the exact-output assertions also confirm the cluster survives byte-for-byte. Two further tests pin the exact off-by-one boundary — a token whose closing delimiter is the single UTF‑16 unit the grapheme-count range drops — and confirm the corrected range strips the intended tag rather than everything (a style attribute in range plus one in the tail; only the tail one was surviving).Verified by generating the set into a scratch suite and running it against both the buggy base source and the fixed HEAD:
Executed 23 tests, with 0 failures.Stacking
Stacked on #25832 (base
jkmassel/wordpressdata-swift-test). That PR relocatesRichContentFormatterinto cross-platformWordPressSharedand makesRichContentFormatterTestsrun on macOS underswift test, which is what lets this fix and its regression tests live in that cross-platform set. The bug is pre-existing — it's also ontrunk, in theWordPressSharedUIcopy. Once #25832 merges, this PR retargets totrunk.Testing instructions
swift test --filter RichContentFormatterTests(macOS host) — 23/23 pass. 15 of the 16 new tests fail on the pre-fix base source — the 13 site-guards (each with its one tag left unstripped) plus the two boundary/selectivity tests — and all pass with the fix; the idempotence guard passes both.