Skip to content

Commit c055a01

Browse files
committed
fix(onboard): keep referenced provider secrets out of plaintext
1 parent e99f6dc commit c055a01

5 files changed

Lines changed: 578 additions & 81 deletions

File tree

‎src/commands/onboard-non-interactive/api-keys.test.ts‎

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,4 +249,46 @@ describe("resolveNonInteractiveApiKey", () => {
249249
const [profileParams] = resolveApiKeyForProfile.mock.calls[0] ?? [];
250250
expect(profileParams?.profileId).toBe("custom-models-custom-local:default");
251251
});
252+
253+
it("retains existing profile reuse in secret-ref mode without inventing an env reference", async () => {
254+
const runtime = createRuntime();
255+
authStore.profiles["custom-models-custom-local:default"] = {
256+
type: "api_key",
257+
provider: "custom-models-custom-local",
258+
key: "fixture-profile-key",
259+
};
260+
resolveEnvApiKey.mockReturnValue(null);
261+
262+
const result = await resolveNonInteractiveApiKey({
263+
provider: "custom-models-custom-local",
264+
cfg: {},
265+
flagName: "--custom-api-key",
266+
envVar: "CUSTOM_API_KEY",
267+
runtime: runtime as never,
268+
secretInputMode: "ref",
269+
});
270+
271+
expect(result).toEqual({ key: "fixture-profile-key", source: "profile" });
272+
expect(runtime.error).not.toHaveBeenCalled();
273+
expect(runtime.exit).not.toHaveBeenCalled();
274+
});
275+
276+
it("keeps intentionally keyless providers optional in secret-ref mode", async () => {
277+
const runtime = createRuntime();
278+
resolveEnvApiKey.mockReturnValue(null);
279+
280+
const result = await resolveNonInteractiveApiKey({
281+
provider: "custom-models-custom-local",
282+
cfg: {},
283+
flagName: "--custom-api-key",
284+
envVar: "CUSTOM_API_KEY",
285+
runtime: runtime as never,
286+
required: false,
287+
secretInputMode: "ref",
288+
});
289+
290+
expect(result).toBeNull();
291+
expect(runtime.error).not.toHaveBeenCalled();
292+
expect(runtime.exit).not.toHaveBeenCalled();
293+
});
252294
});

‎src/commands/onboard-non-interactive/local/auth-choice.test.ts‎

Lines changed: 250 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,12 @@
22
import { beforeEach, describe, expect, it, vi } from "vitest";
33
import type { OpenClawConfig } from "../../../config/config.js";
44
import { resolveAgentModelPrimaryValue } from "../../../config/model-input.js";
5+
import { commitNonInteractiveOnboardConfig } from "../config-write.js";
56
import { applyNonInteractiveAuthChoice } from "./auth-choice.js";
67

8+
const writeWizardConfigFile = vi.hoisted(() => vi.fn(async (config: OpenClawConfig) => config));
9+
vi.mock("../../../wizard/setup.shared.js", () => ({ writeWizardConfigFile }));
10+
711
const formatAuthChoiceChoicesForCli = vi.hoisted(() =>
812
vi.fn(() => "custom-api-key|skip|demo-provider-api-key"),
913
);
@@ -234,6 +238,252 @@ describe("applyNonInteractiveAuthChoice", () => {
234238
expect(apiKeyParams?.secretInputMode).toBe("ref");
235239
});
236240

