@@ -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} ) ;
0 commit comments