Skip to content

Commit 9f5c762

Browse files
Peter SteinbergerZengWen-DT
andcommitted
fix(signal): restore provider-safe original attachment filenames
Co-authored-by: ZengWen-DT <[email protected]>
1 parent fdcb321 commit 9f5c762

3 files changed

Lines changed: 177 additions & 3 deletions

File tree

‎extensions/signal/src/client-container.real-server.test.ts‎

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,11 @@
33
// by the request deadline, not only by the per-chunk idle guard. This exercises the
44
// production containerRpcRequest -> containerRestRequest -> readSignalRestText path
55
// without mocking fetch, unlike the fake-timer unit tests.
6+
import { mkdtemp, rm, writeFile } from "node:fs/promises";
67
import http from "node:http";
78
import type { AddressInfo } from "node:net";
9+
import { tmpdir } from "node:os";
10+
import { join } from "node:path";
811
import { afterEach, describe, expect, it } from "vitest";
912
import { containerRpcRequest } from "./client-container.js";
1013

@@ -95,4 +98,99 @@ describe("signal REST real-server deadline", () => {
9598
);
9699
expect(result).toEqual({ versions: ["v1"], build: 2 });
97100
});
101+
102+
it.each([
103+
{
104+
stagedFilename: "report---a1b2c3d4-5678-90ab-cdef-1234567890ab.jpg",
105+
expectedFilename: "report.jpg",
106+
},
107+
{
108+
stagedFilename: "quarter;final---a1b2c3d4-5678-90ab-cdef-1234567890ab.jpg",
109+
expectedFilename: "quarter_final.jpg",
110+
},
111+
{
112+
stagedFilename: "first;middle;last---a1b2c3d4-5678-90ab-cdef-1234567890ab.jpg",
113+
expectedFilename: "first_middle_last.jpg",
114+
},
115+
{
116+
stagedFilename: "quarter,final---a1b2c3d4-5678-90ab-cdef-1234567890ab.jpg",
117+
expectedFilename: "quarter_final.jpg",
118+
},
119+
{
120+
stagedFilename: "first,middle,last---a1b2c3d4-5678-90ab-cdef-1234567890ab.jpg",
121+
expectedFilename: "first_middle_last.jpg",
122+
},
123+
{
124+
stagedFilename: "hash#name---a1b2c3d4-5678-90ab-cdef-1234567890ab.jpg",
125+
expectedFilename: "hash_name.jpg",
126+
},
127+
{
128+
stagedFilename: "mixed;comma,hash#name---a1b2c3d4-5678-90ab-cdef-1234567890ab.jpg",
129+
expectedFilename: "mixed_comma_hash_name.jpg",
130+
},
131+
{ stagedFilename: "quarter;final.jpg", expectedFilename: "quarter_final.jpg" },
132+
{ stagedFilename: "quarter,final.jpg", expectedFilename: "quarter_final.jpg" },
133+
{ stagedFilename: "hash#name.jpg", expectedFilename: "hash_name.jpg" },
134+
{
135+
stagedFilename: "quarter final---a1b2c3d4-5678-90ab-cdef-1234567890ab.jpg",
136+
expectedFilename: "quarter final.jpg",
137+
},
138+
])(
139+
"posts the provider-safe original filename $expectedFilename",
140+
async ({ stagedFilename, expectedFilename }) => {
141+
let receivedPayload: unknown;
142+
const server = await startServer((req, res) => {
143+
if (req.method !== "POST" || req.url !== "/v2/send") {
144+
res.writeHead(404);
145+
res.end();
146+
return;
147+
}
148+
149+
const chunks: Buffer[] = [];
150+
req.on("data", (chunk: Buffer | string) => {
151+
chunks.push(typeof chunk === "string" ? Buffer.from(chunk) : chunk);
152+
});
153+
req.on("end", () => {
154+
receivedPayload = JSON.parse(Buffer.concat(chunks).toString("utf8"));
155+
res.writeHead(200, { "content-type": "application/json" });
156+
res.end(JSON.stringify({ timestamp: "1735689600000" }));
157+
});
158+
});
159+
160+
const mediaDir = await mkdtemp(join(tmpdir(), "signal-real-filename-"));
161+
const content = Buffer.from([0xff, 0xd8, 0xff, 0xe0]);
162+
const stagedFile = join(mediaDir, stagedFilename);
163+
164+
try {
165+
await writeFile(stagedFile, content);
166+
await expect(
167+
containerRpcRequest(
168+
"send",
169+
{
170+
account: "+14259798283",
171+
recipient: ["+15550001111"],
172+
message: "Photo",
173+
attachments: [stagedFile],
174+
},
175+
{ baseUrl: server.baseUrl, timeoutMs: 1_000 },
176+
),
177+
).resolves.toEqual({ timestamp: 1735689600000 });
178+
179+
expect(receivedPayload).toEqual({
180+
message: "Photo",
181+
number: "+14259798283",
182+
recipients: ["+15550001111"],
183+
base64_attachments: [
184+
`data:image/jpeg;filename=${expectedFilename};base64,${content.toString("base64")}`,
185+
],
186+
});
187+
const attachment = (receivedPayload as { base64_attachments: [string] })
188+
.base64_attachments[0];
189+
const decoded = await (await fetch(attachment)).arrayBuffer();
190+
expect(Buffer.from(decoded)).toEqual(content);
191+
} finally {
192+
await rm(mediaDir, { recursive: true, force: true });
193+
}
194+
},
195+
);
98196
});

