Fix garbled non-MIME-encoded UTF-8 headers#10078
Conversation
Messages with charset=windows-1251 (quoted-printable encoding) display as garbled text in Roundcube. Other clients (Thunderbird, K-9, mutt) display them correctly. `get_message_part()` already converts body charset to UTF-8, but `format_part_body()` converts it again using `$part->charset` which still contains the original charset (e.g. windows-1251). This double conversion produces garbled output. Check `mb_check_encoding()` before converting — if body is already valid UTF-8, skip conversion. Roundcube 1.6.13 + Stalwart mail server, windows-1251 quoted-printable messages now display correctly. Similar UTF-8 detection approach used in roundcube#10078.
b35d2bb to
089e201
Compare
|
@alecpl I cleaned up the PR description. Previous one was too verbose and slop. Would you review this PR? Thank you. |
| $default_charset = $fallback ?: self::get_charset(); | ||
|
|
||
| // RFC 6532: detect raw UTF-8 in headers to avoid wrong charset conversion | ||
| if ($fallback !== false && mb_check_encoding($input, 'UTF-8') && preg_match('/[\x80-\xFF]/', $input)) { |
There was a problem hiding this comment.
You should not assume RCUBE_CHARSET is always UTF-8.
There was a problem hiding this comment.
Then should we change to
$default_charset = 'UTF-8';
instead?
There was a problem hiding this comment.
I have rebased and applied that part.
There was a problem hiding this comment.
for that I had to force push, sorry if it made you inconvenient
|
Independent verification of the current state of this PR (AI-assisted, results verified locally):
Note for maintainers: no CI checks have ever run on this PR (workflow approval pending), so the above may serve as an interim test signal. The branch is based on early-May master; a rebase would let CI run cleanly on current master. |
decode_mime_string()blindly applies the body charset to non-MIME-encoded headers. If the header is raw UTF-8 but the body charset differs, the header gets corrupted.Check for valid UTF-8 before falling back to the body charset, same approach as Thunderbird's
convert8BitHeader(): https://searchfox.org/comm-central/source/mailnews/mime/jsmime/jsmime.mjs#675RFC 6532 (2012) legitimizes raw UTF-8 in email headers via SMTPUTF8 extension.
Screenshots
Before
After