A personal agent with a browser, terminal, files, and work that keeps going built with CopilotKit and AG-UI.
TypeScript
fix: decode folded adjacent MIME header words (#130)
Huanyi Xie5ce92a3e8b0d
diff --git a/packages/integrations/src/google.ts b/packages/integrations/src/google.tsindex 24b031a..d31d0ce 100644--- a/packages/integrations/src/google.ts+++ b/packages/integrations/src/google.ts@@ -146,8 +146,9 @@ function headers(part?: GmailPart): Map<string, string> {return new Map((part?.headers ?? []).map(({ name, value }) => [name.toLowerCase(), value]));}function decodeHeader(value: string): string {+ // RFC 2047: ignore linear whitespace, including folding, between encoded words.return value- .replace(/(\?=)[ \t]+(?==\?)/g, "$1")+ .replace(/(\?=)(?:[ \t]|\r\n[ \t])+(?==\?)/g, "$1").replace(/=\?([^?]+)\?([bq])\?([^?]*)\?=/gi,(original, charset: string, encoding: string, text: string) => {diff --git a/tests/google.test.ts b/tests/google.test.tsindex 9b06c4f..b108681 100644--- a/tests/google.test.ts+++ b/tests/google.test.ts@@ -88,6 +88,57 @@ test("mail reads nested plain text and attachment references over authenticatedassert.deepEqual(mail.attachments, ["msg1:attach1:Permission%3A%20form.pdf"]);});+test("mail decodes folded adjacent encoded words without inserting header whitespace", async () => {+ const encodedWord = (value: string) => `=?UTF-8?B?${Buffer.from(value).toString("base64")}?=`;+ for (const separator of [" ", "\t", "\r\n ", "\r\n\t", " \r\n \t"]) {+ const client = clientWith(() =>+ json({+ id: "thread1",+ messages: [+ {+ id: "msg1",+ threadId: "thread1",+ payload: {+ headers: [+ {+ name: "Subject",+ value: `${encodedWord("Visit résumé")}${separator}${encodedWord(" details")}`,+ },+ {+ name: "From",+ value: `${encodedWord("Community")}${separator}${encodedWord(" Museum")} <[email protected]>`,+ },+ ],+ },+ },+ ],+ }),+ );+ const [mail] = await client.getThread("thread1");+ assert.equal(mail.subject, "Visit résumé details", JSON.stringify(separator));+ assert.equal(mail.sender, "Community Museum", JSON.stringify(separator));+ assert.equal(mail.from, "[email protected]");+ }+});++test("header decoding preserves spaces encoded inside words and next to plain text", async () => {+ const client = clientWith(() =>+ json({+ id: "thread1",+ messages: [+ {+ id: "msg1",+ threadId: "thread1",+ payload: {+ headers: [{ name: "Subject", value: "Re: =?UTF-8?Q?Visit_r=C3=A9sum=C3=A9?= notes" }],+ },+ },+ ],+ }),+ );+ assert.equal((await client.getThread("thread1"))[0].subject, "Re: Visit résumé notes");+});+test("a body charset TextDecoder does not know falls back to UTF-8 instead of failing the list", async () => {const charsets: Record<string, string> = { known: "utf-8", unknown: "unknown-8bit" };const bodies: Record<string, string> = { known: "Opening hours", unknown: "Café tickets" };