Skip to content

Commit a7b4a47

Browse files
fix(cron): reject disabled delivery accounts when scheduling (#116899)
Scheduling an automation against a channel account the operator has turned off succeeded. The job was stored and scheduled against a route its owner had already disabled, and nothing reported that until it fired. `assertValidCronFailureAlert` in this file already applies the rule to the channel on the same route: an explicit unknown channel must not be "stored and only fails later as `channel_not_found`". The account had no equivalent check. Reject a `delivery.accountId` whose `accounts.<id>.enabled` is `false`, beside the existing channel checks. The check is scoped to that account entry and nothing else, because the wider signals all have false positives here: - Cron delivery ids are not always operator-typed - `delivery-context.ts` copies the current context account into inferred jobs. - Channel `isEnabled` adapters report unlisted or credential-suppressed accounts as not enabled (twitch resolves any unlisted id to `enabled: false`). - A top-level `channels.<id>.enabled: false` is not uniformly channel-wide - twitch resolves named accounts from `accounts` alone. Only the account entry itself is an unambiguous statement about this route, so only that blocks a mutation. No channel plugin is resolved on this path. Both cron mutation paths share this validator and the update caller is already gated on `"delivery" in patch`, so edits that do not touch routing stay unblocked by an id stored before this validation existed. `cron-error-classification.ts` learns the field name so the precondition recheck inside `updateWithPrecondition` still classifies it as `INVALID_REQUEST`. `delivery.failureDestination.accountId` is intentionally out of scope: its effective route layers over the global failure-destination config, so it needs route resolution before validation rather than a raw check on the partial override.
1 parent a5d89e1 commit a7b4a47

3 files changed

Lines changed: 324 additions & 0 deletions

File tree

‎src/cron/delivery-channel-validation.ts‎

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import {
55
resolveTargetPrefixedChannel,
66
validateTargetProviderPrefix,
77
} from "../infra/outbound/channel-target-prefix.js";
8+
import { normalizeAccountId } from "../routing/account-id.js";
9+
import { resolveNormalizedAccountEntry } from "../routing/account-lookup.js";
810
import { isDeliverableMessageChannel, normalizeMessageChannel } from "../utils/message-channel.js";
911
import type { CronDelivery, CronFailureAlert, CronJobCreate } from "./types.js";
1012

@@ -55,6 +57,48 @@ async function assertConfiguredAnnounceChannel(params: {
5557
}
5658
}
5759

60+
/**
61+
* Rejects an announce account the operator has turned off in config, so a job is
62+
* not scheduled against a route its owner already disabled - the same late failure
63+
* this file prevents for `channel` (see `assertValidCronFailureAlert`).
64+
*
65+
* Scoped to a matching `accounts.<id>.enabled: false` entry, and nothing else.
66+
* Cron delivery ids are not always operator-typed (`delivery-context.ts` copies
67+
* the current context account into inferred jobs); channel `isEnabled` adapters
68+
* report unlisted or credential-suppressed accounts as not enabled; and a
69+
* top-level `channels.<id>.enabled: false` is not uniformly channel-wide - twitch
70+
* resolves named accounts from `accounts` alone. Only the account entry itself is
71+
* an unambiguous statement about this route.
72+
*/
73+
function assertEnabledAnnounceAccount(params: {
74+
cfg: OpenClawConfig;
75+
channel?: string;
76+
accountId?: string;
77+
field: "delivery.accountId";
78+
}) {
79+
if (!params.accountId || !params.channel || params.channel === "last") {
80+
return;
81+
}
82+
// Aliases are valid delivery channels (`urbit` -> `tlon`) but config lives under
83+
// the canonical id, so normalize before reading it.
84+
const channel = normalizeMessageChannel(params.channel) ?? params.channel;
85+
const channelConfig = (params.cfg.channels as Record<string, unknown> | undefined)?.[channel];
86+
if (!channelConfig || typeof channelConfig !== "object" || Array.isArray(channelConfig)) {
87+
return;
88+
}
89+
const accounts = (channelConfig as { accounts?: Record<string, { enabled?: unknown }> }).accounts;
90+
// Channels resolve account keys canonically (matrix `"Team Ops"` answers to
91+
// `team-ops`), so match the same way or a disabled entry is missed.
92+
if (
93+
resolveNormalizedAccountEntry(accounts, params.accountId, normalizeAccountId)?.enabled !== false
94+
) {
95+
return;
96+
}
97+
throw new Error(
98+
`${params.field}: account "${params.accountId}" is disabled for channel ${channel}`,
99+
);
100+
}
101+
58102
function resolveAnnounceValidationChannel(params: {
59103
channel?: string;
60104
to?: string;
@@ -93,6 +137,12 @@ export async function assertValidCronAnnounceDelivery(params: {
93137
channel: resolveAnnounceValidationChannel(params.delivery),
94138
field: "delivery.channel",
95139
});
140+
assertEnabledAnnounceAccount({
141+
cfg: params.cfg,
142+
channel: resolveAnnounceValidationChannel(params.delivery),
143+
accountId: params.delivery.accountId,
144+
field: "delivery.accountId",
145+
});
96146
}
97147

98148
const failureDestination = params.delivery?.failureDestination;

‎src/gateway/server-methods/cron-error-classification.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ export function isCronInvalidRequestError(err: unknown): boolean {
2121
message.includes('cron.update payload.kind="agentTurn" requires message') ||
2222
message.includes("cron webhook delivery requires") ||
2323
message.includes("delivery.channel") ||
24+
message.includes("delivery.accountId") ||
2425
message.includes("delivery.failureDestination.channel") ||
2526
message.includes("failureAlert.channel") ||
2627
message.includes("cron completion destination webhook requires") ||

‎src/gateway/server-methods/cron.validation.test.ts‎

Lines changed: 273 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,19 @@ function createPrefixOnlyChannelPlugin(
7272
};
7373
}
7474

75+
function createEnablementHostileChannelPlugin(id: string): ChannelPlugin {
76+
const base = createPrefixOnlyChannelPlugin(id, [id]);
77+
return {
78+
...base,
79+
config: {
80+
...base.config,
81+
// Mirrors twitch/discord: an unlisted or credential-suppressed account
82+
// resolves to a not-enabled account, which must NOT read as operator intent.
83+
isEnabled: () => false,
84+
},
85+
};
86+
}
87+
7588
function setCronValidationTestRegistry(): void {
7689
setActivePluginRegistry(
7790
createTestRegistry([
@@ -85,6 +98,11 @@ function setCronValidationTestRegistry(): void {
8598
plugin: createPrefixOnlyChannelPlugin("slack", ["slack"]),
8699
source: "test:slack",
87100
},
101+
{
102+
pluginId: "twitch",
103+
plugin: createEnablementHostileChannelPlugin("twitch"),
104+
source: "test:twitch",
105+
},
88106
{
89107
pluginId: "msteams",
90108
plugin: createPrefixOnlyChannelPlugin("msteams", ["msteams", "teams"], ["teams"]),
@@ -346,6 +364,20 @@ function telegramSlackConfig(params: { includeMainSession?: boolean } = {}): Ope
346364
} as OpenClawConfig;
347365
}
348366

367+
function telegramDisabledAccountConfig(): OpenClawConfig {
368+
return {
369+
channels: {
370+
telegram: {
371+
accounts: {
372+
primary: { botToken: "telegram-token-primary" },
373+
retired: { botToken: "telegram-token-retired", enabled: false },
374+
},
375+
},
376+
},
377+
plugins: pluginEntries("telegram"),
378+
} as OpenClawConfig;
379+
}
380+
349381
function msteamsConfig(): OpenClawConfig {
350382
return {
351383
channels: {
@@ -2134,6 +2166,247 @@ describe("cron method validation", () => {
21342166
expectResponseError(respond, { messageIncludes: "delivery.channel is not configured" });
21352167
});
21362168

2169+
it("rejects an announce accountId whose account is explicitly disabled", async () => {
2170+
setRuntimeConfig(telegramDisabledAccountConfig());
2171+
2172+
const { context, respond } = await invokeCronAdd(
2173+
agentTurnCronParams({
2174+
name: "disabled delivery account",
2175+
delivery: {
2176+
mode: "announce",
2177+
channel: "telegram",
2178+
to: "telegram:123456",
2179+
accountId: "retired",
2180+
},
2181+
}),
2182+
);
2183+
2184+
expect(context.cron.add).not.toHaveBeenCalled();
2185+
expectResponseError(respond, {
2186+
code: "INVALID_REQUEST",
2187+
messageIncludes: "delivery.accountId",
2188+
});
2189+
});
2190+
2191+
it("accepts an announce accountId whose account is enabled", async () => {
2192+
setRuntimeConfig(telegramDisabledAccountConfig());
2193+
2194+
const { context, respond } = await invokeCronAdd(
2195+
agentTurnCronParams({
2196+
name: "enabled delivery account",
2197+
delivery: {
2198+
mode: "announce",
2199+
channel: "telegram",
2200+
to: "telegram:123456",
2201+
accountId: "primary",
2202+
},
2203+
}),
2204+
);
2205+
2206+
expect(context.cron.add).toHaveBeenCalled();
2207+
expectCronSuccess(respond);
2208+
});
2209+
2210+
it("accepts an unlisted accountId so binding-derived ids keep working", async () => {
2211+
// Cron delivery ids are not always operator-typed: delivery-context.ts copies
2212+
// the current context account into inferred jobs, and a channel may resolve an
2213+
// unlisted binding id against its sole token. Only disabled accounts are rejected.
2214+
setRuntimeConfig(telegramConfig());
2215+
2216+
const { context, respond } = await invokeCronAdd(
2217+
agentTurnCronParams({
2218+
name: "binding-derived delivery account",
2219+
delivery: {
2220+
mode: "announce",
2221+
channel: "telegram",
2222+
to: "telegram:123456",
2223+
accountId: "bot:12345",
2224+
},
2225+
}),
2226+
);
2227+
2228+
expect(context.cron.add).toHaveBeenCalled();
2229+
expectCronSuccess(respond);
2230+
});
2231+
2232+
it("accepts an unlisted accountId on a channel whose isEnabled reports it disabled", async () => {
2233+
// twitch/discord resolve an unlisted or credential-suppressed account to a
2234+
// not-enabled account. That is not the operator disabling a route, so cron
2235+
// creation must not be blocked by it - only a config-declared enabled:false is.
2236+
setRuntimeConfig({
2237+
channels: { twitch: { accounts: { main: { accessToken: "t" } } } },
2238+
plugins: pluginEntries("twitch"),
2239+
} as OpenClawConfig);
2240+
2241+
const { respond } = await invokeCronAdd(
2242+
agentTurnCronParams({
2243+
name: "enablement-hostile channel account",
2244+
delivery: {
2245+
mode: "announce",
2246+
channel: "twitch",
2247+
to: "twitch:room",
2248+
accountId: "unlisted-account",
2249+
},
2250+
}),
2251+
);
2252+
2253+
const call = respond.mock.calls.at(0);
2254+
// The channel itself may be judged unconfigured in this fixture; what must not
2255+
// happen is a rejection naming delivery.accountId.
2256+
expect(String(call?.[2] ? (call[2] as { message?: unknown }).message : "")).not.toContain(
2257+
"delivery.accountId",
2258+
);
2259+
});
2260+
2261+
it("accepts a named account when only the top-level channel config is disabled", async () => {
2262+
// A top-level `channels.<id>.enabled: false` is not uniformly channel-wide:
2263+
// twitch resolves named accounts from `accounts` alone, so a disabled root
2264+
// block must not make every account on that channel unschedulable.
2265+
setRuntimeConfig({
2266+
channels: {
2267+
twitch: { enabled: false, accounts: { main: { accessToken: "t" } } },
2268+
},
2269+
plugins: pluginEntries("twitch"),
2270+
} as OpenClawConfig);
2271+
2272+
const { respond } = await invokeCronAdd(
2273+
agentTurnCronParams({
2274+
name: "named account under disabled root",
2275+
delivery: {
2276+
mode: "announce",
2277+
channel: "twitch",
2278+
to: "twitch:room",
2279+
accountId: "main",
2280+
},
2281+
}),
2282+
);
2283+
2284+
const call = respond.mock.calls.at(0);
2285+
expect(String(call?.[2] ? (call[2] as { message?: unknown }).message : "")).not.toContain(
2286+
"delivery.accountId",
2287+
);
2288+
});
2289+
2290+
it("rejects a disabled account whose config key is not canonical", async () => {
2291+
// Channels resolve account keys canonically: matrix treats `"Team Ops"` as
2292+
// `team-ops`. A disabled entry stored under the display form must still match.
2293+
setRuntimeConfig({
2294+
channels: {
2295+
telegram: {
2296+
accounts: {
2297+
primary: { botToken: "telegram-token-primary" },
2298+
"Team Ops": { botToken: "telegram-token-team", enabled: false },
2299+
},
2300+
},
2301+
},
2302+
plugins: pluginEntries("telegram"),
2303+
} as OpenClawConfig);
2304+
2305+
const { context, respond } = await invokeCronAdd(
2306+
agentTurnCronParams({
2307+
name: "noncanonical disabled account key",
2308+
delivery: {
2309+
mode: "announce",
2310+
channel: "telegram",
2311+
to: "telegram:123456",
2312+
accountId: "team-ops",
2313+
},
2314+
}),
2315+
);
2316+
2317+
expect(context.cron.add).not.toHaveBeenCalled();
2318+
expectResponseError(respond, {
2319+
code: "INVALID_REQUEST",
2320+
messageIncludes: "delivery.accountId",
2321+
});
2322+
});
2323+
2324+
it("rejects a disabled account reached through a channel alias", async () => {
2325+
// Aliases are valid delivery channels (msteams answers to `teams`) while the
2326+
// config lives under the canonical id, so the alias must resolve before lookup.
2327+
setRuntimeConfig({
2328+
channels: {
2329+
msteams: {
2330+
accounts: {
2331+
work: { botToken: "teams-token-work", enabled: false },
2332+
},
2333+
},
2334+
},
2335+
plugins: pluginEntries("msteams"),
2336+
} as OpenClawConfig);
2337+
2338+
const { context, respond } = await invokeCronAdd(
2339+
agentTurnCronParams({
2340+
name: "disabled account via alias",
2341+
delivery: {
2342+
mode: "announce",
2343+
channel: "teams",
2344+
to: "msteams:conversation",
2345+
accountId: "work",
2346+
},
2347+
}),
2348+
);
2349+
2350+
expect(context.cron.add).not.toHaveBeenCalled();
2351+
expectResponseError(respond, {
2352+
code: "INVALID_REQUEST",
2353+
messageIncludes: "delivery.accountId",
2354+
});
2355+
});
2356+
2357+
it("accepts announce delivery that omits accountId", async () => {
2358+
setRuntimeConfig(telegramConfig());
2359+
2360+
const { context, respond } = await invokeCronAdd(
2361+
agentTurnCronParams({
2362+
name: "no delivery account",
2363+
delivery: { mode: "announce", channel: "telegram", to: "telegram:123456" },
2364+
}),
2365+
);
2366+
2367+
expect(context.cron.add).toHaveBeenCalled();
2368+
expectCronSuccess(respond);
2369+
});
2370+
2371+
it("rejects a delivery patch that introduces a disabled accountId", async () => {
2372+
setRuntimeConfig(telegramDisabledAccountConfig());
2373+
2374+
const { context, respond } = await invokeCronUpdate(
2375+
{ id: "cron-1", patch: { delivery: { accountId: "retired" } } },
2376+
createCronJob({
2377+
delivery: { mode: "announce", channel: "telegram", to: "telegram:123456" },
2378+
}),
2379+
);
2380+
2381+
expect(context.cron.update).not.toHaveBeenCalled();
2382+
expectResponseError(respond, {
2383+
code: "INVALID_REQUEST",
2384+
messageIncludes: "delivery.accountId",
2385+
});
2386+
});
2387+
2388+
it("keeps a non-routing patch unblocked by a legacy-invalid stored accountId", async () => {
2389+
// Account validation runs only behind the delivery-patch gate, so edits that do
2390+
// not touch routing must not be rejected because of an id stored before this
2391+
// validation existed.
2392+
setRuntimeConfig(telegramDisabledAccountConfig());
2393+
2394+
const { context, respond } = await invokeCronUpdate(
2395+
{ id: "cron-1", patch: { enabled: false } },
2396+
createCronJob({
2397+
delivery: {
2398+
mode: "announce",
2399+
channel: "telegram",
2400+
to: "telegram:123456",
2401+
accountId: "retired",
2402+
},
2403+
}),
2404+
);
2405+
2406+
expect(context.cron.update).toHaveBeenCalled();
2407+
expectCronSuccess(respond);
2408+
});
2409+
21372410
it("accepts provider-prefixed announce target without delivery.channel when multiple channels are configured", async () => {
21382411
setRuntimeConfig(telegramSlackConfig({ includeMainSession: true }));
21392412

0 commit comments

Comments
 (0)