Skip to content

A personal agent with a browser, terminal, files, and work that keeps going built with CopilotKit and AG-UI.

TypeScript

fix(gmail): exclude address comments from mailbox extraction (#203)

BigDataDZ0585a69e9384
diff --git a/packages/integrations/src/google.ts b/packages/integrations/src/google.ts
index 20f7b04..9231493 100644
--- a/packages/integrations/src/google.ts
+++ b/packages/integrations/src/google.ts
@@ -193,10 +193,35 @@ function decodeSnippet(value: string): string {
);
}
function addresses(value: string): string[] {
+ // Comments may contain addresses, nested comments and escaped parentheses.
+ // Quoted display names treat parentheses literally instead.
+ let uncommented = "";
+ let depth = 0;
+ let quoted = false;
+ let escaped = false;
+ // Encoded words are display text, never mailbox syntax, even if their raw
+ // payload happens to contain an address or parentheses.
+ for (const character of value.replace(/=\?[^?]+\?[bq]\?[^?]*\?=/gi, "")) {
+ if (escaped) {
+ if (!depth) uncommented += character;
+ escaped = false;
+ } else if (character === "\\") {
+ if (!depth) uncommented += character;
+ escaped = true;
+ } else if (!depth && character === '"') {
+ quoted = !quoted;
+ uncommented += character;
+ } else if (!quoted && character === "(") {
+ if (!depth) uncommented += " ";
+ depth++;
+ } else if (depth && character === ")") {
+ depth--;
+ } else if (!depth) uncommented += character;
+ }
// A quoted display name may itself contain an e-mail-looking string; only
// addresses outside display names count.
return (
- value
+ uncommented
.replace(/"(?:[^"\\]|\\.)*"\s*(?=<)/g, " ")
.match(/[A-Za-z0-9.!#$%&'*+/=?^_`{|}~-]+@[A-Za-z0-9.-]+/g) ?? []
);
@@ -340,8 +365,9 @@ function mapMessage(message: z.infer<typeof messageSchema>): Mail {
if (!attached) for (const child of part.parts ?? []) visit(child, depth + 1);
};
if (message.payload) visit(message.payload, 0);
- const from = decodeHeader(metadata.get("from") ?? "");
- const address = addresses(from)[0] ?? from;
+ const rawFrom = metadata.get("from") ?? "";
+ const from = decodeHeader(rawFrom);
+ const address = addresses(rawFrom)[0] ?? from;
const displayName = from.includes("<")
? from.slice(0, from.indexOf("<")).trim().replace(/^"|"$/g, "")
: "";
diff --git a/tests/google.test.ts b/tests/google.test.ts
index 05fd6f4..441d0c8 100644
--- a/tests/google.test.ts
+++ b/tests/google.test.ts
@@ -37,6 +37,88 @@ const eventResponse = {
};
const base64url = (text: string) => Buffer.from(text).toString("base64url");
+test("mail addresses exclude comments, including nested and escaped parentheses", async () => {
+ for (const comment of [
+ "(legacy \\) [email protected])",
+ ]) {
+ const client = clientWith(() =>
+ json({
+ id: "thread1",
+ messages: [
+ {
+ id: "msg1",
+ threadId: "thread1",
+ payload: {
+ headers: [
+ { name: "From", value: `Support ${comment} <[email protected]>` },
+ {
+ name: "To",
+ value: `${comment} <[email protected]>, [email protected] ${comment}`,
+ },
+ ],
+ },
+ },
+ ],
+ }),
+ );
+ const [mail] = await client.getThread("thread1");
+ assert.equal(mail.from, "[email protected]", comment);
+ assert.deepEqual(mail.to, ["[email protected]", "[email protected]"], comment);
+ }
+});
+
+test("parentheses inside a quoted display name do not start address comments", async () => {
+ const client = clientWith(() =>
+ json({
+ id: "thread1",
+ messages: [
+ {
+ id: "msg1",
+ threadId: "thread1",
+ payload: {
+ headers: [
+ { name: "From", value: '"Support (West" <[email protected]>' },
+ { name: "To", value: '"Help \\"desk ([email protected])" <[email protected]>' },
+ ],
+ },
+ },
+ ],
+ }),
+ );
+ const [mail] = await client.getThread("thread1");
+ assert.equal(mail.from, "[email protected]");
+ assert.deepEqual(mail.to, ["[email protected]"]);
+});
+
+test("encoded display-name punctuation does not become address syntax", async () => {
+ for (const name of ["Support (West", "Support ) West", "[email protected] (West)"]) {
+ const encoded = `=?UTF-8?B?${Buffer.from(name).toString("base64")}?=`;
+ const client = clientWith(() =>
+ json({
+ id: "thread1",
+ messages: [
+ {
+ id: "msg1",
+ threadId: "thread1",
+ payload: {
+ headers: [
+ { name: "From", value: `${encoded} <[email protected]>` },
+ { name: "To", value: "[email protected]_=28West?= <[email protected]>" },
+ ],
+ },
+ },
+ ],
+ }),
+ );
+ const [mail] = await client.getThread("thread1");
+ assert.equal(mail.from, "[email protected]", name);
+ assert.equal(mail.sender, name);
+ assert.deepEqual(mail.to, ["[email protected]"]);
+ }
+});
+
test("mail reads nested plain text and attachment references over authenticated Gmail paths", async () => {
const paths: URL[] = [];
const client = clientWith((request) => {