fix: garbled display of ISO-2022-JP emails with extended characters#381
Open
jo3nzv wants to merge 3 commits into
Open
fix: garbled display of ISO-2022-JP emails with extended characters#381jo3nzv wants to merge 3 commits into
jo3nzv wants to merge 3 commits into
Conversation
…ters in ActiveSync.
added 2 commits
April 1, 2026 09:33
…or ISO-2022-JP parts
Contributor
|
Dear @Alinto team, @WoodySlum, @c3dr, @ThomasMms23, @QHivert, ... With the recent big Alinto hack, what is the situation?
Thanks in advance. |
Contributor
|
Hi @jo3nzv, thanks for your PR, I'm aware of it and will take a look as soon as I have time. Hello @Neustradamus, This leak does not concern SOGo (opensource) nor SOGomail (Alinto product that uses SOGo). The communication has been made directly with the customers impacted. |
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
Japanese emails declaring
charset=iso-2022-jpthat contain NEC special characters(e.g. circled digits ①–⑳, Roman numerals Ⅰ–Ⅹ) are displayed as garbled ASCII escape
sequences in SOGo. This PR fixes the issue with a two-stage fallback:
cp50220(ISO-2022-JP-MS) via iconv — works on systems with GNU libiconv (e.g. macOS).built-in NEC special character decoder (
bodyStringFromISO2022JPWithNECExtension) thatparses the ISO-2022-JP byte stream directly without relying on iconv CP50220 support.
Problem
Symptom
An email with
Content-Type: text/plain; charset="ISO-2022-JP"andContent-Transfer-Encoding: 7bitis rendered as garbled text like:instead of the correct Japanese text.
Root Cause
The email claims
charset=ISO-2022-JPbut its body contains characters fromJIS X 0208 row 13 (NEC special characters: circled digits ①–⑳, Roman numerals Ⅰ–Ⅹ).
This row was introduced by NEC as a proprietary extension and is not defined in
standard ISO-2022-JP (RFC 1468). They are however included in Microsoft's Windows code
page CP50220 (also known as ISO-2022-JP-MS), which is the encoding actually used by
many Japanese Windows email clients.
The following failure chain results:
[NSString stringWithData:usingEncodingNamed:@"iso-2022-jp"]is called viaSOPE's
NSString+Encodingwhich internally invokesiconv.iconvencounters the undefined JIS row 13 and returnsEILSEQ(illegal byte sequence). The SOPE wrapper returns
nil.nilresult satisfies![bodyString length], triggering the UTF-8 fallback.therefore valid UTF-8. The UTF-8 decode succeeds silently, producing a string
that contains the raw JIS escape sequences as printable ASCII characters
(e.g.
$B,(B). The ESC byte (0x1B) is typically invisible or stripped duringHTML rendering.
Fix
Before falling back to UTF-8, retry the conversion in two stages:
cp50220(ISO-2022-JP-MS / Windows code page 50220) via iconv. Works on systemswith GNU libiconv (macOS, etc.).
bodyStringFromISO2022JPWithNECExtension— a new built-in decoder added toNSData+Mail.mthat parses the ISO-2022-JP byte stream directly. It handles JISescape sequences manually, delegates standard JIS pairs to iconv (
iso-2022-jp),and maps NEC special characters (JIS row 13, first byte
0x2D) to Unicode using asmall lookup table — with no dependency on CP50220 iconv support. This ensures the
fix works on Ubuntu 24.04 / glibc where CP50220 is not available.
If both attempts fail the existing fallback chain (UTF-8 → Latin-1) continues
unchanged, so there is no regression.
Changed Files
SoObjects/Mailer/NSData+Mail.hbodyStringFromISO2022JPWithNECExtensionSoObjects/Mailer/NSData+Mail.mcp50220retry + newbodyStringFromISO2022JPWithNECExtensionmethod inbodyStringFromCharset:SoObjects/Mailer/SOGoMailObject.mcp50220+ built-in NEC decoder retry instringForData:partInfo:ActiveSync/SOGoMailObject+ActiveSync.mcp50220+ built-in NEC decoder retry in three call sites used by the ActiveSync protocol handlerAdditional fixes
Fix 2: Standard ISO-2022-JP garbled when SOPE returns pre-decoded NSString (b4a2b73)
When SOPE successfully decodes an ISO-2022-JP body and returns an
NSString(instead ofNSData), the previous code re-encoded it to UTF-8 bytes and fed them through the NEC decoder — producing garbled output for standard Japanese characters like ■ or ━.Fix: only attempt Latin-1 re-encoding to recover raw 7-bit bytes. If that fails (the string already contains non-Latin-1 Unicode), use the string as-is.
Fix 3: Standard ISO-2022-JP body not delivered via ActiveSync (b847355)
NSString *swas declared but not initialized tonil. In the NSString branch, when Latin-1 re-encoding failed,if (!s) s = bodycould silently skip the assignment ifsheld a garbage (non-nil) value, resulting in an empty/crashed body response and "This message has not been downloaded from the server" on iOS.Fix: initialize
NSString *s = nil.