Skip to content

Commit efe1ad3

Browse files
committed
fix(signal): reject malformed base64 attachment data
signalRpcRequest returns the JSON-RPC result via a bare cast, so getAttachment data reaches Buffer.from unvalidated. Node drops out-of-alphabet characters instead of throwing, so a damaged payload was silently written to disk as a corrupted attachment. Canonicalize after the existing size guard and fail with the attachment id instead.
1 parent 3ae29ad commit efe1ad3

2 files changed

Lines changed: 99 additions & 1 deletion

File tree

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
import type { OpenClawConfig } from "openclaw/plugin-sdk/config-contracts";
2+
import { beforeEach, describe, expect, it, vi } from "vitest";
3+
import type { SignalEventHandlerDeps } from "./monitor/event-handler.types.js";
4+
5+
const signalRpcRequestMock = vi.hoisted(() => vi.fn());
6+
const saveMediaBufferMock = vi.hoisted(() => vi.fn());
7+
8+
let capturedFetchAttachment: SignalEventHandlerDeps["fetchAttachment"] | undefined;
9+
10+
vi.mock("openclaw/plugin-sdk/media-runtime", async () => {
11+
const actual = await vi.importActual<typeof import("openclaw/plugin-sdk/media-runtime")>(
12+
"openclaw/plugin-sdk/media-runtime",
13+
);
14+
return {
15+
...actual,
16+
saveMediaBuffer: saveMediaBufferMock,
17+
};
18+
});
19+
20+
vi.mock("./client-adapter.js", async () => {
21+
const actual = await vi.importActual<typeof import("./client-adapter.js")>("./client-adapter.js");
22+
return {
23+
...actual,
24+
signalRpcRequest: signalRpcRequestMock,
25+
};
26+
});
27+
28+
vi.mock("./monitor/event-handler.js", () => ({
29+
createSignalEventHandler: (deps: SignalEventHandlerDeps) => {
30+
capturedFetchAttachment = deps.fetchAttachment;
31+
return async () => {};
32+
},
33+
}));
34+
35+
vi.mock("./signal-ingress.js", () => ({
36+
startSignalIngressMonitor: async () => ({
37+
receive: async () => {},
38+
stop: async () => {},
39+
}),
40+
}));
41+
42+
vi.mock("./sse-reconnect.js", () => ({
43+
runSignalSseLoop: async () => {},
44+
}));
45+
46+
const { monitorSignalProvider } = await import("./monitor.js");
47+
48+
const config = {
49+
channels: {
50+
signal: {
51+
transport: { kind: "external-native", url: "http://127.0.0.1:8080" },
52+
dmPolicy: "open",
53+
allowFrom: ["*"],
54+
},
55+
},
56+
} satisfies OpenClawConfig;
57+
58+
function requireCapturedFetchAttachment(): SignalEventHandlerDeps["fetchAttachment"] {
59+
if (!capturedFetchAttachment) {
60+
throw new Error("expected monitor to configure fetchAttachment");
61+
}
62+
return capturedFetchAttachment;
63+
}
64+
65+
describe("Signal attachment fetch", () => {
66+
beforeEach(() => {
67+
capturedFetchAttachment = undefined;
68+
signalRpcRequestMock.mockReset();
69+
saveMediaBufferMock.mockReset().mockResolvedValue({
70+
path: "/tmp/signal-attachment.png",
71+
contentType: "image/png",
72+
});
73+
});
74+
75+
it("rejects malformed base64 attachment data", async () => {
76+
signalRpcRequestMock.mockResolvedValue({
77+
data: "iVBORw0KGgoAAAANSUhE%%%UgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVQIW2Nk+M/wHwAF/gL+M6Q10QAAAABJRU5ErkJggg==",
78+
});
79+
80+
await monitorSignalProvider({ config, autoStart: false });
81+
const fetchAttachment = requireCapturedFetchAttachment();
82+
83+
await expect(
84+
fetchAttachment({
85+
baseUrl: "http://127.0.0.1:8080",
86+
attachment: { id: "attachment-123", contentType: "image/png" },
87+
sender: "+15550001111",
88+
maxBytes: 8 * 1024 * 1024,
89+
}),
90+
).rejects.toThrow("Signal attachment attachment-123 returned malformed base64 data");
91+
expect(saveMediaBufferMock).not.toHaveBeenCalled();
92+
});
93+
});

‎extensions/signal/src/monitor.ts‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import type {
99
SignalReactionNotificationMode,
1010
} from "openclaw/plugin-sdk/config-contracts";
1111
import {
12+
canonicalizeBase64,
1213
detectMime,
1314
estimateBase64DecodedBytes,
1415
saveMediaBuffer,
@@ -315,7 +316,11 @@ async function fetchAttachment(params: {
315316
`Signal attachment ${attachment.id} exceeds ${(params.maxBytes / (1024 * 1024)).toFixed(0)}MB limit`,
316317
);
317318
}
318-
const buffer = Buffer.from(result.data, "base64");
319+
const canonicalData = canonicalizeBase64(result.data);
320+
if (!canonicalData) {
321+
throw new Error(`Signal attachment ${attachment.id} returned malformed base64 data`);
322+
}
323+
const buffer = Buffer.from(canonicalData, "base64");
319324
const originalFilename = normalizeOptionalString(attachment.filename ?? undefined);
320325
const contentType =
321326
normalizeOptionalString(attachment.contentType ?? undefined) ??

0 commit comments

Comments
 (0)