Skip to content

Commit a21385a

Browse files
authored
fix(channels): scope wizard credentials to the selected account (#110969)
* fix(channels): scope wizard credentials to selected account * fix(channels): migrate empty accounts map in wizard scope guard * test(channels): guard resolved account id in empty-map regression
1 parent 4e6bb61 commit a21385a

3 files changed

Lines changed: 439 additions & 3 deletions

File tree

‎src/channels/plugins/setup-promotion-keys.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ const COMMON_SINGLE_ACCOUNT_PROMOTION_KEYS = [
55
"name",
66
"token",
77
"tokenFile",
8+
"botId",
9+
"secret",
810
"botToken",
911
"appToken",
1012
"account",
Lines changed: 365 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,365 @@
1+
import { describe, expect, it } from "vitest";
2+
import type { OpenClawConfig } from "../../config/types.openclaw.js";
3+
import { DEFAULT_ACCOUNT_ID, normalizeAccountId } from "../../routing/session-key.js";
4+
import { createChannelTestPluginBase } from "../../test-utils/channel-plugins.js";
5+
import {
6+
createQueuedWizardPrompter,
7+
runSetupWizardConfigure,
8+
} from "../../test-utils/plugin-setup-wizard.js";
9+
import type { ChannelSetupPlugin, ChannelSetupWizard } from "./setup-wizard-types.js";
10+
import { buildChannelSetupWizardAdapterFromSetupWizard } from "./setup-wizard.js";
11+
12+
type AccountConfig = {
13+
botId?: string;
14+
secret?: string;
15+
enabled?: boolean;
16+
marker?: { keep: string };
17+
};
18+
19+
type ChannelConfig = AccountConfig & {
20+
defaultAccount?: string;
21+
accounts?: Record<string, AccountConfig>;
22+
};
23+
24+
function getChannelConfig(cfg: OpenClawConfig): ChannelConfig {
25+
return ((cfg.channels as Record<string, unknown> | undefined)?.demo ?? {}) as ChannelConfig;
26+
}
27+
28+
function resolveDefaultAccountId(cfg: OpenClawConfig): string {
29+
const channel = getChannelConfig(cfg);
30+
return normalizeAccountId(
31+
channel.defaultAccount ?? Object.keys(channel.accounts ?? {})[0] ?? DEFAULT_ACCOUNT_ID,
32+
);
33+
}
34+
35+
function resolveLegacyAccount(cfg: OpenClawConfig): AccountConfig {
36+
const channel = getChannelConfig(cfg);
37+
return {
38+
...channel,
39+
...channel.accounts?.[resolveDefaultAccountId(cfg)],
40+
};
41+
}
42+
43+
function setLegacyAccount(cfg: OpenClawConfig, patch: AccountConfig): OpenClawConfig {
44+
const channel = getChannelConfig(cfg);
45+
if (!channel.accounts) {
46+
return {
47+
...cfg,
48+
channels: {
49+
...cfg.channels,
50+
demo: { ...channel, ...patch },
51+
},
52+
} as OpenClawConfig;
53+
}
54+
const accountId = resolveDefaultAccountId(cfg);
55+
return {
56+
...cfg,
57+
channels: {
58+
...cfg.channels,
59+
demo: {
60+
...channel,
61+
accounts: {
62+
...channel.accounts,
63+
[accountId]: {
64+
...channel.accounts[accountId],
65+
...patch,
66+
},
67+
},
68+
},
69+
},
70+
} as OpenClawConfig;
71+
}
72+
73+
function createLegacyPlugin(): ChannelSetupPlugin {
74+
return {
75+
...createChannelTestPluginBase({
76+
id: "demo",
77+
label: "Demo",
78+
config: {
79+
listAccountIds: (cfg) => {
80+
const ids = Object.keys(getChannelConfig(cfg).accounts ?? {});
81+
return ids.length > 0 ? ids : [DEFAULT_ACCOUNT_ID];
82+
},
83+
defaultAccountId: resolveDefaultAccountId,
84+
},
85+
}),
86+
setup: {
87+
applyAccountConfig: ({ cfg, input }) =>
88+
setLegacyAccount(cfg, {
89+
botId: typeof input.token === "string" ? input.token : undefined,
90+
secret: typeof input.privateKey === "string" ? input.privateKey : undefined,
91+
}),
92+
},
93+
};
94+
}
95+
96+
function createLegacyWizard(): ChannelSetupWizard {
97+
return {
98+
channel: "demo",
99+
status: {
100+
configuredLabel: "Configured",
101+
unconfiguredLabel: "Not configured",
102+
resolveConfigured: ({ cfg }) => Boolean(resolveLegacyAccount(cfg).botId),
103+
},
104+
credentials: [
105+
{
106+
inputKey: "token",
107+
providerHint: "Demo",
108+
credentialLabel: "Bot ID",
109+
envPrompt: "Use Bot ID from environment?",
110+
keepPrompt: "Bot ID already configured. Keep it?",
111+
inputPrompt: "Bot ID",
112+
inspect: ({ cfg }) => {
113+
const botId = resolveLegacyAccount(cfg).botId;
114+
return {
115+
accountConfigured: Boolean(botId),
116+
hasConfiguredValue: Boolean(botId),
117+
resolvedValue: botId,
118+
};
119+
},
120+
applySet: ({ cfg, resolvedValue }) => setLegacyAccount(cfg, { botId: resolvedValue }),
121+
},
122+
{
123+
inputKey: "privateKey",
124+
providerHint: "Demo",
125+
credentialLabel: "Token",
126+
envPrompt: "Use token from environment?",
127+
keepPrompt: "Token already configured. Keep it?",
128+
inputPrompt: "Token",
129+
inspect: ({ cfg }) => {
130+
const secret = resolveLegacyAccount(cfg).secret;
131+
return {
132+
accountConfigured: Boolean(secret),
133+
hasConfiguredValue: Boolean(secret),
134+
resolvedValue: secret,
135+
};
136+
},
137+
applySet: ({ cfg, resolvedValue: secret }) => setLegacyAccount(cfg, { secret }),
138+
},
139+
],
140+
};
141+
}
142+
143+
function createConfigure() {
144+
return buildChannelSetupWizardAdapterFromSetupWizard({
145+
plugin: createLegacyPlugin(),
146+
wizard: createLegacyWizard(),
147+
}).configure;
148+
}
149+
150+
describe("channel setup wizard account scoping", () => {
151+
it("does not prefill or overwrite the existing account when adding a new account", async () => {
152+
const main = {
153+
botId: "test-main-bot-id",
154+
secret: "test-secret",
155+
enabled: false,
156+
marker: { keep: "byte-identical" },
157+
};
158+
const before = JSON.stringify(main);
159+
const queued = createQueuedWizardPrompter({
160+
selectValues: ["__new__"],
161+
textValues: ["alerts", "test-alerts-bot-id", "example-secret"],
162+
confirmValues: [false, false],
163+
});
164+
165+
const result = await runSetupWizardConfigure({
166+
configure: createConfigure(),
167+
cfg: {
168+
channels: {
169+
demo: {
170+
enabled: true,
171+
defaultAccount: "main",
172+
accounts: { main },
173+
},
174+
},
175+
} as OpenClawConfig,
176+
prompter: queued.prompter,
177+
shouldPromptAccountIds: true,
178+
options: { secretInputMode: "plaintext" as const },
179+
});
180+
181+
const channel = getChannelConfig(result.cfg);
182+
expect(result.accountId).toBe("alerts");
183+
expect(channel.defaultAccount).toBe("main");
184+
expect(JSON.stringify(channel.accounts?.main)).toBe(before);
185+
expect(channel.accounts?.alerts).toEqual({
186+
botId: "test-alerts-bot-id",
187+
secret: "example-secret",
188+
});
189+
expect(queued.confirm).not.toHaveBeenCalled();
190+
});
191+
192+
it("promotes mixed root credentials before adding another named account", async () => {
193+
const queued = createQueuedWizardPrompter({
194+
selectValues: ["__new__"],
195+
textValues: ["alerts", "test-alerts-bot-id", "example-secret"],
196+
});
197+
198+
const result = await runSetupWizardConfigure({
199+
configure: createConfigure(),
200+
cfg: {
201+
channels: {
202+
demo: {
203+
defaultAccount: "main",
204+
botId: "test-main-bot-id",
205+
secret: "test-secret",
206+
accounts: { main: { marker: { keep: "mixed-shape" } } },
207+
},
208+
},
209+
} as OpenClawConfig,
210+
prompter: queued.prompter,
211+
shouldPromptAccountIds: true,
212+
options: { secretInputMode: "plaintext" as const },
213+
});
214+
215+
const channel = getChannelConfig(result.cfg);
216+
expect(channel).not.toHaveProperty("botId");
217+
expect(channel).not.toHaveProperty("secret");
218+
expect(channel.defaultAccount).toBe("main");
219+
expect(channel.accounts).toEqual({
220+
main: {
221+
marker: { keep: "mixed-shape" },
222+
botId: "test-main-bot-id",
223+
secret: "test-secret",
224+
},
225+
alerts: { botId: "test-alerts-bot-id", secret: "example-secret" },
226+
});
227+
expect(queued.confirm).not.toHaveBeenCalled();
228+
});
229+
230+
it("migrates stale root credentials when only an empty accounts map exists", async () => {
231+
const queued = createQueuedWizardPrompter({
232+
confirmValues: [false, false],
233+
textValues: ["test-new-bot-id", "mock-secret"],
234+
});
235+
236+
const result = await runSetupWizardConfigure({
237+
configure: createConfigure(),
238+
cfg: {
239+
channels: {
240+
demo: { botId: "test-stale-bot-id", secret: "fixture-secret", accounts: {} },
241+
},
242+
} as OpenClawConfig,
243+
prompter: queued.prompter,
244+
options: { secretInputMode: "plaintext" as const },
245+
});
246+
247+
const accountId = result.accountId;
248+
if (!accountId) {
249+
throw new Error("expected the wizard to resolve an account id");
250+
}
251+
const channel = getChannelConfig(result.cfg);
252+
expect(channel).not.toHaveProperty("botId");
253+
expect(channel).not.toHaveProperty("secret");
254+
expect(channel.accounts?.[accountId]).toEqual({
255+
botId: "test-new-bot-id",
256+
secret: "mock-secret",
257+
});
258+
expect(Object.keys(channel.accounts ?? {})).toEqual([accountId]);
259+
});
260+
261+
it("replaces credentials only in the selected existing account after rejecting keep", async () => {
262+
const main = { botId: "test-main-bot-id", secret: "test-secret" };
263+
const before = JSON.stringify(main);
264+
const queued = createQueuedWizardPrompter({
265+
confirmValues: [false, false],
266+
textValues: ["test-new-bot-id", "mock-secret"],
267+
});
268+
269+
const result = await runSetupWizardConfigure({
270+
configure: createConfigure(),
271+
cfg: {
272+
channels: {
273+
demo: {
274+
defaultAccount: "main",
275+
accounts: {
276+
main,
277+
alerts: { botId: "test-old-bot-id", secret: "fixture-secret" },
278+
},
279+
},
280+
},
281+
} as OpenClawConfig,
282+
prompter: queued.prompter,
283+
accountOverrides: { demo: "alerts" },
284+
options: { secretInputMode: "plaintext" as const },
285+
});
286+
287+
const channel = getChannelConfig(result.cfg);
288+
expect(channel.defaultAccount).toBe("main");
289+
expect(JSON.stringify(channel.accounts?.main)).toBe(before);
290+
expect(channel.accounts?.alerts).toEqual({
291+
botId: "test-new-bot-id",
292+
secret: "mock-secret",
293+
});
294+
expect(queued.confirm).toHaveBeenCalledTimes(2);
295+
});
296+
297+
it("scopes a named default account when another account is the channel default", async () => {
298+
const main = { botId: "test-main-bot-id", secret: "test-secret" };
299+
const before = JSON.stringify(main);
300+
const queued = createQueuedWizardPrompter({
301+
confirmValues: [false, false],
302+
textValues: ["test-new-bot-id", "mock-secret"],
303+
});
304+
305+
const result = await runSetupWizardConfigure({
306+
configure: createConfigure(),
307+
cfg: {
308+
channels: {
309+
demo: {
310+
defaultAccount: "main",
311+
accounts: {
312+
default: { botId: "test-old-bot-id", secret: "fixture-secret" },
313+
main,
314+
},
315+
},
316+
},
317+
} as OpenClawConfig,
318+
prompter: queued.prompter,
319+
accountOverrides: { demo: DEFAULT_ACCOUNT_ID },
320+
options: { secretInputMode: "plaintext" as const },
321+
});
322+
323+
const channel = getChannelConfig(result.cfg);
324+
expect(channel.defaultAccount).toBe("main");
325+
expect(JSON.stringify(channel.accounts?.main)).toBe(before);
326+
expect(channel.accounts?.default).toEqual({
327+
botId: "test-new-bot-id",
328+
secret: "mock-secret",
329+
});
330+
expect(queued.confirm).toHaveBeenCalledTimes(2);
331+
});
332+
333+
it("promotes root credentials before adding the first named account", async () => {
334+
const queued = createQueuedWizardPrompter({
335+
selectValues: ["__new__"],
336+
textValues: ["alerts", "test-alerts-bot-id", "example-secret"],
337+
});
338+
339+
const result = await runSetupWizardConfigure({
340+
configure: createConfigure(),
341+
cfg: {
342+
channels: {
343+
demo: {
344+
enabled: true,
345+
botId: "test-main-bot-id",
346+
secret: "test-secret",
347+
},
348+
},
349+
} as OpenClawConfig,
350+
prompter: queued.prompter,
351+
shouldPromptAccountIds: true,
352+
options: { secretInputMode: "plaintext" as const },
353+
});
354+
355+
const channel = getChannelConfig(result.cfg);
356+
expect(channel).not.toHaveProperty("botId");
357+
expect(channel).not.toHaveProperty("secret");
358+
expect(channel).not.toHaveProperty("defaultAccount");
359+
expect(channel.accounts).toEqual({
360+
default: { botId: "test-main-bot-id", secret: "test-secret" },
361+
alerts: { botId: "test-alerts-bot-id", secret: "example-secret" },
362+
});
363+
expect(queued.confirm).not.toHaveBeenCalled();
364+
});
365+
});

0 commit comments

Comments
 (0)