Skip to content

Commit 53783c0

Browse files
committed
fix: strip plaintext model api keys from models json
1 parent 94ac563 commit 53783c0

2 files changed

Lines changed: 181 additions & 1 deletion

File tree

‎src/agents/models-config.applies-config-env-vars.test.ts‎

Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,4 +276,156 @@ describe("models-config", () => {
276276
expect(process.env[TEST_ENV_VAR]).toBe("from-host");
277277
});
278278
});
279+
280+
it("omits plaintext configured provider apiKeys from generated models.json", async () => {
281+
const plan = await planOpenClawModelsJsonWithDeps(
282+
{
283+
cfg: {
284+
models: {
285+
providers: {
286+
custom: {
287+
baseUrl: "https://custom.example/v1",
288+
api: "openai-responses",
289+
apiKey: "sk-config-plaintext-secret", // pragma: allowlist secret
290+
models: [
291+
{
292+
id: "custom-model",
293+
name: "Custom Model",
294+
input: ["text"],
295+
reasoning: false,
296+
cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 },
297+
contextWindow: 8192,
298+
maxTokens: 2048,
299+
},
300+
],
301+
},
302+
},
303+
},
304+
},
305+
agentDir: "/tmp/openclaw-models-config-env-vars-test",
306+
env: {},
307+
existingRaw: "",
308+
existingParsed: null,
309+
},
310+
{
311+
resolveImplicitProviders: async () => ({}),
312+
},
313+
);
314+
315+
expect(plan.action).toBe("write");
316+
if (plan.action !== "write") {
317+
throw new Error("Expected models.json write plan");
318+
}
319+
const parsed = JSON.parse(plan.contents) as {
320+
providers?: Record<string, { apiKey?: string; models?: Array<{ id?: string }> }>;
321+
};
322+
expect(parsed.providers?.custom?.apiKey).toBeUndefined();
323+
expect(parsed.providers?.custom?.models?.map((model) => model.id)).toEqual(["custom-model"]);
324+
expect(plan.contents).not.toContain("sk-config-plaintext-secret");
325+
});
326+
327+
it("omits plaintext apiKeys merged from existing models.json entries", async () => {
328+
const plan = await planOpenClawModelsJsonWithDeps(
329+
{
330+
cfg: {
331+
models: {
332+
mode: "merge",
333+
providers: {
334+
custom: {
335+
baseUrl: "https://custom.example/v1",
336+
api: "openai-responses",
337+
models: [
338+
{
339+
id: "custom-model",
340+
name: "Custom Model",
341+
input: ["text"],
342+
reasoning: false,
343+
cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 },
344+
contextWindow: 8192,
345+
maxTokens: 2048,
346+
},
347+
],
348+
},
349+
},
350+
},
351+
},
352+
agentDir: "/tmp/openclaw-models-config-env-vars-test",
353+
env: {},
354+
existingRaw: "",
355+
existingParsed: {
356+
providers: {
357+
custom: {
358+
baseUrl: "https://custom.example/v1",
359+
api: "openai-responses",
360+
apiKey: "sk-existing-plaintext-secret", // pragma: allowlist secret
361+
models: [],
362+
},
363+
},
364+
},
365+
},
366+
{
367+
resolveImplicitProviders: async () => ({}),
368+
},
369+
);
370+
371+
expect(plan.action).toBe("write");
372+
if (plan.action !== "write") {
373+
throw new Error("Expected models.json write plan");
374+
}
375+
const parsed = JSON.parse(plan.contents) as {
376+
providers?: Record<string, { apiKey?: string; baseUrl?: string }>;
377+
};
378+
expect(parsed.providers?.custom?.apiKey).toBeUndefined();
379+
expect(parsed.providers?.custom?.baseUrl).toBe("https://custom.example/v1");
380+
expect(plan.contents).not.toContain("sk-existing-plaintext-secret");
381+
});
382+
383+
it("preserves SecretRef-managed provider apiKey markers in generated models.json", async () => {
384+
const plan = await planOpenClawModelsJsonWithDeps(
385+
{
386+
cfg: {
387+
models: {
388+
providers: {
389+
custom: {
390+
baseUrl: "https://custom.example/v1",
391+
api: "openai-responses",
392+
apiKey: {
393+
source: "env",
394+
provider: "default",
395+
id: "CUSTOM_PROVIDER_API_KEY",
396+
},
397+
models: [
398+
{
399+
id: "custom-model",
400+
name: "Custom Model",
401+
input: ["text"],
402+
reasoning: false,
403+
cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 },
404+
contextWindow: 8192,
405+
maxTokens: 2048,
406+
},
407+
],
408+
},
409+
},
410+
},
411+
},
412+
agentDir: "/tmp/openclaw-models-config-env-vars-test",
413+
env: {},
414+
existingRaw: "",
415+
existingParsed: null,
416+
},
417+
{
418+
resolveImplicitProviders: async () => ({}),
419+
},
420+
);
421+
422+
expect(plan.action).toBe("write");
423+
if (plan.action !== "write") {
424+
throw new Error("Expected models.json write plan");
425+
}
426+
const parsed = JSON.parse(plan.contents) as {
427+
providers?: Record<string, { apiKey?: string }>;
428+
};
429+
expect(parsed.providers?.custom?.apiKey).toBe("CUSTOM_PROVIDER_API_KEY"); // pragma: allowlist secret
430+
});
279431
});

