Files
plugins/packages/kiro/profile-host.test.mjs
stringke 13a6f4c226 kiro 0.1.40: Kiro sign-ins made elsewhere are imported from a JSON file or paste by their refresh token (#1132)
Accounts signed in to Kiro elsewhere (Kiro Account Manager lists and
exports, kiro2api, the Kiro IDE's kiro-auth-token.json with its
<clientIdHash>.json client, kiro-cli's tokens) are imported in magpie's
"Import accounts from a file…" by their refresh token. Each is renewed
where its kind is renewed (Google/GitHub at prod.us-east-1.auth.desktop.kiro.dev,
Builder ID / Identity Center at oidc.<region>.amazonaws.com with its client)
and named by the email Kiro has for it. Other providers' entries in a
sign-in store are skipped, duplicates are renewed once, at most 100 a time.

A region or profile ARN read from a file can't pick a host any more, on
every route (import, the IDE's file, kiro-cli's DB, a saved sign-in):
regionOf takes only Kiro's API regions (us-east-1, eu-central-1) from a
profile, arnOf keeps only arn:aws:codewhisperer:<region>:<12 digits>:profile/<id>
(else Kiro is asked for it), and oidc/runtime/management/refreshURL refuse
a region that isn't an AWS region's shape.

Version 0.1.40: #1156 published 0.1.39 first, so main was merged into the
branch and the version raised (f8af341f). With arnOf, #1156's test fixture
ARN (account "1") was dropped and its reasoning-replay test went red on
the merge; every test fixture ARN now has a 12-digit account (0b899428).

What was run, on mote with HOME=$(mktemp -d) as CI:
- PR head 41938ff0, packages/kiro: bun test, 202 pass, 0 fail.
- The earlier review's repro (imported Google sign-in, fresh access
  token, profileArn arn:aws:codewhisperer:attacker.example/#:1:profile/X):
  at 41938ff0 every request goes to *.kiro.dev, nothing saved with the
  crafted ARN. With 5dd9e682's index.mjs put back it and all five
  profile-host.test.mjs tests are red (hosts [management.attacker.example]).
- Merged head f8af341f (main at baeb95a4): root bun test, 8771 pass,
  1 fail (reasoning-replay's fixture, above).
- Reviewed head 0b899428: root bun test, 8772 pass, 0 fail, 613 files.
  Main since gained only 2523b43a (packages/agy-cli/test/smoke.mjs).

Read for credential safety: no logging; errors carry no token; refresh
tokens and client secrets go only to Kiro's auth service and AWS OIDC;
an imported sign-in has no dbKey/idePath, so renewing it writes back to
no kiro-cli or IDE file. No .github, dependency or new host.

Not tried: a real Kiro account imported from a real export.
2026-11-15 13:58:11 +08:00

97 lines
4.9 KiB
JavaScript

// A profile ARN's region and a sign-in's region go into the host every
// token and request is sent to. One read from a file (an imported list,
// the Kiro IDE's kiro-auth-token.json, kiro-cli's database, a saved
// sign-in) is taken only in the shape Kiro gives it: an ARN that isn't one
// is dropped and Kiro is asked for the profile, a profile's region outside
// Kiro's own falls back, and a region that isn't one is refused before any
// request (plugins#1132 review).
import { test, expect, afterAll, beforeAll, afterEach } from "bun:test"
import { homedir, tmpdir } from "node:os"
import { mkdirSync, realpathSync, rmSync, writeFileSync } from "node:fs"
import { join } from "node:path"
let plugin, internal
beforeAll(async () => {
// Bun reads HOME once, at start: run as HOME=$(mktemp -d) bun test, so
// no real sign-in is ever read or written
if (![tmpdir(), realpathSync(tmpdir())].some((t) => homedir().startsWith(t))) throw new Error("run with HOME=$(mktemp -d) bun test")
;({ KiroAuthPlugin: plugin, _internal: internal } = await import("./index.mjs"))
})
const offline = async () => { throw new Error("no network in tests") }
const real = globalThis.fetch
globalThis.fetch = offline
const ideDir = () => join(homedir(), ".aws", "sso", "cache")
afterAll(() => {
globalThis.fetch = real
rmSync(ideDir(), { recursive: true, force: true })
})
afterEach(() => {
globalThis.fetch = offline
rmSync(ideDir(), { recursive: true, force: true })
})
const CRAFTED = "arn:aws:codewhisperer:attacker.example/#:1:profile/X"
const PROFILE = "arn:aws:codewhisperer:us-east-1:111111111111:profile/TEST"
const later = () => new Date(Date.now() + 3600e3).toISOString()
const kiroHost = (h) => h.endsWith(".kiro.dev") || h.endsWith(".amazonaws.com")
// every request's host noted; Kiro answers each as if it were the one asked
function kiro() {
const hosts = []
globalThis.fetch = async (url) => {
hosts.push(new URL(String(url)).host)
return Response.json({ profiles: [{ arn: PROFILE }], userInfo: { email: "x@y.z" }, subscriptionInfo: { subscriptionTitle: "KIRO FREE" }, models: [] })
}
return hosts
}
const hooks = () => plugin({ client: { auth: { set: async () => {} } } })
test("an imported profileArn doesn't pick the host: it is dropped and Kiro asked for the profile", async () => {
const hosts = kiro()
const h = await hooks()
const [got] = await h.auth.import([JSON.stringify([{ email: "u@gmail.com", provider: "Google", refreshToken: "r", accessToken: "a",
expiresAt: later(), profileArn: CRAFTED }])])
expect(hosts.length).toBeGreaterThan(0)
expect(hosts.every(kiroHost)).toBe(true)
expect(got).toMatchObject({ type: "success", profileArn: PROFILE })
})
test("the Kiro IDE's file with a malformed profileArn sends nothing outside Kiro", async () => {
mkdirSync(ideDir(), { recursive: true })
writeFileSync(join(ideDir(), "kiro-auth-token.json"), JSON.stringify({ accessToken: "t", refreshToken: "r", expiresAt: later(),
authMethod: "social", provider: "Google", profileArn: CRAFTED }))
expect((await internal.readIDE()).profile).toBe("")
const hosts = kiro()
const h = await hooks()
await h.provider.models({ models: { auto: {} } }, { auth: { type: "oauth", source: "kiro-ide", access: "", refresh: "", expires: 0 } })
expect(hosts.length).toBeGreaterThan(0)
expect(hosts.every(kiroHost)).toBe(true)
})
test("a saved sign-in's malformed profileArn sends a request nowhere outside Kiro", async () => {
const hosts = kiro()
const h = await hooks()
const stored = { type: "oauth", access: "a", refresh: "r", expires: Date.now() + 3600e3, method: "social", region: "us-east-1", profileArn: CRAFTED, accountId: "u@gmail.com (Google)" }
const l = await h.auth.loader(async () => stored)
await l.fetch("https://kiro.invalid/v1/messages", { method: "POST", body: JSON.stringify({ model: "auto", messages: [{ role: "user", content: "hi" }] }) })
expect(hosts.length).toBeGreaterThan(0)
expect(hosts.every(kiroHost)).toBe(true)
})
test("a region that isn't one is refused before the refresh is sent", async () => {
mkdirSync(ideDir(), { recursive: true })
writeFileSync(join(ideDir(), "kiro-auth-token.json"), JSON.stringify({ accessToken: "t", refreshToken: "r", expiresAt: "2020-01-01T00:00:00Z",
authMethod: "social", provider: "Google", region: "attacker.example/#" }))
const hosts = kiro()
const e = await internal.refresh(await internal.readIDE()).catch((e) => e)
expect(e.message).toContain("isn't an AWS region")
expect(hosts).toEqual([])
})
test("only Kiro's own regions are taken from a profile", () => {
expect(internal.regionOf(CRAFTED, "us-east-1")).toBe("us-east-1")
expect(internal.regionOf("arn:aws:codewhisperer:us-west-2:111111111111:profile/X", "eu-west-1")).toBe("eu-central-1")
expect(internal.regionOf("arn:aws:codewhisperer:eu-central-1:111111111111:profile/X", "us-east-1")).toBe("eu-central-1")
expect(internal.regionOf(PROFILE, "eu-central-1")).toBe("us-east-1")
})