@@ -3,7 +3,9 @@ import os from "node:os";
33import path from "node:path" ;
44import { describe , expect , it , vi } from "vitest" ;
55import type { OpenClawConfig } from "../config/config.js" ;
6+ import { saveAuthProfileStore } from "./auth-profiles/store.js" ;
67import { NON_ENV_SECRETREF_MARKER } from "./model-auth-markers.js" ;
8+ import { planOpenClawModelsJsonWithDeps } from "./models-config.plan.js" ;
79import { normalizeProviders } from "./models-config.providers.normalize.js" ;
810import { resolveApiKeyFromProfiles } from "./models-config.providers.secret-helpers.js" ;
911import { enforceSourceManagedProviderSecrets } from "./models-config.providers.source-managed.js" ;
@@ -15,7 +17,10 @@ function normalizeLmstudioBaseUrl(baseUrl: string): string {
1517
1618vi . mock ( "./models-config.providers.policy.runtime.js" , ( ) => {
1719 return {
18- applyProviderNativeStreamingUsagePolicy : ( ) => undefined ,
20+ applyProviderNativeStreamingUsagePolicy : (
21+ _providerKey : string ,
22+ provider : { baseUrl ?: unknown } | undefined ,
23+ ) => provider ,
1924 normalizeProviderConfigPolicy : (
2025 providerKey : string ,
2126 provider : { baseUrl ?: unknown } | undefined ,
@@ -248,6 +253,142 @@ describe("normalizeProviders", () => {
248253 }
249254 } ) ;
250255
256+ it ( "does not add plaintext auth-profile keys to generated provider config" , async ( ) => {
257+ const agentDir = await fs . mkdtemp ( path . join ( os . tmpdir ( ) , "openclaw-agent-" ) ) ;
258+ const env = { ...process . env } as NodeJS . ProcessEnv ;
259+ delete env . OPENAI_API_KEY ;
260+ try {
261+ saveAuthProfileStore (
262+ {
263+ version : 1 ,
264+ profiles : {
265+ "openai:default" : {
266+ type : "api_key" ,
267+ provider : "openai" ,
268+ key : "sk-profile-plaintext-secret" , // pragma: allowlist secret
269+ } ,
270+ } ,
271+ } ,
272+ agentDir ,
273+ ) ;
274+ const providers : NonNullable < NonNullable < OpenClawConfig [ "models" ] > [ "providers" ] > = {
275+ openai : {
276+ baseUrl : "https://api.openai.com/v1" ,
277+ api : "openai-completions" ,
278+ models : [ createModel ( ) ] ,
279+ } ,
280+ } ;
281+
282+ const normalized = normalizeProviders ( { providers, agentDir, env } ) ;
283+
284+ expect ( normalized ?. openai ?. apiKey ) . toBeUndefined ( ) ;
285+ } finally {
286+ await fs . rm ( agentDir , { recursive : true , force : true } ) ;
287+ }
288+ } ) ;
289+
290+ it ( "strips provider apiKey values that match plaintext auth-profile keys" , async ( ) => {
291+ const agentDir = await fs . mkdtemp ( path . join ( os . tmpdir ( ) , "openclaw-agent-" ) ) ;
292+ const env = { ...process . env } as NodeJS . ProcessEnv ;
293+ delete env . OPENAI_API_KEY ;
294+ try {
295+ saveAuthProfileStore (
296+ {
297+ version : 1 ,
298+ profiles : {
299+ "openai:default" : {
300+ type : "api_key" ,
301+ provider : "openai" ,
302+ key : "sk-profile-plaintext-secret" , // pragma: allowlist secret
303+ } ,
304+ } ,
305+ } ,
306+ agentDir ,
307+ ) ;
308+ const providers : NonNullable < NonNullable < OpenClawConfig [ "models" ] > [ "providers" ] > = {
309+ openai : {
310+ baseUrl : "https://api.openai.com/v1" ,
311+ apiKey : "sk-profile-plaintext-secret" , // pragma: allowlist secret
312+ api : "openai-completions" ,
313+ models : [ createModel ( ) ] ,
314+ } ,
315+ } ;
316+
317+ const normalized = normalizeProviders ( { providers, agentDir, env } ) ;
318+
319+ expect ( normalized ?. openai ?. apiKey ) . toBeUndefined ( ) ;
320+ } finally {
321+ await fs . rm ( agentDir , { recursive : true , force : true } ) ;
322+ }
323+ } ) ;
324+
325+ it ( "strips profile-backed plaintext keys preserved by models.json merge mode" , async ( ) => {
326+ const agentDir = await fs . mkdtemp ( path . join ( os . tmpdir ( ) , "openclaw-agent-" ) ) ;
327+ const env = { ...process . env } as NodeJS . ProcessEnv ;
328+ delete env . OPENAI_API_KEY ;
329+ try {
330+ saveAuthProfileStore (
331+ {
332+ version : 1 ,
333+ profiles : {
334+ "openai:default" : {
335+ type : "api_key" ,
336+ provider : "openai" ,
337+ key : "sk-profile-plaintext-secret" , // pragma: allowlist secret
338+ } ,
339+ } ,
340+ } ,
341+ agentDir ,
342+ ) ;
343+
344+ const plan = await planOpenClawModelsJsonWithDeps (
345+ {
346+ cfg : { models : { mode : "merge" , providers : { } } } ,
347+ agentDir,
348+ env,
349+ existingRaw : "" ,
350+ existingParsed : {
351+ providers : {
352+ openai : {
353+ baseUrl : "https://api.openai.com/v1" ,
354+ apiKey : "sk-profile-plaintext-secret" , // pragma: allowlist secret
355+ api : "openai-completions" ,
356+ models : [ createModel ( { id : "gpt-proof" } ) ] ,
357+ } ,
358+ custom : {
359+ baseUrl : "https://custom.example/v1" ,
360+ apiKey : "CUSTOM_PROXY_API_KEY" , // pragma: allowlist secret
361+ api : "openai-completions" ,
362+ models : [ createModel ( { id : "custom-model" } ) ] ,
363+ } ,
364+ } ,
365+ } ,
366+ } ,
367+ {
368+ resolveImplicitProviders : async ( ) => ( {
369+ openai : {
370+ baseUrl : "https://api.openai.com/v1" ,
371+ api : "openai-completions" ,
372+ models : [ createModel ( { id : "gpt-proof" } ) ] ,
373+ } ,
374+ } ) ,
375+ } ,
376+ ) ;
377+
378+ expect ( plan . action ) . toBe ( "write" ) ;
379+ if ( plan . action !== "write" ) {
380+ throw new Error ( "Expected models.json write plan" ) ;
381+ }
382+ const parsed = JSON . parse ( plan . contents ) as {
383+ providers ?: Record < string , { apiKey ?: string } > ;
384+ } ;
385+ expect ( parsed . providers ?. openai ?. apiKey ) . toBeUndefined ( ) ;
386+ expect ( parsed . providers ?. custom ?. apiKey ) . toBe ( "CUSTOM_PROXY_API_KEY" ) ;
387+ } finally {
388+ await fs . rm ( agentDir , { recursive : true , force : true } ) ;
389+ }
390+ } ) ;
391+
251392 it ( "normalizes SecretRef-managed provider apiKey values to env markers" , async ( ) => {
252393 const agentDir = await fs . mkdtemp ( path . join ( os . tmpdir ( ) , "openclaw-agent-" ) ) ;
253394 const secretRefManagedProviders = new Set < string > ( ) ;
@@ -396,4 +537,71 @@ describe("normalizeProviders", () => {
396537 await fs . rm ( agentDir , { recursive : true , force : true } ) ;
397538 }
398539 } ) ;
540+
541+ it ( "strips profile-backed plaintext keys from merge-preserved providers without models" , async ( ) => {
542+ const agentDir = await fs . mkdtemp ( path . join ( os . tmpdir ( ) , "openclaw-agent-" ) ) ;
543+ const env = { ...process . env } as NodeJS . ProcessEnv ;
544+ delete env . OPENAI_API_KEY ;
545+ try {
546+ saveAuthProfileStore (
547+ {
548+ version : 1 ,
549+ profiles : {
550+ "openai:default" : {
551+ type : "api_key" ,
552+ provider : "openai" ,
553+ key : "sk-profile-plaintext-secret" , // pragma: allowlist secret
554+ } ,
555+ } ,
556+ } ,
557+ agentDir ,
558+ ) ;
559+
560+ const plan = await planOpenClawModelsJsonWithDeps (
561+ {
562+ cfg : {
563+ models : {
564+ mode : "merge" ,
565+ providers : {
566+ custom : {
567+ baseUrl : "https://custom.example/v1" ,
568+ apiKey : "CUSTOM_PROXY_API_KEY" , // pragma: allowlist secret
569+ api : "openai-completions" ,
570+ models : [ createModel ( { id : "custom-model" } ) ] ,
571+ } ,
572+ } ,
573+ } ,
574+ } ,
575+ agentDir,
576+ env,
577+ existingRaw : "" ,
578+ existingParsed : {
579+ providers : {
580+ openai : {
581+ baseUrl : "https://api.openai.com/v1" ,
582+ apiKey : "sk-profile-plaintext-secret" , // pragma: allowlist secret
583+ api : "openai-completions" ,
584+ models : [ ] ,
585+ } ,
586+ } ,
587+ } ,
588+ } ,
589+ {
590+ resolveImplicitProviders : async ( ) => ( { } ) ,
591+ } ,
592+ ) ;
593+
594+ expect ( plan . action ) . toBe ( "write" ) ;
595+ if ( plan . action !== "write" ) {
596+ throw new Error ( "Expected models.json write plan" ) ;
597+ }
598+ const parsed = JSON . parse ( plan . contents ) as {
599+ providers ?: Record < string , { apiKey ?: string } > ;
600+ } ;
601+ expect ( parsed . providers ?. openai ?. apiKey ) . toBeUndefined ( ) ;
602+ expect ( parsed . providers ?. custom ?. apiKey ) . toBe ( "CUSTOM_PROXY_API_KEY" ) ;
603+ } finally {
604+ await fs . rm ( agentDir , { recursive : true , force : true } ) ;
605+ }
606+ } ) ;
399607} ) ;
0 commit comments