‎extensions/signal/src/client-container.test.ts‎

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -912,6 +912,78 @@ describe("containerSendMessage", () => {
912912
await fs.rm(tmpDir, { recursive: true });
913913
});
914914

915+
it.each([
916+
{
917+
stagedFilename: "report---a1b2c3d4-5678-90ab-cdef-1234567890ab.jpg",
918+
expectedFilename: "report.jpg",
919+
},
920+
{
921+
stagedFilename: "quarter;final---a1b2c3d4-5678-90ab-cdef-1234567890ab.jpg",
922+
expectedFilename: "quarter_final.jpg",
923+
},
924+
{
925+
stagedFilename: "first;middle;last---a1b2c3d4-5678-90ab-cdef-1234567890ab.jpg",
926+
expectedFilename: "first_middle_last.jpg",
927+
},
928+
{
929+
stagedFilename: "quarter,final---a1b2c3d4-5678-90ab-cdef-1234567890ab.jpg",
930+
expectedFilename: "quarter_final.jpg",
931+
},
932+
{
933+
stagedFilename: "first,middle,last---a1b2c3d4-5678-90ab-cdef-1234567890ab.jpg",
934+
expectedFilename: "first_middle_last.jpg",
935+
},
936+
{
937+
stagedFilename: "hash#name---a1b2c3d4-5678-90ab-cdef-1234567890ab.jpg",
938+
expectedFilename: "hash_name.jpg",
939+
},
940+
{
941+
stagedFilename: "mixed;comma,hash#name---a1b2c3d4-5678-90ab-cdef-1234567890ab.jpg",
942+
expectedFilename: "mixed_comma_hash_name.jpg",
943+
},
944+
{ stagedFilename: "quarter;final.jpg", expectedFilename: "quarter_final.jpg" },
945+
{ stagedFilename: "quarter,final.jpg", expectedFilename: "quarter_final.jpg" },
946+
{ stagedFilename: "hash#name.jpg", expectedFilename: "hash_name.jpg" },
947+
{
948+
stagedFilename: "quarter final---a1b2c3d4-5678-90ab-cdef-1234567890ab.jpg",
949+
expectedFilename: "quarter final.jpg",
950+
},
951+
])(
952+
"restores the provider-safe original attachment filename $expectedFilename",
953+
async ({ stagedFilename, expectedFilename }) => {
954+
const fs = await import("node:fs/promises");
955+
const os = await import("node:os");
956+
const path = await import("node:path");
957+
958+
const tmpDir = await fs.mkdtemp(path.join(os.tmpdir(), "signal-test-"));
959+
try {
960+
const stagedFile = path.join(tmpDir, stagedFilename);
961+
const content = Buffer.from([0xff, 0xd8, 0xff, 0xe0]);
962+
await fs.writeFile(stagedFile, content);
963+
964+
mockFetch.mockResolvedValue({
965+
ok: true,
966+
status: 200,
967+
...bodyStream(JSON.stringify({})),
968+
});
969+
970+
await containerSendMessage({
971+
baseUrl: "http://localhost:8080",
972+
account: "+14259798283",
973+
recipients: ["+15550001111"],
974+
message: "Photo",
975+
attachments: [stagedFile],
976+
});
977+
978+
expect(parseFetchBody().base64_attachments).toEqual([
979+
`data:image/jpeg;filename=${expectedFilename};base64,${content.toString("base64")}`,
980+
]);
981+
} finally {
982+
await fs.rm(tmpDir, { recursive: true, force: true });
983+
}
984+
},
985+
);
986+
915987
it("rejects outbound attachments that exceed the size cap", async () => {
916988
const fs = await import("node:fs/promises");
917989
const os = await import("node:os");

‎extensions/signal/src/client-container.ts‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,13 @@
66
* to keep the two modes cleanly isolated.
77
*/
88

9-
import nodePath from "node:path";
109
import { toErrorObject } from "openclaw/plugin-sdk/error-runtime";
1110
import { resolveFetch } from "openclaw/plugin-sdk/fetch-runtime";
12-
import { detectMime, parseMediaContentLength } from "openclaw/plugin-sdk/media-runtime";
11+
import {
12+
detectMime,
13+
extractOriginalFilename,
14+
parseMediaContentLength,
15+
} from "openclaw/plugin-sdk/media-runtime";
1316
import {
1417
parseStrictNonNegativeInteger,
1518
resolveTimerTimeoutMs,
@@ -554,7 +557,8 @@ async function filesToBase64DataUris(
554557
});
555558
remainingBytes -= buffer.byteLength;
556559
const mime = (await detectMime({ buffer, filePath })) ?? "application/octet-stream";
557-
const filename = nodePath.basename(filePath);
560+
// Signal splits on semicolons; commas and fragments break RFC 2397 attachment data.
561+
const filename = extractOriginalFilename(filePath).replace(/[,;#]/g, "_");
558562
const b64 = buffer.toString("base64");
559563
results.push(`data:${mime};filename=${filename};base64,${b64}`);
560564
}

0 commit comments

Comments
 (0)