fix: MIME Decoding corrupts non-ASCII characters in Base64-encoded words

fromBase64() defaults to returning a UTF-8 decoded string, which is then
passed to codepage.utils.decode() that treats each char code as a raw
byte.
For multi-byte UTF-8 characters, this double-decoding produces garbage
(e.g. "café" becomes "caf退").

Pass returnType="byteArray" so codepage receives raw bytes and performs
the single correct UTF-8 decode.

Closes #2280
This commit is contained in:
Willi Ballenthin 2026-03-24 09:42:48 +01:00
parent b0fa1f8d1b
commit 77fe0721dc
2 changed files with 34 additions and 1 deletions

View File

@ -87,7 +87,7 @@ class MIMEDecoding extends Operation {
end = cur + j + "?=".length; end = cur + j + "?=".length;
if (encoding.toLowerCase() === "b") { if (encoding.toLowerCase() === "b") {
text = fromBase64(text); text = fromBase64(text, undefined, "byteArray");
} else if (encoding.toLowerCase() === "q") { } else if (encoding.toLowerCase() === "q") {
text = this.parseQEncodedWord(text); text = this.parseQEncodedWord(text);
} else { } else {

View File

@ -75,6 +75,39 @@ TestRegister.addTests([
} }
] ]
}, },
{
name: "UTF-8 Base64 non-ASCII",
input: "Subject: =?UTF-8?B?Y2Fmw6k=?=",
expectedOutput: "Subject: café",
recipeConfig: [
{
"op": "MIME Decoding",
"args": []
}
]
},
{
name: "UTF-8 Base64 multibyte CJK",
input: "Subject: =?UTF-8?B?5pel5pys6Kqe?=",
expectedOutput: "Subject: 日本語",
recipeConfig: [
{
"op": "MIME Decoding",
"args": []
}
]
},
{
name: "UTF-8 Base64 ASCII-only",
input: "Subject: =?UTF-8?B?aGVsbG8=?=",
expectedOutput: "Subject: hello",
recipeConfig: [
{
"op": "MIME Decoding",
"args": []
}
]
},
{ {
name: "ISO Decoding", name: "ISO Decoding",
input: "From: =?US-ASCII?Q?Keith_Moore?= <moore@cs.utk.edu>\nTo: =?ISO-8859-1?Q?Keld_J=F8rn_Simonsen?= <keld@dkuug.dk>\nCC: =?ISO-8859-1?Q?Andr=E9?= Pirard <PIRARD@vm1.ulg.ac.be>\nSubject: =?ISO-8859-1?B?SWYgeW91IGNhbiByZWFkIHRoaXMgeW8=?=\n=?ISO-8859-2?B?dSB1bmRlcnN0YW5kIHRoZSBleGFtcGxlLg==?=", input: "From: =?US-ASCII?Q?Keith_Moore?= <moore@cs.utk.edu>\nTo: =?ISO-8859-1?Q?Keld_J=F8rn_Simonsen?= <keld@dkuug.dk>\nCC: =?ISO-8859-1?Q?Andr=E9?= Pirard <PIRARD@vm1.ulg.ac.be>\nSubject: =?ISO-8859-1?B?SWYgeW91IGNhbiByZWFkIHRoaXMgeW8=?=\n=?ISO-8859-2?B?dSB1bmRlcnN0YW5kIHRoZSBleGFtcGxlLg==?=",