Skip to content

Commit b0c7f1e

Browse files
committed
fix: harden sessions_spawn delivery params and telegram account routing (#31000, #31110)
1 parent 684ac44 commit b0c7f1e

6 files changed

Lines changed: 89 additions & 2 deletions

File tree

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,8 @@ Docs: https://docs.openclaw.ai
111111
- Docs/Docker images: clarify the official GHCR image source and tag guidance (`main`, `latest`, `<version>`), and document that `OPENCLAW_IMAGE` skips local image builds but still uses the repo-local compose/setup flow. (#27214, #31180) Fixes #15655. Thanks @ipl31.
112112
- Agents/Model fallback: classify additional network transport errors (`ECONNREFUSED`, `ENETUNREACH`, `EHOSTUNREACH`, `ENETRESET`, `EAI_AGAIN`) as failover-worthy so fallback chains advance when primary providers are unreachable. Landed from contributor PR #19077 by @ayanesakura. Thanks @ayanesakura.
113113
- Agents/Copilot token refresh: refresh GitHub Copilot runtime API tokens after auth-expiry failures and re-run with the renewed token so long-running embedded/subagent turns do not fail on mid-session 401 expiry. Landed from contributor PR #8805 by @Arthur742Ramos. Thanks @Arthur742Ramos.
114+
- Agents/Subagents delivery params: reject unsupported `sessions_spawn` channel-delivery params (`target`, `channel`, `to`, `threadId`, `replyTo`, `transport`) with explicit input errors so delivery intent does not silently leak output to the parent conversation. (#31000)
115+
- Telegram/Multi-account fallback isolation: fail closed for non-default Telegram accounts when route resolution falls back to `matchedBy=default`, preventing cross-account DM/session contamination without explicit account bindings. (#31110)
114116
- Discord/Allowlist diagnostics: add debug logs for guild/channel allowlist drops so operators can quickly identify ignored inbound messages and required allowlist entries. Landed from contributor PR #30966 by @haosenwang1018. Thanks @haosenwang1018.
115117
- Discord/Ack reactions: add Discord-account-level `ackReactionScope` override and support explicit `off`/`none` values in shared config schemas to disable ack reactions per account. Landed from contributor PR #30400 by @BlueBirdBack. Thanks @BlueBirdBack.
116118
- Discord/Forum thread tags: support `appliedTags` on Discord thread-create actions and map to `applied_tags` for forum/media starter posts, with targeted thread-creation regression coverage. Landed from contributor PR #30358 by @pushkarsingh32. Thanks @pushkarsingh32.

docs/tools/subagents.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ Tool params:
9191
- `mode: "session"` requires `thread: true`
9292
- `cleanup?` (`delete|keep`, default `keep`)
9393
- `sandbox?` (`inherit|require`, default `inherit`; `require` rejects spawn unless target child runtime is sandboxed)
94+
- `sessions_spawn` does **not** accept channel-delivery params (`target`, `channel`, `to`, `threadId`, `replyTo`, `transport`). For delivery, use `message`/`sessions_send` from the spawned run.
9495

9596
## Thread-bound sessions
9697

src/agents/tools/sessions-spawn-tool.test.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,4 +136,26 @@ describe("sessions_spawn tool", () => {
136136
);
137137
expect(hoisted.spawnSubagentDirectMock).not.toHaveBeenCalled();
138138
});
139+
140+
it.each(["target", "transport", "channel", "to", "threadId", "thread_id", "replyTo", "reply_to"])(
141+
"rejects unsupported routing parameter %s",
142+
async (key) => {
143+
const tool = createSessionsSpawnTool({
144+
agentSessionKey: "agent:main:main",
145+
agentChannel: "discord",
146+
agentAccountId: "default",
147+
agentTo: "channel:123",
148+
agentThreadId: "456",
149+
});
150+
151+
await expect(
152+
tool.execute("call-unsupported-param", {
153+
task: "build feature",
154+
[key]: "value",
155+
}),
156+
).rejects.toThrow(`sessions_spawn does not support "${key}"`);
157+
expect(hoisted.spawnSubagentDirectMock).not.toHaveBeenCalled();
158+
expect(hoisted.spawnAcpDirectMock).not.toHaveBeenCalled();
159+
},
160+
);
139161
});

src/agents/tools/sessions-spawn-tool.ts

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,20 @@ import { ACP_SPAWN_MODES, spawnAcpDirect } from "../acp-spawn.js";
44
import { optionalStringEnum } from "../schema/typebox.js";
55
import { SUBAGENT_SPAWN_MODES, spawnSubagentDirect } from "../subagent-spawn.js";
66
import type { AnyAgentTool } from "./common.js";
7-
import { jsonResult, readStringParam } from "./common.js";
7+
import { jsonResult, readStringParam, ToolInputError } from "./common.js";
88

99
const SESSIONS_SPAWN_RUNTIMES = ["subagent", "acp"] as const;
1010
const SESSIONS_SPAWN_SANDBOX_MODES = ["inherit", "require"] as const;
11+
const UNSUPPORTED_SESSIONS_SPAWN_PARAM_KEYS = [
12+
"target",
13+
"transport",
14+
"channel",
15+
"to",
16+
"threadId",
17+
"thread_id",
18+
"replyTo",
19+
"reply_to",
20+
] as const;
1121

1222
const SessionsSpawnToolSchema = Type.Object({
1323
task: Type.String(),
@@ -47,6 +57,14 @@ export function createSessionsSpawnTool(opts?: {
4757
parameters: SessionsSpawnToolSchema,
4858
execute: async (_toolCallId, args) => {
4959
const params = args as Record<string, unknown>;
60+
const unsupportedParam = UNSUPPORTED_SESSIONS_SPAWN_PARAM_KEYS.find((key) =>
61+
Object.hasOwn(params, key),
62+
);
63+
if (unsupportedParam) {
64+
throw new ToolInputError(
65+
`sessions_spawn does not support "${unsupportedParam}". Use "message" or "sessions_send" for channel delivery.`,
66+
);
67+
}
5068
const task = readStringParam(params, "task", { required: true });
5169
const label = typeof params.label === "string" ? params.label.trim() : "";
5270
const runtime = params.runtime === "acp" ? "acp" : "subagent";

src/telegram/bot-message-context.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ import type { DmPolicy, TelegramGroupConfig, TelegramTopicConfig } from "../conf
3434
import { logVerbose, shouldLogVerbose } from "../globals.js";
3535
import { recordChannelActivity } from "../infra/channel-activity.js";
3636
import { resolveAgentRoute } from "../routing/resolve-route.js";
37-
import { resolveThreadSessionKeys } from "../routing/session-key.js";
37+
import { DEFAULT_ACCOUNT_ID, resolveThreadSessionKeys } from "../routing/session-key.js";
3838
import { withTelegramApiErrorLogging } from "./api-logging.js";
3939
import {
4040
firstDefined,
@@ -188,6 +188,17 @@ export const buildTelegramMessageContext = async ({
188188
},
189189
parentPeer,
190190
});
191+
// Fail closed for named Telegram accounts when route resolution falls back to
192+
// default-agent routing. This prevents cross-account DM/session contamination.
193+
if (route.accountId !== DEFAULT_ACCOUNT_ID && route.matchedBy === "default") {
194+
logInboundDrop({
195+
log: logVerbose,
196+
channel: "telegram",
197+
reason: "non-default account requires explicit binding",
198+
target: route.accountId,
199+
});
200+
return null;
201+
}
191202
const baseSessionKey = route.sessionKey;
192203
// DMs: use raw messageThreadId for thread sessions (not forum topic ids)
193204
const dmThreadId = threadSpec.scope === "dm" ? threadSpec.id : undefined;

src/telegram/bot.create-telegram-bot.test.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -911,6 +911,39 @@ describe("createTelegramBot", () => {
911911
expect(payload.AccountId).toBe("opie");
912912
expect(payload.SessionKey).toBe("agent:opie:main");
913913
});
914+
915+
it("drops non-default account DMs without explicit bindings", async () => {
916+
loadConfig.mockReturnValue({
917+
channels: {
918+
telegram: {
919+
accounts: {
920+
opie: {
921+
botToken: "tok-opie",
922+
dmPolicy: "open",
923+
},
924+
},
925+
},
926+
},
927+
});
928+
929+
createTelegramBot({ token: "tok", accountId: "opie" });
930+
const handler = getOnHandler("message") as (ctx: Record<string, unknown>) => Promise<void>;
931+
932+
await handler({
933+
message: {
934+
chat: { id: 123, type: "private" },
935+
from: { id: 999, username: "testuser" },
936+
text: "hello",
937+
date: 1736380800,
938+
message_id: 42,
939+
},
940+
me: { username: "openclaw_bot" },
941+
getFile: async () => ({ download: async () => new Uint8Array() }),
942+
});
943+
944+
expect(replySpy).not.toHaveBeenCalled();
945+
});
946+
914947
it("applies group mention overrides and fallback behavior", async () => {
915948
const cases: Array<{
916949
config: Record<string, unknown>;

0 commit comments

Comments
 (0)