Skip to content

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

TypeScript

fix: unpaired-surrogate attachment names no longer break Gmail sync (#121)

Charan Rathore83fd91525227
diff --git a/packages/integrations/src/google.ts b/packages/integrations/src/google.ts
index cb21f2c..63ea131 100644
--- a/packages/integrations/src/google.ts
+++ b/packages/integrations/src/google.ts
@@ -270,6 +270,17 @@ function htmlToPlainText(html: string): string {
.replace(/\n{3,}/g, "\n\n")
.trim();
}
+/**
+ * Replace unpaired surrogates with U+FFFD so encodeURIComponent cannot throw "URI
+ * malformed" on a sender-supplied name. (String.prototype.toWellFormed, but the
+ * project targets ES2023.)
+ */
+const wellFormed = (value: string): string =>
+ value.replace(
+ /[\uD800-\uDBFF](?![\uDC00-\uDFFF])|(?<![\uD800-\uDBFF])[\uDC00-\uDFFF]/g,
+ "\uFFFD",
+ );
+
function mapMessage(message: z.infer<typeof messageSchema>): Mail {
const metadata = headers(message.payload);
const plain: string[] = [];
@@ -279,7 +290,7 @@ function mapMessage(message: z.infer<typeof messageSchema>): Mail {
if (depth > 30) throw new Error("Gmail message MIME nesting exceeds the limit");
if (part.filename && part.body?.attachmentId)
attachments.push(
- `${message.id}:${part.body.attachmentId}:${encodeURIComponent(part.filename)}`,
+ `${message.id}:${part.body.attachmentId}:${encodeURIComponent(wellFormed(part.filename))}`,
);
if (
!part.filename &&
@@ -737,7 +748,7 @@ export class GoogleClient {
const parts = [
textPart,
...attachments.map((attachment) => {
- const name = encodeURIComponent(attachment.name).replace(
+ const name = encodeURIComponent(wellFormed(attachment.name)).replace(
/['()*]/g,
(char) => `%${char.charCodeAt(0).toString(16).toUpperCase()}`,
);
diff --git a/tests/google.test.ts b/tests/google.test.ts
index 9b2e645..47b5990 100644
--- a/tests/google.test.ts
+++ b/tests/google.test.ts
@@ -758,3 +758,44 @@ test("single-event validation is repeated at execution and read failures never d
await assert.rejects(failing.deleteEvent("primary", "event-1"), GoogleApiError);
assert.equal(writes, 0);
});
+
+test("a sender's unpaired-surrogate attachment filename does not break the inbox", async () => {
+ const client = clientWith((request) => {
+ const url = new URL(request.url);
+ if (url.pathname.endsWith("/messages")) return json({ messages: [{ id: "msg1" }] });
+ return json({
+ id: "msg1",
+ threadId: "thread1",
+ internalDate: "1791658800000",
+ labelIds: ["INBOX"],
+ payload: {
+ headers: [{ name: "From", value: "[email protected]" }],
+ mimeType: "multipart/mixed",
+ parts: [
+ { mimeType: "text/plain", body: { data: base64url("See attached.") } },
+ {
+ mimeType: "application/pdf",
+ filename: "form\uD800.pdf",
+ body: { attachmentId: "attach1", size: 10 },
+ },
+ ],
+ },
+ });
+ });
+ const [mail] = await client.listMail();
+ assert.deepEqual(mail.attachments, ["msg1:attach1:form%EF%BF%BD.pdf"]);
+});
+
+test("an outgoing attachment name with an unpaired surrogate still sends", async () => {
+ const client = clientWith(async (request) => {
+ if (new URL(request.url).pathname.endsWith("/profile"))
+ return json({ emailAddress: "[email protected]" });
+ const { raw } = await request.json();
+ const mime = Buffer.from(raw, "base64url").toString("utf8");
+ assert.match(mime, /filename\*=UTF-8''form%EF%BF%BD\.pdf/);
+ return json({ id: "sent1", threadId: "thread1" });
+ });
+ await client.sendEmail(email(), [
+ { name: "form\uD800.pdf", mimeType: "application/pdf", bytes: Buffer.from([1]) },
+ ]);
+});