fix telegram dm last-route metadata leak

This commit is contained in:
guirguispierre 2026-02-17 13:38:10 -08:00 committed by Ayaan Zaidi
parent 54e5f80424
commit ea1ff473d8
4 changed files with 85 additions and 4 deletions

View File

@ -0,0 +1,78 @@
import type { MsgContext } from "../auto-reply/templating.js";
import { beforeEach, describe, expect, it, vi } from "vitest";
const recordSessionMetaFromInboundMock = vi.fn(() => Promise.resolve(undefined));
const updateLastRouteMock = vi.fn(() => Promise.resolve(undefined));
vi.mock("../config/sessions.js", () => ({
recordSessionMetaFromInbound: (...args: unknown[]) => recordSessionMetaFromInboundMock(...args),
updateLastRoute: (...args: unknown[]) => updateLastRouteMock(...args),
}));
describe("recordInboundSession", () => {
const ctx: MsgContext = {
Provider: "telegram",
From: "telegram:1234",
SessionKey: "agent:main:telegram:1234:thread:42",
OriginatingTo: "telegram:1234",
};
beforeEach(() => {
recordSessionMetaFromInboundMock.mockClear();
updateLastRouteMock.mockClear();
});
it("does not pass ctx when updating a different session key", async () => {
const { recordInboundSession } = await import("./session.js");
await recordInboundSession({
storePath: "/tmp/openclaw-session-store.json",
sessionKey: "agent:main:telegram:1234:thread:42",
ctx,
updateLastRoute: {
sessionKey: "agent:main:main",
channel: "telegram",
to: "telegram:1234",
},
onRecordError: vi.fn(),
});
expect(updateLastRouteMock).toHaveBeenCalledWith(
expect.objectContaining({
sessionKey: "agent:main:main",
ctx: undefined,
deliveryContext: expect.objectContaining({
channel: "telegram",
to: "telegram:1234",
}),
}),
);
});
it("passes ctx when updating the same session key", async () => {
const { recordInboundSession } = await import("./session.js");
await recordInboundSession({
storePath: "/tmp/openclaw-session-store.json",
sessionKey: "agent:main:telegram:1234:thread:42",
ctx,
updateLastRoute: {
sessionKey: "agent:main:telegram:1234:thread:42",
channel: "telegram",
to: "telegram:1234",
},
onRecordError: vi.fn(),
});
expect(updateLastRouteMock).toHaveBeenCalledWith(
expect.objectContaining({
sessionKey: "agent:main:telegram:1234:thread:42",
ctx,
deliveryContext: expect.objectContaining({
channel: "telegram",
to: "telegram:1234",
}),
}),
);
});
});

View File

@ -45,7 +45,8 @@ export async function recordInboundSession(params: {
accountId: update.accountId,
threadId: update.threadId,
},
ctx,
// Avoid leaking inbound origin metadata into a different target session.
ctx: update.sessionKey === sessionKey ? ctx : undefined,
groupResolution,
});
}

View File

@ -41,8 +41,9 @@ describe("buildTelegramMessageContext DM topic threadId in deliveryContext (#889
expect(recordInboundSessionMock).toHaveBeenCalled();
// Check that updateLastRoute includes threadId
const updateLastRoute = getUpdateLastRoute() as { threadId?: string } | undefined;
const updateLastRoute = getUpdateLastRoute() as { threadId?: string; to?: string } | undefined;
expect(updateLastRoute).toBeDefined();
expect(updateLastRoute?.to).toBe("telegram:1234");
expect(updateLastRoute?.threadId).toBe("42");
});
@ -57,8 +58,9 @@ describe("buildTelegramMessageContext DM topic threadId in deliveryContext (#889
expect(recordInboundSessionMock).toHaveBeenCalled();
// Check that updateLastRoute does NOT include threadId
const updateLastRoute = getUpdateLastRoute() as { threadId?: string } | undefined;
const updateLastRoute = getUpdateLastRoute() as { threadId?: string; to?: string } | undefined;
expect(updateLastRoute).toBeDefined();
expect(updateLastRoute?.to).toBe("telegram:1234");
expect(updateLastRoute?.threadId).toBeUndefined();
});

View File

@ -765,7 +765,7 @@ export const buildTelegramMessageContext = async ({
? {
sessionKey: route.mainSessionKey,
channel: "telegram",
to: String(chatId),
to: `telegram:${chatId}`,
accountId: route.accountId,
// Preserve DM topic threadId for replies (fixes #8891)
threadId: dmThreadId != null ? String(dmThreadId) : undefined,