Skip to content

Commit b3a5035

Browse files
committed
fix(qa): sanitize channel dispatch logs
1 parent 1dd8089 commit b3a5035

3 files changed

Lines changed: 67 additions & 6 deletions

File tree

‎extensions/qa-channel/src/channel.test.ts‎

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,9 @@ function createMockQaRuntime(params?: {
115115
replyOptions,
116116
}: {
117117
ctx: { BodyForAgent?: string; Body?: string };
118-
dispatcherOptions: { deliver: (payload: { text: string }) => Promise<void> };
118+
dispatcherOptions: {
119+
deliver: (payload: { text: string }, info: { kind: string }) => Promise<void>;
120+
};
119121
replyOptions?: {
120122
onToolStart?: (payload: {
121123
name?: string;
@@ -128,9 +130,12 @@ function createMockQaRuntime(params?: {
128130
await replyOptions?.onToolStart?.(toolStart);
129131
}
130132
params?.onDispatch?.(ctx as Record<string, unknown>);
131-
await dispatcherOptions.deliver({
132-
text: `qa-echo: ${ctx.BodyForAgent ?? ctx.Body ?? ""}`,
133-
});
133+
await dispatcherOptions.deliver(
134+
{
135+
text: `qa-echo: ${ctx.BodyForAgent ?? ctx.Body ?? ""}`,
136+
},
137+
{ kind: "final" },
138+
);
134139
},
135140
},
136141
inbound: {

‎extensions/qa-channel/src/inbound.test.ts‎

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,48 @@ describe("handleQaInbound", () => {
190190
});
191191
});
192192

193+
it("escapes control characters in dispatch error logs", async () => {
194+
const runtime = createPluginRuntimeMock();
195+
const warn = vi.spyOn(console, "warn").mockImplementation(() => undefined);
196+
const c1Control = String.fromCharCode(0x9b);
197+
const lineSeparator = String.fromCodePoint(0x2028);
198+
const paragraphSeparator = String.fromCodePoint(0x2029);
199+
vi.mocked(deleteQaBusMessage).mockRejectedValueOnce(
200+
new Error(`cleanup\nforged\u001b[31m${c1Control}32m${lineSeparator}next`),
201+
);
202+
setQaChannelRuntime(runtime);
203+
204+
try {
205+
await handleQaInbound(createQaInboundParams());
206+
207+
const assembled = firstRunAssembledParams(runtime);
208+
await assembled.replyOptions?.onPartialReply?.({ text: "unfinished preview" });
209+
assembled.delivery.onError?.(new Error(`dispatch\r\nforged${paragraphSeparator}next`), {
210+
kind: "final",
211+
});
212+
213+
await vi.waitFor(() => {
214+
expect(warn).toHaveBeenCalledTimes(2);
215+
});
216+
assembled.delivery.onError?.(undefined, { kind: "final" });
217+
await vi.waitFor(() => {
218+
expect(warn).toHaveBeenCalledTimes(3);
219+
});
220+
const output = warn.mock.calls.flat().join(" ");
221+
expect(output).not.toContain("\r");
222+
expect(output).not.toContain("\n");
223+
expect(output).not.toContain(String.fromCharCode(0x1b));
224+
expect(output).not.toContain(c1Control);
225+
expect(output).not.toContain(lineSeparator);
226+
expect(output).not.toContain(paragraphSeparator);
227+
expect(output).toContain("dispatch\\u000d\\u000aforged\\u2029next");
228+
expect(output).toContain("cleanup\\u000aforged\\u001b[31m\\u009b32m\\u2028next");
229+
expect(output).toContain("[object Undefined]");
230+
} finally {
231+
warn.mockRestore();
232+
}
233+
});
234+
193235
it("marks group messages that match configured mention patterns", async () => {
194236
const runtime = createPluginRuntimeMock();
195237
vi.mocked(runtime.channel.mentions.buildMentionRegexes).mockReturnValue([/\b@?openclaw\b/i]);

‎extensions/qa-channel/src/inbound.ts‎

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import { resolveStableChannelMessageIngress } from "openclaw/plugin-sdk/channel-ingress-runtime";
33
import { resolveNativeCommandSessionTargets } from "openclaw/plugin-sdk/command-auth-native";
44
import type { OpenClawConfig } from "openclaw/plugin-sdk/config-contracts";
5+
import { formatErrorMessage } from "openclaw/plugin-sdk/error-runtime";
56
import { resolveInboundRouteEnvelopeBuilderWithRuntime } from "openclaw/plugin-sdk/inbound-envelope";
67
import {
78
buildAgentMediaPayload,
@@ -97,6 +98,19 @@ function resolveQaGroupConfig(params: {
9798
return groups?.[params.conversationId] ?? groups?.[params.target] ?? groups?.["*"];
9899
}
99100

101+
function formatQaErrorForLog(error: unknown): string {
102+
let escaped = "";
103+
const message = formatErrorMessage(error) || Object.prototype.toString.call(error);
104+
for (const character of message) {
105+
const codePoint = character.codePointAt(0) ?? 0;
106+
const isControl = codePoint <= 0x1f || (codePoint >= 0x7f && codePoint <= 0x9f);
107+
const isLineSeparator = codePoint === 0x2028 || codePoint === 0x2029;
108+
escaped +=
109+
isControl || isLineSeparator ? `\\u${codePoint.toString(16).padStart(4, "0")}` : character;
110+
}
111+
return escaped;
112+
}
113+
100114
function createQaReplyPreview(params: {
101115
account: ResolvedQaChannelAccount;
102116
inbound: QaBusMessage;
@@ -363,10 +377,10 @@ export async function handleQaInbound(params: {
363377
onError: (error) => {
364378
void preview.clear().catch((clearError: unknown) => {
365379
console.warn(
366-
`[qa-channel] failed to clear reply preview after dispatch error: ${String(clearError)}`,
380+
`[qa-channel] failed to clear reply preview after dispatch error: ${formatQaErrorForLog(clearError)}`,
367381
);
368382
});
369-
console.warn(`[qa-channel] reply dispatch failed: ${String(error)}`);
383+
console.warn(`[qa-channel] reply dispatch failed: ${formatQaErrorForLog(error)}`);
370384
},
371385
},
372386
replyOptions: {

0 commit comments

Comments
 (0)