Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 42 additions & 11 deletions server/src/responsibilities/email.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,19 +157,46 @@ function unfoldHeaders(block: string) {
}
return headers;
}
function decodeBody(body: string, encoding: string | undefined) {
/**
* The text a part's transfer-encoded bytes spell, in the charset its Content-Type names.
*
* Base64 and quoted-printable carry bytes, not text: a `charset=iso-8859-1` part saying "caf=E9" is
* "café", and reading those bytes as UTF-8 turned every accented letter into U+FFFD. A charset the
* runtime does not know falls back to UTF-8, as before.
*/
function decodeBytes(bytes: Buffer, charset: string | undefined) {
if (charset) {
try {
return new TextDecoder(charset).decode(bytes);
} catch {
// unknown label: read as UTF-8
}
}
return bytes.toString("utf8");
}
function decodeBody(
body: string,
encoding: string | undefined,
charset: string | undefined,
) {
const kind = encoding?.toLowerCase();
if (kind === "base64")
return Buffer.from(body.replace(/\s+/g, ""), "base64").toString("utf8");
return decodeBytes(
Buffer.from(body.replace(/\s+/g, ""), "base64"),
charset,
);
if (kind === "quoted-printable")
return Buffer.from(
body
.replace(/=\r?\n/g, "")
.replace(/=([0-9A-F]{2})/gi, (_, hex: string) =>
String.fromCharCode(Number.parseInt(hex, 16)),
),
"latin1",
).toString("utf8");
return decodeBytes(
Buffer.from(
body
.replace(/=\r?\n/g, "")
.replace(/=([0-9A-F]{2})/gi, (_, hex: string) =>
String.fromCharCode(Number.parseInt(hex, 16)),
),
"latin1",
),
charset,
);
return body;
}
/** Minimal MIME walk: the first text/plain part, else text/html with tags stripped. */
Expand Down Expand Up @@ -199,7 +226,11 @@ export function extractEmailText(raw: string, depth = 0): string {
plain ?? texts.find((part) => /^text\/html/i.test(part.type));
return chosen ? extractEmailText(chosen.part, depth + 1) : "";
}
const decoded = decodeBody(body, headers.get("content-transfer-encoding"));
const decoded = decodeBody(
body,
headers.get("content-transfer-encoding"),
/charset="?([^";\s]+)"?/i.exec(type)?.[1],
);
return /^text\/html/i.test(type)
? decoded
.replace(/<style[\s\S]*?<\/style>|<script[\s\S]*?<\/script>/gi, "")
Expand Down
23 changes: 23 additions & 0 deletions server/tests/responsibility-triggers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -696,6 +696,29 @@ describe("inbound email via SES -> SNS", () => {
test("MIME text extraction prefers text/plain and decodes quoted-printable", () => {
expect(extractEmailText(mime)).toBe("The disk on db-1 is at 99%.");
});
test("MIME text extraction decodes in the charset the part names", () => {
const latin1 = [
"Content-Type: text/plain; charset=iso-8859-1",
"Content-Transfer-Encoding: quoted-printable",
"",
"Caf=E9 ferm=E9 =E0 minuit.",
].join("\r\n");
expect(extractEmailText(latin1)).toBe("Café fermé à minuit.");
const base64 = [
'Content-Type: text/plain; charset="windows-1252"',
"Content-Transfer-Encoding: base64",
"",
Buffer.from("Na\xefve r\xe9sum\xe9", "latin1").toString("base64"),
].join("\r\n");
expect(extractEmailText(base64)).toBe("Naïve résumé");
const utf8 = [
"Content-Type: text/plain; charset=utf-8",
"Content-Transfer-Encoding: quoted-printable",
"",
"Caf=C3=A9",
].join("\r\n");
expect(extractEmailText(utf8)).toBe("Café");
});
test("signing certificate URLs must be HTTPS on an sns.<region>.amazonaws.com host", () => {
expect(isSnsUrl(CERT_URL, true)).toBe(true);
expect(
Expand Down