@@ -425,6 +425,131 @@ describe("kiro oauth — import-first", () => {
425425 expect ( existsSync ( kiroCliRecoveryPath ( ) ) ) . toBe ( false ) ;
426426 } ) ;
427427
428+ test ( "whoami supplies a valid profileArn when the SQLite import lacks one (#993)" , async ( ) => {
429+ const arn = "arn:aws:codewhisperer:us-east-1:123456789012:profile/ABCD1234" ;
430+ seedKiroCliDb ( { access_token : "aoa-builder" , refresh_token : "rt-builder" } ) ;
431+ const runner = async ( args : string [ ] ) => {
432+ if ( args [ 0 ] === "whoami" ) {
433+ return { exitCode :
0 , stdout :
JSON . stringify ( { email :
"[email protected] " , profileArn :
arn } ) } ; 434+ }
435+ throw new Error ( `unexpected Kiro CLI command: ${ args [ 0 ] } ` ) ;
436+ } ;
437+
438+ const cred = await loginKiro ( { } , { cliRunner : runner } ) ;
439+
440+ expect ( cred ) . toMatchObject ( {
441+ access : "aoa-builder" ,
442+ accountId : arn ,
443+ kiro : { profileArn : arn } ,
444+ } ) ;
445+ } ) ;
446+
447+ test ( "a nested profile.arn shape is accepted (#993)" , async ( ) => {
448+ const arn = "arn:aws:codewhisperer:eu-west-1:123456789012:profile/XY-99" ;
449+ seedKiroCliDb ( { access_token : "aoa-nested" , refresh_token : "rt-nested" } ) ;
450+ const nestedRunner = async ( args : string [ ] ) => {
451+ if ( args [ 0 ] === "whoami" ) return { exitCode : 0 , stdout : JSON . stringify ( { profile : { arn } } ) } ;
452+ throw new Error ( "unexpected" ) ;
453+ } ;
454+ const nested = await loginKiro ( { } , { cliRunner : nestedRunner } ) ;
455+ expect ( nested . accountId ) . toBe ( arn ) ;
456+ } ) ;
457+
458+ test ( "a malformed whoami ARN is ignored without breaking the login (#993)" , async ( ) => {
459+ seedKiroCliDb ( { access_token : "aoa-bad" , refresh_token : "rt-bad" } ) ;
460+ const badRunner = async ( args : string [ ] ) => {
461+ if ( args [ 0 ] === "whoami" ) {
462+ return { exitCode :
0 , stdout :
JSON . stringify ( { profileArn :
"not-an-arn" , email :
"[email protected] " } ) } ; 463+ }
464+ throw new Error ( "unexpected" ) ;
465+ } ;
466+ const bad = await loginKiro ( { } , { cliRunner : badRunner } ) ;
467+ // Malformed ARN: login still succeeds for ungated models, but no ARN is borrowed.
468+ expect ( bad . accountId ) . toBeUndefined ( ) ;
469+ expect ( bad . kiro ?. profileArn ) . toBeUndefined ( ) ;
470+ } ) ;
471+
472+ test ( "an imported SQLite profileArn stays authoritative over whoami (#993)" , async ( ) => {
473+ const sqliteArn = "arn:aws:codewhisperer:us-east-1:123456789012:profile/SQLITE" ;
474+ const whoamiArn = "arn:aws:codewhisperer:us-east-1:123456789012:profile/WHOAMI" ;
475+ seedKiroCliDb ( { access_token : "aoa-both" , refresh_token : "rt-both" , profile_arn : sqliteArn } ) ;
476+ const runner = async ( args : string [ ] ) => {
477+ if ( args [ 0 ] === "whoami" ) return { exitCode : 0 , stdout : JSON . stringify ( { profileArn : whoamiArn } ) } ;
478+ throw new Error ( "unexpected" ) ;
479+ } ;
480+ const cred = await loginKiro ( { } , { cliRunner : runner } ) ;
481+ expect ( cred . accountId ) . toBe ( sqliteArn ) ;
482+ } ) ;
483+
484+ test ( "a session switch between the SQLite read and whoami discards whoami's ARN (#993)" , async ( ) => {
485+ const arnB = "arn:aws:codewhisperer:us-east-1:123456789012:profile/OTHER-ACCOUNT" ;
486+ seedKiroCliDb ( { access_token : "aoa-accountA" , refresh_token : "rt-accountA" } ) ;
487+ const runner = async ( args : string [ ] ) => {
488+ if ( args [ 0 ] === "whoami" ) {
489+ // Another process switches the active CLI session mid-flight.
490+ removeKiroCliDb ( ) ;
491+ seedKiroCliDb ( { access_token : "aoa-accountB" , refresh_token : "rt-accountB" } ) ;
492+ return { exitCode :
0 , stdout :
JSON . stringify ( { email :
"[email protected] " , profileArn :
arnB } ) } ; 493+ }
494+ throw new Error ( "unexpected" ) ;
495+ } ;
496+ const cred = await loginKiro ( { } , { cliRunner : runner } ) ;
497+ // Account A's token must never carry account B's ARN.
498+ expect ( cred . accountId ) . toBeUndefined ( ) ;
499+ expect ( cred . kiro ?. profileArn ) . toBeUndefined ( ) ;
500+ } ) ;
501+
502+ test ( "the discarded whoami identity takes the email with it, not just the ARN (#993)" , async ( ) => {
503+ // Review finding: asserting only that the ARN is absent also passes against
504+ // pre-fix code, which ignored whoami's ARN entirely. The email is the part
505+ // that pre-fix code WOULD have kept, so it is the assertion that actually
506+ // proves the mismatch path clears the whole identity.
507+ const arnB = "arn:aws:codewhisperer:us-east-1:123456789012:profile/OTHER-ACCOUNT" ;
508+ seedKiroCliDb ( { access_token : "aoa-accountA" , refresh_token : "rt-accountA" } ) ;
509+ const runner = async ( args : string [ ] ) => {
510+ if ( args [ 0 ] === "whoami" ) {
511+ removeKiroCliDb ( ) ;
512+ seedKiroCliDb ( { access_token : "aoa-accountB" , refresh_token : "rt-accountB" } ) ;
513+ return { exitCode :
0 , stdout :
JSON . stringify ( { email :
"[email protected] " , profileArn :
arnB } ) } ; 514+ }
515+ throw new Error ( "unexpected" ) ;
516+ } ;
517+ const cred = await loginKiro ( { } , { cliRunner : runner } ) ;
518+ expect ( cred . email ) . toBeUndefined ( ) ;
519+ expect ( cred . kiro ?. profileArn ) . toBeUndefined ( ) ;
520+ } ) ;
521+
522+ test ( "with no refresh token the access token is the revalidation key (#993)" , async ( ) => {
523+ // The implementation falls back to the access token when refresh is absent.
524+ // Without this case that branch is unexercised in either direction.
525+ const arn = "arn:aws:codewhisperer:us-east-1:123456789012:profile/BUILDER" ;
526+ seedKiroCliDb ( { access_token : "aoa-only" } ) ;
527+ const runner = async ( args : string [ ] ) => {
528+ if ( args [ 0 ] === "whoami" ) {
529+ return { exitCode :
0 , stdout :
JSON . stringify ( { email :
"[email protected] " , profileArn :
arn } ) } ; 530+ }
531+ throw new Error ( "unexpected" ) ;
532+ } ;
533+ const cred = await loginKiro ( { } , { cliRunner : runner } ) ;
534+ expect ( cred . kiro ?. profileArn ) . toBe ( arn ) ;
535+ } ) ;
536+
537+ test ( "an access-token-only session that changes under whoami is rejected (#993)" , async ( ) => {
538+ const arnB = "arn:aws:codewhisperer:us-east-1:123456789012:profile/OTHER-ACCOUNT" ;
539+ seedKiroCliDb ( { access_token : "aoa-accountA" } ) ;
540+ const runner = async ( args : string [ ] ) => {
541+ if ( args [ 0 ] === "whoami" ) {
542+ removeKiroCliDb ( ) ;
543+ seedKiroCliDb ( { access_token : "aoa-accountB" } ) ;
544+ return { exitCode :
0 , stdout :
JSON . stringify ( { email :
"[email protected] " , profileArn :
arnB } ) } ; 545+ }
546+ throw new Error ( "unexpected" ) ;
547+ } ;
548+ const cred = await loginKiro ( { } , { cliRunner : runner } ) ;
549+ expect ( cred . kiro ?. profileArn ) . toBeUndefined ( ) ;
550+ expect ( cred . email ) . toBeUndefined ( ) ;
551+ } ) ;
552+
428553 test ( "invalid recovery data names the file the operator must remove" , async ( ) => {
429554 seedKiroCliDb ( { access_token : "aoa-prior" , refresh_token : "rt-prior" } ) ;
430555 writeFileSync ( kiroCliRecoveryPath ( ) , "not a recovery database" , { mode : 0o600 } ) ;
0 commit comments