‎src/agents/models-config.plan.ts‎

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import type { OpenClawConfig } from "../config/types.openclaw.js";
22
import type { PluginMetadataSnapshot } from "../plugins/plugin-metadata-snapshot.js";
33
import { isRecord } from "../utils.js";
4+
import { isNonSecretApiKeyMarker } from "./model-auth-markers.js";
45
import {
56
mergeProviders,
67
mergeWithExistingProviderSecrets,
@@ -105,6 +106,30 @@ function resolveProvidersForMode(params: {
105106
});
106107
}
107108

109+
function stripPlaintextProviderApiKeysForModelsJson(params: {
110+
providers: Record<string, ProviderConfig>;
111+
secretRefManagedProviders: ReadonlySet<string>;
112+
}): Record<string, ProviderConfig> {
113+
let nextProviders: Record<string, ProviderConfig> | null = null;
114+
for (const [providerKey, provider] of Object.entries(params.providers)) {
115+
if (!isRecord(provider) || provider.apiKey === undefined) {
116+
continue;
117+
}
118+
if (
119+
typeof provider.apiKey === "string" &&
120+
(params.secretRefManagedProviders.has(providerKey.trim()) ||
121+
isNonSecretApiKeyMarker(provider.apiKey))
122+
) {
123+
continue;
124+
}
125+
126+
const { apiKey: _apiKey, ...providerWithoutPlaintextApiKey } = provider;
127+
nextProviders ??= { ...params.providers };
128+
nextProviders[providerKey] = providerWithoutPlaintextApiKey;
129+
}
130+
return nextProviders ?? params.providers;
131+
}
132+
108133
export async function planOpenClawModelsJsonWithDeps(
109134
params: {
110135
cfg: OpenClawConfig;
@@ -177,7 +202,10 @@ export async function planOpenClawModelsJsonWithDeps(
177202
sourceSecretDefaults: params.sourceConfigForSecrets?.secrets?.defaults,
178203
secretRefManagedProviders,
179204
}) ?? normalizedMergedProviders;
180-
const finalProviders = applyNativeStreamingUsageCompat(secretEnforcedProviders);
205+
const finalProviders = stripPlaintextProviderApiKeysForModelsJson({
206+
providers: applyNativeStreamingUsageCompat(secretEnforcedProviders),
207+
secretRefManagedProviders,
208+
});
181209
const nextContents = `${JSON.stringify({ providers: finalProviders }, null, 2)}\n`;
182210

183211
if (params.existingRaw === nextContents) {

0 commit comments

Comments
 (0)