Skip to content

Reject malformed UTF-8 in tidy_bytes - #1706

Open
adityawrk wants to merge 1 commit into
mikel:masterfrom
adityawrk:fix/tidy-bytes-utf8-bounds
Open

adityawrk wants to merge 1 commit into
mikel:masterfrom
adityawrk:fix/tidy-bytes-utf8-bounds

Conversation

@adityawrk

@adityawrk adityawrk commented Nov 29, 2026 •

Copy link
Copy Markdown

Problem

Mail::Multibyte::Unicode.tidy_bytes treated every byte above 0xF0 as unused. A valid character such as U+40000 (F1 80 80 80) or U+10FFFF (F4 8F BF BF) was split into a Latin-1 lead plus CP1252 bytes.

The same walk accepted a sequence whenever the following bytes merely looked like continuations. An overlong encoding and a truncated euro sign then raised ArgumentError from unpack("U*"). A surrogate (ED A0 80) was left in the result, so the string was still invalid UTF-8.

Fix

A sequence is kept only when it is complete and the codepoint is in range: leads are C2..F4, with the usual second-byte limits for E0, ED, F0, and F4. Anything else is cleaned one byte at a time, which leaves a following character alone.

U+40000 and U+10FFFF now round-trip. U+00E9, the euro sign, an emoji, and the CP1252 quote 0x93 still do too, including tidy_bytes(bytes, true). Overlong, surrogate, out-of-range, and truncated input returns valid UTF-8 instead of raising, and a surrogate no longer hides the é after it.

spec/mail/multibyte_spec.rb and spec/mail/utilities_spec.rb: 106 examples, 0 failures.

Fixes #1680

Valid 4-byte characters from U+40000 upward were split apart, and an
overlong, surrogate, or truncated sequence was kept or raised. A bad
lead is now cleaned alone so the following character stays intact.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Multibyte tidy_bytes accepts malformed UTF-8 sequences and miscomputes boundaries

1 participant