From f71d7a8cf3439126a4a72f70efc0d224eecadda8 Mon Sep 17 00:00:00 2001 From: primevishnu <71140952+primevishnu@users.noreply.github.com> Date: Tue, 17 Nov 2026 08:45:58 +0530 Subject: [PATCH] fix(kiro): validate whoami output before reporting authentication --- .../provider-kiro/src/server/driver.test.ts | 14 +++++++++ packages/provider-kiro/src/server/status.ts | 30 ++++++++++--------- 2 files changed, 30 insertions(+), 14 deletions(-) diff --git a/packages/provider-kiro/src/server/driver.test.ts b/packages/provider-kiro/src/server/driver.test.ts index 0159b903d9dea..2090ef2c42f89 100644 --- a/packages/provider-kiro/src/server/driver.test.ts +++ b/packages/provider-kiro/src/server/driver.test.ts @@ -45,6 +45,20 @@ describe("KiroDriver", () => { assert.deepEqual(kiroAuthFromWhoami({ code: 1, stdout: '{"account":null}\n' }, {}), { status: "unauthenticated", }); + for (const stdout of ['{"account": null}\n', '{\n "account": null\n}\n']) { + assert.deepEqual(kiroAuthFromWhoami({ code: 1, stdout }, {}), { + status: "unauthenticated", + }); + } + for (const stdout of [ + "", + "not-json", + "{}", + '{"email":"dev@example.com"}', + '{"accountType":""}', + ]) { + assert.deepEqual(kiroAuthFromWhoami({ code: 0, stdout }, {}), { status: "unknown" }); + } assert.deepEqual( kiroAuthFromWhoami({ code: 0, stdout: '{"accountType":"ApiKey","email":null}\n' }, {}), { status: "authenticated", type: "api_key", label: "Kiro API key" }, diff --git a/packages/provider-kiro/src/server/status.ts b/packages/provider-kiro/src/server/status.ts index 46f3361147fe3..e953519bf6efb 100644 --- a/packages/provider-kiro/src/server/status.ts +++ b/packages/provider-kiro/src/server/status.ts @@ -127,37 +127,39 @@ const runKiroCli = Effect.fn("KiroProvider.runKiroCli")(function* ( }); const KiroWhoami = Schema.fromJsonString( - Schema.Struct({ - accountType: Schema.optional(Schema.NullOr(Schema.String)), - email: Schema.optional(Schema.NullOr(Schema.String)), - }), + Schema.Union([ + Schema.Struct({ account: Schema.Null }), + Schema.Struct({ + accountType: Schema.String, + email: Schema.optional(Schema.NullOr(Schema.String)), + }), + ]), ); const decodeKiroWhoami = Schema.decodeUnknownOption(KiroWhoami); /** * Reads `kiro-cli whoami --format json`: `{"account":null}` with exit 1 means - * signed out. Only the first line is decoded, since an Identity Center login - * follows it with a plain-text `Profile:` block. + * signed out. Identity Center follows its first JSON line with a `Profile:` block. */ export function kiroAuthFromWhoami( output: { readonly code: number; readonly stdout: string } | undefined, environment: NodeJS.ProcessEnv, ): ServerProviderAuth { if (output === undefined) return { status: "unknown" }; - if (output.code !== 0) { - return output.stdout.includes('"account":null') - ? { status: "unauthenticated" } - : { status: "unknown" }; - } const firstLine = output.stdout.trimStart().split("\n", 1)[0] ?? ""; - const account = Option.getOrUndefined(decodeKiroWhoami(firstLine.trim())); - if (account?.accountType === "ApiKey" || environment[KIRO_API_KEY_ENV]?.trim()) { + const account = + Option.getOrUndefined(decodeKiroWhoami(output.stdout)) ?? + Option.getOrUndefined(decodeKiroWhoami(firstLine.trim())); + if (account === undefined) return { status: "unknown" }; + if ("account" in account) return { status: "unauthenticated" }; + if (output.code !== 0 || !account.accountType.trim()) return { status: "unknown" }; + if (account.accountType === "ApiKey" || environment[KIRO_API_KEY_ENV]?.trim()) { return { status: "authenticated", type: "api_key", label: "Kiro API key" }; } return { status: "authenticated", label: "Kiro account", - ...(account?.email ? { email: account.email } : {}), + ...(account.email ? { email: account.email } : {}), }; }