95 lines
2.9 KiB
TypeScript
95 lines
2.9 KiB
TypeScript
import { describe, expect, it } from "vitest";
|
|
import {
|
|
attachments,
|
|
decodeHeaderValue,
|
|
parseHeaders,
|
|
parseMessage,
|
|
walkParts,
|
|
} from "../../src/core/mime";
|
|
|
|
const MULTIPART = `From: =?UTF-8?Q?Ada_=E2=9C=93?= <ada@example.test>\r
|
|
Subject: folded\r
|
|
value\r
|
|
MIME-Version: 1.0\r
|
|
Content-Type: multipart/mixed; boundary="b"\r
|
|
\r
|
|
--b\r
|
|
Content-Type: text/plain; charset=utf-8\r
|
|
Content-Transfer-Encoding: quoted-printable\r
|
|
\r
|
|
hello=20world\r
|
|
--b\r
|
|
Content-Type: application/octet-stream\r
|
|
Content-Disposition: attachment; filename*=UTF-8''report%20one.txt\r
|
|
Content-Transfer-Encoding: base64\r
|
|
\r
|
|
YWJj\r
|
|
--b--`;
|
|
|
|
describe("MIME parser", () => {
|
|
it("unfolds fields and decodes RFC 2047 words", () => {
|
|
const headers = parseHeaders(
|
|
"Subject: =?UTF-8?Q?Hello_=E2=9C=93?=\r\n\tworld",
|
|
);
|
|
expect(headers[0]?.value).toBe("Hello ✓ world");
|
|
expect(decodeHeaderValue("=?ISO-8859-1?Q?Andr=E9?=")).toBe("André");
|
|
expect(decodeHeaderValue("=?UTF-8?Q?joined?= =?UTF-8?Q?_words?=")).toBe(
|
|
"joined words",
|
|
);
|
|
});
|
|
|
|
it("builds nested parts and decodes transfer encodings", () => {
|
|
const parsed = parseMessage(MULTIPART);
|
|
expect(walkParts(parsed.root).map((part) => part.mediaType)).toEqual([
|
|
"multipart/mixed",
|
|
"text/plain",
|
|
"application/octet-stream",
|
|
]);
|
|
expect(parsed.root.children[0]?.text).toBe("hello world");
|
|
expect([...parsed.root.children[1]!.bytes]).toEqual([97, 98, 99]);
|
|
expect(attachments(parsed.root)[0]).toMatchObject({
|
|
filename: "report one.txt",
|
|
size: 3,
|
|
});
|
|
});
|
|
|
|
it("parses nested message/rfc822 entities", () => {
|
|
const parsed = parseMessage(
|
|
"From: a@example.test\nContent-Type: message/rfc822\n\nFrom: b@example.test\nContent-Type: text/plain\n\nnested",
|
|
);
|
|
expect(parsed.root.children[0]?.text).toBe("nested");
|
|
});
|
|
|
|
it("decodes and exposes transferred message attachments", () => {
|
|
const nested = btoa(
|
|
"From: b@example.test\r\nContent-Type: text/plain\r\n\r\nnested",
|
|
);
|
|
const parsed = parseMessage(
|
|
`From: a@example.test\nContent-Type: message/rfc822; name="forwarded.eml"\nContent-Disposition: attachment; filename="forwarded.eml"\nContent-Transfer-Encoding: base64\n\n${nested}`,
|
|
);
|
|
expect(parsed.root.children[0]?.text).toBe("nested");
|
|
expect(attachments(parsed.root)[0]).toMatchObject({
|
|
filename: "forwarded.eml",
|
|
});
|
|
expect(attachments(parsed.root)[0]?.size).toBeGreaterThan(0);
|
|
});
|
|
|
|
it("fails closed for malformed and excessive structures", () => {
|
|
expect(() => parseMessage("Bad header\n\nbody")).toThrow(
|
|
/malformed header/iu,
|
|
);
|
|
expect(() =>
|
|
parseMessage("Content-Type: multipart/mixed; boundary=x\n\nnone"),
|
|
).toThrow(/boundary/iu);
|
|
expect(() =>
|
|
parseMessage("From: a@example.test\n\n12345", {
|
|
maxChars: 4,
|
|
maxHeaders: 2,
|
|
maxParts: 2,
|
|
maxDepth: 2,
|
|
maxDecodedBytes: 8,
|
|
}),
|
|
).toThrow(/limit/iu);
|
|
});
|
|
});
|