Skip to content

Commit 254c4c2

Browse files
author
Codex Fix Runner
committed
fix: normalize source provider lookup before redaction
1 parent dd10217 commit 254c4c2

2 files changed

Lines changed: 80 additions & 3 deletions

File tree

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

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -439,6 +439,65 @@ describe("models-config", () => {
439439
expect(plan.contents).not.toContain("sk-existing-plaintext");
440440
});
441441

442+
it("replaces existing plaintext api keys when source provider keys need trimming", async () => {
443+
const plan = await planOpenClawModelsJsonWithDeps(
444+
{
445+
cfg: {
446+
models: {
447+
mode: "merge",
448+
providers: {
449+
" custom ": {
450+
baseUrl: "https://custom.example/v1",
451+
api: "openai-completions",
452+
apiKey: "sk-config-plaintext",
453+
models: [createTestModel()],
454+
},
455+
},
456+
},
457+
},
458+
sourceConfigForSecrets: {
459+
models: {
460+
providers: {
461+
" custom ": {
462+
baseUrl: "https://custom.example/v1",
463+
api: "openai-completions",
464+
apiKey: "sk-config-plaintext",
465+
models: [createTestModel()],
466+
},
467+
},
468+
},
469+
},
470+
agentDir: "/tmp/openclaw-models-config-env-vars-test",
471+
env: {},
472+
existingRaw: "",
473+
existingParsed: {
474+
providers: {
475+
custom: {
476+
baseUrl: "https://old.example/v1",
477+
api: "openai-completions",
478+
apiKey: "sk-existing-plaintext",
479+
models: [createTestModel("old-model", "Old")],
480+
},
481+
},
482+
},
483+
},
484+
{
485+
resolveImplicitProviders: async () => ({}),
486+
},
487+
);
488+
489+
expect(plan.action).toBe("write");
490+
if (plan.action !== "write") {
491+
throw new Error("Expected models.json write plan");
492+
}
493+
const parsed = JSON.parse(plan.contents) as {
494+
providers?: Record<string, { apiKey?: string; baseUrl?: string }>;
495+
};
496+
expect(parsed.providers?.custom?.apiKey).toBe(OPENCLAW_MANAGED_AUTH_MARKER);
497+
expect(plan.contents).not.toContain("sk-config-plaintext");
498+
expect(plan.contents).not.toContain("sk-existing-plaintext");
499+
});
500+
442501
it("uses config env.vars entries for implicit provider discovery without mutating process.env", async () => {
443502
await withTempEnv(["OPENROUTER_API_KEY", TEST_ENV_VAR], async () => {
444503
unsetEnv(["OPENROUTER_API_KEY", TEST_ENV_VAR]);

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

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ function sanitizeProvidersForModelsJson(params: {
117117
}): Record<string, ProviderConfig> {
118118
let mutated = false;
119119
const next: Record<string, ProviderConfig> = {};
120+
const sourceProvidersByKey = normalizeSourceProviderLookup(params.sourceProviders);
120121
for (const [providerKey, provider] of Object.entries(params.providers)) {
121122
const apiKey = typeof provider.apiKey === "string" ? provider.apiKey.trim() : "";
122123
const keepApiKey =
@@ -132,7 +133,7 @@ function sanitizeProvidersForModelsJson(params: {
132133
providerKey,
133134
apiKey,
134135
existingParsed: params.existingParsed,
135-
sourceProviders: params.sourceProviders,
136+
sourceProvidersByKey,
136137
})
137138
) {
138139
next[providerKey] = provider;
@@ -150,9 +151,9 @@ function shouldKeepExistingModelsJsonApiKey(params: {
150151
providerKey: string;
151152
apiKey: string;
152153
existingParsed: unknown;
153-
sourceProviders?: Record<string, ProviderConfig>;
154+
sourceProvidersByKey: Readonly<Record<string, ProviderConfig>>;
154155
}): boolean {
155-
if (params.sourceProviders?.[params.providerKey]?.apiKey !== undefined) {
156+
if (params.sourceProvidersByKey[params.providerKey.trim()]?.apiKey !== undefined) {
156157
return false;
157158
}
158159
const existing = params.existingParsed;
@@ -166,6 +167,23 @@ function shouldKeepExistingModelsJsonApiKey(params: {
166167
return existingProvider.apiKey === params.apiKey;
167168
}
168169

170+
function normalizeSourceProviderLookup(
171+
providers: Record<string, ProviderConfig> | undefined,
172+
): Record<string, ProviderConfig> {
173+
if (!providers) {
174+
return {};
175+
}
176+
const out: Record<string, ProviderConfig> = {};
177+
for (const [key, provider] of Object.entries(providers)) {
178+
const normalizedKey = key.trim();
179+
if (!normalizedKey || !isRecord(provider)) {
180+
continue;
181+
}
182+
out[normalizedKey] = provider;
183+
}
184+
return out;
185+
}
186+
169187
export async function planOpenClawModelsJsonWithDeps(
170188
params: {
171189
cfg: OpenClawConfig;

0 commit comments

Comments
 (0)