@@ -17,6 +17,7 @@ import { fileURLToPath } from "node:url";
1717import { computePushPlan , pushBlockingFindings } from "../src/push-plan.ts" ;
1818import type { LoadedResources , ResourceType , StateFile } from "../src/types.ts" ;
1919import { VALID_RESOURCE_TYPES } from "../src/types.ts" ;
20+ import { validateCredentialPins } from "../src/validate-credential-pins.ts" ;
2021
2122process . argv = [ "node" , "test" , "preflight" ] ;
2223process . env . VAPI_TOKEN = "fake" ;
@@ -129,18 +130,71 @@ test("plan: squad auto-applied assistant errors block, including existing assist
129130 assert . deepEqual ( pushBlockingFindings ( [ ] , new Set ( ) , false ) , [ ] ) ;
130131} ) ;
131132
133+ test ( "credential-pin severity and scope survive push preflight" , ( ) => {
134+ const local = loaded ( {
135+ assistants : {
136+ bot : { credentialIds : [ "model" ] , voice : { provider : "11labs" } } ,
137+ } ,
138+ squads : { team : { members : [ { assistantId : "bot" } ] } } ,
139+ } ) ;
140+ const tracked = state ( ) ;
141+ tracked . credentials = { model : { uuid : UUID } , voice : { uuid : "voice-id" } } ;
142+ const kinds = {
143+ model : { provider : "openai" } ,
144+ voice : { provider : "11labs" } ,
145+ } ;
146+ for ( const customerKeysOnly of [ false , true ] ) {
147+ for ( const recorded of [ kinds , undefined ] ) {
148+ const findings = validateCredentialPins ( {
149+ loaded : local ,
150+ org : "preflight" ,
151+ state : tracked ,
152+ kinds : recorded ,
153+ kindsFile : "state/preflight/credential-providers.json" ,
154+ customerKeysOnly,
155+ } ) ;
156+ assert . equal ( findings . length , recorded ? 2 : 1 ) ;
157+ assert . ok (
158+ findings . every (
159+ ( finding ) =>
160+ finding . rule ===
161+ ( recorded
162+ ? "credential-pin-hides-key"
163+ : "credential-pin-unchecked" ) ,
164+ ) ,
165+ ) ;
166+ const plan = computePushPlan (
167+ { ...loaded ( ) , squads : local . squads } ,
168+ local ,
169+ tracked ,
170+ ) ;
171+ assert . equal (
172+ pushBlockingFindings ( findings , plan , false ) . length ,
173+ customerKeysOnly ? findings . length : 0 ,
174+ ) ;
175+ assert . deepEqual ( pushBlockingFindings ( findings , new Set ( ) , false ) , [ ] ) ;
176+ assert . equal (
177+ pushBlockingFindings ( findings , new Set ( ) , true ) . length ,
178+ customerKeysOnly ? findings . length : 0 ,
179+ ) ;
180+ }
181+ }
182+ assert . deepEqual ( pushBlockingFindings ( [ ] , new Set ( ) , true ) , [ ] ) ;
183+ } ) ;
184+
132185async function runFixture ( options : {
133186 scoped ?: boolean ;
134187 dryRun ?: boolean ;
135188 credential ?: boolean ;
136189 strict ?: boolean ;
190+ credentialPin ?: boolean ;
137191} ) {
138192 const dir = mkdtempSync ( join ( tmpdir ( ) , "push-preflight-" ) ) ;
139193 const mutations : string [ ] = [ ] ;
140194 const good = { id : UUID , name : "Good" } ;
141195 const bad = {
142196 id : "b0000000-0000-4000-8000-000000000002" ,
143- name : options . credential ? "Bad" : BAD ,
197+ name : options . credential || options . credentialPin ? "Bad" : BAD ,
144198 ...( options . credential
145199 ? { server : { url : "https://example.com" , credentialId : "missing" } }
146200 : { } ) ,
@@ -173,11 +227,25 @@ async function runFixture(options: {
173227 writeFileSync ( join ( folder , "good.yml" ) , "name: Good\n" ) ;
174228 writeFileSync (
175229 join ( folder , "zbad.yml" ) ,
176- `name: ${ bad . name } \n${ options . credential ? "server:\n url: https://example.com\n credentialId: missing\n" : "" } ` ,
230+ `name: ${ bad . name } \n${ options . credential ? "server:\n url: https://example.com\n credentialId: missing\n" : "" } ${ options . credentialPin ? "credentialIds:\n - unused\nvoice:\n provider: 11labs\n voiceId: sarah\n" : "" } ` ,
177231 ) ;
178232 const initial = state ( ) ;
179233 initial . assistants . good = { uuid : good . id } ;
180234 initial . assistants . zbad = { uuid : bad . id } ;
235+ if ( options . credentialPin ) {
236+ initial . credentials . voice = { uuid : "voice-id" } ;
237+ writeFileSync (
238+ join ( dir , "resources/preflight/.vapi-policy.yml" ) ,
239+ "customerKeysOnly: true\n" ,
240+ ) ;
241+ writeFileSync (
242+ join ( dir , "resources/preflight/.vapi-credential-providers.json" ) ,
243+ JSON . stringify ( {
244+ unused : { provider : "openai" } ,
245+ voice : { provider : "11labs" } ,
246+ } ) ,
247+ ) ;
248+ }
181249 writeFileSync (
182250 join ( dir , ".vapi-state.preflight.json" ) ,
183251 JSON . stringify ( initial ) ,
@@ -225,6 +293,12 @@ for (const [label, options] of [
225293 [ "dry-run" , { dryRun : true } ] ,
226294 [ "unresolved credential" , { credential : true } ] ,
227295 [ "strict scoped push" , { scoped : true , strict : true } ] ,
296+ [ "credential pin" , { credentialPin : true } ] ,
297+ [ "credential pin dry-run" , { credentialPin : true , dryRun : true } ] ,
298+ [
299+ "credential pin strict scoped push" ,
300+ { credentialPin : true , scoped : true , strict : true } ,
301+ ] ,
228302] as const ) {
229303 test ( `preflight blocks ${ label } before any mutation` , async ( ) => {
230304 const result = await runFixture ( options ) ;
@@ -233,6 +307,8 @@ for (const [label, options] of [
233307 assert . deepEqual ( result . mutations , [ ] ) ;
234308 if ( "credential" in options )
235309 assert . match ( result . output , / u n r e s o l v e d - c r e d e n t i a l / ) ;
310+ if ( "credentialPin" in options )
311+ assert . match ( result . output , / c r e d e n t i a l - p i n - h i d e s - k e y / ) ;
236312 } ) ;
237313}
238314
0 commit comments