Skip to content

Commit 47fe3cc

Browse files
authored
fix: preserve data URL attachment filename extensions (#130625)
Carry the normalized MIME into the existing attachment filename inference. Cover PNG/CSV metadata across actions and the real action-to-plugin dispatch boundary. Explicit filenames and MIME overrides remain unchanged. Closes #130618.
1 parent 6e3df0d commit 47fe3cc

2 files changed

Lines changed: 30 additions & 9 deletions

File tree

‎src/infra/outbound/message-action-params.attachments.test.ts‎

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ const onePixelPng = Buffer.from(
2929
const onePixelPngBase64 = onePixelPng.toString("base64");
3030
const wrappedOnePixelPngBase64 = onePixelPngBase64.match(/.{1,24}/g)?.join("\r\n") ?? "";
3131
const parameterizedPngDataUrl = `data:image/png;charset=utf-8;name=../../ignored.svg;base64,${wrappedOnePixelPngBase64}`;
32+
const csvBase64 = Buffer.from("name,value\nexample,1\n").toString("base64");
3233

3334
function firstMockArg(
3435
mock: { mock: { calls: readonly unknown[][] } },
@@ -106,10 +107,29 @@ describe("runMessageAction media behavior", () => {
106107
await resetMessageActionMediaMocks();
107108
});
108109

109-
it.each(["send", "sendAttachment", "reply", "upload-file", "setGroupIcon"] as const)(
110-
"normalizes parameterized, line-wrapped image data URLs for %s",
111-
async (action) => {
112-
const args: Record<string, unknown> = { buffer: parameterizedPngDataUrl };
110+
it.each(
111+
(["send", "sendAttachment", "reply", "upload-file", "setGroupIcon"] as const).flatMap(
112+
(action) => [
113+
{
114+
action,
115+
buffer: parameterizedPngDataUrl,
116+
base64: onePixelPngBase64,
117+
contentType: "image/png",
118+
filename: "attachment.png",
119+
},
120+
{
121+
action,
122+
buffer: `data:text/csv;base64,${csvBase64}`,
123+
base64: csvBase64,
124+
contentType: "text/csv",
125+
filename: "attachment.csv",
126+
},
127+
],
128+
),
129+
)(
130+
"normalizes $contentType data URLs and infers filenames for $action",
131+
async ({ action, buffer, base64, contentType, filename }) => {
132+
const args: Record<string, unknown> = { buffer };
113133

114134
await hydrateAttachmentParamsForAction({
115135
cfg: {},
@@ -120,14 +140,13 @@ describe("runMessageAction media behavior", () => {
120140
mediaPolicy: { mode: "host" },
121141
});
122142

123-
expect(args.contentType).toBe("image/png");
143+
expect(args.contentType).toBe(contentType);
144+
expect(args.filename).toBe(filename);
124145
if (action === "send") {
125146
expect(args.media).toBe("buffer://message-send/attachment");
126-
expect(args.filename).toBe("attachment.png");
127147
} else {
128-
expect(canonicalizeBase64(String(args.buffer))).toBe(onePixelPngBase64);
148+
expect(canonicalizeBase64(String(args.buffer))).toBe(base64);
129149
}
130-
expect(args.filename).not.toBe("../../ignored.svg");
131150
},
132151
);
133152

@@ -329,6 +348,7 @@ describe("runMessageAction media behavior", () => {
329348

330349
const payload = requireActionPayload(result);
331350
expect(payload.contentType).toBe("image/png");
351+
expect(payload.filename).toBe("attachment.png");
332352
expect(canonicalizeBase64(String(payload.buffer))).toBe(onePixelPngBase64);
333353
},
334354
);
@@ -617,6 +637,7 @@ describe("runMessageAction media behavior", () => {
617637

618638
const handlerParams = firstMockArg(handleActionMock, "handleAction");
619639
expect(handlerParams.contentType).toBe("image/png");
640+
expect(handlerParams.filename).toBe("attachment.png");
620641
expect(canonicalizeBase64(String(handlerParams.buffer))).toBe(onePixelPngBase64);
621642
});
622643

‎src/infra/outbound/message-action-params.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -538,7 +538,7 @@ async function hydrateAttachmentPayload(params: {
538538
} else if (!filename) {
539539
params.args.filename = inferAttachmentFilename({
540540
mediaHint: mediaSource,
541-
contentType: contentTypeParam ?? undefined,
541+
contentType: normalized.contentType,
542542
});
543543
}
544544
}

0 commit comments

Comments
 (0)