Skip to content

Commit 90b426b

Browse files
committed
fix(cron): reject disabled delivery accounts when scheduling
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 c75e435 commit 90b426b

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"]),
@@ -345,6 +363,20 @@ function telegramSlackConfig(params: { includeMainSession?: boolean } = {}): Ope
345363
} as OpenClawConfig;
346364
}
347365

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

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

0 commit comments

Comments
 (0)