241+
it("never commits an existing profile key as plaintext during custom secret-ref onboarding", async () => {
242+
const runtime = createRuntime();
243+
const nextConfig = { agents: { defaults: {} } } as OpenClawConfig;
244+
const profileKey = "fixture-custom-profile-secret";
245+
resolveNonInteractiveApiKey.mockResolvedValueOnce({ key: profileKey, source: "profile" });
246+
247+
const result = await applyNonInteractiveAuthChoice({
248+
nextConfig,
249+
authChoice: "custom-api-key",
250+
opts: {
251+
customBaseUrl: "https://models.custom.local/v1",
252+
customModelId: "local-large",
253+
secretInputMode: "ref",
254+
} as never,
255+
runtime: runtime as never,
256+
baseConfig: nextConfig,
257+
target,
258+
});
259+
260+
expect(result).not.toBeNull();
261+
await commitNonInteractiveOnboardConfig({
262+
nextConfig: result!,
263+
baseConfig: nextConfig,
264+
});
265+
266+
const persistedConfig = writeWizardConfigFile.mock.calls.at(-1)?.[0];
267+
expect(
268+
persistedConfig?.models?.providers?.["custom-models-custom-local"]?.apiKey,
269+
).toBeUndefined();
270+
expect(JSON.stringify(persistedConfig)).not.toContain(profileKey);
271+
expect(runtime.error).not.toHaveBeenCalled();
272+
expect(runtime.exit).not.toHaveBeenCalled();
273+
});
274+
275+
it.each([
276+
{ source: "flag", key: "fixture-custom-literal-secret" },
277+
{ source: "env", key: "fixture-custom-anonymous-env-secret" },
278+
] as const)(
279+
"never serializes an unreferenceable custom $source key in secret-ref mode",
280+
async (resolved) => {
281+
const runtime = createRuntime();
282+
const nextConfig = { agents: { defaults: {} } } as OpenClawConfig;
283+
resolveNonInteractiveApiKey.mockResolvedValueOnce(resolved);
284+
285+
const result = await applyNonInteractiveAuthChoice({
286+
nextConfig,
287+
authChoice: "custom-api-key",
288+
opts: {
289+
customBaseUrl: "https://models.custom.local/v1",
290+
customModelId: "local-large",
291+
secretInputMode: "ref",
292+
} as never,
293+
runtime: runtime as never,
294+
baseConfig: nextConfig,
295+
target,
296+
});
297+
298+
expect(result).toBeNull();
299+
expect(writeWizardConfigFile).not.toHaveBeenCalled();
300+
expect(runtime.exit).toHaveBeenCalledWith(1);
301+
const errorText = runtime.error.mock.calls.map(([message]) => String(message)).join("\n");
302+
expect(errorText).toContain("CUSTOM_API_KEY");
303+
expect(errorText).toContain("--secret-input-mode ref");
304+
expect(errorText).not.toContain(resolved.key);
305+
},
306+
);
307+
308+
it.each([
309+
{ source: "env", provider: "default", id: "EXISTING_CUSTOM_API_KEY" },
310+
{ source: "file", provider: "local", id: "/providers/custom" },
311+
{ source: "exec", provider: "vault", id: "custom-provider" },
312+
] as const)(
313+
"preserves existing $source custom SecretRefs when reusing an auth profile",
314+
async (ref) => {
315+
const runtime = createRuntime();
316+
const providerId = "custom-models-custom-local";
317+
const nextConfig = {
318+
models: {
319+
providers: {
320+
[providerId]: {
321+
baseUrl: "https://models.custom.local/v1",
322+
apiKey: ref,
323+
models: [],
324+
},
325+
},
326+
},
327+
} as OpenClawConfig;
328+
resolveNonInteractiveApiKey.mockResolvedValueOnce({
329+
key: "fixture-existing-profile-secret",
330+
source: "profile",
331+
});
332+
333+
const result = await applyNonInteractiveAuthChoice({
334+
nextConfig,
335+
authChoice: "custom-api-key",
336+
opts: {
337+
customBaseUrl: "https://models.custom.local/v1",
338+
customModelId: "local-large",
339+
secretInputMode: "ref",
340+
} as never,
341+
runtime: runtime as never,
342+
baseConfig: nextConfig,
343+
target,
344+
});
345+
346+
expect(result?.models?.providers?.[providerId]?.apiKey).toEqual(ref);
347+
expect(runtime.error).not.toHaveBeenCalled();
348+
},
349+
);
350+
351+
it("preserves intentionally keyless custom setup in secret-ref mode", async () => {
352+
const runtime = createRuntime();
353+
const nextConfig = { agents: { defaults: {} } } as OpenClawConfig;
354+
resolveNonInteractiveApiKey.mockResolvedValueOnce(null);
355+
356+
const result = await applyNonInteractiveAuthChoice({
357+
nextConfig,
358+
authChoice: "custom-api-key",
359+
opts: {
360+
customBaseUrl: "https://models.custom.local/v1",
361+
customModelId: "local-large",
362+
secretInputMode: "ref",
363+
} as never,
364+
runtime: runtime as never,
365+
baseConfig: nextConfig,
366+
target,
367+
});
368+
369+
expect(result?.models?.providers?.["custom-models-custom-local"]?.apiKey).toBeUndefined();
370+
expect(runtime.error).not.toHaveBeenCalled();
371+
expect(runtime.exit).not.toHaveBeenCalled();
372+
});
373+
374+
it("preserves existing custom profile serialization in explicit plaintext mode", async () => {
375+
const runtime = createRuntime();
376+
const nextConfig = { agents: { defaults: {} } } as OpenClawConfig;
377+
resolveNonInteractiveApiKey.mockResolvedValueOnce({
378+
key: "fixture-plaintext-profile-key",
379+
source: "profile",
380+
});
381+
382+
const result = await applyNonInteractiveAuthChoice({
383+
nextConfig,
384+
authChoice: "custom-api-key",
385+
opts: {
386+
customBaseUrl: "https://models.custom.local/v1",
387+
customModelId: "local-large",
388+
secretInputMode: "plaintext",
389+
} as never,
390+
runtime: runtime as never,
391+
baseConfig: nextConfig,
392+
target,
393+
});
394+
395+
expect(result?.models?.providers?.["custom-models-custom-local"]?.apiKey).toBe(
396+
"fixture-plaintext-profile-key",
397+
);
398+
});
399+
400+
it.each([
401+
{ source: "profile", key: "fixture-plugin-profile-secret" },
402+
{ source: "flag", key: "fixture-plugin-literal-secret" },
403+
{ source: "env", key: "fixture-plugin-env-secret" },
404+
] as const)(
405+
"rejects non-referenceable $source plugin credentials in secret-ref mode",
406+
async (resolved) => {
407+
const runtime = createRuntime();
408+
const nextConfig = { agents: { defaults: {} } } as OpenClawConfig;
409+
applyNonInteractivePluginProviderChoice.mockResolvedValueOnce(nextConfig as never);
410+
411+
await applyNonInteractiveAuthChoice({
412+
nextConfig,
413+
authChoice: "demo-provider-api-key",
414+
opts: { secretInputMode: "ref" } as never,
415+
runtime: runtime as never,
416+
baseConfig: nextConfig,
417+
target,
418+
});
419+
420+
const [pluginParams] = applyNonInteractivePluginProviderChoice.mock.calls.at(
421+
-1,
422+
) as unknown as [
423+
{
424+
toApiKeyCredential: (params: { provider: string; resolved: typeof resolved }) => unknown;
425+
},
426+
];
427+
const credential = pluginParams.toApiKeyCredential({
428+
provider: "demo-provider",
429+
resolved,
430+
});
431+
432+
expect(credential).toBeNull();
433+
expect(runtime.exit).toHaveBeenCalledWith(1);
434+
const errorText = runtime.error.mock.calls.map(([message]) => String(message)).join("\n");
435+
expect(errorText).toContain("--secret-input-mode ref");
436+
expect(errorText).toContain("demo-provider");
437+
expect(errorText).not.toContain(resolved.key);
438+
},
439+
);
440+
441+
it("preserves env-backed plugin credentials and profile metadata in secret-ref mode", async () => {
442+
const runtime = createRuntime();
443+
const nextConfig = { agents: { defaults: {} } } as OpenClawConfig;
444+
applyNonInteractivePluginProviderChoice.mockResolvedValueOnce(nextConfig as never);
445+
446+
await applyNonInteractiveAuthChoice({
447+
nextConfig,
448+
authChoice: "demo-provider-api-key",
449+
opts: { secretInputMode: "ref" } as never,
450+
runtime: runtime as never,
451+
baseConfig: nextConfig,
452+
target,
453+
});
454+
455+
const [pluginParams] = applyNonInteractivePluginProviderChoice.mock.calls.at(-1) as unknown as [
456+
{
457+
toApiKeyCredential: (params: {
458+
provider: string;
459+
resolved: { key: string; source: "env"; envVarName: string };
460+
email: string;
461+
metadata: Record<string, string>;
462+
}) => unknown;
463+
},
464+
];
465+
expect(
466+
pluginParams.toApiKeyCredential({
467+
provider: "demo-provider",
468+
resolved: {
469+
key: "fixture-valid-plugin-env-secret",
470+
source: "env",
471+
envVarName: "DEMO_PROVIDER_API_KEY",
472+
},
473+
474+
metadata: { account: "work" },
475+
}),
476+
).toEqual({
477+
type: "api_key",
478+
provider: "demo-provider",
479+
keyRef: { source: "env", provider: "default", id: "DEMO_PROVIDER_API_KEY" },
480+
481+
metadata: { account: "work" },
482+
});
483+
expect(runtime.error).not.toHaveBeenCalled();
484+
expect(runtime.exit).not.toHaveBeenCalled();
485+
});
486+
237487
it("stores custom provider OpenAI Responses compatibility", async () => {
238488
const runtime = createRuntime();
239489
const nextConfig = { agents: { defaults: {} } } as OpenClawConfig;

0 commit comments

Comments
 (0)