diff --git a/apps/server/src/http.test.ts b/apps/server/src/http.test.ts index 2db7db420e3be..a3b69e5041601 100644 --- a/apps/server/src/http.test.ts +++ b/apps/server/src/http.test.ts @@ -939,6 +939,7 @@ describe("Kin chat file URLs", () => { const layerRouteServices = Layer.mergeAll( Layer.succeed(KinServer)({ taskCardUpdater: Layer.empty, + t3GitHubCredential: Layer.empty, services: Option.some(kin as unknown as Context.Context), subscribeForViewer: Option.none(), }), diff --git a/apps/server/src/kin/KinServer.ts b/apps/server/src/kin/KinServer.ts index 64be09a316b3f..396f915e6c16c 100644 --- a/apps/server/src/kin/KinServer.ts +++ b/apps/server/src/kin/KinServer.ts @@ -1,4 +1,6 @@ +import type * as SourceControlHost from "@t3tools/source-control-core/server/SourceControlHost"; import type * as Layer from "effect/Layer"; +import type { ProjectStoreV2 } from "../orchestration-v2/ProjectStore.ts"; import type { OrchestratorV2 } from "../orchestration-v2/Orchestrator.ts"; import * as Context from "effect/Context"; import * as Option from "effect/Option"; @@ -65,6 +67,15 @@ export class KinServer extends Context.Service< KinServer, { readonly taskCardUpdater: Layer.Layer; + /** + * Installs T3's own credential reader into the executor's slot. The slot stays out of + * `services`, so nothing holding them can install a reader that hides T3's tokens first. + */ + readonly t3GitHubCredential: Layer.Layer< + never, + never, + SourceControlHost.SourceControlHost | ProjectStoreV2 + >; readonly services: Option.Option>; readonly subscribeForViewer: Option.Option; } diff --git a/apps/server/src/kin/approvals/ActionExecutor.test.ts b/apps/server/src/kin/approvals/ActionExecutor.test.ts index edc82021df3da..927c2d8e1d932 100644 --- a/apps/server/src/kin/approvals/ActionExecutor.test.ts +++ b/apps/server/src/kin/approvals/ActionExecutor.test.ts @@ -13,6 +13,7 @@ import { } from "@t3tools/contracts"; import { encodeGrantSigningInput } from "@t3tools/shared/kinGrant"; import * as Clock from "effect/Clock"; +import * as HostProcess from "@t3tools/shared/HostProcess"; import * as Context from "effect/Context"; import * as ConfigProvider from "effect/ConfigProvider"; import * as Deferred from "effect/Deferred"; @@ -21,6 +22,7 @@ import * as Fiber from "effect/Fiber"; import * as Layer from "effect/Layer"; import * as Logger from "effect/Logger"; import * as Option from "effect/Option"; +import * as Queue from "effect/Queue"; import * as Redacted from "effect/Redacted"; import * as Schema from "effect/Schema"; import * as SqlClient from "effect/sql/SqlClient"; @@ -32,6 +34,7 @@ import * as KinBotReads from "../github/KinBotReads.ts"; import * as KinGitHubApp from "../github/KinGitHubApp.ts"; import * as OwnerMergeClient from "../github/OwnerMergeClient.ts"; import { + BOT_ID, fakeGitHub, memoryKeychain, scopeLayer, @@ -246,6 +249,15 @@ function harness( readonly outcome: string; readonly note?: string; }> = []; + // What the owner is told about the credential check; `posted` lets a test await each one. + const credentialNotices: { + readonly sent: Array<{ + readonly orgId: string; + readonly failures: ReadonlyArray; + readonly recoveredFrom?: number; + }>; + posted?: (failures: ReadonlyArray) => Effect.Effect; + } = { sent: [] }; const noticed = Deferred.makeUnsafe(); // Each credential check runs `during` with its count; `failing` makes the identity unreadable. const checks: { @@ -306,25 +318,28 @@ function harness( Layer.provide(Layer.mergeAll(app, github.layer, keychain.layer, scope)), ); const identity = Layer.succeed(CredentialHygiene.T3GitHubIdentity, { - identities: Effect.suspend(() => { - checks.count += 1; - return checks.during?.(checks.count) ?? Effect.void; - }).pipe( - Effect.andThen(() => - checks.failing - ? Effect.fail(new CredentialHygiene.T3GitHubIdentityError({ cause: "probe failed" })) - : Effect.succeed({ - t3: Option.some({ login: "kin-bot", source: "app" }), - ghAccounts: ["kin-bot"], - gitCredential: Option.none(), - ssh: ["git@github.com: Permission denied (publickey)."], - }), + identities: () => + Effect.suspend(() => { + checks.count += 1; + return checks.during?.(checks.count) ?? Effect.void; + }).pipe( + Effect.andThen(() => + checks.failing + ? Effect.fail(new CredentialHygiene.T3GitHubIdentityError({ cause: "probe failed" })) + : Effect.succeed({ + t3: [{ login: "kin-bot", id: BOT_ID, source: "app" }], + ghAccounts: ["kin-bot"], + gitCredential: Option.none(), + gitCredentialFor: [], + ssh: ["git@github.com: Permission denied (publickey)."], + providerEnvironments: [], + }), + ), ), - ), }); const hygiene = CredentialHygiene.layer.pipe( Layer.provide(identity), - Layer.provide(ConfigProvider.layer(ConfigProvider.fromEnv({ env: {} }))), + Layer.provide(Layer.succeed(HostProcess.Environment, {})), Layer.provide(scope), ); const handlers = Layer.effect( @@ -341,6 +356,14 @@ function harness( const notifications = Layer.succeed( KinExecutionNotices, KinExecutionNotices.of({ + credentialCheck: ({ orgId: org, report, recoveredFrom }) => + Effect.sync(() => + credentialNotices.sent.push({ + orgId: org, + failures: report.failures, + ...(recoveredFrom === undefined ? {} : { recoveredFrom }), + }), + ).pipe(Effect.andThen(credentialNotices.posted?.(report.failures) ?? Effect.void)), finished: ({ action, outcome, note }) => Effect.sync( () => @@ -397,6 +420,7 @@ function harness( world, requests: github.requests, notices, + credentialNotices, noticed, checks, mints, @@ -806,6 +830,8 @@ describe("KinActionExecutor", () => { // Check 1 runs before the claim, check 2 right after it; each case changes state there. const afterClaim = (source: string, change: Effect.Effect) => Effect.gen(function* () { + // Past the shared probe's lifetime, so the pre-claim check probes again as check 1. + yield* TestClock.adjust("1 minute"); checks.count = 0; checks.during = (count) => (count === 2 ? change : Effect.void); const ref = yield* approvedMerge(source); @@ -981,10 +1007,14 @@ describe("KinActionExecutor", () => { ); assert.deepStrictEqual( failed.map((entry) => entry.result), - ["identity_unknown"], + // The probe's fixed reason rides along, so the owner can see which source failed. + ["identity_unknown: probe failed"], ); - // Fixed before the grant lapses: it runs. + // Both holds within the minute shared one probe. + assert.strictEqual(checks.count, 1); + // Fixed before the grant lapses: the next tick's check sees it, and it runs. checks.failing = false; + yield* TestClock.adjust("1 minute"); assert.deepInclude(yield* executor.execute(ref), { outcome: "succeeded" }); assert.deepStrictEqual(writes(requests), [`PUT ${PULL}/merge`]); }).pipe(Effect.provide(layer)); @@ -1372,6 +1402,72 @@ describe("KinActionExecutor", () => { }).pipe(Effect.provide(deps)); }); + it.effect( + "the worker checks credentials at startup and hourly, telling the owner of changes", + () => { + const { checks, credentialNotices, deps } = harness(); + return Effect.gen(function* () { + yield* seed; + const posted = yield* Queue.unbounded>(); + credentialNotices.posted = (failures) => Queue.offer(posted, failures); + checks.failing = true; + yield* Effect.gen(function* () { + yield* ActionExecutor.KinActionExecutor; + assert.deepStrictEqual(yield* Queue.take(posted), ["identity_unknown"]); + // An unchanged hour stays quiet; the next one that passes says so. + yield* TestClock.adjust("1 hour"); + assert.strictEqual(checks.count, 2); + checks.failing = false; + yield* TestClock.adjust("1 hour"); + assert.deepStrictEqual(yield* Queue.take(posted), []); + }).pipe(Effect.provide(ActionExecutor.layer), Effect.scoped); + assert.strictEqual(checks.count, 3); + const failed = (yield* auditEntries).filter( + (entry) => entry.event === "credential_check_failed", + ); + assert.deepStrictEqual( + failed.map((entry) => [entry.actionId, entry.result]), + [[undefined, "identity_unknown: probe failed"]], + ); + assert.deepStrictEqual(credentialNotices.sent, [ + { orgId, failures: ["identity_unknown"] }, + { orgId, failures: [], recoveredFrom: failed[0]!.seq as number }, + ]); + }).pipe(Effect.provide(deps)); + }, + ); + + it.effect("tells only orgs with a merge token, and only of changes across restarts", () => { + const { checks, credentialNotices, deps } = harness(); + return Effect.gen(function* () { + const { sql } = yield* seed; + // An org that never saved a merge token hasn't set GitHub up: no notice, no audit. + yield* sql`INSERT INTO kin_orgs (org_id, name, created_at) + VALUES ('org_02', 'Other', '2026-11-07T00:00:00.000Z')`; + checks.failing = true; + yield* (yield* ActionExecutor.make).checkCredentials; + assert.deepStrictEqual(credentialNotices.sent, [{ orgId, failures: ["identity_unknown"] }]); + const others = yield* sql`SELECT seq FROM kin_audit WHERE org_id = 'org_02'`; + assert.lengthOf(others, 0); + // Restarted while still failing: the owner already heard, so no second notice or entry. + for (const _ of [1, 2]) yield* (yield* ActionExecutor.make).checkCredentials; + assert.lengthOf(credentialNotices.sent, 1); + const [failure, ...again] = (yield* auditEntries).filter( + (entry) => entry.event === "credential_check_failed", + ); + assert.lengthOf(again, 0); + + // Restarted after the fix: each new executor says it passes again, under one notice id. + checks.failing = false; + yield* TestClock.adjust("1 minute"); + for (const _ of [1, 2]) yield* (yield* ActionExecutor.make).checkCredentials; + assert.deepStrictEqual(credentialNotices.sent.slice(1), [ + { orgId, failures: [], recoveredFrom: failure!.seq as number }, + { orgId, failures: [], recoveredFrom: failure!.seq as number }, + ]); + }).pipe(Effect.provide(deps)); + }); + it.effect("the worker executes an approval as it lands", () => { const { requests, deps } = harness(); return Effect.gen(function* () { @@ -1416,6 +1512,8 @@ describe("KinActionExecutor", () => { yield* Effect.gen(function* () { yield* ActionExecutor.KinActionExecutor; yield* TestClock.adjust("1 second"); + // Past the startup check's shared probe, so the pre-claim check probes again as check 1. + yield* TestClock.adjust("1 minute"); // The first run's post-claim credential check never returns, so it holds the org's lock. checks.count = 0; checks.during = (count) => (count === 2 ? Effect.never : Effect.void); @@ -1544,11 +1642,14 @@ describe("KinActionExecutor", () => { KinExecutionNotices, KinExecutionNotices.of({ finished: () => Effect.void, + credentialCheck: () => Effect.void, lapsed: ({ holdReason }) => Effect.sync(() => void lapses.push(holdReason)), }), ), Layer.succeed(KinKeyNotices, KinKeyNotices.of({ changed: () => Effect.void })), ConfigProvider.layer(ConfigProvider.fromEnv({ env: {} })), + // The hygiene check reads the server's environment; this one sets nothing. + Layer.succeed(HostProcess.Environment, {}), KeySeal.layerTest(), Sqlite.layerMemory, ), @@ -1623,6 +1724,12 @@ describe("KinActionExecutor", () => { it.effect("a configured host holds GitHub kinds on the missing identity probe", () => { const lapses: Array = []; + const identityUnavailable = Layer.succeed(CredentialHygiene.T3GitHubIdentity, { + identities: () => + Effect.fail( + new CredentialHygiene.T3GitHubIdentityError({ cause: "the identity probe is missing" }), + ), + }); const github = fakeGitHub(() => ({ status: 404, body: { message: "Not Found" } })); const platform = Layer.mergeAll( Layer.succeed(KinGitHubApp.KinGitHubAppConfig, { appId: "1" }), @@ -1640,7 +1747,7 @@ describe("KinActionExecutor", () => { return Effect.gen(function* () { yield* TestClock.setTime(NOW); const context = yield* Layer.build( - production(ApprovalsLayer.makeExecutorLayer(platform), lapses), + production(ApprovalsLayer.makeExecutorLayer(platform, identityUnavailable), lapses), ); yield* Effect.gen(function* () { yield* seed; @@ -1651,11 +1758,13 @@ describe("KinActionExecutor", () => { (yield* auditEntries) .filter((entry) => entry.event === "credential_check_failed") .map((entry) => entry.result), - ["identity_unknown"], + ["identity_unknown: the identity probe is missing"], ); yield* TestClock.adjust("16 minutes"); assert.deepInclude(yield* stateOf(ref), { state: "voided", reason: "grant_expired" }); - assert.deepStrictEqual(lapses, ["credential_check_failed: identity_unknown"]); + assert.deepStrictEqual(lapses, [ + "credential_check_failed: identity_unknown: the identity probe is missing", + ]); }).pipe(Effect.provide(context)); assert.deepStrictEqual(github.requests, []); }).pipe(Effect.scoped); diff --git a/apps/server/src/kin/approvals/ActionExecutor.ts b/apps/server/src/kin/approvals/ActionExecutor.ts index 023cd0f0c5640..556fe86f15a8d 100644 --- a/apps/server/src/kin/approvals/ActionExecutor.ts +++ b/apps/server/src/kin/approvals/ActionExecutor.ts @@ -1,7 +1,7 @@ import { KinApproverPublicJwk, + KinOrgId, type KinGrant, - type KinOrgId, type KinPendingAction, } from "@t3tools/contracts"; import { decodeKinGrant, verifyKinGrant } from "@t3tools/shared/kinGrant"; @@ -19,9 +19,9 @@ import * as Semaphore from "effect/Semaphore"; import * as SqlClient from "effect/sql/SqlClient"; import { - toPersistenceSqlError, type PersistenceDecodeError, type PersistenceSqlError, + toPersistenceSqlError, } from "../../persistence/Errors.ts"; import { isOrgPaused } from "../lead/orgState.ts"; import { RETIRED_ACTION_REASON } from "../lead/Retirement.ts"; @@ -33,6 +33,7 @@ import { } from "./ActionHandlers.ts"; import { KinAudit } from "./Audit.ts"; import { CredentialHygiene } from "./CredentialHygiene.ts"; +import { type CredentialHygieneReport, failureResult } from "./CredentialHygieneReport.ts"; import { KinExecutionNotices, type KinExecutionOutcome } from "./ExecutionNotices.ts"; import { KinOrgRoles } from "./GrantPorts.ts"; import { grantMatchesRecord } from "./grantFacts.ts"; @@ -45,6 +46,8 @@ const TICK_INTERVAL = "1 minute"; const SWEEP_INTERVAL = "1 minute"; /** Reconciliation that can't read GitHub retries until it can; it never re-runs the action. */ const RECONCILE_RETRY = Schedule.spaced("30 seconds"); +/** Spec section 12: the credential check also runs at startup and this often. */ +const CREDENTIAL_CHECK_INTERVAL = "1 hour"; // GitHub's edge answers 502 after ~10 s while a slow merge keeps running; 2 minutes after the // write was sent covers that. const NOT_DONE_AFTER_MS = 2 * 60 * 1000; @@ -78,6 +81,14 @@ export class KinActionExecutor extends Context.Service< readonly execute: (ref: ActionRef) => Effect.Effect; /** One reconciliation attempt for an action left `executing`. */ readonly reconcile: (ref: ActionRef) => Effect.Effect; + /** + * The credential check for every org. A changed result is audited when failing and posted to + * the owner. The worker runs it at startup and hourly. + */ + readonly checkCredentials: Effect.Effect< + ReadonlyArray<{ readonly orgId: KinOrgId; readonly report: CredentialHygieneReport }>, + Errors + >; /** Reconciles until decided, retrying while GitHub can't tell. */ readonly settle: (ref: ActionRef) => Effect.Effect; /** `execute`, then `settle` when the outcome is unknown. What the worker forks. */ @@ -211,6 +222,8 @@ export const make = Effect.gen(function* () { } return { _tag: "held", reason }; }); + // The last result each org's owner was told about, so an unchanged hourly result stays quiet. + const reportedChecks = new Map(); const paused = (orgId: KinOrgId) => isOrgPaused(orgId).pipe(Effect.provideService(SqlClient.SqlClient, sql)); @@ -306,7 +319,7 @@ export const make = Effect.gen(function* () { (error) => new KinExecError({ reason: "credential_check_failed", - detail: error.failures.join(","), + detail: failureResult(error), }), ), ), @@ -397,9 +410,9 @@ export const make = Effect.gen(function* () { if (!owner.value) return yield* voided("approver_not_owner"); // Checked before the claim too, so a misconfigured host holds the grant instead of spending it. if (handler.value.credentialCheck) { - const check = yield* hygiene.requirePassing(action.orgId).pipe(Effect.flip, Effect.option); - if (Option.isSome(check)) - return yield* held(action, "credential_check_failed", check.value.failures.join(",")); + const check = yield* hygiene.check(action.orgId); + if (!check.passed) + return yield* held(action, "credential_check_failed", failureResult(check)); } // 3. The stored grant verifies against its sealed key and binds this action. if (!(yield* grantVerifies(action, grant))) { @@ -443,6 +456,68 @@ export const make = Effect.gen(function* () { ); }); + const checkCredentials = Effect.gen(function* () { + const orgs = yield* sql<{ readonly orgId: string }>` + SELECT org_id AS "orgId" FROM kin_orgs ORDER BY org_id`.pipe( + Effect.mapError(toPersistenceSqlError("KinActionExecutor.checkCredentials")), + ); + // Whether the host was failing when it last ran, and why: an execution only starts on a pass. + const lastCredentialEvent = (orgId: KinOrgId) => + sql<{ + readonly seq: number; + readonly event: string; + readonly result: string | null; + readonly held: number; + }>` + SELECT seq, event, json_extract(entry_json, '$.result') AS result, + json_extract(entry_json, '$.actionId') IS NOT NULL AS held + FROM kin_audit + WHERE org_id = ${orgId} AND event IN ('credential_check_failed', 'execution_started') + ORDER BY seq DESC LIMIT 1`.pipe( + Effect.map(([row]) => (row?.event === "credential_check_failed" ? row : undefined)), + Effect.mapError(toPersistenceSqlError("KinActionExecutor.checkCredentials")), + ); + return yield* Effect.forEach(orgs, (row) => + Effect.gen(function* () { + const orgId = KinOrgId.make(row.orgId); + const report = yield* hygiene.check(orgId); + const result = [...report.failures, ...report.warnings, ...report.reasons].join(","); + let previous = reportedChecks.get(orgId); + // After a restart, a failure this check already reported (not an action's hold) stays quiet. + if (previous === undefined && !report.passed) { + const last = yield* lastCredentialEvent(orgId); + if (last !== undefined && !last.held && last.result === failureResult(report)) { + previous = result; + } + } + reportedChecks.set(orgId, result); + // An org with no merge token hasn't set GitHub up for Kin, so there is no one to tell. + if (previous === result || report.failures.includes("merge_token_missing")) { + return { orgId, report }; + } + if (!report.passed) { + yield* audit + .append({ orgId, event: "credential_check_failed", result: failureResult(report) }) + .pipe(Effect.catch((error) => Effect.logWarning("Kin audit not written", error))); + yield* notices.credentialCheck({ orgId, report }); + } else if (report.warnings.length > 0) { + yield* notices.credentialCheck({ orgId, report }); + } else { + // After a restart, the audit log remembers a failure the owner hasn't heard the end of. + const failedAt = (yield* lastCredentialEvent(orgId))?.seq; + if (previous !== undefined || failedAt !== undefined) { + yield* notices.credentialCheck({ + orgId, + report, + ...(failedAt === undefined ? {} : { recoveredFrom: failedAt }), + }); + } + } + return { orgId, report }; + }), + ); + }).pipe(Effect.withSpan("KinActionExecutor.checkCredentials")); + const settle = (ref: ActionRef) => reconcile(ref).pipe( Effect.repeat({ @@ -480,13 +555,14 @@ export const make = Effect.gen(function* () { Effect.withSpan("KinActionExecutor.sweep"), ); - return KinActionExecutor.of({ execute, reconcile, settle, run, pickup, sweep }); + return KinActionExecutor.of({ execute, reconcile, checkCredentials, settle, run, pickup, sweep }); }); /** * The executor with its worker: every approval as it lands, a minute tick that retries held * approvals and reconciles orphaned `executing` rows (all of them at startup), and the expiry - * sweep on its own schedule. Each execution is forked, so one stuck call holds up only its org. + * sweep on its own schedule, and the credential check at startup and hourly. Each execution is + * forked, so one stuck call holds up only its org. */ export const layer = Layer.effect( KinActionExecutor, @@ -512,6 +588,10 @@ export const layer = Layer.effect( Effect.forever, Effect.forkScoped, ); + yield* logged("credential check failed", executor.checkCredentials).pipe( + Effect.repeat(Schedule.spaced(CREDENTIAL_CHECK_INTERVAL)), + Effect.forkScoped, + ); const tick = executor.pickup.pipe( Effect.flatMap(({ approved, orphaned }) => Effect.all([ diff --git a/apps/server/src/kin/approvals/CredentialHygiene.test.ts b/apps/server/src/kin/approvals/CredentialHygiene.test.ts index 7a3c26bccb0ea..5d94610b6a56d 100644 --- a/apps/server/src/kin/approvals/CredentialHygiene.test.ts +++ b/apps/server/src/kin/approvals/CredentialHygiene.test.ts @@ -1,14 +1,15 @@ import { assert, it } from "@effect/vitest"; import type { KinOrgId } from "@t3tools/contracts"; -import * as ConfigProvider from "effect/ConfigProvider"; +import * as HostProcess from "@t3tools/shared/HostProcess"; import * as Effect from "effect/Effect"; +import * as Fiber from "effect/Fiber"; import * as Layer from "effect/Layer"; import * as Option from "effect/Option"; import * as TestClock from "effect/testing/TestClock"; import { describe } from "vite-plus/test"; import * as OwnerMergeClient from "../github/OwnerMergeClient.ts"; -import { fakeGitHub, memoryKeychain, scopeLayer } from "../github/testFakes.ts"; +import { BOT_ID, fakeGitHub, memoryKeychain, scopeLayer } from "../github/testFakes.ts"; import * as CredentialHygiene from "./CredentialHygiene.ts"; const ORG = "org_kin" as KinOrgId; @@ -20,10 +21,21 @@ const SSH_DENIED = "git@github.com: Permission denied (publickey)."; type Identity = | { - readonly t3?: Option.Option<{ readonly login: string; readonly source: string }>; + /** Without an `id`, the bot's login has the bot's id and any other login another one. */ + readonly t3?: ReadonlyArray<{ + readonly login: string; + readonly source: string; + readonly id?: number; + }>; readonly gh?: ReadonlyArray; readonly git?: Option.Option; + readonly gitId?: number; readonly ssh?: ReadonlyArray; + readonly gitFor?: ReadonlyArray; + readonly providers?: ReadonlyArray<{ + readonly instance: string; + readonly names: ReadonlyArray; + }>; } | "unreachable"; @@ -31,8 +43,14 @@ const setup = (input: { readonly identity: Identity; readonly env?: Record; readonly mergeToken?: { readonly login: string; readonly expiresAt: number } | null; + /** Null leaves `KIN_GITHUB_BOT_ID` unset. */ + readonly botId?: number | null; }) => { - const state = { identity: input.identity }; + const state = { + identity: input.identity, + probes: 0, + ownerLogins: [] as ReadonlyArray, + }; const mergeToken = input.mergeToken === undefined ? { login: "Nishchay", expiresAt: NOW + 30 * DAY } @@ -51,16 +69,34 @@ const setup = (input: { const identity = Layer.succeed( CredentialHygiene.T3GitHubIdentity, CredentialHygiene.T3GitHubIdentity.of({ - identities: Effect.suspend(() => - state.identity === "unreachable" - ? Effect.fail(new CredentialHygiene.T3GitHubIdentityError({ cause: "offline" })) - : Effect.succeed({ - t3: state.identity.t3 ?? Option.none(), - ghAccounts: state.identity.gh ?? [], - gitCredential: state.identity.git ?? Option.none(), - ssh: state.identity.ssh ?? [SSH_DENIED], - }), - ), + identities: ({ ownerLogins }) => + Effect.suspend(() => { + state.probes += 1; + state.ownerLogins = ownerLogins; + // Takes a second, so a check that starts meanwhile shares it. + return Effect.sleep("1 second"); + }).pipe( + Effect.andThen(() => { + const found = state.identity; + return found === "unreachable" + ? Effect.fail(new CredentialHygiene.T3GitHubIdentityError({ cause: "offline" })) + : Effect.succeed({ + t3: (found.t3 ?? []).map(({ login, source, id }) => ({ + login, + source, + id: id ?? idOf(login), + })), + ghAccounts: found.gh ?? [], + gitCredential: Option.map(found.git ?? Option.none(), (login) => ({ + login, + id: found.gitId ?? idOf(login), + })), + gitCredentialFor: found.gitFor ?? [], + ssh: found.ssh ?? [SSH_DENIED], + providerEnvironments: found.providers ?? [], + }); + }), + ), }), ); const layer = CredentialHygiene.layer.pipe( @@ -68,14 +104,16 @@ const setup = (input: { Layer.provide(OwnerMergeClient.layer), Layer.provide(keychain.layer), Layer.provide(fakeGitHub(() => ({ status: 500 })).layer), - Layer.provide(ConfigProvider.layer(ConfigProvider.fromEnv({ env: input.env ?? {} }))), - Layer.provide(scopeLayer()), + Layer.provide(Layer.succeed(HostProcess.Environment, input.env ?? {})), + Layer.provide(scopeLayer(input.botId === undefined ? {} : { botId: input.botId })), ); return { state, layer }; }; +const idOf = (login: string) => (login.toLowerCase() === "kin-bot" ? BOT_ID : 9001); + const bot: Identity = { - t3: Option.some({ login: "kin-bot", source: "gh" }), + t3: [{ login: "kin-bot", source: "gh" }], gh: ["kin-bot"], git: Option.some("Kin-Bot"), ssh: [sshHi("kin-bot"), SSH_DENIED], @@ -84,21 +122,31 @@ const bot: Identity = { const check = Effect.gen(function* () { yield* TestClock.setTime(NOW); const hygiene = yield* CredentialHygiene.CredentialHygiene; - return yield* hygiene.check(ORG); + const running = yield* Effect.forkChild(hygiene.check(ORG)); + yield* TestClock.adjust("1 second"); + return yield* Fiber.join(running); }); +/** Runs `effect` past the probe's one-second wait. */ +const settled = (effect: Effect.Effect) => + Effect.gen(function* () { + const running = yield* Effect.forkChild(effect); + yield* TestClock.adjust("1 second"); + return yield* Fiber.join(running); + }); + describe("CredentialHygiene", () => { it.effect("privileged execution is refused while the credential hygiene check fails", () => { const { state, layer } = setup({ - identity: { t3: Option.some({ login: "nishchay", source: "gh" }) }, + identity: { t3: [{ login: "nishchay", source: "gh" }] }, }); return Effect.gen(function* () { yield* TestClock.setTime(NOW); const hygiene = yield* CredentialHygiene.CredentialHygiene; let merges = 0; - const execute = hygiene - .requirePassing(ORG) - .pipe(Effect.andThen(Effect.sync(() => void merges++))); + const execute = settled(hygiene.requirePassing(ORG)).pipe( + Effect.andThen(Effect.sync(() => void merges++)), + ); const refused = yield* execute.pipe(Effect.flip); assert.deepStrictEqual(refused.failures, ["owner_identity"]); @@ -111,14 +159,27 @@ describe("CredentialHygiene", () => { state.identity = bot; yield* execute; assert.strictEqual(merges, 1); - assert.strictEqual((yield* hygiene.requirePassing(ORG)).orgId, ORG); + assert.strictEqual((yield* settled(hygiene.requirePassing(ORG))).orgId, ORG); }).pipe(Effect.provide(layer)); }); it.effect.each([ [ "T3 signed in as the owner", - { identity: { t3: Option.some({ login: "NISHCHAY", source: "app" }) } }, + { identity: { t3: [{ login: "NISHCHAY", source: "app" }] } }, + ["owner_identity"], + ], + [ + "one of T3's credentials signed in as the owner", + { + identity: { + ...bot, + t3: [ + { login: "kin-bot", source: "settings" }, + { login: "Nishchay", source: "app" }, + ], + }, + }, ["owner_identity"], ], [ @@ -181,21 +242,78 @@ describe("CredentialHygiene", () => { ], [ "T3 using a login that isn't the bot", - { identity: { t3: Option.some({ login: "someone-else", source: "app" }) } }, + { identity: { t3: [{ login: "someone-else", source: "app" }] } }, + ["non_bot_identity"], + ], + // A login can be renamed and registered again, so a token must name the bot's account id. + [ + "a T3 credential with the bot's login on another account", + { identity: { ...bot, t3: [{ login: "kin-bot", source: "gh", id: 1 }] } }, + ["non_bot_identity"], + ], + [ + "the git helper's credential with the bot's login on another account", + { identity: { ...bot, gitId: 1 } }, ["non_bot_identity"], ], + ["KIN_GITHUB_BOT_ID unset", { identity: bot, botId: null }, ["identity_unknown"]], ["GH_TOKEN in the environment", { identity: bot, env: { GH_TOKEN: "ghp_x" } }, ["env_token"]], [ "GITHUB_TOKEN in the environment", { identity: bot, env: { GITHUB_TOKEN: "x" } }, ["env_token"], ], + ...( + [ + "GH_ENTERPRISE_TOKEN", + "GITHUB_ENTERPRISE_TOKEN", + "GIT_ASKPASS", + "SSH_ASKPASS", + "GIT_SSH", + "GIT_SSH_COMMAND", + "GIT_CONFIG_PARAMETERS", + "GIT_CONFIG_COUNT", + // Patterns, not a list: anything git, gh or ssh reads, and any token. + "GITHUB_PERSONAL_ACCESS_TOKEN", + "GIT_TEMPLATE_DIR", + "GIT_EDITOR", + "SSH_CONNECTION", + "OPENAI_API_TOKEN", + "github_token", + ] as const + ).map( + (name) => + [ + `${name} in the environment`, + { identity: bot, env: { [name]: "x" } }, + ["env_token"], + ] as const, + ), [ "T3 using an environment token", - { identity: { t3: Option.some({ login: "kin-bot", source: "env" }) } }, + { identity: { t3: [{ login: "kin-bot", source: "env" }] } }, ["env_token"], ], ["the identity unreachable", { identity: "unreachable" }, ["identity_unknown"]], + // Another config or startup file than the one the probe read. + ...(["HOME", "XDG_CONFIG_HOME", "XDG_DATA_HOME", "BASH_ENV", "ENV", "ZDOTDIR"] as const).map( + (name) => + [ + `a provider instance's environment changing ${name}`, + { identity: { ...bot, providers: [{ instance: "codex", names: [name] }] } }, + ["env_token"], + ] as const, + ), + [ + "a provider instance's environment with a token", + { identity: { ...bot, providers: [{ instance: "codex", names: ["GH_TOKEN"] }] } }, + ["env_token"], + ], + [ + "the git helper holding a password for the owner's login", + { identity: { ...bot, gitFor: ["nishchay"] } }, + ["owner_identity"], + ], ["no merge token", { identity: bot, mergeToken: null }, ["merge_token_missing"]], [ "an expired merge token", @@ -210,6 +328,100 @@ describe("CredentialHygiene", () => { }).pipe(Effect.provide(setup(input).layer)), ); + it.effect("checks share one probe for a minute; requirePassing always probes afresh", () => { + const { state, layer } = setup({ identity: bot }); + return Effect.gen(function* () { + yield* TestClock.setTime(NOW); + const hygiene = yield* CredentialHygiene.CredentialHygiene; + const other = "org_other" as KinOrgId; + const reports = yield* settled( + Effect.all([hygiene.check(ORG), hygiene.check(other), hygiene.check(ORG)], { + concurrency: "unbounded", + }), + ); + assert.deepStrictEqual( + reports.map((report) => report.passed), + [true, false, true], + ); + assert.strictEqual(state.probes, 1); + + state.identity = { ...bot, gh: ["kin-bot", "Nishchay"] }; + assert.isTrue((yield* settled(hygiene.check(ORG))).passed); + assert.strictEqual(state.probes, 1); + // The gate before a side effect never reuses the cached pass. + assert.deepStrictEqual( + (yield* settled(hygiene.requirePassing(ORG)).pipe(Effect.flip)).failures, + ["owner_identity"], + ); + assert.strictEqual(state.probes, 2); + + yield* TestClock.adjust(CredentialHygiene.PROBE_CACHE_TTL); + assert.isFalse((yield* settled(hygiene.check(ORG))).passed); + assert.strictEqual(state.probes, 3); + }).pipe(Effect.provide(layer)); + }); + + it.effect("says why in fixed words, naming sources and variables but never values", () => + Effect.gen(function* () { + const { state, layer } = setup({ + identity: { + ...bot, + gitFor: ["Nishchay"], + providers: [ + // The server's own GH_TOKEN is named once, under the server. + { instance: "codex", names: ["GH_TOKEN", "SSH_AUTH_SOCK"] }, + { instance: "claude", names: ["PATH", "XDG_CONFIG_HOME", "CLAUDE_CONFIG_DIR"] }, + ], + t3: [{ login: "kin-bot", source: "gh", id: 1 }], + }, + env: { GH_TOKEN: "ghp_secret_value", PATH: "/bin" }, + }); + const report = yield* check.pipe(Effect.provide(layer)); + assert.deepStrictEqual(report.failures, ["env_token", "owner_identity", "non_bot_identity"]); + assert.deepStrictEqual(report.reasons, [ + "the server's environment sets GH_TOKEN", + "the git credential helper holds a password for the owner's login", + `provider instance "codex"'s environment sets SSH_AUTH_SOCK`, + `provider instance "claude"'s environment changes PATH`, + `provider instance "claude"'s environment changes XDG_CONFIG_HOME`, + "t3: the bot's login belongs to an account other than KIN_GITHUB_BOT_ID", + ]); + assert.notInclude(JSON.stringify(report), "ghp_secret_value"); + // The helper is asked about the owner's login and the merge token's, in both cases. + assert.deepStrictEqual(state.ownerLogins, ["Nishchay", "nishchay"]); + + const unreachable = setup({ identity: "unreachable" }); + const failed = yield* Effect.gen(function* () { + yield* TestClock.setTime(NOW); + const hygiene = yield* CredentialHygiene.CredentialHygiene; + return yield* settled(hygiene.requirePassing(ORG)).pipe(Effect.flip); + }).pipe(Effect.provide(unreachable.layer)); + // The live layer's `source: reason`, kept rather than dropped. + assert.deepStrictEqual(failed.reasons, ["offline"]); + }), + ); + + it.effect("passes with variables T3 sets itself and ones that pick no credential", () => + Effect.gen(function* () { + const report = yield* check; + assert.deepInclude(report, { passed: true, failures: [], reasons: [], envTokenNames: [] }); + }).pipe( + Effect.provide( + setup({ + identity: bot, + env: { + GIT_TERMINAL_PROMPT: "0", + T3_MCP_BEARER_TOKEN: "t3_x", + T3CODE_DEV_AUTH_TOKEN: "t3_dev", + GH_TOKEN: " ", + PATH: "/usr/bin", + HOME: "/Users/kin", + }, + }).layer, + ), + ), + ); + it.effect("names environment tokens without their values", () => Effect.gen(function* () { const report = yield* check; diff --git a/apps/server/src/kin/approvals/CredentialHygiene.ts b/apps/server/src/kin/approvals/CredentialHygiene.ts index c7a0d48d07ca7..ca2bb7682487c 100644 --- a/apps/server/src/kin/approvals/CredentialHygiene.ts +++ b/apps/server/src/kin/approvals/CredentialHygiene.ts @@ -1,30 +1,25 @@ import type { KinOrgId } from "@t3tools/contracts"; +import * as HostProcess from "@t3tools/shared/HostProcess"; import * as Clock from "effect/Clock"; -import * as Config from "effect/Config"; -import * as ConfigProvider from "effect/ConfigProvider"; import * as Context from "effect/Context"; import * as Effect from "effect/Effect"; import * as Layer from "effect/Layer"; import * as Option from "effect/Option"; import * as Schema from "effect/Schema"; +import * as Semaphore from "effect/Semaphore"; import { KinGitHubScope } from "../github/KinGitHubScope.ts"; import * as OwnerMergeClient from "../github/OwnerMergeClient.ts"; +import { + CredentialHygieneFailure, + type CredentialHygieneReport, +} from "./CredentialHygieneReport.ts"; +import { forbiddenEnvNames, refusedProviderEnvChanges } from "./HostConfigAllowlist.ts"; /** - * Agent CLIs inherit the server's environment, so any of these hands them a GitHub credential. - * `SSH_AUTH_SOCK` reaches every key the SSH agent holds, and the SSH probe sees only the first one - * GitHub accepts. + * The probe the live layer (`GitHubIdentityProbe.ts`) runs for `T3GitHubIdentities.ssh`, with + * `-F ` added and without `SSH_AUTH_SOCK`. */ -const ENV_TOKEN_NAMES = [ - "GH_TOKEN", - "GITHUB_TOKEN", - "GH_ENTERPRISE_TOKEN", - "GITHUB_ENTERPRISE_TOKEN", - "SSH_AUTH_SOCK", -]; - -/** @public The probe APPR-9's live layer runs for `T3GitHubIdentities.ssh`, without `SSH_AUTH_SOCK`. */ export const GITHUB_SSH_PROBE = [ "ssh", "-T", @@ -32,6 +27,11 @@ export const GITHUB_SSH_PROBE = [ "BatchMode=yes", "-o", "StrictHostKeyChecking=yes", + // A shared connection would answer each per-key probe as whoever opened it. + "-o", + "ControlMaster=no", + "-o", + "ControlPath=none", // A `~/.ssh/config` block can turn key auth off for the probe while agents turn it back on. "-o", "PubkeyAuthentication=yes", @@ -69,77 +69,65 @@ export class T3GitHubIdentityError extends Schema.TaggedError; + readonly t3: ReadonlyArray<{ + readonly login: string; + readonly id: number; + readonly source: string; + }>; /** Every account `gh auth status --hostname github.com` lists, active or not. */ readonly ghAccounts: ReadonlyArray; /** - * The login behind `git credential fill` for `https://github.com` (`GET /user` with the - * helper's password, never logged). None when no helper answers. + * The login and account id behind `git credential fill` for `https://github.com` (`GET /user` + * with the helper's password, never logged). None when no helper answers. */ - readonly gitCredential: Option.Option; + readonly gitCredential: Option.Option<{ readonly login: string; readonly id: number }>; + /** The owner logins asked about that the git helper also holds a password for. */ + readonly gitCredentialFor: ReadonlyArray; /** * The combined stdout and stderr of each SSH probe: `GITHUB_SSH_PROBE` as is, then once per - * private key in `~/.ssh` with `-o IdentitiesOnly=yes -i ` added, since GitHub greets only - * the first key it accepts. The check parses them; any answer it can't read fails it. + * private key (each `IdentityFile` the SSH configs name, and every key in `~/.ssh`) with + * `-o IdentitiesOnly=yes -i ` added, since GitHub greets only the first key it accepts. The + * check parses them; any answer it can't read fails it. */ readonly ssh: ReadonlyArray; + /** + * The variables each provider instance's environment adds to or changes from the server's, by + * name: agents and the terminal inherit these on top of the server's. + */ + readonly providerEnvironments: ReadonlyArray<{ + readonly instance: string; + readonly names: ReadonlyArray; + }>; } export class T3GitHubIdentity extends Context.Service< T3GitHubIdentity, - { readonly identities: Effect.Effect } + { + /** `ownerLogins` are the usernames to ask the git helper about. */ + readonly identities: (input: { + readonly ownerLogins: ReadonlyArray; + }) => Effect.Effect; + } >()("t3/kin/approvals/CredentialHygiene/T3GitHubIdentity") {} -/** - * Until the live probe lands (spec section 12: `gh`, the git credential helper, SSH and T3's own - * credential), no identity can be read, so every check fails `identity_unknown` and nothing runs. - */ -export const identityUnavailableLayer = Layer.succeed( - T3GitHubIdentity, - T3GitHubIdentity.of({ - identities: Effect.fail( - new T3GitHubIdentityError({ cause: "the live GitHub identity probe is not installed" }), - ), - }), -); - -export const CredentialHygieneFailure = Schema.Literals([ - "env_token", - "owner_identity", - "non_bot_identity", - "identity_unknown", - "merge_token_missing", - "merge_token_expired", -]); -export type CredentialHygieneFailure = typeof CredentialHygieneFailure.Type; - -export interface CredentialHygieneReport { - readonly passed: boolean; - readonly failures: ReadonlyArray; - readonly warnings: ReadonlyArray<"merge_token_expiring">; - /** Credential variables set in the server's environment. Names only, never values. */ - readonly envTokenNames: ReadonlyArray; - readonly t3Login: string | null; - /** Every login found, with where it came from. Logins only; no credential leaves the probe. */ - readonly identities: ReadonlyArray<{ - readonly source: "t3" | "gh" | "git_credential" | "ssh"; - readonly login: string; - }>; - readonly checkedAt: number; -} - const ISSUE_PASS: unique symbol = Symbol("CredentialHygienePass.issue"); let issuePass: (orgId: KinOrgId, report: CredentialHygieneReport) => CredentialHygienePass; @@ -177,16 +165,24 @@ export class CredentialHygienePass { export class CredentialHygieneFailedError extends Schema.TaggedError()( "CredentialHygieneFailedError", - { failures: Schema.Array(CredentialHygieneFailure) }, + { + failures: Schema.Array(CredentialHygieneFailure), + /** The report's fixed reasons, which never hold a value. */ + reasons: Schema.Array(Schema.String), + }, ) { override get message(): string { return `Privileged actions are refused until the credential check passes (${this.failures.join(", ")}).`; } } +/** How long `check` reuses one probe, across every org and held action. */ +export const PROBE_CACHE_TTL = "60 seconds"; + /** - * Spec section 12. The executor runs `check` at startup and hourly (audit `credential_check_failed` - * and the owner's card on failure) and calls `requirePassing` before every privileged run. + * Spec section 12. The executor runs `check` at startup, hourly and to hold an action (audit + * `credential_check_failed` and the owner's card on failure), and calls `requirePassing` before + * every privileged run. `check` shares one probe for `PROBE_CACHE_TTL`; `requirePassing` never does. */ export class CredentialHygiene extends Context.Service< CredentialHygiene, @@ -203,25 +199,42 @@ const make = Effect.gen(function* () { const scope = yield* KinGitHubScope; const mergeTokens = yield* OwnerMergeClient.OwnerMergeClient; // Read at each check: agent CLIs inherit whatever the environment holds when they spawn. - const configProvider = yield* ConfigProvider.ConfigProvider; + const environment = yield* HostProcess.Environment; - const envTokenNames = Effect.forEach(ENV_TOKEN_NAMES, (name) => - Config.option(Config.String(name)).pipe( - Effect.map((value) => (Option.isSome(value) && value.value.trim() !== "" ? [name] : [])), - // An unreadable variable is treated as set: fail closed. - Effect.orElseSucceed(() => [name]), - ), - ).pipe( - Effect.map((names) => names.flat()), - Effect.provideService(ConfigProvider.ConfigProvider, configProvider), - ); + // One probe per set of owner logins, shared for `PROBE_CACHE_TTL`. Single-flight too: concurrent + // checks wait on the probe already running. + const probes = new Map>(); + const probesLock = Semaphore.makeUnsafe(1); + const cachedIdentities = (ownerLogins: ReadonlyArray) => + probesLock + .withPermits(1)( + Effect.gen(function* () { + const key = ownerLogins.join("\n"); + const cached = probes.get(key); + if (cached !== undefined) return cached; + const made = yield* Effect.cachedWithTTL( + identity.identities({ ownerLogins }), + PROBE_CACHE_TTL, + ); + probes.set(key, made); + return made; + }), + ) + .pipe(Effect.flatten); - const check = Effect.fn("CredentialHygiene.check")(function* (orgId: KinOrgId) { + const evaluate = Effect.fnUntraced(function* ( + orgId: KinOrgId, + probe: ( + ownerLogins: ReadonlyArray, + ) => Effect.Effect, + ) { const failures = new Set(); + const reasons: Array = []; const warnings: Array<"merge_token_expiring"> = []; - const envNames = yield* envTokenNames; + const envNames = forbiddenEnvNames(environment); if (envNames.length > 0) failures.add("env_token"); + for (const name of envNames) reasons.push(`the server's environment sets ${name}`); const status = yield* mergeTokens .status(orgId) @@ -233,50 +246,106 @@ const make = Effect.gen(function* () { if (status._tag === "missing") failures.add("merge_token_missing"); if (status._tag === "expired") failures.add("merge_token_expired"); if (status._tag === "active" && status.expiringSoon) warnings.push("merge_token_expiring"); + // A helper matches the username as stored, so both spellings are asked about. + const askAbout = [ + ...new Set( + [scope.ownerLogin, ...(status._tag === "missing" ? [] : [status.login])].flatMap( + (login) => [login, login.toLowerCase()], + ), + ), + ]; + + // A login can be renamed and registered again; the id can't. Without one, nothing to compare. + if (Option.isNone(scope.botId)) { + failures.add("identity_unknown"); + reasons.push("KIN_GITHUB_BOT_ID isn't set"); + } + const tokenBacked: Array<{ + readonly source: string; + readonly login: string; + readonly id: number; + }> = []; - const probed = yield* identity.identities.pipe(Effect.option); + const probed = yield* probe(askAbout).pipe(Effect.result); let t3Login: string | null = null; const found: Array = []; - if (Option.isNone(probed)) { + if (probed._tag === "Failure") { failures.add("identity_unknown"); + reasons.push(probed.failure.reason); } else { - const { t3, ghAccounts, gitCredential, ssh } = probed.value; - if (Option.isSome(t3)) { - t3Login = t3.value.login; - if (t3.value.source === "env") failures.add("env_token"); - found.push({ source: "t3", login: t3.value.login }); + const { t3, ghAccounts, gitCredential, gitCredentialFor, ssh, providerEnvironments } = + probed.success; + t3Login = t3[0]?.login ?? null; + for (const { login, id, source } of t3) { + if (source === "env") failures.add("env_token"); + found.push({ source: "t3", login }); + tokenBacked.push({ source: "t3", login, id }); } for (const login of ghAccounts) found.push({ source: "gh", login }); if (Option.isSome(gitCredential)) { - found.push({ source: "git_credential", login: gitCredential.value }); + found.push({ source: "git_credential", login: gitCredential.value.login }); + tokenBacked.push({ source: "git_credential", ...gitCredential.value }); + } + if (gitCredentialFor.length > 0) { + failures.add("owner_identity"); + reasons.push("the git credential helper holds a password for the owner's login"); + } + for (const { instance, names } of providerEnvironments) { + const shown = /^[A-Za-z0-9_-]{1,64}$/.test(instance) ? `"${instance}"` : "an instance"; + const own = forbiddenEnvNames(Object.fromEntries(names.map((name) => [name, "set"]))); + for (const name of own.filter((name) => !envNames.includes(name))) { + failures.add("env_token"); + reasons.push(`provider instance ${shown}'s environment sets ${name}`); + } + // Agents would then run another program, config or startup file than the probe read. + for (const name of refusedProviderEnvChanges(names)) { + failures.add("env_token"); + reasons.push(`provider instance ${shown}'s environment changes ${name}`); + } } // The probe as is always runs, so no answer means the live layer skipped SSH. - if (ssh.length === 0) failures.add("identity_unknown"); + if (ssh.length === 0) { + failures.add("identity_unknown"); + reasons.push("ssh: no answer"); + } for (const output of ssh) { const answer = parseGitHubSshAnswer(output); - if (answer._tag === "unknown") failures.add("identity_unknown"); - if (answer._tag !== "logins") continue; - for (const login of answer.logins) { - if (!found.some((item) => item.source === "ssh" && item.login === login)) { - found.push({ source: "ssh", login }); - } + if (answer._tag === "unknown") { + failures.add("identity_unknown"); + reasons.push("ssh: an answer couldn't be read"); } + if (answer._tag !== "logins") continue; + for (const login of answer.logins) found.push({ source: "ssh", login }); } } + const identities = found.filter( + (item, index) => + found.findIndex((other) => other.source === item.source && other.login === item.login) === + index, + ); const bot = scope.botLogin.toLowerCase(); - for (const { login } of found) { + for (const { login } of identities) { const normalized = login.toLowerCase(); if (ownerLogins.has(normalized)) failures.add("owner_identity"); else if (normalized !== bot) failures.add("non_bot_identity"); } + // SSH greets by login only; every token names its account id too. + for (const { source, login, id } of tokenBacked) { + if (login.toLowerCase() !== bot || Option.isNone(scope.botId) || id === scope.botId.value) { + continue; + } + failures.add("non_bot_identity"); + reasons.push(`${source}: the bot's login belongs to an account other than KIN_GITHUB_BOT_ID`); + } const report: CredentialHygieneReport = { passed: failures.size === 0, failures: [...failures], + reasons: [...new Set(reasons)], warnings, envTokenNames: envNames, t3Login, - identities: found, + identities, checkedAt: yield* Clock.currentTimeMillis, }; yield* Effect.annotateCurrentSpan({ @@ -286,10 +355,19 @@ const make = Effect.gen(function* () { return report; }); + const check = Effect.fn("CredentialHygiene.check")(function* (orgId: KinOrgId) { + return yield* evaluate(orgId, cachedIdentities); + }); + const fresh = (ownerLogins: ReadonlyArray) => identity.identities({ ownerLogins }); + const requirePassing = Effect.fn("CredentialHygiene.requirePassing")(function* (orgId: KinOrgId) { - const report = yield* check(orgId); + // A fresh probe, started after the caller's claim, so a cached pass never gates a side effect. + const report = yield* evaluate(orgId, fresh); if (!report.passed) { - return yield* new CredentialHygieneFailedError({ failures: report.failures }); + return yield* new CredentialHygieneFailedError({ + failures: report.failures, + reasons: report.reasons, + }); } return issuePass(orgId, report); }); diff --git a/apps/server/src/kin/approvals/CredentialHygieneReport.ts b/apps/server/src/kin/approvals/CredentialHygieneReport.ts new file mode 100644 index 0000000000000..ce99c7f27583f --- /dev/null +++ b/apps/server/src/kin/approvals/CredentialHygieneReport.ts @@ -0,0 +1,45 @@ +import * as Schema from "effect/Schema"; + +/** + * The credential check's result, apart from the check itself, so the owner's chat can show one + * without importing what the check reaches. + */ +export const CredentialHygieneFailure = Schema.Literals([ + "env_token", + "owner_identity", + "non_bot_identity", + "identity_unknown", + "merge_token_missing", + "merge_token_expired", +]); +export type CredentialHygieneFailure = typeof CredentialHygieneFailure.Type; + +export interface CredentialHygieneReport { + readonly passed: boolean; + readonly failures: ReadonlyArray; + /** + * Why, in fixed words that name a source, file, directive, key, variable or provider instance, + * never a value. Shown to the owner and written to the audit log. + */ + readonly reasons: ReadonlyArray; + readonly warnings: ReadonlyArray<"merge_token_expiring">; + /** Credential variables set in the server's environment. Names only, never values. */ + readonly envTokenNames: ReadonlyArray; + /** The first T3 credential's login, in the order T3 ranks them (saved, app, then personal). */ + readonly t3Login: string | null; + /** Every login found, with where it came from. Logins only; no credential leaves the probe. */ + readonly identities: ReadonlyArray<{ + readonly source: "t3" | "gh" | "git_credential" | "ssh"; + readonly login: string; + }>; + readonly checkedAt: number; +} + +/** The audit entry's `result` for a failing check: the failures, then their reasons. */ +export const failureResult = (report: { + readonly failures: ReadonlyArray; + readonly reasons: ReadonlyArray; +}) => + report.reasons.length === 0 + ? report.failures.join(",") + : `${report.failures.join(",")}: ${report.reasons.join("; ")}`; diff --git a/apps/server/src/kin/approvals/ExecutionNotices.ts b/apps/server/src/kin/approvals/ExecutionNotices.ts index 6179d7260d7ab..fbceb77c66f09 100644 --- a/apps/server/src/kin/approvals/ExecutionNotices.ts +++ b/apps/server/src/kin/approvals/ExecutionNotices.ts @@ -8,6 +8,8 @@ import type { import * as Context from "effect/Context"; import type * as Effect from "effect/Effect"; +import type { CredentialHygieneReport } from "./CredentialHygieneReport.ts"; + export type KinExecutionOutcome = "succeeded" | "failed" | "interrupted"; /** @@ -25,6 +27,13 @@ export class KinExecutionNotices extends Context.Service< /** Something the owner should know about a success, such as a head branch left in place. */ readonly note?: string; }) => Effect.Effect; + /** Tells the owner the credential check's result changed: failing, warning, or passing again. */ + readonly credentialCheck: (input: { + readonly orgId: KinOrgId; + readonly report: CredentialHygieneReport; + /** For "passes again": the `seq` of the failure it ends, so one failure gets one notice. */ + readonly recoveredFrom?: number; + }) => Effect.Effect; /** An approval whose grant lapsed while the executor held it; `holdReason` says why. */ readonly lapsed: (input: { readonly action: KinPendingAction; diff --git a/apps/server/src/kin/approvals/GitHubIdentityProbe.test.ts b/apps/server/src/kin/approvals/GitHubIdentityProbe.test.ts new file mode 100644 index 0000000000000..7ae9b50879406 --- /dev/null +++ b/apps/server/src/kin/approvals/GitHubIdentityProbe.test.ts @@ -0,0 +1,1517 @@ +import * as NodeServices from "@effect/platform-node/NodeServices"; +import { assert, describe, it } from "@effect/vitest"; +import { + type KinOrgId, + type VcsError, + VcsProcessSpawnError, + VcsProcessTimeoutError, +} from "@t3tools/contracts"; +import * as HostProcess from "@t3tools/shared/HostProcess"; +import * as Context from "effect/Context"; +import * as Effect from "effect/Effect"; +import * as Fiber from "effect/Fiber"; +import * as FileSystem from "effect/FileSystem"; +import * as Layer from "effect/Layer"; +import * as Logger from "effect/Logger"; +import * as Option from "effect/Option"; +import * as Path from "effect/Path"; +import * as PlatformError from "effect/PlatformError"; +import * as Redacted from "effect/Redacted"; +import type * as Scope from "effect/Scope"; +import * as Tracer from "effect/Tracer"; +import { HttpClient, HttpClientResponse } from "effect/http"; +import { ChildProcessSpawner } from "effect/process"; +import * as TestClock from "effect/testing/TestClock"; + +import { VcsProcess, type VcsProcessInput, type VcsProcessOutput } from "../../vcs/VcsProcess.ts"; +import { credentialCheckBody } from "../chat/executionNotices.ts"; +import * as OwnerMergeClient from "../github/OwnerMergeClient.ts"; +import { BOT_ID, fakeGitHub, memoryKeychain, scopeLayer } from "../github/testFakes.ts"; +import * as CredentialHygiene from "./CredentialHygiene.ts"; +import * as GitHubIdentityProbe from "./GitHubIdentityProbe.ts"; +import * as T3GitHubCredential from "./T3GitHubCredential.ts"; + +const ORG = "org_kin" as KinOrgId; +const NOW = Date.UTC(2026, 10, 9); +const DAY = 24 * 60 * 60 * 1000; +const APP_TOKEN = "ghu_t3_app_token"; +const SAVED_TOKEN = "ghp_t3_saved_token"; +const GH_TOKEN = "gho_gh_keyring_token"; +const OWNER_TOKEN = "gho_owner_token"; +const GIT_PASSWORD = "gho_git_helper_password_never_logged"; +const SSH_G_SECRET = "ssh_g_output_never_kept"; +// What the fake `ssh-keygen -y -P ""` answers depends only on these contents. +const KEY = + "-----BEGIN OPENSSH PRIVATE KEY-----\nnot a real key\n-----END OPENSSH PRIVATE KEY-----\n"; +const KEY_AFTER_NOTE = `# the bot's deploy key\n${KEY}`; +const PASSPHRASE_KEY = + "-----BEGIN OPENSSH PRIVATE KEY-----\nencrypted\n-----END OPENSSH PRIVATE KEY-----\n"; +const UNLOADABLE_KEY = + "-----BEGIN RSA PRIVATE KEY-----\nloads only with 0600\n-----END RSA PRIVATE KEY-----\n"; +const sshHi = (login: string) => + `Hi ${login}! You've successfully authenticated, but GitHub does not provide shell access.`; +const SSH_DENIED = "git@github.com: Permission denied (publickey)."; +const HELPER_PROMPTS_OFF = + "f() { exit 1 get: -c: line 1: syntax error: unexpected end of file\nfatal: could not read Username for 'https://github.com': terminal prompts disabled"; +const NO_HELPER_PROMPTS_OFF = + "fatal: could not read Username for 'https://github.com': terminal prompts disabled"; + +/** A Kin host's `~/.ssh/config` using every allowed directive. */ +const USER_SSH_CONFIG = [ + "# Kin bot", + "Host github.com", + " HostName github.com", + " User git", + " Port 22", + " IdentityFile ~/.ssh/id_ed25519 # the bot's key", + " IdentitiesOnly yes", + " IdentityAgent none", + " AddKeysToAgent no", + " ControlMaster no", + " ControlPath none", + " StrictHostKeyChecking yes", + " UserKnownHostsFile ~/.ssh/known_hosts", + "Host *", + " ServerAliveInterval=30", + " ServerAliveCountMax 3", + " ConnectTimeout 10", + ' COMPRESSION "no"', + "Include config.d/*.conf", +].join("\n"); +/** macOS's `/etc/ssh` as shipped, with relative includes. */ +const SYSTEM_SSH = { + ssh_config: "Include ssh_config.d/*\nHost *\n SendEnv LANG LC_*\nInclude crypto.conf\n", + "ssh_config.d/100-macos.conf": "Host *\n SendEnv LANG\n", + "crypto.conf": [ + "Ciphers ^aes128-gcm@openssh.com", + "KexAlgorithms ^ecdh-sha2-nistp256", + "MACs ^hmac-sha2-256", + "HostKeyAlgorithms ^ssh-ed25519", + "PubkeyAcceptedAlgorithms ^ssh-ed25519", + "CASignatureAlgorithms ^ssh-ed25519", + "HostbasedAcceptedAlgorithms ^ssh-ed25519", + "HashKnownHosts yes", + "GSSAPIAuthentication no", + ].join("\n"), +}; + +type Run = Effect.Effect; +const answer = (input: { + readonly stdout?: string; + readonly stderr?: string; + readonly exitCode?: number; +}): Run => + Effect.succeed({ + exitCode: ChildProcessSpawner.ExitCode(input.exitCode ?? 0), + stdout: input.stdout ?? "", + stderr: input.stderr ?? "", + stdoutTruncated: false, + stderrTruncated: false, + }); +const spawnFailure = (command: string, reason: "NotFound" | "PermissionDenied"): Run => + Effect.fail( + new VcsProcessSpawnError({ + operation: "test", + command, + cwd: "/", + cause: PlatformError.systemError({ _tag: reason, module: "test", method: "spawn" }), + }), + ); +const missing = (command: string) => spawnFailure(command, "NotFound"); +const timeout = (command: string) => + new VcsProcessTimeoutError({ operation: "test", command, cwd: "/", timeoutMs: 1 }); +const ghStatus = (...logins: ReadonlyArray) => + answer({ + stdout: JSON.stringify({ + hosts: { + "github.com": logins.map((login, index) => ({ + state: "success", + active: index === 0, + host: "github.com", + login, + tokenSource: "keyring", + })), + }, + }), + }); +const gitFill = (password: string) => + answer({ stdout: `protocol=https\nhost=github.com\nusername=x\npassword=${password}\n` }); + +/** `git config --list --show-origin -z`; a null value is a bare boolean key. */ +type GitEntry = readonly [key: string, value: string | null, origin?: string]; +const GIT_FILE = "file:/Users/kin/.gitconfig"; +const gitList = (...entries: ReadonlyArray) => + answer({ + stdout: entries + .map( + ([key, value, origin]) => + `${origin ?? GIT_FILE}\0${key}${value === null ? "" : `\n${value}`}\0`, + ) + .join(""), + }); +/** A Kin host's git config, with a key from every allowed group. */ +const BOT_GIT: ReadonlyArray = [ + ["credential.helper", "osxkeychain", "file:/Library/Developer/CommandLineTools/gitconfig"], + ["user.name", "kin-bot"], + ["user.email", "kin-bot@users.noreply.github.com"], + ["core.autocrlf", "input"], + ["core.bare", null], + ["init.defaultbranch", "main"], + ["color.ui", "auto"], + ["alias.st", "status"], + ["pull.rebase", "true"], + ["push.autosetupremote", "true"], + ["fetch.prune", "true"], + ["merge.conflictstyle", "zdiff3"], + ["diff.algorithm", "histogram"], + ["filter.lfs.process", "git-lfs filter-process"], + ["filter.lfs.required", "true"], + // `VcsProcess` adds this to every run. + ["safe.barerepository", "explicit", "command line:"], +]; + +const session = ( + token: string, + input: { readonly expiresAt?: number | null; readonly refreshUntil?: number | null } = {}, +) => + Option.some({ + accessToken: Redacted.make(token), + accessTokenExpiresAt: input.expiresAt ?? null, + refreshable: input.refreshUntil !== undefined, + refreshTokenExpiresAt: input.refreshUntil ?? null, + }); +const stored = ( + input: Partial = {}, +): T3GitHubCredential.T3StoredCredentials => ({ + saved: Option.none(), + app: Option.none(), + account: Option.none(), + providerEnvironments: [], + projectRoots: [], + ...input, +}); +/** What the check asks the git helper about: the owner's login and the merge token's, both cases. */ +const OWNER_LOGINS = ["Nishchay", "nishchay"]; + +/** The machine the probe looks at. Each test changes what one source answers. */ +interface Machine { + t3: T3GitHubCredential.T3StoredCredentials | "uninstalled"; + /** The server's environment; `` stands for the home directory. */ + env: Record; + /** Whether the passwd entry names the home directory. */ + passwdHome: boolean; + /** + * Who `GET /user` names for each token: a login (the bot's has `BOT_ID`, any other another + * id), or a login and id; a number is the status, `hang` never answers. + */ + users: Record; + /** Null leaves `KIN_GITHUB_BOT_ID` unset. */ + botId?: number | null; + gh: Run; + /** `gh auth token`, for the `--user` account or the active one. */ + ghToken: (account: string | undefined) => Run; + gitConfig: Run; + git: Run; + /** `git credential fill` asked for one username. */ + gitFor: (username: string) => Run; + /** Each project's `git config --local` and `--worktree`, by folder under the temp root. */ + repos: Record; + /** `ssh -v -G`; by default it names `~/.ssh/config` and the system config when they exist. */ + sshG?: (paths: { readonly home: string; readonly etc: string }) => Run; + /** Answers one SSH probe; `key` is the `-i` file, or undefined for the probe as is. */ + ssh: (key: string | undefined) => Run; + /** Files under the home directory, by relative path. */ + files: Record; + /** Files under the system SSH folder, by relative path. */ + system: Record; +} + +const botMachine = (): Machine => ({ + t3: stored({ app: session(APP_TOKEN) }), + env: {}, + passwdHome: true, + users: { [APP_TOKEN]: "kin-bot", [GH_TOKEN]: "kin-bot", [GIT_PASSWORD]: "kin-bot" }, + gh: ghStatus("kin-bot"), + ghToken: (account) => + account === undefined + ? answer({ stdout: `${GH_TOKEN}\n` }) + : answer({ + exitCode: 1, + stderr: `no oauth token found for github.com account ${account}\n`, + }), + gitConfig: gitList(...BOT_GIT), + git: gitFill(GIT_PASSWORD), + gitFor: () => answer({ exitCode: 128, stderr: NO_HELPER_PROMPTS_OFF }), + repos: {}, + ssh: (key) => answer({ stderr: key === undefined ? sshHi("kin-bot") : SSH_DENIED }), + files: { + ".ssh/id_ed25519": KEY, + ".ssh/id_ed25519.pub": "ssh-ed25519 AAAA kin-bot", + ".ssh/config": USER_SSH_CONFIG, + ".ssh/known_hosts": "github.com ssh-ed25519 AAAA\n", + }, + system: SYSTEM_SSH, +}); + +/** `botMachine` with `lines` added to `~/.ssh/config`. */ +const withSshConfig = (...lines: ReadonlyArray): Partial => ({ + files: { ...botMachine().files, ".ssh/config": [USER_SSH_CONFIG, ...lines].join("\n") }, +}); +/** `botMachine` with `lines` added to `/etc/ssh/ssh_config`. */ +const withSystemConfig = (...lines: ReadonlyArray): Partial => ({ + system: { ...SYSTEM_SSH, ssh_config: [SYSTEM_SSH.ssh_config, ...lines].join("\n") }, +}); +const withGit = (...entries: ReadonlyArray): Partial => ({ + gitConfig: gitList(...BOT_GIT, ...entries), +}); +/** `botMachine` with one project whose own `git config --local` lists `entries`. */ +const withRepo = (...entries: ReadonlyArray): Partial => ({ + t3: stored({ app: session(APP_TOKEN), projectRoots: ["/repo"] }), + repos: { + repo: { + local: gitList( + ...entries.map(([key, value, origin]): GitEntry => [ + key, + value, + origin ?? "file:.git/config", + ]), + ), + }, + }, +}); +/** `ssh -v -G` naming `files` (under the temp root) after the user's config. */ +const sshReads = + (...files: ReadonlyArray) => + ({ home }: { readonly home: string }) => + answer({ + stderr: [ + `debug1: Reading configuration data ${home}/.ssh/config`, + ...files.map((file) => `debug1: Reading configuration data ${home}/../${file}`), + ].join("\n"), + }); + +/** A fake `ssh-keygen -y -P ""`, answering by the file's contents as OpenSSH does. */ +const keygen = (fs: FileSystem.FileSystem, file: string): Run => + fs.readFileString(file).pipe( + Effect.option, + Effect.flatMap((text) => { + if (Option.isNone(text)) { + return answer({ exitCode: 255, stderr: `Load key "${file}": Permission denied` }); + } + if (text.value === KEY || text.value === KEY_AFTER_NOTE) { + return answer({ stdout: "ssh-ed25519 AAAA\n" }); + } + const reason = + text.value === PASSPHRASE_KEY + ? "incorrect passphrase supplied to decrypt private key" + : text.value === UNLOADABLE_KEY + ? "bad permissions" + : "invalid format"; + return answer({ exitCode: 255, stderr: `Load key "${file}": ${reason}` }); + }), + ); + +const writeFiles = (root: string, files: Record) => + Effect.gen(function* () { + const fs = yield* FileSystem.FileSystem; + const path = yield* Path.Path; + for (const [name, text] of Object.entries(files)) { + yield* fs.makeDirectory(path.dirname(path.join(root, name)), { recursive: true }); + yield* fs.writeFileString(path.join(root, name), text); + } + }); + +/** Builds the live probe over `machine` in a fresh home, and the check over that probe. */ +const probe = (machine: Machine) => + Effect.gen(function* () { + yield* TestClock.setTime(NOW); + const fs = yield* FileSystem.FileSystem; + const path = yield* Path.Path; + const root = yield* fs.makeTempDirectoryScoped(); + const home = path.join(root, "home"); + const etc = path.join(root, "etc", "ssh"); + const bin = path.join(root, "bin"); + yield* fs.makeDirectory(path.join(home, ".ssh", "sockets"), { recursive: true }); + yield* writeFiles(home, machine.files); + yield* writeFiles(etc, machine.system); + // The ssh agents would run; the fake runner answers for it. + yield* writeFiles(bin, { ssh: "#!/bin/sh\n" }); + yield* fs.chmod(path.join(bin, "ssh"), 0o755); + for (const repo of Object.keys(machine.repos)) { + yield* fs.makeDirectory(path.join(root, repo), { recursive: true }); + } + const sshG = + machine.sshG?.({ home, etc }) ?? + answer({ + stdout: "user git\nhostname github.com\n", + stderr: [ + "OpenSSH_10.5p1, LibreSSL 3.3.6", + ...(".ssh/config" in machine.files + ? [`debug1: Reading configuration data ${path.join(home, ".ssh", "config")}`] + : []), + ...("ssh_config" in machine.system + ? [`debug1: Reading configuration data ${path.join(etc, "ssh_config")}`] + : []), + ].join("\n"), + }); + const runs: Array = []; + const vcs = VcsProcess.of({ + run: (input) => { + runs.push(input); + const [first] = input.args; + if (input.command === "gh") { + if (input.args[1] === "status") return machine.gh; + const user = input.args.find((arg) => arg.startsWith("--user=")); + return machine.ghToken(user?.slice("--user=".length)); + } + if (input.command === "git") { + if (first === "-C") { + const repo = machine.repos[path.relative(root, input.args[1] ?? "")]; + if (repo === undefined) return answer({ exitCode: 128, stderr: "no such repo" }); + return input.args.at(-1) === "--worktree" ? (repo.worktree ?? answer({})) : repo.local; + } + if (first === "config") return machine.gitConfig; + const username = /^username=(.*)$/m.exec(input.stdin ?? "")?.[1]; + return username === undefined ? machine.git : machine.gitFor(username); + } + if (input.command === "ssh-keygen") return keygen(fs, input.args.at(-1) ?? ""); + if (input.args.includes("-G")) return sshG; + const key = input.args.indexOf("-i"); + return machine.ssh(key === -1 ? undefined : input.args[key + 1]); + }, + }); + const requests: Array = []; + const github = Layer.succeed( + HttpClient.HttpClient, + HttpClient.make((request) => { + requests.push(request.headers.authorization); + const user = machine.users[request.headers.authorization?.replace(/^Bearer /, "") ?? ""]; + if (user === "hang") return Effect.never; + const body = + typeof user === "string" + ? { login: user, id: user === "kin-bot" ? BOT_ID : 9001 } + : typeof user === "object" + ? user + : { message: "Bad credentials" }; + return Effect.succeed( + HttpClientResponse.fromWeb( + request, + new Response(JSON.stringify(body), { + status: typeof user === "number" ? user : user === undefined ? 401 : 200, + headers: { "content-type": "application/json" }, + }), + ), + ); + }), + ); + const slot = Context.get( + yield* Layer.build(T3GitHubCredential.slotLayer), + T3GitHubCredential.T3GitHubCredential, + ); + if (machine.t3 !== "uninstalled") { + const t3 = machine.t3; + const projectRoots = t3.projectRoots.map((dir) => dir.replace("", root)); + yield* slot.install(Effect.succeed({ ...t3, projectRoots })); + } + const env = Object.fromEntries( + Object.entries({ PATH: "", ...machine.env }).map(([name, value]) => [ + name, + value.replace("", home).replace("", bin).replace("", root), + ]), + ); + const identity = yield* GitHubIdentityProbe.make.pipe( + Effect.provideService(VcsProcess, vcs), + Effect.provideService(HostProcess.PasswdHomeDirectory, machine.passwdHome ? home : null), + Effect.provideService(HostProcess.Environment, env), + Effect.provideService(HostProcess.Username, "kin"), + Effect.provideService(HostProcess.UserId, 501), + Effect.provideService(T3GitHubCredential.T3GitHubCredential, slot), + Effect.provide(Layer.mergeAll(github, scopeLayer())), + ); + const keychain = memoryKeychain({ + [`kin-owner-merge/${ORG}`]: JSON.stringify({ + token: "github_pat_owner", + login: "Nishchay", + expiresAt: NOW + 30 * DAY, + savedAt: NOW, + }), + }); + const hygiene = yield* Layer.build( + CredentialHygiene.layer.pipe( + Layer.provide(Layer.succeed(CredentialHygiene.T3GitHubIdentity, identity)), + Layer.provide(OwnerMergeClient.layer), + Layer.provide(keychain.layer), + Layer.provide(fakeGitHub(() => ({ status: 500 })).layer), + Layer.provide(Layer.succeed(HostProcess.Environment, {})), + Layer.provide(scopeLayer(machine.botId === undefined ? {} : { botId: machine.botId })), + ), + ); + const check = Context.get(hygiene, CredentialHygiene.CredentialHygiene).check(ORG); + return { + identities: identity.identities({ ownerLogins: OWNER_LOGINS }), + check, + runs, + home, + root, + bin, + requests, + slot, + }; + }); + +const run = (effect: Effect.Effect) => + effect.pipe(Effect.scoped, Effect.provide(NodeServices.layer)); + +const argv = (input: VcsProcessInput) => [input.command, ...input.args]; +const SSH_PROBE_OPTIONS = [ + "-T", + "-o", + "BatchMode=yes", + "-o", + "StrictHostKeyChecking=yes", + "-o", + "ControlMaster=no", + "-o", + "ControlPath=none", + "-o", + "PubkeyAuthentication=yes", + "-o", + "ConnectTimeout=10", +]; + +describe("GitHubIdentityProbe", () => { + it.effect("reports every source, and the check passes with only the bot's login", () => + run( + Effect.gen(function* () { + const path = yield* Path.Path; + const { identities, check, runs, home, root, bin } = yield* probe({ + ...botMachine(), + env: { HOME: "/" }, + t3: stored({ + app: session(APP_TOKEN), + projectRoots: ["/repo", "/plain", "/gone"], + providerEnvironments: [{ instance: "codex", names: ["CODEX_HOME"] }], + }), + repos: { + repo: { + local: gitList( + ["core.repositoryformatversion", "0", "file:.git/config"], + ["user.email", "bot@example.com", "file:.git/config"], + ["extensions.objectformat", "sha256", "file:.git/config"], + ["remote.origin.url", "git@github.com:kin/app.git", "file:.git/config"], + ["extensions.worktreeconfig", "true", "file:.git/config"], + ), + worktree: gitList(["core.sparsecheckout", "true", "file:.git/config.worktree"]), + }, + plain: { + local: answer({ + exitCode: 128, + stderr: "fatal: --local can only be used inside a git repository", + }), + }, + }, + }); + const found = yield* identities; + const probed = [...runs]; + assert.deepStrictEqual(found, { + t3: [ + { login: "kin-bot", id: BOT_ID, source: "app" }, + { login: "kin-bot", id: BOT_ID, source: "gh" }, + ], + ghAccounts: ["kin-bot"], + gitCredential: Option.some({ login: "kin-bot", id: BOT_ID }), + gitCredentialFor: [], + ssh: [sshHi("kin-bot"), SSH_DENIED], + providerEnvironments: [{ instance: "codex", names: ["CODEX_HOME"] }], + }); + assert.deepInclude(yield* check, { passed: true, failures: [], reasons: [] }); + + const ssh = path.join(home, ".ssh"); + const config = path.join(ssh, "config"); + const sshBin = path.join(bin, "ssh"); + const repoConfig = (repo: string, scope: string) => [ + "git", + "-C", + path.join(root, repo), + "config", + "--list", + "--show-origin", + "-z", + scope, + ]; + assert.sameDeepMembers(probed.map(argv), [ + ["gh", "auth", "token", "--hostname", "github.com"], + // The owner's login: a token for it means the owner is signed in to gh. + ["gh", "auth", "token", "--hostname", "github.com", "--user=Nishchay"], + ["gh", "auth", "status", "--hostname", "github.com", "--json", "hosts"], + ["git", "config", "--list", "--show-origin", "-z"], + // Once as is, then once per owner username. + ["git", "credential", "fill"], + ["git", "credential", "fill"], + ["git", "credential", "fill"], + // Each project's own config; `--worktree` only where it's turned on. + repoConfig("repo", "--local"), + repoConfig("repo", "--worktree"), + repoConfig("plain", "--local"), + // Which files the ssh on PATH reads. + [sshBin, "-v", "-G", "git@github.com"], + // Every file in ~/.ssh is offered to ssh-keygen; only loadable private keys are probed. + ...["config", "id_ed25519", "id_ed25519.pub", "known_hosts"].map((name) => [ + "ssh-keygen", + "-y", + "-P", + "", + "-f", + path.join(ssh, name), + ]), + // Only the validated config, with no shared connection and no agent (`SSH_AUTH_SOCK`). + [sshBin, "-F", config, ...SSH_PROBE_OPTIONS, "git@github.com"], + [ + sshBin, + "-F", + config, + ...SSH_PROBE_OPTIONS, + "-o", + "IdentitiesOnly=yes", + "-i", + path.join(ssh, "id_ed25519"), + "git@github.com", + ], + ]); + const envs = { + gh: { GH_DEBUG: "", GH_PROMPT_DISABLED: "1", GH_NO_UPDATE_NOTIFIER: "1", NO_COLOR: "1" }, + git: { + GIT_TERMINAL_PROMPT: "0", + GIT_ASKPASS: "", + SSH_ASKPASS: "", + GCM_INTERACTIVE: "never", + }, + ssh: { SSH_AUTH_SOCK: "", SSH_ASKPASS: "", SSH_ASKPASS_REQUIRE: "never" }, + "ssh-keygen": { SSH_AUTH_SOCK: "", SSH_ASKPASS: "", SSH_ASKPASS_REQUIRE: "never" }, + }; + for (const input of probed) { + assert.strictEqual(input.cwd, path.parse(home).root); + assert.isAbove(input.timeoutMs ?? 0, 0); + // Output past the cap fails the run instead of being cut short. + assert.strictEqual(input.outputMode, "error"); + assert.strictEqual(input.maxOutputBytes, 64 * 1024); + assert.deepStrictEqual( + input.env, + envs[path.basename(input.command) as keyof typeof envs], + ); + } + const fills = probed.filter((input) => input.args[0] === "credential"); + assert.deepStrictEqual( + fills.map((input) => input.stdin), + [ + "protocol=https\nhost=github.com\n\n", + "protocol=https\nhost=github.com\nusername=Nishchay\n\n", + "protocol=https\nhost=github.com\nusername=nishchay\n\n", + ], + ); + }), + ), + ); + + it.effect("reads no login from sources that have none", () => + run( + Effect.gen(function* () { + const path = yield* Path.Path; + const { identities, check, requests, runs, bin } = yield* probe({ + ...botMachine(), + // Both of the app session's tokens have lapsed, so GitHub accepts neither. + t3: stored({ app: session(APP_TOKEN, { expiresAt: NOW - 2, refreshUntil: NOW - 1 }) }), + ghToken: () => answer({ exitCode: 1, stderr: "no oauth token found for github.com\n" }), + gh: missing("gh"), + gitConfig: gitList(["user.name", "kin-bot"]), + git: answer({ exitCode: 128, stderr: NO_HELPER_PROMPTS_OFF }), + ssh: () => answer({ stderr: SSH_DENIED }), + files: {}, + system: { ssh_config: "" }, + }); + assert.deepStrictEqual(yield* identities, { + t3: [], + ghAccounts: [], + gitCredential: Option.none(), + gitCredentialFor: [], + ssh: [SSH_DENIED], + providerEnvironments: [], + }); + assert.deepInclude(yield* check, { passed: true, identities: [] }); + assert.deepStrictEqual(requests, []); + // Without a `~/.ssh/config`, the probe reads none at all. + assert.include( + runs.map((input) => argv(input).join(" ")), + [path.join(bin, "ssh"), "-F", "none", ...SSH_PROBE_OPTIONS, "git@github.com"].join(" "), + ); + }), + ), + ); + + it.effect("waits for the server to install T3's reader instead of failing at startup", () => + run( + Effect.gen(function* () { + const { identities, slot } = yield* probe({ ...botMachine(), t3: "uninstalled" }); + const early = yield* Effect.forkChild(identities); + yield* TestClock.adjust("5 seconds"); + yield* slot.install(Effect.succeed(stored({ app: session(APP_TOKEN) }))); + assert.deepInclude((yield* Fiber.join(early)).t3, { + login: "kin-bot", + id: BOT_ID, + source: "app", + }); + }), + ), + ); + + it.effect.each([ + ["git is not installed", { gitConfig: missing("git") }], + [ + "a project's own config has only repository keys", + withRepo( + ["core.bare", "false"], + ["core.logallrefupdates", null], + ["remote.origin.url", "https://github.com/kin/app.git"], + ["remote.origin.pushurl", "ssh://git@github.com/kin/app.git"], + ["remote.origin.fetch", "+refs/heads/*:refs/remotes/origin/*"], + ["remote.origin.prune", "true"], + ["remote.pushdefault", "origin"], + ["branch.main.remote", "origin"], + ["branch.main.pushremote", "fork.v2"], + ["branch.main.merge", "refs/heads/main"], + ["submodule.lib.url", "git@github.com:kin/lib.git"], + ["submodule.lib.active", "true"], + ["lfs.repositoryformatversion", "0"], + ["extensions.worktreeconfig", "false"], + ), + ], + [ + "a project folder is gone or isn't a repository", + { + t3: stored({ app: session(APP_TOKEN), projectRoots: ["/gone", "/plain"] }), + repos: { + plain: { + local: answer({ + exitCode: 128, + stderr: "fatal: --local can only be used inside a git repository", + }), + }, + }, + }, + ], + [ + "ssh is in a later PATH folder, and a Homebrew system config ssh names is allowed", + { + env: { PATH: "/empty:" }, + sshG: sshReads("brew/ssh_config"), + files: { ...botMachine().files, "../brew/ssh_config": "Host *\n SendEnv LANG\n" }, + }, + ], + [ + "a later empty helper resets the list, as `gh auth setup-git` writes it", + withGit( + ["credential.https://github.com.helper", ""], + ["credential.https://github.com.helper", "!gh auth git-credential"], + ), + ], + ] as const)("passes when %s", ([, change]) => + run( + Effect.gen(function* () { + const { check } = yield* probe({ ...botMachine(), ...change } as Machine); + assert.deepInclude(yield* check, { passed: true, failures: [] }); + }), + ), + ); + + const ownerOn = (suffix: string) => (key: string | undefined) => + answer({ stderr: key?.endsWith(suffix) === true ? sshHi("Nishchay") : SSH_DENIED }); + + it.effect.each([ + [ + "the token saved in Settings, behind a bot app sign-in", + { t3: stored({ saved: Option.some(Redacted.make(OWNER_TOKEN)), app: session(APP_TOKEN) }) }, + ], + [ + "the app sign-in, behind a bot token saved in Settings", + { t3: stored({ saved: Option.some(Redacted.make(GH_TOKEN)), app: session(OWNER_TOKEN) }) }, + ], + ["GH_TOKEN, which T3 would use", { env: { GH_TOKEN: OWNER_TOKEN } }], + [ + "a gh keyring token gh auth status no longer lists", + { ghToken: () => answer({ stdout: OWNER_TOKEN }) }, + ], + [ + "the gh account Settings picks", + { + t3: stored({ app: session(APP_TOKEN), account: Option.some("kin-bot-2") }), + ghToken: (account: string | undefined) => + answer({ stdout: account === "kin-bot-2" ? OWNER_TOKEN : GH_TOKEN }), + }, + ], + [ + "a gh token held for the owner's login", + { + ghToken: (account: string | undefined) => + answer({ stdout: account === "Nishchay" ? OWNER_TOKEN : GH_TOKEN }), + }, + ], + ["an inactive gh account", { gh: ghStatus("kin-bot", "Nishchay") }], + ["the git credential helper", { git: gitFill(OWNER_TOKEN) }], + [ + "the git helper's second account, asked for by username", + { + gitFor: (username: string) => + username === "Nishchay" + ? gitFill(OWNER_TOKEN) + : answer({ exitCode: 128, stderr: NO_HELPER_PROMPTS_OFF }), + }, + ], + [ + "SSH without a key", + { + ssh: (key: string | undefined) => + answer({ stderr: key === undefined ? sshHi("Nishchay") : SSH_DENIED }), + }, + ], + [ + "one key in ~/.ssh", + { + ssh: ownerOn("owner_ed25519"), + files: { ...botMachine().files, ".ssh/owner_ed25519": KEY }, + }, + ], + [ + "a key with a note before its header", + { + ssh: ownerOn("owner_rsa"), + files: { ...botMachine().files, ".ssh/owner_rsa": KEY_AFTER_NOTE }, + }, + ], + [ + "a key in a folder of ~/.ssh", + { ssh: ownerOn("work/owner"), files: { ...botMachine().files, ".ssh/work/owner": KEY } }, + ], + [ + "a key two folders down in ~/.ssh", + { ssh: ownerOn("a/b/owner"), files: { ...botMachine().files, ".ssh/a/b/owner": KEY } }, + ], + [ + "a key outside ~/.ssh under a host alias", + { + ssh: ownerOn("keys/github_kin_501"), + files: { + ...withSshConfig( + "Host github-owner", + " HostName github.com", + " IdentityFile %d/keys/github_%u_%i", + ).files, + "keys/github_kin_501": KEY, + }, + }, + ], + [ + "a key an included file names", + { + ssh: ownerOn("keys/included"), + files: { + ...botMachine().files, + ".ssh/config.d/owner.conf": "Host ssh.github.com\n IdentityFile ~/keys/included\n", + "keys/included": KEY, + }, + }, + ], + [ + "a key the system SSH config names", + { + ssh: ownerOn("keys/system"), + files: { ...botMachine().files, "keys/system": KEY }, + system: { + ...SYSTEM_SSH, + "ssh_config.d/200-owner.conf": "Host *\n IdentityFile %d/keys/system\n", + }, + }, + ], + ] as const)("fails as owner_identity when %s is the owner", ([, change]) => + run( + Effect.gen(function* () { + const machine = { ...botMachine(), ...change } as Machine; + const { check } = yield* probe({ + ...machine, + users: { ...botMachine().users, [OWNER_TOKEN]: "Nishchay" }, + }); + const report = yield* check; + assert.isFalse(report.passed); + assert.include(report.failures, "owner_identity"); + }), + ), + ); + + it.effect.each([ + [ + "the token saved in Settings", + { t3: stored({ saved: Option.some(Redacted.make(OWNER_TOKEN)) }) }, + ], + [ + "a gh token held for another account", + { + ghToken: (account: string | undefined) => + answer({ stdout: account === "Nishchay" ? OWNER_TOKEN : GH_TOKEN }), + }, + ], + ["the git credential helper", { git: gitFill(OWNER_TOKEN) }], + ] as const)( + "fails as non_bot_identity when %s names the bot's login on another account", + ([, change]) => + run( + Effect.gen(function* () { + const machine = { ...botMachine(), ...change } as Machine; + // A renamed bot whose login someone registered again: only the id tells them apart. + const { check } = yield* probe({ + ...machine, + users: { ...botMachine().users, [OWNER_TOKEN]: { login: "Kin-Bot", id: 7 } }, + }); + const report = yield* check; + assert.deepInclude(report, { passed: false, failures: ["non_bot_identity"] }); + assert.match(report.reasons[0] ?? "", /^(t3|git_credential): the bot's login belongs/); + }), + ), + ); + + it.effect("fails closed while KIN_GITHUB_BOT_ID is unset, even with only the bot's logins", () => + run( + Effect.gen(function* () { + const { check } = yield* probe({ ...botMachine(), botId: null }); + assert.deepInclude(yield* check, { + passed: false, + failures: ["identity_unknown"], + reasons: ["KIN_GITHUB_BOT_ID isn't set"], + }); + }), + ), + ); + + it.effect.each([ + // The host's environment. + ["$HOME isn't the passwd home", { env: { HOME: "/Users/someone-else" } }, "home: $HOME isn't"], + [ + "the passwd entry names no home", + { passwdHome: false }, + "home: the passwd entry names no home", + ], + // T3. + ["T3's reader isn't installed", { t3: "uninstalled" }, "t3: the reader isn't installed"], + [ + "GitHub refuses T3's credential", + { users: { [GH_TOKEN]: "kin-bot", [GIT_PASSWORD]: "kin-bot" } }, + "t3: ", + ], + [ + "GET /user names no login", + { users: { ...botMachine().users, [APP_TOKEN]: "" } }, + "t3: GET /user named no login", + ], + [ + "GET /user never answers", + { users: { ...botMachine().users, [APP_TOKEN]: "hang" } }, + "t3: GET /user timed out", + ], + [ + "T3's app sign-in needs a refresh to name its login", + { t3: stored({ app: session(APP_TOKEN, { expiresAt: NOW - 1, refreshUntil: NOW + DAY }) }) }, + "t3: the app sign-in needs a refresh", + ], + [ + "gh auth token fails for another reason", + { ghToken: () => answer({ exitCode: 1, stderr: "error connecting to keyring" }) }, + "t3: gh auth token exited 1", + ], + [ + "gh auth token prints nothing", + { ghToken: () => answer({ stdout: "\n" }) }, + "t3: gh auth token printed no token", + ], + // gh. + ["gh can't be started", { gh: spawnFailure("gh", "PermissionDenied") }, "VcsProcessSpawnError"], + [ + "gh is too old for --json", + { gh: answer({ exitCode: 1, stderr: "unknown flag: --json" }) }, + "gh: gh is older than 2.81", + ], + [ + "gh auth status says it isn't logged in", + { gh: answer({ exitCode: 1, stderr: "You are not logged into any GitHub hosts." }) }, + "gh: gh auth status exited 1", + ], + [ + "gh answers something else", + { gh: answer({ stdout: "github.com\n Logged in" }) }, + "gh: gh auth status --json was unreadable", + ], + [ + "a gh account has no login", + { gh: ghStatus("kin-bot", "") }, + "gh: an account isn't a GitHub login", + ], + [ + "a gh login is longer than GitHub allows", + { gh: ghStatus("kin-bot", "a".repeat(40)) }, + "gh: an account isn't a GitHub login", + ], + [ + "a gh login isn't a GitHub login", + { gh: ghStatus("kin-bot\nApproved actions run") }, + "gh: an account isn't a GitHub login", + ], + ["gh times out", { gh: Effect.fail(timeout("gh")) }, "gh: VcsProcessTimeoutError"], + // git: the process and the helper. + [ + "git can't be started", + { gitConfig: spawnFailure("git", "PermissionDenied") }, + "git_credential: VcsProcessSpawnError", + ], + [ + "git config fails", + { gitConfig: answer({ exitCode: 128, stderr: "fatal: bad config" }) }, + "git_credential: git config exited 128", + ], + [ + "git config's list is cut short", + { gitConfig: answer({ stdout: `${GIT_FILE}\0user.name\nkin-bot` }) }, + "git_credential: git config --list was unreadable", + ], + [ + "more than one credential helper is configured", + withGit(["credential.https://github.com.helper", "store"]), + "git_credential: more than one credential helper", + ], + [ + "the configured helper answers nothing", + { git: answer({ exitCode: 128, stderr: HELPER_PROMPTS_OFF }) }, + "git_credential: git credential fill exited 128", + ], + [ + "the helper answers without a password", + { git: answer({ stdout: "username=kin-bot\n" }) }, + "git_credential: the helper answered without a password", + ], + // git's HTTPS transport sends these through curl, and `git credential fill` never sees them. + ...([".netrc", "_netrc"] as const).map( + (name) => + [ + `~/${name} exists`, + { files: { ...botMachine().files, [name]: "machine github.com login x password y\n" } }, + "git_credential: ~/.netrc exists", + ] as const, + ), + [ + "the helper's password is refused", + { users: { [APP_TOKEN]: "kin-bot", [GH_TOKEN]: "kin-bot" } }, + "git_credential: ", + ], + [ + "GitHub is down for the helper", + { users: { ...botMachine().users, [GIT_PASSWORD]: 502 } }, + "git_credential: ", + ], + // git: keys outside the allowlist. A subsection can hold a URL with a token, so it never shows. + ...( + [ + ["include.path", "include.path"], + ["includeif.gitdir:~/work/.path", "includeif.*.path"], + ["http.extraheader", "http.extraheader"], + ["http.https://github.com/.extraheader", "http.*.extraheader"], + ["url.https://ghp_secret@github.com/.insteadof", "url.*.insteadof"], + ["credential.username", "credential.username"], + ["credential.https://github.com.username", "credential.*.username"], + ["credential.https://gitlab.com.helper", "credential.*.helper"], + ["core.askpass", "core.askpass"], + ["core.sshcommand", "core.sshcommand"], + ["core.gitproxy", "core.gitproxy"], + ["core.fsmonitor", "core.fsmonitor"], + ["core.hookspath", "core.hookspath"], + ["gpg.program", "gpg.program"], + ["gpg.ssh.program", "gpg.*.program"], + ["commit.gpgsign", "commit.gpgsign"], + ["ghp_0123456789abcdef0123456789abcdef01234567.x", "an unrecognised key"], + ] as const + ).map( + ([key, shown]) => + [ + `git config sets ${key}`, + withGit([key, "x"]), + `git_credential: git config sets ${shown}`, + ] as const, + ), + [ + "git config comes from GIT_CONFIG_PARAMETERS", + withGit(["user.name", "kin-bot", "command line:"]), + "git_credential: git config is set from the environment", + ], + [ + "git config comes from somewhere other than a file", + withGit(["user.name", "kin-bot", "blob:HEAD:.gitconfig"]), + "git_credential: git config comes from an unexpected source", + ], + // SSH: directives outside the allowlist, in any case and either form. + ...( + [ + ['Match exec "test -d ~/owner"', "Match"], + ["Match host github.com", "Match"], + ["ProxyCommand nc %h %p", "ProxyCommand"], + ["PROXYCOMMAND=nc %h %p", "ProxyCommand"], + ["ProxyJump bastion", "ProxyJump"], + ["PKCS11Provider /usr/lib/opensc.so", "PKCS11Provider"], + ["SecurityKeyProvider internal", "SecurityKeyProvider"], + ["CertificateFile ~/.ssh/owner-cert.pub", "CertificateFile"], + ["KnownHostsCommand /bin/cat", "KnownHostsCommand"], + ["LocalCommand id", "LocalCommand"], + ["PermitLocalCommand yes", "PermitLocalCommand"], + ["CanonicalizeHostname yes", "CanonicalizeHostname"], + ["SendEnv LANG", "SendEnv"], + ["ghp_0123456789abcdef0123456789abcdef01234567", "an unrecognised directive"], + ] as const + ).map( + ([line, name]) => + [ + `~/.ssh/config has ${line}`, + withSshConfig(line), + `ssh: ~/.ssh/config sets ${name}`, + ] as const, + ), + ...( + [ + ["ControlMaster auto", "ControlMaster"], + ["ControlPath ~/.ssh/sockets/%C", "ControlPath"], + [ + "IdentityAgent ~/Library/Group Containers/2BUA8C4S2C.com.1password/t/agent.sock", + "IdentityAgent", + ], + ["IdentityAgent SSH_AUTH_SOCK", "IdentityAgent"], + ["AddKeysToAgent yes", "AddKeysToAgent"], + ] as const + ).map( + ([line, name]) => + [ + `~/.ssh/config has ${line}`, + withSshConfig(line), + `ssh: ~/.ssh/config sets ${name} to a value other than the allowed one`, + ] as const, + ), + [ + "an included file has a forbidden directive", + { files: { ...botMachine().files, ".ssh/config.d/work.conf": "ProxyJump bastion\n" } }, + "ssh: ~/.ssh/config sets ProxyJump", + ], + [ + "a file included without a glob has a forbidden directive", + { + files: { + ...withSshConfig("Include work").files, + ".ssh/work": "Host *\n IdentityAgent /tmp/agent\n", + }, + }, + "ssh: ~/.ssh/config sets IdentityAgent", + ], + [ + "the system SSH config has Match", + withSystemConfig("Match exec true"), + "ssh: the system SSH config sets Match", + ], + [ + "a file the system SSH config includes has ProxyCommand", + { system: { ...SYSTEM_SSH, "ssh_config.d/50-proxy.conf": "ProxyCommand nc %h %p\n" } }, + "ssh: the system SSH config sets ProxyCommand", + ], + ["a line has no directive", withSshConfig("= yes"), "ssh: ~/.ssh/config can't be parsed"], + [ + "an Include includes itself", + withSshConfig("Include config"), + "ssh: ~/.ssh/config includes too many files", + ], + [ + "an Include uses a token", + withSshConfig("Include %d/.ssh/more"), + "ssh: an Include names a path the probe can't expand", + ], + [ + "an Include names another user's home", + withSshConfig("Include ~nishchay/.ssh/config"), + "ssh: an Include names a path the probe can't expand", + ], + [ + "an Include globs a folder", + withSshConfig("Include */config"), + "ssh: an Include names a path the probe can't expand", + ], + [ + "an Include names a folder", + { files: { ...withSshConfig("Include keys").files, ".ssh/keys/k": KEY } }, + "ssh: ~/.ssh/config includes something that isn't a small file", + ], + [ + "~/.ssh/config is too large to check", + withSshConfig(`# ${"x".repeat(64 * 1024)}`), + "ssh: ~/.ssh/config includes something that isn't a small file", + ], + ...( + [ + ["~/.ssh/github_%h", "a host token"], + ["/keys/${USER}/id", "an environment variable"], + ["~nishchay/.ssh/id", "another user's home"], + ["keys/id", "a relative path"], + ] as const + ).map( + ([file, what]) => + [ + `an IdentityFile uses ${what}`, + withSshConfig(`IdentityFile ${file}`), + "ssh: ~/.ssh/config has an IdentityFile the probe can't expand", + ] as const, + ), + // SSH: arguments ssh's argv_split reads differently, which fail closed instead. + ...( + [ + ["Include 'config.d/owner conf'", "single quotes"], + ["Include config.d/owner\\ conf", "a backslash"], + ['IdentityFile "~/.ssh/a"b', "a quote that runs on"], + ['IdentityFile a"b', "a quote inside an argument"], + ['Include "config.d/owner', "an unterminated quote"], + ["IdentityFile ~/.ssh/a#b", "a # inside an argument"], + ] as const + ).map( + ([line, what]) => + [ + `~/.ssh/config uses ${what}`, + withSshConfig(line), + "ssh: ~/.ssh/config can't be parsed", + ] as const, + ), + [ + "an Include without a glob names a file that doesn't exist", + withSshConfig("Include owner.conf"), + "ssh: ~/.ssh/config names a file that doesn't exist", + ], + [ + "a directive ssh doesn't know, which could be a pasted secret", + withSshConfig("supersecretpassword"), + "ssh: ~/.ssh/config sets an unrecognised directive", + ], + // SSH: the ssh on PATH, and the system config it reads. + ["ssh isn't on PATH", { env: { PATH: "/empty" } }, "ssh: ssh isn't on PATH"], + [ + "PATH has a relative folder before ssh", + { env: { PATH: "bin:" } }, + "ssh: PATH has a relative folder", + ], + [ + "the system config the ssh on PATH reads has ProxyCommand", + { + sshG: sshReads("brew/ssh_config"), + files: { ...botMachine().files, "../brew/ssh_config": "ProxyCommand nc %h %p\n" }, + }, + "ssh: the system SSH config sets ProxyCommand", + ], + [ + "a file ssh reads doesn't exist", + { sshG: sshReads("brew/ssh_config") }, + "ssh: the system SSH config names a file that doesn't exist", + ], + [ + "ssh -G names no config file", + { sshG: () => answer({ stderr: "OpenSSH_10.5p1" }) }, + "ssh: ssh -G named no config", + ], + [ + "ssh -G fails", + { sshG: () => answer({ exitCode: 255, stderr: "Bad configuration option" }) }, + "ssh: ssh -G exited 255", + ], + // git: the helper asked for the owner's username. + [ + "the helper fails when asked for the owner's username", + { gitFor: () => answer({ exitCode: 1, stderr: "keychain locked" }) }, + "git_credential: git credential fill exited 1", + ], + [ + "the helper answers for the owner's username without a password", + { gitFor: () => answer({ stdout: "protocol=https\nhost=github.com\nusername=Nishchay\n" }) }, + "git_credential: the helper answered without a password", + ], + // git: keys only the repository allowlist refuses in the host's config. + ...( + [ + ["init.templatedir", "init.templatedir"], + ["remote.origin.pushurl", "remote.*.pushurl"], + ["url.core.example.com.insteadof", "url.*.insteadof"], + ["url.user.example.com.insteadof", "url.*.insteadof"], + ["filter.crypt.clean", "filter.*.clean"], + ] as const + ).map( + ([key, shown]) => + [ + `git config sets ${key}`, + withGit([key, "x"]), + `git_credential: git config sets ${shown}`, + ] as const, + ), + // git: a project's own config. A URL with a token never shows. + ...( + [ + ["credential.helper", "store", "credential.helper"], + ["http.extraheader", "AUTHORIZATION: bearer ghp_x", "http.extraheader"], + ["url.https://ghp_x@github.com/.insteadof", "https://github.com/", "url.*.insteadof"], + ["include.path", "../owner.gitconfig", "include.path"], + ["includeif.onbranch:main.path", "x", "includeif.*.path"], + ["core.sshcommand", "ssh -i ~/owner", "core.sshcommand"], + ["core.askpass", "/bin/owner", "core.askpass"], + ["remote.origin.url", "https://x:ghp_secret@github.com/kin/app", "remote.*.url"], + ["remote.origin.url", "https://ghp_secret@github.com/kin/app", "remote.*.url"], + ["remote.origin.pushurl", "ssh://git:ghp_secret@github.com/k/a", "remote.*.pushurl"], + ["remote.origin.url", "ext::sh -c owner", "remote.*.url"], + ["remote.origin.url", "fd::7", "remote.*.url"], + ["remote.origin.url", null, "remote.*.url"], + ["remote.origin.uploadpack", "owner-upload", "remote.*.uploadpack"], + ["remote.origin.vcs", "owner", "remote.*.vcs"], + ["submodule.lib.url", "https://ghp_secret@github.com/kin/lib", "submodule.*.url"], + ["submodule.lib.update", "!owner", "submodule.*.update"], + // git takes a URL wherever it takes a remote's name. + ["branch.main.remote", "http://owner:ghp_secret@127.0.0.1/r", "branch.*.remote"], + ["branch.main.pushremote", "https://ghp_secret@github.com/k/a", "branch.*.pushremote"], + ["branch.main.remote", "git@github.com:kin/app.git", "branch.*.remote"], + ["branch.main.remote", null, "branch.*.remote"], + ["remote.pushdefault", "https://x:ghp_secret@github.com/k/a", "remote.pushdefault"], + ["remote.pushdefault", null, "remote.pushdefault"], + ["gc.auto", "0", "gc.auto"], + ] as const + ).map( + ([key, value, shown]) => + [ + `a project's own config sets ${key} to ${value ?? "nothing"}`, + withRepo([key, value]), + `git_repo: `, + `repository config sets ${shown}`, + ] as const, + ), + [ + "a project's worktree config sets a helper", + { + ...withRepo(["extensions.worktreeconfig", "true"]), + repos: { + repo: { + local: gitList(["extensions.worktreeconfig", "true", "file:.git/config"]), + worktree: gitList(["credential.helper", "store", "file:.git/config.worktree"]), + }, + }, + }, + "repository config sets credential.helper", + ], + [ + "a project's git can't read it", + { + ...withRepo(), + repos: { + repo: { local: answer({ exitCode: 128, stderr: "fatal: detected dubious ownership" }) }, + }, + }, + "exited 128", + ], + [ + "there are more projects than the probe reads", + { + t3: stored({ + app: session(APP_TOKEN), + projectRoots: Array.from({ length: 65 }, (_, index) => `/p${index}`), + }), + }, + "git_repo: more than 64 projects", + ], + // SSH: keys and probes. + [ + "a key needs a passphrase", + { files: { ...botMachine().files, ".ssh/owner_ed25519": PASSPHRASE_KEY } }, + "ssh: a private key can't be loaded without a passphrase", + ], + [ + "a private key ssh won't load", + { files: { ...botMachine().files, ".ssh/work/owner_rsa": UNLOADABLE_KEY } }, + "ssh: a private key can't be loaded without a passphrase", + ], + [ + "~/.ssh holds more than 32 keys", + { + files: Object.fromEntries( + Array.from({ length: 33 }, (_, index) => [`.ssh/key_${index}`, KEY]), + ), + }, + "ssh: more than 32 private keys", + ], + [ + "an SSH probe times out", + { ssh: () => Effect.fail(timeout("ssh")) }, + "ssh: VcsProcessTimeoutError", + ], + ] as const)("fails closed when %s", ([, change, ...reasons]) => + run( + Effect.gen(function* () { + const { identities, check } = yield* probe({ ...botMachine(), ...change } as Machine); + const flipped = yield* Effect.forkChild(Effect.flip(identities)); + yield* TestClock.adjust("1 minute"); + const error = yield* Fiber.join(flipped); + assert.strictEqual(error._tag, "T3GitHubIdentityError"); + // The fixed reason, so a case can't pass by failing for some other reason. + for (const reason of reasons) assert.include(String(error.cause), reason); + assert.notInclude(String(error.cause), "ghp_"); + const checked = yield* Effect.forkChild(check); + yield* TestClock.adjust("1 minute"); + const report = yield* Fiber.join(checked); + assert.deepInclude(report, { passed: false, failures: ["identity_unknown"] }); + // The same reason reaches the report, and so the audit entry and the owner's chat. + assert.deepStrictEqual(report.reasons, [String(error.cause)]); + assert.include(credentialCheckBody(ORG, report), String(error.cause)); + }), + ), + ); + + it.effect("runs the first executable ssh on PATH for every probe", () => + run( + Effect.gen(function* () { + const path = yield* Path.Path; + const { identities, runs, bin } = yield* probe({ + ...botMachine(), + env: { PATH: "/noexec:" }, + // Not executable, so ssh wouldn't run it. + files: { ...botMachine().files, "../noexec/ssh": "#!/bin/sh\n" }, + }); + yield* identities; + const ssh = runs.filter((input) => path.basename(input.command) === "ssh"); + assert.isAbove(ssh.length, 2); + for (const input of ssh) assert.strictEqual(input.command, path.join(bin, "ssh")); + }), + ), + ); + + it.effect("asks ssh which files it reads only after the user's config passes", () => + run( + Effect.gen(function* () { + const { identities, runs } = yield* probe({ + ...botMachine(), + ...withSshConfig("ProxyJump bastion"), + }); + yield* identities.pipe(Effect.flip); + assert.isFalse(runs.some((input) => input.args.includes("-G"))); + }), + ), + ); + + it.effect.each([ + // Search-only, so `~/.ssh/config` still reads but the folder can't be listed. + ["~/.ssh can't be listed", ".ssh", 0o100], + ["a key can't be read", ".ssh/id_ed25519", 0o000], + ] as const)("fails closed when %s", ([reason, locked, mode]) => + run( + Effect.gen(function* () { + const fs = yield* FileSystem.FileSystem; + const path = yield* Path.Path; + const { identities, home } = yield* probe(botMachine()); + yield* fs.chmod(path.join(home, locked), mode); + const error = yield* identities.pipe(Effect.flip); + yield* fs.chmod(path.join(home, locked), 0o700); + assert.strictEqual(error._tag, "T3GitHubIdentityError"); + assert.include(String(error.cause), reason); + }), + ), + ); + + it.effect("never puts a credential in a log, error, span, report, audit entry or chat", () => { + const logged: Array = []; + const logger = Logger.make(({ message, cause }) => { + logged.push(JSON.stringify(message), String(cause)); + }); + const spans: Array = []; + const tracer = Tracer.make({ + span: (options) => { + const span = new Tracer.NativeSpan(options); + spans.push(span); + return span; + }, + }); + const secrets = [GIT_PASSWORD, GH_TOKEN, APP_TOKEN, SAVED_TOKEN, OWNER_TOKEN, SSH_G_SECRET]; + return run( + Effect.gen(function* () { + const seen: Array = []; + const t3 = stored({ + saved: Option.some(Redacted.make(SAVED_TOKEN)), + app: session(APP_TOKEN), + }); + const users = { + ...botMachine().users, + [SAVED_TOKEN]: "kin-bot", + [OWNER_TOKEN]: "Nishchay", + }; + const ghToken = (account: string | undefined) => + answer({ stdout: account === "Nishchay" ? OWNER_TOKEN : GH_TOKEN }); + // A helper holding the owner's account too, and `ssh -G` printing config values. + const gitFor = () => gitFill(OWNER_TOKEN); + const sshG = ({ home, etc }: { readonly home: string; readonly etc: string }) => + answer({ + stdout: `identityfile ~/.ssh/${SSH_G_SECRET}\n`, + stderr: [ + `debug1: ${SSH_G_SECRET}`, + `debug1: Reading configuration data ${home}/.ssh/config`, + `debug1: Reading configuration data ${etc}/ssh_config`, + ].join("\n"), + }); + for (const [index, change] of [ + {}, + // Each credential refused in turn, so its error path runs too. + { users: { ...users, [GIT_PASSWORD]: 500 } }, + { users: { ...users, [GH_TOKEN]: 401 } }, + { users: { ...users, [SAVED_TOKEN]: 401 } }, + { users: { ...users, [OWNER_TOKEN]: 502 } }, + ].entries()) { + const { identities, check, requests } = yield* probe({ + ...botMachine(), + t3, + users, + ghToken, + gitFor, + sshG, + ...change, + }); + const result = yield* Effect.result(identities); + const report = yield* check; + seen.push( + JSON.stringify(result), + String(result), + JSON.stringify(report), + // What the executor writes to the audit log and posts to the owner's chat. + report.failures.join(","), + credentialCheckBody(ORG, report), + ); + yield* Effect.logWarning("probe", result); + // Each does reach GitHub, as a bearer token and nowhere else. + if (index === 0) { + for (const secret of secrets.filter((value) => value !== SSH_G_SECRET)) { + assert.include(requests, `Bearer ${secret}`); + } + } + } + for (const span of spans) { + seen.push( + span.name, + JSON.stringify([...span.attributes]), + JSON.stringify(span.events), + span.status._tag === "Ended" ? String(span.status.exit) : "", + ); + } + assert.isAbove(spans.length, 0); + const haystack = [...seen, ...logged].join("\n"); + assert.include(haystack, "owner_identity"); + for (const secret of secrets) assert.notInclude(haystack, secret); + }).pipe( + Effect.withTracer(tracer), + Effect.provide(Logger.layer([logger], { mergeWithExisting: false })), + ), + ); + }); +}); diff --git a/apps/server/src/kin/approvals/GitHubIdentityProbe.ts b/apps/server/src/kin/approvals/GitHubIdentityProbe.ts new file mode 100644 index 0000000000000..06d21c8fd0f0a --- /dev/null +++ b/apps/server/src/kin/approvals/GitHubIdentityProbe.ts @@ -0,0 +1,771 @@ +import * as HostProcess from "@t3tools/shared/HostProcess"; +import * as Clock from "effect/Clock"; +import * as Effect from "effect/Effect"; +import * as FileSystem from "effect/FileSystem"; +import * as Layer from "effect/Layer"; +import * as Option from "effect/Option"; +import * as Path from "effect/Path"; +import * as PlatformError from "effect/PlatformError"; +import * as Redacted from "effect/Redacted"; +import * as Schema from "effect/Schema"; +import { HttpClient } from "effect/http"; + +import { VcsProcess, type VcsProcessInput } from "../../vcs/VcsProcess.ts"; +import { gitHubJson } from "../github/githubRest.ts"; +import { KinGitHubScope } from "../github/KinGitHubScope.ts"; +import { + GITHUB_SSH_PROBE, + T3GitHubIdentity, + T3GitHubIdentityError, + type T3GitHubIdentities, +} from "./CredentialHygiene.ts"; +import { + checkGitConfig, + checkRepoGitConfig, + expandIdentityFile, + parseGitConfigList, + parseSshConfig, + sshConfigPaths, + sshViolation, +} from "./HostConfigAllowlist.ts"; +import { T3GitHubCredential, type T3StoredCredentials } from "./T3GitHubCredential.ts"; + +const HOST = "github.com"; +const PROCESS_TIMEOUT_MS = 15_000; +// `GITHUB_SSH_PROBE` gives up connecting after 10 seconds; this also covers the handshake. +const SSH_TIMEOUT_MS = 20_000; +const MAX_OUTPUT_BYTES = 64 * 1024; +const MAX_SSH_KEYS = 32; +const MAX_SSH_FILES = 128; +/** `~/.ssh` and this many levels of folders below it. */ +const SSH_DIR_DEPTH = 2; +const MAX_KEY_FILE_BYTES = 4 * 1024 * 1024; +const MAX_SSH_CONFIG_FILES = 32; +const MAX_SSH_CONFIG_BYTES = 64 * 1024; +const MAX_PROJECT_ROOTS = 64; +const GLOB = /[*?[]/; +// Below `VcsProcess`'s four slots for non-git commands, which T3's own gh runs share. +const SSH_CONCURRENCY = 2; +const PRIVATE_KEY_MARKER = /-----BEGIN [A-Z0-9 ]*PRIVATE KEY-----/; +// GitHub logins are at most 39 characters; managed users add `_shortcode` within that. +const GITHUB_LOGIN = /^[A-Za-z0-9][A-Za-z0-9_-]{0,38}$/; +const GH_NO_TOKEN = /^no oauth token found for github\.com( account \S+)?$/; +const GH_TOKEN_NAMES = ["GH_TOKEN", "GITHUB_TOKEN"]; + +// Every probe runs non-interactively: a prompt would hang until the timeout. +const GH_ENV = { GH_DEBUG: "", GH_PROMPT_DISABLED: "1", GH_NO_UPDATE_NOTIFIER: "1", NO_COLOR: "1" }; +const GIT_ENV = { + GIT_TERMINAL_PROMPT: "0", + GIT_ASKPASS: "", + SSH_ASKPASS: "", + GCM_INTERACTIVE: "never", +}; +// Without the agent, each key answers for itself (`CredentialHygiene` fails on the agent anyway). +const SSH_ENV = { SSH_AUTH_SOCK: "", SSH_ASKPASS: "", SSH_ASKPASS_REQUIRE: "never" }; + +/** Names the source and a fixed reason only: process output can hold a credential. */ +const unreadable = (source: string, reason: string) => + new T3GitHubIdentityError({ cause: `${source}: ${reason}` }); + +const isMissingCommand = (error: { readonly _tag: string; readonly cause?: unknown }) => + error._tag === "VcsProcessSpawnError" && + error.cause instanceof PlatformError.PlatformError && + error.cause.reason._tag === "NotFound"; + +const isNotFound = (error: PlatformError.PlatformError) => error.reason._tag === "NotFound"; + +// The id names the account for good: a login can be renamed and registered again. +const GitHubUser = Schema.Struct({ login: Schema.String, id: Schema.Int }); +const GhAuthStatus = Schema.fromJsonString( + Schema.Struct({ + hosts: Schema.Record(Schema.String, Schema.Array(Schema.Struct({ login: Schema.String }))), + }), +); +type GitHubUserAnswer = { readonly login: string; readonly id: number }; +const decodeGhAuthStatus = Schema.decodeUnknownOption(GhAuthStatus); + +/** + * `GITHUB_SSH_PROBE` with the ssh on PATH, reading only the user config the probe validated + * (`none` without one). With `key`, ssh offers it before any other, so GitHub answers for that key. + */ +const sshProbe = (ssh: string, config: string, key?: string) => [ + ssh, + "-F", + config, + ...GITHUB_SSH_PROBE.slice(1, -1), + ...(key === undefined ? [] : ["-o", "IdentitiesOnly=yes", "-i", key]), + ...GITHUB_SSH_PROBE.slice(-1), +]; + +/** + * The live `T3GitHubIdentity` (spec section 12). It reports what each source answers and leaves + * judging it to `CredentialHygiene`; any source it can't read fails the whole probe. + */ +export const make = Effect.gen(function* () { + const vcs = yield* VcsProcess; + const fs = yield* FileSystem.FileSystem; + const path = yield* Path.Path; + const client = yield* HttpClient.HttpClient; + const t3Credential = yield* T3GitHubCredential; + const { ownerLogin } = yield* KinGitHubScope; + // ssh reads `~/.ssh` from the passwd entry while git and gh follow `$HOME`; `homeMatches` checks they agree. + const home = (yield* HostProcess.PasswdHomeDirectory) ?? ""; + const environment = yield* HostProcess.Environment; + const username = yield* HostProcess.Username; + const userId = yield* HostProcess.UserId; + + // From the filesystem root, outside any repository, so no repository config picks the helper. + const cwd = path.parse(path.resolve(home || "/")).root; + + const homeMatches = Effect.suspend(() => { + if (home === "") return Effect.fail(unreadable("home", "the passwd entry names no home")); + const env = environment.HOME; + return env === undefined || path.resolve(env) === path.resolve(home) + ? Effect.void + : Effect.fail(unreadable("home", "$HOME isn't the passwd entry's home")); + }); + const run = ( + input: Pick< + VcsProcessInput, + "operation" | "command" | "args" | "env" | "stdin" | "timeoutMs" | "concurrency" + >, + ) => + vcs.run({ + ...input, + cwd, + allowNonZeroExit: true, + maxOutputBytes: MAX_OUTPUT_BYTES, + outputMode: "error", + }); + + const loginOf = (source: string, token: Redacted.Redacted) => + gitHubJson({ + client, + operation: `identity.${source}.user`, + method: "GET", + path: "/user", + token, + schema: GitHubUser, + }).pipe( + Effect.timeoutOrElse({ + duration: "15 seconds", + orElse: () => Effect.fail(unreadable(source, "GET /user timed out")), + }), + // A refused credential reaches no one today, but its holder can't be named: fail closed. + Effect.mapError((error) => + error._tag === "T3GitHubIdentityError" ? error : unreadable(source, error.message), + ), + Effect.map((response) => ({ login: response.body.login.trim(), id: response.body.id })), + Effect.filterOrFail( + (user) => user.login !== "", + () => unreadable(source, "GET /user named no login"), + ), + ); + + /** `gh auth token`, which also reads keyring tokens that `gh auth status` no longer lists. */ + const ghToken = (account: string | undefined) => + run({ + operation: "KinGitHubIdentity.ghToken", + command: "gh", + args: [ + "auth", + "token", + "--hostname", + HOST, + ...(account === undefined ? [] : [`--user=${account}`]), + ], + env: GH_ENV, + timeoutMs: PROCESS_TIMEOUT_MS, + concurrency: { key: "gh", limit: 4 }, + }).pipe( + Effect.catch((error) => + isMissingCommand(error) ? Effect.succeed(null) : Effect.fail(unreadable("t3", error._tag)), + ), + Effect.flatMap((output) => { + if (output === null) return Effect.succeedNone; + if (output.exitCode !== 0) { + return GH_NO_TOKEN.test(output.stderr.trim()) + ? Effect.succeedNone + : Effect.fail(unreadable("t3", `gh auth token exited ${output.exitCode}`)); + } + const token = output.stdout.trim(); + return token === "" + ? Effect.fail(unreadable("t3", "gh auth token printed no token")) + : Effect.succeedSome(Redacted.make(token)); + }), + ); + + /** Every credential T3 holds, whichever it would pick and whether or not the host is enabled. */ + const t3 = Effect.fn("KinGitHubIdentity.t3")(function* (stored: T3StoredCredentials) { + const now = yield* Clock.currentTimeMillis; + const tokens: Array<{ readonly source: string; readonly token: Redacted.Redacted }> = + []; + if (Option.isSome(stored.saved)) tokens.push({ source: "settings", token: stored.saved.value }); + if (Option.isSome(stored.app)) { + const app = stored.app.value; + if (app.accessTokenExpiresAt === null || now < app.accessTokenExpiresAt) { + tokens.push({ source: "app", token: app.accessToken }); + } else if ( + app.refreshable && + (app.refreshTokenExpiresAt === null || now < app.refreshTokenExpiresAt) + ) { + // Refreshing here would spend the single-use refresh token T3 itself relies on. + return yield* unreadable("t3", "the app sign-in needs a refresh to name its login"); + } + } + const fromEnv = GH_TOKEN_NAMES.map((name) => environment[name]?.trim() ?? "").find( + (value) => value !== "", + ); + if (fromEnv !== undefined) tokens.push({ source: "env", token: Redacted.make(fromEnv) }); + // The active account, the one Settings picks, and the owner's, which a token there makes present. + const named = [...Option.toArray(stored.account), ownerLogin].filter( + (account, index, all) => + all.findIndex((other) => other.toLowerCase() === account.toLowerCase()) === index, + ); + for (const account of [undefined, ...named]) { + const token = yield* ghToken(account); + if (Option.isSome(token)) tokens.push({ source: "gh", token: token.value }); + } + return yield* Effect.forEach(tokens, ({ source, token }) => + loginOf("t3", token).pipe(Effect.map((user) => ({ ...user, source }))), + ); + }); + + // `--json` lists every account, active or not, and exits 0 whatever their state (gh 2.81+). + const ghAccounts = run({ + operation: "KinGitHubIdentity.gh", + command: "gh", + args: ["auth", "status", "--hostname", HOST, "--json", "hosts"], + env: GH_ENV, + timeoutMs: PROCESS_TIMEOUT_MS, + // The key `GitHubCredentials` uses, so Kin's gh runs share its throttle. + concurrency: { key: "gh", limit: 4 }, + }).pipe( + Effect.catch((error) => + isMissingCommand(error) ? Effect.succeed(null) : Effect.fail(unreadable("gh", error._tag)), + ), + Effect.flatMap((output) => { + if (output === null) return Effect.succeed>([]); + if (output.exitCode !== 0) { + return Effect.fail( + unreadable( + "gh", + /unknown flag: --json/.test(output.stderr) + ? "gh is older than 2.81" + : `gh auth status exited ${output.exitCode}`, + ), + ); + } + return Option.match(decodeGhAuthStatus(output.stdout), { + onNone: () => Effect.fail(unreadable("gh", "gh auth status --json was unreadable")), + onSome: (status) => { + const logins = Object.entries(status.hosts) + .filter(([host]) => host.trim().toLowerCase() === HOST) + .flatMap(([, accounts]) => accounts.map((account) => account.login.trim())); + // A login GitHub couldn't have issued could be anything; it never reaches the chat. + return logins.every((login) => GITHUB_LOGIN.test(login)) + ? Effect.succeed>(logins) + : Effect.fail(unreadable("gh", "an account isn't a GitHub login")); + }, + }); + }), + ); + + /** + * Every key git reads from `/`, checked against the allowlist: how many credential helpers it + * would call for github.com, or null when git isn't installed. + */ + const gitHelpers = run({ + operation: "KinGitHubIdentity.gitConfig", + command: "git", + args: ["config", "--list", "--show-origin", "-z"], + env: GIT_ENV, + timeoutMs: PROCESS_TIMEOUT_MS, + }).pipe( + Effect.catch((error) => + isMissingCommand(error) + ? Effect.succeed(null) + : Effect.fail(unreadable("git_credential", error._tag)), + ), + Effect.flatMap((output) => { + if (output === null) return Effect.succeed(null); + if (output.exitCode !== 0) { + return Effect.fail(unreadable("git_credential", `git config exited ${output.exitCode}`)); + } + const entries = parseGitConfigList(output.stdout); + if (entries === null) { + return Effect.fail(unreadable("git_credential", "git config --list was unreadable")); + } + const checked = checkGitConfig(entries); + return checked._tag === "refused" + ? Effect.fail(unreadable("git_credential", checked.reason)) + : Effect.succeed(checked.helpers); + }), + ); + + /** + * git's HTTPS transport sends `~/.netrc` (or `_netrc`) through curl with no helper configured, + * and `git credential fill` never sees it, so any such file fails. + */ + const netrcAbsent = Effect.forEach( + [".netrc", "_netrc"], + (name) => + fs.stat(path.join(home, name)).pipe( + Effect.flatMap(() => Effect.fail(unreadable("git_credential", "~/.netrc exists"))), + Effect.catch((error) => + error._tag === "T3GitHubIdentityError" + ? Effect.fail(error) + : isNotFound(error) + ? Effect.void + : Effect.fail(unreadable("git_credential", "~/.netrc can't be read")), + ), + ), + { discard: true }, + ); + + const fill = (username?: string) => + run({ + operation: "KinGitHubIdentity.gitCredential", + command: "git", + args: ["credential", "fill"], + stdin: `protocol=https\nhost=${HOST}\n${username === undefined ? "" : `username=${username}\n`}\n`, + env: GIT_ENV, + timeoutMs: PROCESS_TIMEOUT_MS, + }).pipe( + Effect.mapError((error) => unreadable("git_credential", error._tag)), + Effect.map((output) => { + const line = output.stdout.split("\n").find((entry) => entry.startsWith("password=")); + const password = line?.slice("password=".length).replace(/\r$/, "") ?? ""; + return { + exitCode: output.exitCode, + // git falls back to a terminal prompt, which is off, when no helper answers. + promptsOff: /terminal prompts disabled/i.test(output.stderr), + password: password === "" ? Option.none() : Option.some(Redacted.make(password)), + }; + }), + ); + + /** + * The login behind `git credential fill` for github.com, and which of `ownerLogins` the helper + * also holds a password for: one helper can keep several accounts, and answers for the one asked. + * The passwords stay in this pipeline: each is wrapped at once and only the first goes to GitHub. + */ + const gitCredential = Effect.fn("KinGitHubIdentity.gitCredential")(function* ( + ownerLogins: ReadonlyArray, + ) { + yield* netrcAbsent; + const helpers = yield* gitHelpers; + if (helpers === null) return { login: Option.none(), ownerHeld: [] }; + // `git credential fill` answers with the first helper's credential and hides the rest. + if (helpers > 1) { + return yield* unreadable("git_credential", "more than one credential helper is configured"); + } + const answer = yield* fill(); + let login = Option.none(); + if (answer.exitCode !== 0) { + // With a helper configured, prompts being off also covers a crashed, locked or refusing one. + if (!answer.promptsOff || helpers !== 0) { + return yield* unreadable("git_credential", `git credential fill exited ${answer.exitCode}`); + } + } else if (Option.isNone(answer.password)) { + return yield* unreadable("git_credential", "the helper answered without a password"); + } else { + login = Option.some(yield* loginOf("git_credential", answer.password.value)); + } + const ownerHeld: Array = []; + for (const username of ownerLogins) { + if (!GITHUB_LOGIN.test(username)) { + return yield* unreadable("git_credential", "an owner login isn't a GitHub login"); + } + const held = yield* fill(username); + // Asked for one account, prompts being off means the helper has none for it. + if (held.exitCode !== 0 && held.promptsOff) continue; + if (held.exitCode !== 0) { + return yield* unreadable("git_credential", `git credential fill exited ${held.exitCode}`); + } + if (Option.isNone(held.password)) { + return yield* unreadable("git_credential", "the helper answered without a password"); + } + ownerHeld.push(username); + } + return { login, ownerHeld }; + }); + + const repoConfig = (root: string, scope: "--local" | "--worktree") => + run({ + operation: "KinGitHubIdentity.repoConfig", + command: "git", + args: ["-C", root, "config", "--list", "--show-origin", "-z", scope], + env: GIT_ENV, + timeoutMs: PROCESS_TIMEOUT_MS, + }).pipe(Effect.mapError((error) => unreadable("git_repo", error._tag))); + + /** + * Each project's own git config, which agents working there read on top of the host's, checked + * against the repository allowlist. A folder that's gone or isn't a repository has none. + */ + const repositories = Effect.fn("KinGitHubIdentity.repositories")(function* ( + roots: ReadonlyArray, + ) { + if (roots.length > MAX_PROJECT_ROOTS) { + return yield* unreadable("git_repo", `more than ${MAX_PROJECT_ROOTS} projects`); + } + for (const root of roots) { + const info = yield* fs.stat(root).pipe( + Effect.asSome, + Effect.catch((error) => + isNotFound(error) + ? Effect.succeedNone + : Effect.fail(unreadable("git_repo", `${root} can't be read`)), + ), + ); + if (Option.isNone(info) || info.value.type !== "Directory") continue; + const scopes = ["--local", "--worktree"] as const; + for (const scope of scopes) { + const output = yield* repoConfig(root, scope); + if ( + scope === "--local" && + output.exitCode !== 0 && + /--local can only be used inside a git repository/.test(output.stderr) + ) { + break; + } + if (output.exitCode !== 0) { + return yield* unreadable("git_repo", `git config in ${root} exited ${output.exitCode}`); + } + const entries = parseGitConfigList(output.stdout); + if (entries === null) { + return yield* unreadable("git_repo", `git config in ${root} was unreadable`); + } + const checked = checkRepoGitConfig(entries); + if (checked._tag === "refused") { + return yield* unreadable("git_repo", `${root}: ${checked.reason}`); + } + if (!checked.worktreeConfig) break; + } + } + }); + + /** The files an `Include` pattern names, globbing its last path segment as ssh does. */ + const includeTargets = Effect.fn("KinGitHubIdentity.sshInclude")(function* ( + pattern: string, + base: string, + ) { + const tilde = pattern === "~" || pattern.startsWith("~/") ? home + pattern.slice(1) : pattern; + const full = path.isAbsolute(tilde) ? tilde : path.join(base, tilde); + const dir = path.dirname(full); + const name = path.basename(full); + if (tilde.startsWith("~") || /%|\$\{/.test(full) || GLOB.test(dir) || name.includes("[")) { + return yield* unreadable("ssh", "an Include names a path the probe can't expand"); + } + if (!GLOB.test(name)) return { glob: false, files: [full] }; + const matcher = new RegExp( + `^${name + .replace(/[.+^$(){}|\\\]]/g, "\\$&") + .replaceAll("*", ".*") + .replaceAll("?", ".")}$`, + ); + const names = yield* fs + .readDirectory(dir) + .pipe( + Effect.catch((error) => + isNotFound(error) + ? Effect.succeed>([]) + : Effect.fail(unreadable("ssh", "an Include's folder can't be listed")), + ), + ); + // Like glob(3), `*` doesn't match a leading dot. + const files = names + .filter((entry) => (name.startsWith(".") || !entry.startsWith(".")) && matcher.test(entry)) + .toSorted() + .map((entry) => path.join(dir, entry)); + return { glob: true, files }; + }); + + /** + * Reads one SSH config and every file it includes against the allowlist, and returns each + * `IdentityFile` it names under any `Host`, or null when the file doesn't exist. + */ + const readSshConfig = Effect.fn("KinGitHubIdentity.readSshConfig")(function* ( + top: string, + options: { + readonly system: boolean; + /** Where relative includes resolve: `~/.ssh` for the user's config, ssh's own for the system's. */ + readonly base: string; + readonly mustExist: boolean; + }, + ) { + const { system, base } = options; + const label = system ? "the system SSH config" : "~/.ssh/config"; + const identityFiles: Array = []; + const files: Array = []; + let read = 0; + const visit = Effect.fn("KinGitHubIdentity.readSshConfigFile")(function* ( + file: string, + mustExist: boolean, + ): Effect.fn.Return { + const info = yield* fs.stat(file).pipe( + Effect.asSome, + Effect.catch((error) => + isNotFound(error) + ? Effect.succeedNone + : Effect.fail(unreadable("ssh", `${label} can't be read`)), + ), + ); + // ssh skips a glob that matches nothing; a missing named file may be one ssh finds elsewhere. + if (Option.isNone(info)) { + return mustExist + ? yield* unreadable("ssh", `${label} names a file that doesn't exist`) + : false; + } + files.push(file); + read += 1; + // Each nested level reads a file, so this bounds the depth too. + if (read > MAX_SSH_CONFIG_FILES) { + return yield* unreadable("ssh", `${label} includes too many files`); + } + if (info.value.type !== "File" || Number(info.value.size) > MAX_SSH_CONFIG_BYTES) { + return yield* unreadable("ssh", `${label} includes something that isn't a small file`); + } + const text = yield* fs + .readFileString(file) + .pipe(Effect.mapError(() => unreadable("ssh", `${label} can't be read`))); + const directives = parseSshConfig(text); + if (directives === null) return yield* unreadable("ssh", `${label} can't be parsed`); + for (const directive of directives) { + const violation = sshViolation(directive, system); + if (violation !== undefined) return yield* unreadable("ssh", `${label} ${violation}`); + if (directive.keyword === "identityfile") { + for (const value of directive.args) { + if (value.toLowerCase() === "none") continue; + const expanded = expandIdentityFile(value, { + home, + username, + ...(userId === undefined ? {} : { userId }), + }); + if (expanded === undefined) { + return yield* unreadable( + "ssh", + `${label} has an IdentityFile the probe can't expand`, + ); + } + identityFiles.push(expanded); + } + } + if (directive.keyword === "include") { + for (const pattern of directive.args) { + const targets = yield* includeTargets(pattern, base); + for (const target of targets.files) yield* visit(target, !targets.glob); + } + } + } + return true; + }); + const exists = yield* visit(top, options.mustExist); + return { exists, identityFiles, files }; + }); + + const isRegularFile = (file: string) => + fs.stat(file).pipe( + Effect.map((info) => info.type === "File"), + Effect.catch((error) => + isNotFound(error) + ? Effect.succeed(false) + : Effect.fail(unreadable("ssh", "a key can't be read")), + ), + ); + + /** Regular files in `~/.ssh` and its folders, to `SSH_DIR_DEPTH` levels. */ + const sshDirFiles = Effect.gen(function* () { + const files: Array = []; + const walk = Effect.fn("KinGitHubIdentity.listSshDir")(function* ( + dir: string, + depth: number, + ): Effect.fn.Return { + const names = yield* fs + .readDirectory(dir) + .pipe( + Effect.catch((error) => + depth === 0 && isNotFound(error) + ? Effect.succeed>([]) + : Effect.fail(unreadable("ssh", "~/.ssh can't be listed")), + ), + ); + for (const name of names.toSorted()) { + const file = path.join(dir, name); + const info = yield* fs.stat(file).pipe( + Effect.asSome, + Effect.catch((error) => + isNotFound(error) + ? Effect.succeedNone + : Effect.fail(unreadable("ssh", "~/.ssh can't be listed")), + ), + ); + if (Option.isNone(info)) continue; + if (info.value.type === "Directory" && depth < SSH_DIR_DEPTH) { + yield* walk(file, depth + 1); + } else if (info.value.type === "File") { + files.push(file); + if (files.length > MAX_SSH_FILES) { + return yield* unreadable("ssh", `more than ${MAX_SSH_FILES} files in ~/.ssh`); + } + } + } + }); + yield* walk(path.join(home, ".ssh"), 0); + return files; + }); + + /** + * Whether `file` is a private key ssh can offer. `ssh-keygen -y -P ""` loads it as ssh does, + * with no passphrase; a file it can't load that still holds a private key fails closed. + */ + const isUsableKey = Effect.fn("KinGitHubIdentity.isUsableKey")(function* (file: string) { + if (!(yield* isRegularFile(file))) return false; + const loaded = yield* run({ + operation: "KinGitHubIdentity.sshKey", + command: "ssh-keygen", + args: ["-y", "-P", "", "-f", file], + env: SSH_ENV, + timeoutMs: PROCESS_TIMEOUT_MS, + }).pipe(Effect.mapError((error) => unreadable("ssh", error._tag))); + if (loaded.exitCode === 0) return true; + const size = yield* fs.stat(file).pipe( + Effect.map((info) => Number(info.size)), + Effect.mapError(() => unreadable("ssh", "a key can't be read")), + ); + if (size > MAX_KEY_FILE_BYTES) + return yield* unreadable("ssh", "a file in ~/.ssh is too large to check"); + const text = yield* fs + .readFileString(file) + .pipe(Effect.mapError(() => unreadable("ssh", "a key can't be read"))); + if (PRIVATE_KEY_MARKER.test(text) || /incorrect passphrase/.test(loaded.stderr)) { + return yield* unreadable("ssh", "a private key can't be loaded without a passphrase"); + } + return false; + }); + + const sshAnswer = (argv: ReadonlyArray) => + run({ + operation: "KinGitHubIdentity.ssh", + command: argv[0]!, + args: argv.slice(1), + env: SSH_ENV, + timeoutMs: SSH_TIMEOUT_MS, + }).pipe( + Effect.mapError((error) => unreadable("ssh", error._tag)), + Effect.map((output) => + [output.stdout, output.stderr].filter((text) => text !== "").join("\n"), + ), + ); + + /** + * The ssh agents run: the first on PATH, as an absolute path. Homebrew, MacPorts and Nix builds + * each read their own system config, so the probe asks this one which files it reads. + */ + const resolveSsh = Effect.gen(function* () { + for (const dir of (environment.PATH ?? "").split(":")) { + // An agent in another folder would find a different ssh. + if (!path.isAbsolute(dir)) return yield* unreadable("ssh", "PATH has a relative folder"); + const candidate = path.join(dir, "ssh"); + const info = yield* fs.stat(candidate).pipe(Effect.option); + if (Option.isSome(info) && info.value.type === "File" && (info.value.mode & 0o111) !== 0) { + return candidate; + } + } + return yield* unreadable("ssh", "ssh isn't on PATH"); + }); + + /** The files `ssh -v -G` reads, from its path lines only: its other output is never kept. */ + const sshConfigFiles = (ssh: string) => + run({ + operation: "KinGitHubIdentity.sshConfigFiles", + command: ssh, + args: ["-v", "-G", "git@github.com"], + env: SSH_ENV, + timeoutMs: PROCESS_TIMEOUT_MS, + }).pipe( + Effect.mapError((error) => unreadable("ssh", error._tag)), + Effect.flatMap((output) => { + if (output.exitCode !== 0) { + return Effect.fail(unreadable("ssh", `ssh -G exited ${output.exitCode}`)); + } + const files = sshConfigPaths(output.stderr); + // Every ssh reads a system config, so none listed means the output wasn't understood. + if (files.length === 0) return Effect.fail(unreadable("ssh", "ssh -G named no config")); + if (files.length > MAX_SSH_CONFIG_FILES) { + return Effect.fail(unreadable("ssh", "ssh -G named too many config files")); + } + return Effect.succeed(files); + }), + ); + + const ssh = Effect.gen(function* () { + const sshPath = yield* resolveSsh; + const userConfig = path.join(home, ".ssh", "config"); + const fromUser = yield* readSshConfig(userConfig, { + system: false, + base: path.join(home, ".ssh"), + mustExist: false, + }); + // Asked only after the user's config passed, since `-G` evaluates it. + const reported = yield* sshConfigFiles(sshPath); + const seen = new Set(fromUser.files); + const systemFiles = reported.filter((file) => !seen.has(file)); + const named = [...fromUser.identityFiles]; + for (const file of systemFiles) { + if (seen.has(file)) continue; + // Agents' ssh reads these after the user's config; the probe's `-F` skips them, so their + // keys go by `-i`. Relative includes resolve against the main system config's folder. + const fromSystem = yield* readSshConfig(file, { + system: true, + base: path.dirname(systemFiles[0]!), + mustExist: true, + }); + for (const read of fromSystem.files) seen.add(read); + named.push(...fromSystem.identityFiles); + } + const files = [...new Set([...named, ...(yield* sshDirFiles)])]; + const usable = yield* Effect.forEach(files, isUsableKey, { concurrency: SSH_CONCURRENCY }); + const keys = files.filter((_, index) => usable[index]); + if (keys.length > MAX_SSH_KEYS) { + return yield* unreadable("ssh", `more than ${MAX_SSH_KEYS} private keys`); + } + const config = fromUser.exists ? userConfig : "none"; + const plain = yield* sshAnswer(sshProbe(sshPath, config)); + const perKey = yield* Effect.forEach(keys, (key) => sshAnswer(sshProbe(sshPath, config, key)), { + concurrency: SSH_CONCURRENCY, + }); + return [plain, ...perKey]; + }); + + return T3GitHubIdentity.of({ + // T3's credentials first: until the server installs its reader, nothing is spawned. + identities: Effect.fn("KinGitHubIdentity.identities")(function* ({ ownerLogins }) { + yield* homeMatches; + const stored = yield* t3Credential.read.pipe( + Effect.mapError((error) => unreadable("t3", error.reason)), + ); + const t3Identities = yield* t3(stored); + const rest = yield* Effect.all( + { + ghAccounts, + git: gitCredential(ownerLogins), + repositories: repositories(stored.projectRoots), + ssh, + }, + { concurrency: "unbounded" }, + ); + return { + t3: t3Identities, + ghAccounts: rest.ghAccounts, + gitCredential: rest.git.login, + gitCredentialFor: rest.git.ownerHeld, + ssh: rest.ssh, + providerEnvironments: stored.providerEnvironments, + } satisfies T3GitHubIdentities; + }), + }); +}); + +export const layer = Layer.effect(T3GitHubIdentity, make); diff --git a/apps/server/src/kin/approvals/HostConfigAllowlist.ts b/apps/server/src/kin/approvals/HostConfigAllowlist.ts new file mode 100644 index 0000000000000..8d3c78d01e647 --- /dev/null +++ b/apps/server/src/kin/approvals/HostConfigAllowlist.ts @@ -0,0 +1,406 @@ +/** + * What the Kin host's SSH config, git config and environment may contain (spec section 12). + * Anything else fails the identity probe closed: listing every place a credential can hide can't + * be finished, so the probe proves the setup is minimal instead. Reasons name a directive, key or + * variable, never its value. + */ + +export interface SshDirective { + /** Lower case: ssh reads directive names case-insensitively. */ + readonly keyword: string; + readonly args: ReadonlyArray; +} + +const any = () => true; +const only = (value: string) => (args: ReadonlyArray) => + args.length === 1 && args[0] === value; + +/** Allowed directives, by lower-case name, with the arguments each may take. */ +const SSH_ALLOWED: Record) => boolean> = { + // Which host and key: every IdentityFile under every Host is probed, so an alias hides nothing. + host: any, + hostname: any, + user: any, + port: any, + identityfile: any, + identitiesonly: any, + // Host key checks decide whether ssh connects, not as whom. + stricthostkeychecking: any, + userknownhostsfile: any, + // The probe reads each included file against this same list. + include: any, + // Agents and shared connections answer with keys the probe can't list. + addkeystoagent: only("no"), + identityagent: only("none"), + controlmaster: only("no"), + controlpath: only("none"), + // Connection tuning. + serveraliveinterval: any, + serveralivecountmax: any, + connecttimeout: any, + compression: any, +}; + +/** + * What distro system configs ship, allowed in system files only. macOS: `SendEnv` and the crypto + * policy's algorithm lists; Debian and Ubuntu: `HashKnownHosts` and `GSSAPIAuthentication` + * (GitHub takes no GSSAPI login). Algorithm lists and forwarded locale variables add no identity. + */ +const SSH_SYSTEM_ALLOWED = new Set([ + "sendenv", + "ciphers", + "kexalgorithms", + "macs", + "hostkeyalgorithms", + "pubkeyacceptedalgorithms", + "casignaturealgorithms", + "hostbasedacceptedalgorithms", + "hashknownhosts", + "gssapiauthentication", +]); + +/** ssh_config's keywords, for naming one in a reason. Anything else could be a pasted secret. */ +const SSH_KEYWORDS = ` + AddKeysToAgent AddressFamily BatchMode BindAddress BindInterface CASignatureAlgorithms + CanonicalDomains CanonicalizeFallbackLocal CanonicalizeHostname CanonicalizeMaxDots + CanonicalizePermittedCNAMEs CertificateFile ChannelTimeout CheckHostIP Ciphers + ClearAllForwardings Compression ConnectTimeout ConnectionAttempts ControlMaster ControlPath + ControlPersist DynamicForward EnableEscapeCommandline EnableSSHKeysign EscapeChar + ExitOnForwardFailure FingerprintHash ForkAfterAuthentication ForwardAgent ForwardX11 + ForwardX11Timeout ForwardX11Trusted GSSAPIAuthentication GSSAPIDelegateCredentials + GatewayPorts GlobalKnownHostsFile HashKnownHosts Host HostKeyAlgorithms HostKeyAlias HostName + HostbasedAcceptedAlgorithms HostbasedAuthentication IPQoS IdentitiesOnly IdentityAgent + IdentityFile IgnoreUnknown Include KbdInteractiveAuthentication KbdInteractiveDevices + KexAlgorithms KnownHostsCommand LocalCommand LocalForward LogLevel LogVerbose MACs Match + NoHostAuthenticationForLocalhost NoHostAuthenticationForProxyCommand NumberOfPasswordPrompts + ObscureKeystrokeTiming PKCS11Provider PasswordAuthentication PermitLocalCommand + PermitRemoteOpen Port PreferredAuthentications ProxyCommand ProxyJump ProxyUseFdpass + PubkeyAcceptedAlgorithms PubkeyAuthentication RefuseConnection RekeyLimit RemoteCommand + RemoteForward RequestTTY RequiredRSASize RevokedHostKeys SecurityKeyProvider SendEnv + ServerAliveCountMax ServerAliveInterval SessionType SetEnv StdinNull StreamLocalBindMask + StreamLocalBindUnlink StrictHostKeyChecking SyslogFacility TCPKeepAlive Tag Tunnel + TunnelDevice UpdateHostKeys UseKeychain User UserKnownHostsFile VerifyHostKeyDNS + VersionAddendum VisualHostKey WarnWeakCrypto XAuthLocation +` + .split(/\s+/) + .filter((name) => name !== ""); +const sshName = (keyword: string) => + SSH_KEYWORDS.find((name) => name.toLowerCase() === keyword) ?? "an unrecognised directive"; + +/** + * A line's arguments: whitespace splits them, a double-quoted argument may hold spaces, and a `#` + * argument ends the line. Null on single quotes, backslashes, or a quote or `#` inside an + * argument, which ssh's `argv_split` reads in ways this doesn't follow. + */ +const splitSshArgs = (text: string): Array | null => { + const args: Array = []; + for (let index = 0; index < text.length;) { + if (/\s/.test(text[index]!)) { + index += 1; + continue; + } + if (text[index] === "#") break; + const quoted = text[index] === '"'; + const end = quoted ? text.indexOf('"', index + 1) : text.slice(index).search(/\s|$/) + index; + if (end === -1) return null; + const arg = quoted ? text.slice(index + 1, end) : text.slice(index, end); + if (/['\\]/.test(arg) || (!quoted && /["#]/.test(arg))) return null; + index = quoted ? end + 1 : end; + if (quoted && index < text.length && !/\s/.test(text[index]!)) return null; + args.push(arg); + } + return args; +}; + +/** + * Splits a config file as ssh's `readconf.c` does: `keyword args` or `keyword=args`, with + * `splitSshArgs` for the arguments. Null when a line can't be read that way. + */ +export const parseSshConfig = (text: string): ReadonlyArray | null => { + const directives: Array = []; + for (const raw of text.split("\n")) { + const line = raw.trim(); + if (line === "" || line.startsWith("#")) continue; + const match = /^([^\s="]+)(?:\s*=\s*|\s+|$)(.*)$/.exec(line); + if (match === null) return null; + const args = splitSshArgs(match[2]!); + if (args === null) return null; + directives.push({ keyword: match[1]!.toLowerCase(), args }); + } + return directives; +}; + +/** Why `directive` fails the allowlist, or undefined when it's allowed. */ +export const sshViolation = (directive: SshDirective, system: boolean): string | undefined => { + const allowed = SSH_ALLOWED[directive.keyword]; + if (allowed?.(directive.args) === true) return undefined; + if (allowed === undefined && system && SSH_SYSTEM_ALLOWED.has(directive.keyword)) + return undefined; + const name = sshName(directive.keyword); + return allowed === undefined + ? `sets ${name}` + : `sets ${name} to a value other than the allowed one`; +}; + +/** Pulls the files ssh read from `ssh -v -G` output, using only its `Reading configuration data` lines. */ +export const sshConfigPaths = (output: string): ReadonlyArray => + [...output.matchAll(/^debug1: Reading configuration data (\/.*)$/gm)].map((match) => match[1]!); + +/** + * Expands an `IdentityFile` as ssh would for the server's OS user. Only `~/`, `%d`, `%u`, `%i` and + * `%%` are allowed: host tokens, `${VAR}` and `~user` depend on more than this user. Undefined when + * the value uses anything else or stays relative, which ssh would resolve against the agent's cwd. + */ +export const expandIdentityFile = ( + value: string, + user: { readonly home: string; readonly username: string | null; readonly userId?: number }, +): string | undefined => { + if (value.includes("${")) return undefined; + const tokens: Record = { + "%": "%", + d: user.home, + u: user.username ?? undefined, + i: user.userId === undefined ? undefined : String(user.userId), + }; + let known = true; + const tilde = value === "~" || value.startsWith("~/") ? user.home + value.slice(1) : value; + const expanded = tilde.replace(/%(.?)/g, (_, token: string) => { + const replacement = tokens[token]; + if (replacement === undefined) known = false; + return replacement ?? ""; + }); + return known && expanded.startsWith("/") ? expanded : undefined; +}; + +export interface GitConfigEntry { + readonly origin: string; + /** As `git config --list` prints it: section and name lower case, the subsection as written. */ + readonly key: string; + /** Null for a bare boolean key such as `[core] bare`. */ + readonly value: string | null; +} + +/** Reads `git config --list --show-origin -z`: `origin NUL key LF value NUL`, or `key NUL` alone. */ +export const parseGitConfigList = (stdout: string): ReadonlyArray | null => { + const fields = stdout.split("\0"); + if (fields.at(-1) !== "") return null; + fields.pop(); + if (fields.length % 2 !== 0) return null; + const entries: Array = []; + for (let index = 0; index < fields.length; index += 2) { + const entry = fields[index + 1]!; + const newline = entry.indexOf("\n"); + entries.push({ + origin: fields[index]!, + key: newline === -1 ? entry : entry.slice(0, newline), + value: newline === -1 ? null : entry.slice(newline + 1), + }); + } + return entries; +}; + +const HELPER_KEYS = new Set(["credential.helper", "credential.https://github.com.helper"]); +/** + * Refused because each decides how git authenticates or reaches a server (`askPass`, `sshCommand`, + * `gitProxy`) or runs a program on ordinary commands the agent didn't ask for (`fsmonitor`, + * `hooksPath`), so a credential could be picked without the agent choosing one. + */ +const CORE_REFUSED = new Set([ + "core.askpass", + "core.sshcommand", + "core.gitproxy", + "core.fsmonitor", + "core.hookspath", +]); +/** + * Allowed keys in every scope git reads from `/`, matched from the start of the key. None picks a + * credential. Some run a program the agent asks for (`core.pager`, `diff.external`, + * `merge.*.driver`, `!` aliases, the LFS filter); they're allowed because the agent could run that + * program anyway, not because they run nothing. + */ +const GIT_ALLOWED: ReadonlyArray = [ + // Commit identity. + /^user\./, + // Local repository behaviour; the keys above are refused. + /^core\./, + // `init.templateDir` would copy its config and hooks into every new repository. + /^init\.defaultbranch$/, + /^color\./, + // Shorthands for commands the agent could run anyway. + /^alias\./, + // How git integrates and pushes, not which credential it uses. + /^(pull|push|fetch|merge|diff)\./, + // What `git lfs install` writes. + /^filter\.lfs\./, +]; + +const gitKeyName = (key: string) => { + const first = key.indexOf("."); + const last = key.lastIndexOf("."); + const section = key.slice(0, first); + const name = key.slice(last + 1); + // The subsection can hold a URL with a token in it, so it is never named. + const shown = first === last ? `${section}.${name}` : `${section}.*.${name}`; + return /^[a-z0-9-]{1,32}(\.\*)?\.[a-z0-9-]{1,32}$/.test(shown) ? shown : "an unrecognised key"; +}; + +type GitConfigVerdict = + | { readonly _tag: "refused"; readonly reason: string } + | ({ readonly _tag: "allowed" } & Allowed); + +/** The origin check every scope shares: files only, plus what `VcsProcess` sets on every run. */ +const originRefusal = ({ origin, key, value }: GitConfigEntry): string | null | undefined => { + if (origin === "command line:") { + // The check fails on any other `GIT_CONFIG_*` variable, so only `VcsProcess` sets these. + return key === "safe.barerepository" && value === "explicit" + ? null + : "git config is set from the environment"; + } + return origin.startsWith("file:") ? undefined : "git config comes from an unexpected source"; +}; + +/** + * Checks every entry git reads from `/`, in order: a forbidden key's reason, or how many credential + * helpers git would call for `https://github.com` (an empty value resets the list, as in git). + */ +export const checkGitConfig = ( + entries: ReadonlyArray, +): GitConfigVerdict<{ readonly helpers: number }> => { + let helpers = 0; + for (const entry of entries) { + const refusal = originRefusal(entry); + if (refusal === null) continue; + if (refusal !== undefined) return { _tag: "refused", reason: refusal }; + const { key, value } = entry; + if (HELPER_KEYS.has(key)) { + helpers = value === null || value.trim() === "" ? 0 : helpers + 1; + continue; + } + if (CORE_REFUSED.has(key) || !GIT_ALLOWED.some((pattern) => pattern.test(key))) { + return { _tag: "refused", reason: `git config sets ${gitKeyName(key)}` }; + } + } + return { _tag: "allowed", helpers }; +}; + +/** + * Whether a remote or submodule URL could carry a credential: any `::` remote helper + * (`ext::` runs a command, `fd::` uses an open socket), or userinfo on an HTTP or FTP URL, or a + * password on any other. `git@github.com:o/r` and `ssh://git@github.com/o/r` name only the ssh user. + */ +const urlCarriesCredential = (value: string | null) => { + if (value === null) return true; + const url = value.trim(); + if (/^[A-Za-z][A-Za-z0-9+.-]*::/.test(url)) return true; + const parsed = /^([A-Za-z][A-Za-z0-9+.-]*):\/\/([^/?#]*)/.exec(url); + if (parsed === null) return false; + const at = parsed[2]!.lastIndexOf("@"); + if (at === -1) return false; + return /^(https?|ftps?)$/i.test(parsed[1]!) || parsed[2]!.slice(0, at).includes(":"); +}; + +/** `remote..*` keys that only shape fetch and push: no program, helper or proxy. */ +const REMOTE_PLAIN = new Set([ + "fetch", + "push", + "mirror", + "skipdefaultupdate", + "skipfetchall", + "tagopt", + "prune", + "prunetags", + "promisor", + "partialclonefilter", +]); +const GIT_TRUE = /^(true|yes|on|1)$/i; +/** git takes a URL wherever it takes a remote's name, so these may hold only a name. */ +const REMOTE_NAME_KEYS = new Set(["remote", "pushremote"]); +const isRemoteName = (value: string | null) => value !== null && /^[A-Za-z0-9._-]+$/.test(value); + +/** + * Checks a repository's own config (`--local`, or `--worktree`) against the stricter repository + * allowlist: whether git reads a `config.worktree` too, or the first forbidden key's reason. + */ +export const checkRepoGitConfig = ( + entries: ReadonlyArray, +): GitConfigVerdict<{ readonly worktreeConfig: boolean }> => { + let worktreeConfig = false; + for (const entry of entries) { + const refusal = originRefusal(entry); + if (refusal === null) continue; + if (refusal !== undefined) return { _tag: "refused", reason: refusal }; + const { key, value } = entry; + const refused = { + _tag: "refused", + reason: `repository config sets ${gitKeyName(key)}`, + } as const; + const section = key.slice(0, key.indexOf(".")); + const name = key.slice(key.lastIndexOf(".") + 1); + const subsection = key.indexOf(".") !== key.lastIndexOf("."); + if (section === "core") { + if (CORE_REFUSED.has(key)) return refused; + } else if (key === "extensions.worktreeconfig") { + worktreeConfig = value === null || GIT_TRUE.test(value.trim()); + } else if (section === "remote" && !subsection) { + if (key !== "remote.pushdefault" || !isRemoteName(value)) return refused; + } else if (section === "remote") { + const url = name === "url" || name === "pushurl"; + if (url ? urlCarriesCredential(value) : !REMOTE_PLAIN.has(name)) return refused; + } else if (section === "branch" && subsection && REMOTE_NAME_KEYS.has(name)) { + if (!isRemoteName(value)) return refused; + } else if (section === "submodule" && subsection) { + if (name === "url" ? urlCarriesCredential(value) : name !== "active") return refused; + } else if ( + // A repository's own commit identity and storage format pick no credential. + section !== "branch" && + section !== "user" && + key !== "extensions.objectformat" && + key !== "extensions.refstorage" && + key !== "lfs.repositoryformatversion" + ) { + return refused; + } + } + return { _tag: "allowed", worktreeConfig }; +}; + +// Variables git, gh and ssh read, and tokens for anything: each can pick a credential. +const ENV_REFUSED = /^(GH|GITHUB|GIT|SSH)_|_TOKEN$/i; +/** + * Allowed despite the pattern. `GIT_TERMINAL_PROMPT` only turns git's prompt on or off. T3 sets the + * other two itself: they authenticate to T3, never to GitHub. + */ +const ENV_EXCEPTIONS = new Set([ + "GIT_TERMINAL_PROMPT", + "T3_MCP_BEARER_TOKEN", + "T3CODE_DEV_AUTH_TOKEN", +]); + +const shownEnvName = (name: string) => + /^[A-Za-z0-9_]{1,64}$/.test(name) ? name : "a variable with an unusual name"; + +/** The forbidden variables `env` sets to something, by name, sorted. Values are never read out. */ +export const forbiddenEnvNames = (env: Readonly>) => + Object.entries(env) + .filter( + ([name, value]) => + (value?.trim() ?? "") !== "" && + ENV_REFUSED.test(name) && + !ENV_EXCEPTIONS.has(name.toUpperCase()), + ) + .map(([name]) => shownEnvName(name)) + .toSorted(); + +/** + * Variables a provider instance may not change from the server's, since the probe ran with the + * server's: `PATH` picks another ssh, git or gh, `HOME` and `XDG_*` another git and gh config, and + * `BASH_ENV`, `ENV` and `ZDOTDIR` a shell startup file that can export a token. + */ +const PROVIDER_ENV_REFUSED = /^(PATH|HOME|XDG_.*|BASH_ENV|ENV|ZDOTDIR)$/i; + +/** Which of the variables a provider instance changes it may not, by name, sorted. */ +export const refusedProviderEnvChanges = (names: ReadonlyArray) => + names + .filter((name) => PROVIDER_ENV_REFUSED.test(name)) + .map(shownEnvName) + .toSorted(); diff --git a/apps/server/src/kin/approvals/T3GitHubCredential.test.ts b/apps/server/src/kin/approvals/T3GitHubCredential.test.ts new file mode 100644 index 0000000000000..4e287557206da --- /dev/null +++ b/apps/server/src/kin/approvals/T3GitHubCredential.test.ts @@ -0,0 +1,185 @@ +import { assert, describe, it } from "@effect/vitest"; +import { + DEFAULT_SERVER_SETTINGS, + ServerSettingsError, + type ServerSettings, +} from "@t3tools/contracts"; +import * as SourceControlHost from "@t3tools/source-control-core/server/SourceControlHost"; +import * as HostProcess from "@t3tools/shared/HostProcess"; +import * as Context from "effect/Context"; +import * as Effect from "effect/Effect"; +import * as Fiber from "effect/Fiber"; +import * as Layer from "effect/Layer"; +import * as Option from "effect/Option"; +import * as Redacted from "effect/Redacted"; +import * as Schema from "effect/Schema"; +import * as TestClock from "effect/testing/TestClock"; + +import { ProjectStoreV2 } from "../../orchestration-v2/ProjectStore.ts"; +import * as T3GitHubCredential from "./T3GitHubCredential.ts"; + +const session = (accessToken: string) => + JSON.stringify({ + accessToken, + accessTokenExpiresAt: 1_000, + refreshToken: "ghr_refresh", + refreshTokenExpiresAt: 2_000, + }); + +const isSettingsError = Schema.is(ServerSettingsError); + +/** The slot with the server's reader installed over Settings that read as `github`. */ +const slotOver = ( + github: unknown | ServerSettingsError, + rest: { + readonly providerInstances?: unknown; + readonly projectRoots?: ReadonlyArray | "unreadable"; + } = {}, +) => + Effect.gen(function* () { + const projects = ProjectStoreV2.of({ + list: () => + rest.projectRoots === "unreadable" + ? Effect.die("unused") + : Effect.succeed((rest.projectRoots ?? []).map((workspaceRoot) => ({ workspaceRoot }))), + } as unknown as ProjectStoreV2["Service"]); + const host = SourceControlHost.SourceControlHost.of({ + settings: { + getWithSecrets: () => + isSettingsError(github) + ? Effect.fail(github) + : Effect.succeed({ + ...DEFAULT_SERVER_SETTINGS, + ...(rest.providerInstances === undefined + ? {} + : { providerInstances: rest.providerInstances }), + sourceControlHosts: { github }, + } as unknown as ServerSettings), + }, + } as unknown as SourceControlHost.SourceControlHost["Service"]); + const context = yield* Layer.build( + T3GitHubCredential.installLayer.pipe( + Layer.provideMerge(T3GitHubCredential.slotLayer), + Layer.provide(Layer.succeed(SourceControlHost.SourceControlHost, host)), + Layer.provide(Layer.succeed(ProjectStoreV2, projects)), + ), + ); + return Context.get(context, T3GitHubCredential.T3GitHubCredential); + }); + +describe("T3GitHubCredential", () => { + it.effect("reads every stored credential, without refreshing, even with the host disabled", () => + Effect.gen(function* () { + const slot = yield* slotOver({ + hosts: { "github.com": { enabled: false, account: "kin-bot" } }, + tokens: { "github.com": " ghp_saved " }, + signIns: { "github.com": session("ghu_app") }, + }); + const stored = yield* slot.read; + assert.strictEqual( + Option.map(stored.saved, Redacted.value).pipe(Option.getOrNull), + "ghp_saved", + ); + assert.deepStrictEqual( + Option.map(stored.app, (app) => ({ ...app, accessToken: Redacted.value(app.accessToken) })), + Option.some({ + accessToken: "ghu_app", + accessTokenExpiresAt: 1_000, + refreshable: true, + refreshTokenExpiresAt: 2_000, + }), + ); + assert.deepStrictEqual(stored.account, Option.some("kin-bot")); + for (const empty of [{}, { signIns: { "github.com": "" } }]) { + assert.deepInclude(yield* (yield* slotOver(empty)).read, { + saved: Option.none(), + app: Option.none(), + account: Option.none(), + }); + } + }).pipe(Effect.scoped), + ); + + it.effect("reads what each provider instance's environment sets, and every project root", () => + Effect.gen(function* () { + const slot = yield* slotOver( + {}, + { + providerInstances: { + codex: { + driver: "codex", + environment: [ + { name: "GITHUB_PERSONAL_ACCESS_TOKEN", value: "ghp_secret_value" }, + { name: "SSH_AUTH_SOCK", value: { provider: "vault", reference: "ssh#sock" } }, + // Blank clears what the server's environment sets. + { name: "HOME_TOOL", value: " " }, + ], + }, + claude: { driver: "claudeAgent", enabled: false }, + }, + projectRoots: ["/work/app", "/work/site"], + }, + ).pipe(Effect.provideService(HostProcess.Environment, { PATH: "/bin", HOME_TOOL: "x" })); + const stored = yield* slot.read; + assert.deepStrictEqual(stored.providerEnvironments, [ + { instance: "codex", names: ["GITHUB_PERSONAL_ACCESS_TOKEN", "SSH_AUTH_SOCK"] }, + // Disabled instances count: the terminal opens any instance's environment. This one + // changes nothing the server's environment doesn't already set. + { instance: "claude", names: [] }, + ]); + assert.deepStrictEqual(stored.projectRoots, ["/work/app", "/work/site"]); + assert.notInclude(JSON.stringify(stored.providerEnvironments), "ghp_secret_value"); + }).pipe(Effect.scoped), + ); + + it.effect.each([ + [ + "Settings can't be read", + new ServerSettingsError({ settingsPath: "s", operation: "read-file" }), + ], + ["the GitHub settings don't decode", { tokens: { "github.com": 5 } }], + [ + "the saved token names a secret that can't be resolved", + { tokens: { "github.com": { provider: "vault", reference: "gh#token" } } }, + ], + ["the app sign-in doesn't decode", { signIns: { "github.com": "{not json" } }], + ] as const)("fails closed when %s", ([, github]) => + Effect.gen(function* () { + const slot = yield* slotOver(github); + const error = yield* slot.read.pipe(Effect.flip); + assert.strictEqual(error._tag, "T3GitHubCredentialError"); + }).pipe(Effect.scoped), + ); + + it.effect("waits for the server to install the reader, then fails closed", () => + Effect.gen(function* () { + const slot = Context.get( + yield* Layer.build(T3GitHubCredential.slotLayer), + T3GitHubCredential.T3GitHubCredential, + ); + const empty = { + saved: Option.none(), + app: Option.none(), + account: Option.none(), + providerEnvironments: [], + projectRoots: [], + } satisfies T3GitHubCredential.T3StoredCredentials; + // A read at startup, before the server installs the reader, gets the reader's answer. + const early = yield* Effect.forkChild(slot.read); + yield* TestClock.adjust("10 seconds"); + yield* slot.install(Effect.succeed(empty)); + assert.deepStrictEqual(yield* Fiber.join(early), empty); + // Installed once: a second reader is ignored. + yield* slot.install(Effect.die("second reader")); + assert.deepStrictEqual(yield* slot.read, empty); + + const never = Context.get( + yield* Layer.build(T3GitHubCredential.slotLayer), + T3GitHubCredential.T3GitHubCredential, + ); + const waiting = yield* Effect.forkChild(Effect.flip(never.read)); + yield* TestClock.adjust(T3GitHubCredential.INSTALL_TIMEOUT); + assert.strictEqual((yield* Fiber.join(waiting)).reason, "the reader isn't installed"); + }).pipe(Effect.scoped), + ); +}); diff --git a/apps/server/src/kin/approvals/T3GitHubCredential.ts b/apps/server/src/kin/approvals/T3GitHubCredential.ts new file mode 100644 index 0000000000000..530dc7da3c688 --- /dev/null +++ b/apps/server/src/kin/approvals/T3GitHubCredential.ts @@ -0,0 +1,177 @@ +import { requireSettingsSecretLiteral } from "@t3tools/contracts"; +import { mergeProviderInstanceEnvironment } from "@t3tools/provider-core/server/instanceEnvironment"; +import * as HostProcess from "@t3tools/shared/HostProcess"; +import * as SourceControlHost from "@t3tools/source-control-core/server/SourceControlHost"; +import type { HostCredentialSettings } from "@t3tools/source-control-core/shared/definition"; +import { GitHubAppSession } from "@t3tools/source-control-github/server/driver"; +import * as GitHubClient from "@t3tools/source-control-github/shared/definition"; +import * as Context from "effect/Context"; +import * as Deferred from "effect/Deferred"; +import * as Effect from "effect/Effect"; +import * as Layer from "effect/Layer"; +import * as Option from "effect/Option"; +import * as Redacted from "effect/Redacted"; +import * as Schema from "effect/Schema"; + +import { ProjectStoreV2 } from "../../orchestration-v2/ProjectStore.ts"; + +const HOST = "github.com"; +/** How long a read waits for the server to install the reader before failing closed. */ +export const INSTALL_TIMEOUT = "30 seconds"; + +export class T3GitHubCredentialError extends Schema.TaggedError()( + "T3GitHubCredentialError", + // A fixed reason, never the credential or anything GitHub answered. + { reason: Schema.String }, +) { + override get message(): string { + return `Could not read T3's GitHub credential (${this.reason}).`; + } +} + +/** + * Every GitHub credential T3 keeps for github.com, as stored: nothing is refreshed, and the host + * being disabled in Settings changes nothing, since the stored tokens are still on disk. + */ +export interface T3StoredCredentials { + /** The token saved in Settings. */ + readonly saved: Option.Option>; + /** The app sign-in session. */ + readonly app: Option.Option<{ + readonly accessToken: Redacted.Redacted; + readonly accessTokenExpiresAt: number | null; + /** Whether the session holds a refresh token, and until when it can be spent. */ + readonly refreshable: boolean; + readonly refreshTokenExpiresAt: number | null; + }>; + /** The gh account Settings picks for T3's personal credential. */ + readonly account: Option.Option; + /** + * The variables each provider instance's environment adds to or changes from the server's, as + * `mergeProviderInstanceEnvironment` builds it, by name (a secret reference counts as set). + * Agents and the terminal, which opens any instance's environment, enabled or not, inherit these. + */ + readonly providerEnvironments: ReadonlyArray<{ + readonly instance: string; + readonly names: ReadonlyArray; + }>; + /** Every project's workspace root, where agents run git with that repository's own config. */ + readonly projectRoots: ReadonlyArray; +} +type ReadT3Credential = Effect.Effect; + +/** + * T3's stored GitHub credentials. The executor is built before the server's source control host, + * so the server installs the reader later (`install`, once); a read waits up to + * `INSTALL_TIMEOUT` for it, then fails, and so does the credential check. + */ +export class T3GitHubCredential extends Context.Service< + T3GitHubCredential, + { + readonly read: ReadT3Credential; + /** Installs the reader; any later call is ignored. */ + readonly install: (read: ReadT3Credential) => Effect.Effect; + } +>()("t3/kin/approvals/T3GitHubCredential") {} + +export const slotLayer = Layer.effect( + T3GitHubCredential, + Effect.gen(function* () { + const installed = yield* Deferred.make(); + return T3GitHubCredential.of({ + read: Deferred.await(installed).pipe( + Effect.timeoutOrElse({ + duration: INSTALL_TIMEOUT, + orElse: () => + Effect.fail(new T3GitHubCredentialError({ reason: "the reader isn't installed" })), + }), + Effect.flatten, + ), + install: Effect.fn("T3GitHubCredential.install")(function* (read: ReadT3Credential) { + yield* Deferred.succeed(installed, read); + }), + }); + }), +); + +const decodeSettings = Schema.decodeUnknownOption(GitHubClient.settings); +const decodeSession = Schema.decodeUnknownOption(Schema.fromJsonString(GitHubAppSession)); + +/** Reads Settings directly: the request policy would refresh the app session and spend its token. */ +const readStored = Effect.fn("T3GitHubCredential.read")(function* ( + host: SourceControlHost.SourceControlHost["Service"], + projects: ProjectStoreV2["Service"], + serverEnv: NodeJS.ProcessEnv, +) { + const fail = (reason: string) => Effect.fail(new T3GitHubCredentialError({ reason })); + const settings = yield* host.settings + .getWithSecrets(GitHubClient.definition.kind) + .pipe(Effect.catch(() => fail("Settings can't be read"))); + const raw = settings.sourceControlHosts[GitHubClient.definition.kind]; + // Settings that don't decode read as empty elsewhere; here that would hide a token. + const github = decodeSettings(raw ?? {}); + if (Option.isNone(github)) return yield* fail("the GitHub settings can't be decoded"); + // Every credential field upstream keeps: a new one fails to compile here until it is read. + const { tokens, signIns, hosts } = { + tokens: github.value.tokens, + signIns: github.value.signIns, + hosts: github.value.hosts, + } satisfies Record; + const token = yield* requireSettingsSecretLiteral(tokens[HOST], settings.secretProviders).pipe( + Effect.catch(() => fail("the saved token's secret can't be resolved")), + ); + // An empty sign-in is signed out, as upstream reads it. + const signIn = signIns[HOST]?.trim() ?? ""; + const session = signIn === "" ? Option.none() : decodeSession(signIn); + if (signIn !== "" && Option.isNone(session)) { + return yield* fail("the app sign-in can't be decoded"); + } + // Built-in instances without an entry, and project overrides, set no variables. + const providerEnvironments = yield* Effect.forEach( + Object.entries(settings.providerInstances), + ([instance, config]) => + mergeProviderInstanceEnvironment( + (config.environment ?? []).map((variable) => + typeof variable.value === "string" ? variable : { ...variable, value: "secret" }, + ), + serverEnv, + ).pipe( + Effect.map((env) => ({ + instance, + // Only what the instance adds or changes: the server's own environment is checked as is. + names: Object.keys(env).filter( + (name) => (env[name]?.trim() ?? "") !== "" && env[name] !== serverEnv[name], + ), + })), + ), + ); + const projectRoots = yield* projects.list().pipe( + Effect.map((rows) => rows.map((row) => row.workspaceRoot)), + Effect.catch(() => fail("the project list can't be read")), + ); + return { + saved: token.trim() === "" ? Option.none() : Option.some(Redacted.make(token.trim())), + app: Option.map(session, (value) => ({ + accessToken: Redacted.make(value.accessToken), + accessTokenExpiresAt: value.accessTokenExpiresAt, + refreshable: value.refreshToken !== null && value.refreshToken !== "", + refreshTokenExpiresAt: value.refreshTokenExpiresAt, + })), + account: Option.fromNullishOr(hosts[HOST]?.account), + providerEnvironments, + projectRoots, + } satisfies T3StoredCredentials; +}); + +export const installLayer = Layer.effectDiscard( + Effect.gen(function* () { + const slot = yield* T3GitHubCredential; + yield* slot.install( + readStored( + yield* SourceControlHost.SourceControlHost, + yield* ProjectStoreV2, + yield* HostProcess.Environment, + ), + ); + }), +); diff --git a/apps/server/src/kin/approvals/layer.ts b/apps/server/src/kin/approvals/layer.ts index 2b7afba8b248f..4b2831ce72e17 100644 --- a/apps/server/src/kin/approvals/layer.ts +++ b/apps/server/src/kin/approvals/layer.ts @@ -5,6 +5,7 @@ import * as Effect from "effect/Effect"; import * as Layer from "effect/Layer"; import * as FetchHttpClient from "effect/http/FetchHttpClient"; +import * as VcsProcess from "../../vcs/VcsProcess.ts"; import * as Keychain from "../github/Keychain.ts"; import * as KinBotReads from "../github/KinBotReads.ts"; import * as KinGitHubApp from "../github/KinGitHubApp.ts"; @@ -17,6 +18,9 @@ import * as ApproverKeys from "./ApproverKeys.ts"; import * as Audit from "./Audit.ts"; import * as AttestationTrust from "./attestation/AttestationTrust.ts"; import * as CredentialHygiene from "./CredentialHygiene.ts"; +import type { CredentialHygieneReport } from "./CredentialHygieneReport.ts"; +import * as GitHubIdentityProbe from "./GitHubIdentityProbe.ts"; +import * as T3GitHubCredential from "./T3GitHubCredential.ts"; import * as GrantPorts from "./GrantPorts.ts"; import * as GrantVerifier from "./GrantVerifier.ts"; import { makeDeleteBranchHandler } from "./handlers/deleteBranch.ts"; @@ -69,25 +73,38 @@ const githubPlatform = Layer.mergeAll( ); type GitHubPlatform = Layer.Success; +// Its process runner and filesystem are the server's, by layer memoization. +const liveIdentity = GitHubIdentityProbe.layer.pipe( + Layer.provide(VcsProcess.layer), + Layer.provide(NodeServices.layer), +); + // The bot App reads, the owner merge token writes, and the hygiene check gates both. -const github = (platform: Layer.Layer) => +const github = ( + platform: Layer.Layer, + identity: Layer.Layer, +) => Layer.mergeAll(KinBotReads.layer, CredentialHygiene.layer).pipe( Layer.provideMerge(OwnerMergeClient.layer), - Layer.provide(CredentialHygiene.identityUnavailableLayer), + Layer.provide(identity), Layer.provide(KinGitHubApp.layer), Layer.provideMerge(platform), ); -const handlers = (platform: Layer.Layer) => +const handlers = ( + platform: Layer.Layer, + identity: Layer.Layer, +) => Layer.effect( ActionHandlers.KinActionHandlers, Effect.all([setLevelHandler, makeMergePullRequestHandler, makeDeleteBranchHandler]).pipe( Effect.map(ActionHandlers.make), ), - ).pipe(Layer.provideMerge(github(platform))); + ).pipe(Layer.provideMerge(github(platform, identity))); // Without the GitHub App and scope configured, the GitHub kinds have no handler but stay held // until their grants lapse, and the hygiene check can't pass, so only set_level runs. +const UNCONFIGURED = "the GitHub App and scope aren't configured"; const unconfigured = Layer.mergeAll( Layer.effect( ActionHandlers.KinActionHandlers, @@ -99,9 +116,10 @@ const unconfigured = Layer.mergeAll( CredentialHygiene.CredentialHygiene, CredentialHygiene.CredentialHygiene.of({ check: Effect.fn("CredentialHygiene.unconfigured")(function* () { - return yield* Effect.succeed({ + return yield* Effect.succeed({ passed: false, failures: ["identity_unknown", "merge_token_missing"], + reasons: [UNCONFIGURED], warnings: [], envTokenNames: [], t3Login: null, @@ -112,6 +130,7 @@ const unconfigured = Layer.mergeAll( requirePassing: Effect.fn("CredentialHygiene.unconfiguredRequirePassing")(function* () { return yield* new CredentialHygiene.CredentialHygieneFailedError({ failures: ["identity_unknown", "merge_token_missing"], + reasons: [UNCONFIGURED], }); }), }), @@ -123,13 +142,16 @@ const unconfigured = Layer.mergeAll( * anything here would reach every service holding `kin.services`. Built on top of the layer that * supplies `KinPendingActions`, so it takes the very approvals that instance publishes. */ -export const makeExecutorLayer = (platform: Layer.Layer) => +export const makeExecutorLayer = ( + platform: Layer.Layer, + identity: Layer.Layer, +) => Layer.empty.pipe( Layer.provide( ActionExecutor.layer.pipe( Layer.provide( Layer.merge( - handlers(platform).pipe( + handlers(platform, identity).pipe( Layer.catchCause((cause) => unconfigured.pipe( Layer.tap(() => @@ -151,12 +173,14 @@ export const makeExecutorLayer = (platform: Layer.Layer string> = + { + env_token: (report) => + report.envTokenNames.length > 0 + ? `${report.envTokenNames.join(", ")} ${report.envTokenNames.length === 1 ? "is" : "are"} set in the server's environment` + : "an environment agents inherit can hand them a GitHub credential", + owner_identity: () => "a GitHub login on this machine is yours", + non_bot_identity: () => "a GitHub login on this machine isn't the bot's", + identity_unknown: () => "Kin couldn't read every GitHub login on this machine", + merge_token_missing: () => "no merge token is saved", + merge_token_expired: () => "the merge token has expired", + }; + +/** Plain text for the owner's chat when the credential check's result changes (spec section 12). */ +export const credentialCheckBody = (orgId: KinOrgId, report: CredentialHygieneReport) => { + if (report.passed) { + return report.warnings.includes("merge_token_expiring") + ? `Your merge token expires within 7 days. Save a new one with \`node scripts/kin-github-app.ts --merge-token --kin-org ${orgId}\` on the Kin host.` + : "The credential check passes again, so approved actions can run."; + } + const failures = report.failures.map((failure) => FAILURE_TEXT[failure](report)); + const logins = report.identities.map(({ source, login }) => `${login} (${source})`); + return [ + `Approved actions won't run until the credential check passes: ${failures.join("; ")}.`, + ...(report.reasons.length > 0 ? [`Why: ${report.reasons.join("; ")}.`] : []), + ...(logins.length > 0 ? [`GitHub logins found: ${logins.join(", ")}.`] : []), + "Kin checks again before each action and every hour.", + ].join("\n"); +}; + const holdText = (holdReason: string) => { - const [reason, failures] = holdReason.split(": "); + const split = holdReason.indexOf(": "); + const reason = split === -1 ? holdReason : holdReason.slice(0, split); + const failures = split === -1 ? undefined : holdReason.slice(split + 2); switch (reason) { case "paused": return "Kin was paused"; @@ -76,6 +113,11 @@ export const layer = Layer.effect( Effect.gen(function* () { const chat = yield* KinChatService; const sql = yield* SqlClient.SqlClient; + const ownerChat = (orgId: KinOrgId) => + sql<{ readonly chatId: string }>` + SELECT l.owner_chat_id AS "chatId" FROM kin_leads l + JOIN kin_agents a ON a.org_id = l.org_id AND a.agent_id = l.agent_id + WHERE l.org_id = ${orgId} AND a.retired_at IS NULL ORDER BY l.agent_id LIMIT 1`; /** * To the owner's chat with the lead, saying who approved when it wasn't him, and to the chat * the request came from when that is a co-owner's own, so whoever asked hears the result. @@ -120,6 +162,31 @@ export const layer = Layer.effect( }); }); return KinExecutionNotices.of({ + credentialCheck: Effect.fn("KinExecutionNotices.credentialCheck")( + function* ({ orgId, report, recoveredFrom }) { + const [lead] = yield* ownerChat(orgId); + if (lead === undefined) { + yield* Effect.logWarning("Kin credential check changed with no owner chat", { + orgId, + passed: report.passed, + failures: report.failures.join(","), + }); + return; + } + yield* chat.notice({ + orgId, + chatId: KinChatId.make(lead.chatId), + messageId: KinMessageId.make( + recoveredFrom === undefined + ? `kin-hygiene:${orgId}:${report.checkedAt}` + : `kin-hygiene:${orgId}:recovered:${recoveredFrom}`, + ), + audience: "owner", + body: credentialCheckBody(orgId, report), + }); + }, + Effect.catchCause((cause) => Effect.logWarning("Kin credential notice not posted", cause)), + ), finished: Effect.fn("KinExecutionNotices.finished")( function* (input) { yield* post(input.action, executionNoticeBody(input)); diff --git a/apps/server/src/kin/github/KinGitHubScope.ts b/apps/server/src/kin/github/KinGitHubScope.ts index f1c94abce9a13..211f6edb1731d 100644 --- a/apps/server/src/kin/github/KinGitHubScope.ts +++ b/apps/server/src/kin/github/KinGitHubScope.ts @@ -2,6 +2,7 @@ import { RepoSlug } from "@t3tools/contracts"; import * as Config from "effect/Config"; import * as Context from "effect/Context"; import * as Layer from "effect/Layer"; +import type * as Option from "effect/Option"; import * as Schema from "effect/Schema"; const GitHubLogin = Schema.String.check( @@ -12,6 +13,8 @@ const GitHubLogin = Schema.String.check( ), ); +const GitHubUserId = Schema.Int.check(Schema.isGreaterThan(0)); + /** The owner's personal GitHub login; also read alone by the App setup command. */ export const KinGitHubOwnerLogin = Config.schema(GitHubLogin, "KIN_GITHUB_OWNER_LOGIN"); @@ -23,6 +26,11 @@ export class KinGitHubScope extends Context.Service< readonly ownerLogin: string; /** The only login T3 and the agent CLIs it spawns may use on github.com. */ readonly botLogin: string; + /** + * The bot account's numeric id, which a rename can't reuse. Token-backed identities must match + * it; while it's unset the credential check fails closed. + */ + readonly botId: Option.Option; /** The repositories Kin works on. Tokens are minted and probed for these only. */ readonly repos: ReadonlyArray; } @@ -32,6 +40,7 @@ export class KinGitHubScope extends Context.Service< Config.all({ ownerLogin: KinGitHubOwnerLogin, botLogin: Config.schema(GitHubLogin, "KIN_GITHUB_BOT_LOGIN"), + botId: Config.option(Config.schema(GitHubUserId, "KIN_GITHUB_BOT_ID")), // Comma-separated owner/name slugs. Empty means no repository is in scope. repos: Config.Array(RepoSlug, "KIN_GITHUB_REPOS"), }), diff --git a/apps/server/src/kin/github/OwnerMergeClient.test.ts b/apps/server/src/kin/github/OwnerMergeClient.test.ts index 8ca9486aacfca..20f1a992dc1ce 100644 --- a/apps/server/src/kin/github/OwnerMergeClient.test.ts +++ b/apps/server/src/kin/github/OwnerMergeClient.test.ts @@ -1,7 +1,7 @@ import { assert, it } from "@effect/vitest"; import type { KinOrgId } from "@t3tools/contracts"; -import * as ConfigProvider from "effect/ConfigProvider"; import * as DateTime from "effect/DateTime"; +import * as HostProcess from "@t3tools/shared/HostProcess"; import * as Effect from "effect/Effect"; import * as Exit from "effect/Exit"; import * as Fiber from "effect/Fiber"; @@ -81,15 +81,18 @@ const withHygiene = (effect: Effect.Effect) => CredentialHygiene.layer.pipe( Layer.provide( Layer.succeed(CredentialHygiene.T3GitHubIdentity, { - identities: Effect.succeed({ - t3: Option.none(), - ghAccounts: [], - gitCredential: Option.none(), - ssh: ["git@github.com: Permission denied (publickey)."], - }), + identities: () => + Effect.succeed({ + t3: [], + ghAccounts: [], + gitCredential: Option.none(), + gitCredentialFor: [], + ssh: ["git@github.com: Permission denied (publickey)."], + providerEnvironments: [], + }), }), ), - Layer.provide(ConfigProvider.layer(ConfigProvider.fromEnv({ env: {} }))), + Layer.provide(Layer.succeed(HostProcess.Environment, {})), Layer.provide(scopeLayer({ repos: REPOS })), ), ), diff --git a/apps/server/src/kin/github/testFakes.ts b/apps/server/src/kin/github/testFakes.ts index 30d622622e5a7..a24bfef01bb3d 100644 --- a/apps/server/src/kin/github/testFakes.ts +++ b/apps/server/src/kin/github/testFakes.ts @@ -9,10 +9,15 @@ import type { RepoSlug } from "@t3tools/contracts"; import * as Keychain from "./Keychain.ts"; import { KinGitHubScope } from "./KinGitHubScope.ts"; +/** The fake bot account's numeric id, which `scopeLayer` sets as `KIN_GITHUB_BOT_ID`. */ +export const BOT_ID = 4242; + export const scopeLayer = ( scope: { readonly ownerLogin?: string; readonly botLogin?: string; + /** Null leaves `KIN_GITHUB_BOT_ID` unset. */ + readonly botId?: number | null; readonly repos?: ReadonlyArray; } = {}, ) => @@ -21,6 +26,7 @@ export const scopeLayer = ( KinGitHubScope.of({ ownerLogin: scope.ownerLogin ?? "Nishchay", botLogin: scope.botLogin ?? "kin-bot", + botId: Option.fromNullishOr(scope.botId === undefined ? BOT_ID : scope.botId), repos: (scope.repos ?? ["GLDRoger/t3code"]) as ReadonlyArray, }), ); diff --git a/apps/server/src/kin/layer.ts b/apps/server/src/kin/layer.ts index 3c1e51d738dc4..d66a5a8f2a9d6 100644 --- a/apps/server/src/kin/layer.ts +++ b/apps/server/src/kin/layer.ts @@ -3,6 +3,7 @@ import { KinChatRepository } from "./chat/KinChatRepository.ts"; import type * as SqlClient from "effect/sql/SqlClient"; import * as TaskCardUpdater from "./chat/TaskCardUpdater.ts"; import * as Cause from "effect/Cause"; +import * as Context from "effect/Context"; import * as KinEnabled from "./KinEnabled.ts"; import * as Effect from "effect/Effect"; import * as Layer from "effect/Layer"; @@ -13,6 +14,7 @@ import * as KinAttachmentStore from "./chat/KinAttachmentStore.ts"; import * as KinApprovals from "./approvals/layer.ts"; import * as RetiredActions from "./approvals/retiredActions.ts"; import * as KeySeal from "./approvals/KeySeal.ts"; +import * as T3GitHubCredential from "./approvals/T3GitHubCredential.ts"; import * as ApprovalCards from "./chat/approvalCards.ts"; import * as ExecutionNotices from "./chat/executionNotices.ts"; import * as KinPush from "./push/layer.ts"; @@ -50,6 +52,7 @@ const enabled = (seal: Layer.Layer) => KinServer, Effect.gen(function* () { const services = yield* Effect.context(); + const slot = yield* T3GitHubCredential.T3GitHubCredential; yield* requireRefreshObserver(services); const { owner } = yield* KinIdentity; // A failed seed leaves Kin running; the next start tries the missing ones again. @@ -74,7 +77,11 @@ const enabled = (seal: Layer.Layer) => taskCardUpdater: TaskCardUpdater.layer.pipe( Layer.provide(Layer.succeedContext(updaterServices)), ), - services: Option.some(services), + t3GitHubCredential: T3GitHubCredential.installLayer.pipe( + Layer.provide(Layer.succeed(T3GitHubCredential.T3GitHubCredential, slot)), + ), + // Context lookups go by key, so the slot is dropped, not just left out of the type. + services: Option.some(Context.omit(T3GitHubCredential.T3GitHubCredential)(services)), subscribeForViewer: Option.some(hub.subscribeForViewer), }; }), @@ -124,6 +131,7 @@ const enabled = (seal: Layer.Layer) => ); const disabled = Layer.succeed(KinServer, { taskCardUpdater: Layer.empty, + t3GitHubCredential: Layer.empty, services: Option.none(), subscribeForViewer: Option.none(), }); @@ -140,6 +148,7 @@ const runtime = (seal: Layer.Layer) => config.reportFailure(cause).pipe( Effect.as({ taskCardUpdater: Layer.empty, + t3GitHubCredential: Layer.empty, services: Option.none(), subscribeForViewer: Option.none(), }), @@ -157,6 +166,7 @@ const runtime = (seal: Layer.Layer) => Effect.logError("Kin disabled: invalid configuration.", Cause.pretty(cause)).pipe( Effect.as({ taskCardUpdater: Layer.empty, + t3GitHubCredential: Layer.empty, services: Option.none(), subscribeForViewer: Option.none(), }), @@ -191,8 +201,29 @@ const taskCardUpdater = Layer.unwrap( }), ); +// Built with the server's source control host, which the executor's layer comes before. +const t3GitHubCredential = Layer.unwrap( + Effect.gen(function* () { + const kin = yield* KinServer; + return Option.isSome(kin.services) + ? kin.t3GitHubCredential.pipe( + Layer.catchCause((cause) => + Layer.effectDiscard( + Effect.logWarning( + "Kin: T3's GitHub credential can't be read; the credential check fails.", + { + cause: Cause.pretty(cause).slice(0, 500), + }, + ), + ), + ), + ) + : Layer.empty; + }), +); + export const mcpLayer = (base: Parameters[0]) => - Layer.mergeAll(KinMcp.transport(base), taskCardUpdater); + Layer.mergeAll(KinMcp.transport(base), taskCardUpdater, t3GitHubCredential); export const mcpRegistryLayer = ( base: Layer.Layer, diff --git a/apps/server/src/kin/mcp/layer.test.ts b/apps/server/src/kin/mcp/layer.test.ts index bd06b6f784322..b8665f8bfda18 100644 --- a/apps/server/src/kin/mcp/layer.test.ts +++ b/apps/server/src/kin/mcp/layer.test.ts @@ -45,6 +45,7 @@ import { liveThreadShell } from "../../mcp/McpToolAccess.testkit.ts"; import * as McpSessions from "../../mcp/McpSessionRegistry.ts"; import * as Sqlite from "../../persistence/Sqlite.ts"; import * as VcsProcess from "../../vcs/VcsProcess.ts"; +import * as SourceControlHost from "@t3tools/source-control-core/server/SourceControlHost"; import * as Kin from "../layer.ts"; import { ServerActivation } from "../../serverActivation.ts"; import { KinIdentity } from "../chat/KinIdentity.ts"; @@ -59,6 +60,7 @@ import * as McpHttpServer from "../../mcp/McpHttpServer.ts"; import { PreviewAutomationBroker } from "../../mcp/PreviewAutomationBroker.ts"; import { OrchestratorV2 } from "../../orchestration-v2/Orchestrator.ts"; import { OrchestrationEventStore } from "../../persistence/OrchestrationEventStore.ts"; +import { ProjectStoreV2 } from "../../orchestration-v2/ProjectStore.ts"; import { ProviderAdapterRegistryV2 } from "../../orchestration-v2/ProviderAdapterRegistry.ts"; import { ThreadLaunchService } from "../../orchestration-v2/ThreadLaunchService.ts"; import { ThreadManagementService } from "../../orchestration-v2/ThreadManagementService.ts"; @@ -85,6 +87,7 @@ import { GitVcsDriver } from "../../vcs/GitVcsDriver.ts"; import { VcsStatusBroadcaster } from "../../vcs/VcsStatusBroadcaster.ts"; import { DocumentText } from "../../provider/DocumentText.ts"; import { ProviderRegistry } from "../../provider/ProviderRegistry.ts"; +import * as T3GitHubCredential from "../approvals/T3GitHubCredential.ts"; const decodeToolList = Schema.decodeUnknownSync( Schema.fromJsonString( Schema.Struct({ @@ -114,6 +117,7 @@ const toolHandlerServices = Layer.mergeAll( Layer.mock(ProjectService)({}), Layer.mock(ProjectSetupScriptRunner)({}), Layer.mock(ProcessRunner)({}), + Layer.mock(ProjectStoreV2)({}), FetchHttpClient.layer, Layer.mock(ProviderAdapterRegistryV2)({}), Layer.mock(ProviderInstanceRegistry)({}), @@ -131,6 +135,11 @@ const toolHandlerServices = Layer.mergeAll( Layer.mock(ServerSecretStore)({}), ServerSettings.layerTest(), Layer.mock(SourceControlRepositoryService)({}), + // Kin installs T3's GitHub credential reader on this; nothing here reads it. + Layer.succeed( + SourceControlHost.SourceControlHost, + {} as SourceControlHost.SourceControlHost["Service"], + ), Layer.mock(ThreadLaunchService)({}), Layer.mock(ThreadManagementService)({ getThreadShell: (id) => Effect.succeed(liveThreadShell(id)), @@ -274,6 +283,12 @@ it.effect.each([true, false])( assert.isTrue(enabled); const identity = Context.get(kin.services.value, KinIdentity); const chatService = Context.get(kin.services.value, KinChatService); + // Nothing holding Kin's services can install a reader that hides T3's tokens. + assert.isTrue( + Option.isNone( + Context.getOption(kin.services.value, T3GitHubCredential.T3GitHubCredential), + ), + ); yield* pairSession(AuthSessionId.make("owner")).pipe( Effect.provideService(SqlClient.SqlClient, Context.get(services, SqlClient.SqlClient)), ); diff --git a/apps/server/src/kin/people/http.test.ts b/apps/server/src/kin/people/http.test.ts index 695953162540f..b7a4376ff5fdf 100644 --- a/apps/server/src/kin/people/http.test.ts +++ b/apps/server/src/kin/people/http.test.ts @@ -69,6 +69,7 @@ it.effect( Layer.mergeAll( Layer.succeed(KinServer)({ taskCardUpdater: Layer.empty, + t3GitHubCredential: Layer.empty, services: Option.some(context as unknown as Context.Context), subscribeForViewer: Option.none(), }), diff --git a/docs/kin/specs/approvals.md b/docs/kin/specs/approvals.md index c8fc1ba52c70e..36e8b96b02365 100644 --- a/docs/kin/specs/approvals.md +++ b/docs/kin/specs/approvals.md @@ -320,7 +320,7 @@ Handlers register through `KinActionHandlers` (a Context service, built from one Run order for an `approved` action: 1. Stop switch (lead spec) off → else stay `approved` until the grant lapses, then the sweep voids it. -2. Approver still owner (`KinOrgRoles`), and for GitHub kinds (section 12) `CredentialHygiene.requirePassing` passes → else stay `approved` (one `credential_check_failed` entry per action and failure set) until the grant lapses. Checking before the claim keeps a misconfigured host from spending the grant. +2. Approver still owner (`KinOrgRoles`), and for GitHub kinds (section 12) `CredentialHygiene.check` (its shared probe, section 12) passes → else stay `approved` (one `credential_check_failed` entry per action and failure set) until the grant lapses. Checking before the claim keeps a misconfigured host from spending the grant. 3. Before the claim, the stored grant must verify again: its signature against its key's public JWK, that key's seal (check 2), and its payload binding this action's org, id, revision, `jti`, approver and facts (check 7) → else `voided: grant_unverified`. A grant row or an `approved` state written straight into SQLite stops here. 4. **Claim:** `INSERT INTO kin_executions(action_id, …)` and CAS `approved → executing` in one transaction. The insert also requires the grant unconsumed and alive, the signing key usable, and the org not paused. A failed insert means it already ran, is running or may not run: refuse. 5. A fresh hygiene pass for the kinds step 2 checks, and the signing key and the approver's owner role checked again, then `precheck` with the bot's read-only `server` token (each read gives up after 15 seconds, which ends the action `failed`): PR open, `head.sha == grant.headSha`, `base.ref == grant.base`, and no `merge_queue` rule on the base (`GET /repos/{o}/{r}/rules/branches/{base}`; phase 1 refuses merge queues: `merge_queue_unsupported`). Whether the repo allows the method is left to GitHub, whose 405 ends the action `failed: not_mergeable`. Any failure from here on is `failed`: the grant is spent. @@ -395,7 +395,7 @@ export const KinProvenance = Schema.Struct({ Webhook events (consumed by the triggers spec's App webhook source, which filters to exactly these): `pull_request`, `pull_request_review`, `pull_request_review_comment`, `issue_comment`, `issues`, `check_suite`, `workflow_run`, `push`. Approvals reads check runs and statuses over REST at verify time, so it needs no `check_run` or `status` events. The App's private key is in the OS keychain (`AsyncEntry("kin-github-app", "")`, a JSON item with the PEM, webhook secret and client secret); the server finds it through `KIN_GITHUB_APP_ID`. Installation tokens last one hour; each worker gets one minted with `POST /app/installations/{id}/access_tokens` narrowed by `repositories` and `permissions` (no agent token carries `contents: write`). `KinGitHubApp.installationToken` takes exactly one repository from `KIN_GITHUB_REPOS`, a non-empty subset of the table at or below its levels, and the `holder` (`lead`, `worker` or `server`). It refuses anything else before calling GitHub, including `contents: write` for any holder but `server` (open question 7). A `server` token stays in Kin's own process, for its own pushes and the triggers spec's read-only deferral and watchdog reads, and is never handed to an agent. That refusal only holds if `holder` is true: callers hard-code it and never take it, or anything that picks it, from model input. It also refuses when `GET /repos/{o}/{r}/installation` reports `repository_selection: "all"`. If GitHub's answer is wider than asked (more repositories, or a permission not requested or at a higher rank, beyond the `metadata: read` it always adds), or doesn't decode, it revokes the token with `DELETE /installation/token` and fails. A failed revoke is logged without the token; the token still expires within the hour. -`KinGitHubScope` (`apps/server/src/kin/github/KinGitHubScope.ts`) holds the scope the checks compare against: `KIN_GITHUB_OWNER_LOGIN` (the owner's personal login), `KIN_GITHUB_BOT_LOGIN` (the only login the Kin host may use, section 12) and `KIN_GITHUB_REPOS` (comma-separated `owner/name`). +`KinGitHubScope` (`apps/server/src/kin/github/KinGitHubScope.ts`) holds the scope the checks compare against: `KIN_GITHUB_OWNER_LOGIN` (the owner's personal login), `KIN_GITHUB_BOT_LOGIN` (the only login the Kin host may use, section 12), `KIN_GITHUB_BOT_ID` (that account's numeric id, which a token's `GET /user` must also name, section 12) and `KIN_GITHUB_REPOS` (comma-separated `owner/name`). `scripts/kin-github-app.ts` runs on the Kin host as the server's OS user, so the secrets land in that user's keychain. With no flags it serves the manifest form on `127.0.0.1:47821` (forward it over SSH) and completes `POST /app-manifests/{code}/conversions` from the redirect, checking `state` and that the `Host` header is `127.0.0.1:` or `localhost:`. It checks that a Secret Service answers before serving the form (open question 6), because the conversion answer is the only copy of the private key. It stores the secrets only when the new App's permissions match the table and its owner is `--org` or `KIN_GITHUB_OWNER_LOGIN`; otherwise it stores nothing and says to delete the App. Any local process can reach the loopback port and read `state` from the form, so another local user could send a callback for an App they created; the owner and permission checks refuse that App. It prints only the App id, slug and install URL. `--webhook-url` subscribes the App webhook to the events above; without it the App has no webhook, which matches the triggers spec's per-repo hooks. `--check` diffs the App's permissions and every installation's accepted permissions against the table, which is one constant (`KIN_GITHUB_APP_PERMISSIONS` in `apps/server/src/kin/github/KinGitHubPermissions.ts`). It also fails an installation on all repositories, and reads every page of installations. The command itself is `apps/server/src/kin/github/appSetup.ts`: the composite `scripts` TypeScript project can't include server files (TS6307), so the script loads it by URL. @@ -530,21 +530,56 @@ Each org has its own chain. The first entry's `prevHash` is 64 zeros, and `seq` ### 12. Credential hygiene check -At startup and hourly, the executor checks every GitHub identity reachable from the server's OS user: T3's own (`GitHubRequestPolicy.credential("github.com")`, then `GET /user`), every account `gh auth status` lists for github.com, active or not, and the login behind the github.com git credential helper, and every login GitHub greets over SSH. If any of them isn't `KIN_GITHUB_BOT_LOGIN`, or `GH_TOKEN`, `GITHUB_TOKEN` or `SSH_AUTH_SOCK` is set in the server environment, the executor writes `credential_check_failed`, refuses privileged execution, and posts a card to the owner. Agent CLIs run as children of the server and inherit its `gh` login and environment, so on the Kin machine that identity must be the bot. The triggers spec's Kin host setup owns making it so. +At startup and hourly, the executor checks every GitHub identity reachable from the server's OS user: every credential T3 holds (each named by `GET /user`, by login and account id), every account `gh` holds for github.com, active or not, the logins the github.com git credential helper answers for, and every login GitHub greets over SSH. Listing every place a credential could hide can't be finished, so the check doesn't try: it requires the host's SSH config, git config (the host's and each project's) and environment (the server's and each provider instance's) to be minimal and known (an allowlist), and fails closed on anything outside it. If any login found isn't `KIN_GITHUB_BOT_LOGIN`, a token-backed one (T3's, `gh auth token`'s or the git helper's) names another account id than `KIN_GITHUB_BOT_ID`, or the host is outside the allowlist, the executor writes `credential_check_failed`, refuses privileged execution, and posts a card to the owner. Agent CLIs run as children of the server and inherit its logins, config and environment, so on the Kin machine all of them must be the bot's. The triggers spec's Kin host setup owns making it so. + +What the check is for: it guards against _accidental_ owner-credential use, such as a host set up with the owner's gh login, an SSH agent holding the owner's key, or a token in an environment variable. It can't stop a hostile agent. An agent running as the server's OS user can read anything that user can, including the owner's merge token and the App's private key in the keychain (open question 4), an SSH agent socket on disk, or a key or token outside the paths the probe reads. The only real boundary is a separate executor OS user that holds the merge token, with agents running as a user that can't read it. Until then, the Kin host keeps the owner's credentials off the server's OS user entirely. `CredentialHygiene.check(orgId)` (`apps/server/src/kin/approvals/CredentialHygiene.ts`) returns a report, and the executor calls `requirePassing` before every privileged run. Failures: -- `env_token`: `GH_TOKEN`, `GITHUB_TOKEN`, either `*_ENTERPRISE_TOKEN` or `SSH_AUTH_SOCK` is set, or T3's credential came from the environment. The report names the variables, never their values. An SSH agent can hold several keys and GitHub greets only the first it accepts, so the SSH probe can't see past it; start the Kin server without `SSH_AUTH_SOCK` (macOS launchd sets it for every login session). -- `owner_identity`: a login found equals the merge token's login or `KIN_GITHUB_OWNER_LOGIN`, case-insensitively. -- `non_bot_identity`: a login found is neither the owner's nor `KIN_GITHUB_BOT_LOGIN`. -- `identity_unknown`: the identity probe failed, or an SSH answer was neither `Hi !` nor `Permission denied (publickey)`. This fails closed. +- `env_token`: the server's environment, or a provider instance's, sets a variable outside the environment allowlist below, or T3's credential came from the environment. +- `owner_identity`: a login found equals the merge token's login or `KIN_GITHUB_OWNER_LOGIN`, case-insensitively, or the git helper returns a password when asked for either login as the username. +- `non_bot_identity`: a login found is neither the owner's nor `KIN_GITHUB_BOT_LOGIN`, or a token names the bot's login with an account id other than `KIN_GITHUB_BOT_ID`. A login can be renamed and registered again by someone else; the id can't. SSH greetings name only a login, so they're compared by login. +- `identity_unknown`: the identity probe failed, the host is outside the allowlist, an SSH answer was neither `Hi !` nor `Permission denied (publickey)`, or `KIN_GITHUB_BOT_ID` isn't set. Without the id there's nothing to compare tokens against, so the check fails closed until the operator sets it (`gh api users/ --jq .id`). - `merge_token_missing` or `merge_token_expired`: no owner login to compare against, and nothing to merge with. -A merge token in its last 7 days passes with the `merge_token_expiring` warning, which drives the expiry card. `requirePassing` returns a `CredentialHygienePass`, which `OwnerMergeClient.token` requires (section 6). The merge token itself is probed once, at `save` (section 10); the hourly check reads only its stored login and expiry. GitHub lets the owner widen a fine-grained token's permissions without changing its value, so a token widened after `save` (for example to Pull requests: write) goes unnoticed until it's saved again. Rerunning the read-only probes in the hourly check is a follow-up. +The report's `reasons` say why in fixed words: the source, and the file, directive, key, variable or provider instance at fault, never a value. A git subsection, which can hold a URL with a token, is shown as `*`, and an SSH directive that isn't an OpenSSH keyword is "an unrecognised directive", since it could be a pasted secret. The reasons go into the audit entry's `result` (`: `) and the owner's notice. -The identity probe is the `T3GitHubIdentity` port. `@t3tools/source-control-github` exports only its driver, so `GitHubRequestPolicy.credential` can't be reached from `apps/server` without a one-line export there. APPR-9 wires the executor with `identityUnavailableLayer`, which fails every probe, so no privileged action runs on a real host until APPR-16 adds that export and the live layer. Allowing a phone never takes the check: it writes nothing to GitHub. The port's `identities` has a required field per source (`t3`, `ghAccounts`, `gitCredential`, `ssh`), so the live layer must report all four. It reads `gh` accounts from `gh auth status --hostname github.com`, and the git helper's login from `git credential fill` for `https://github.com` followed by `GET /user` with the returned password, never logging it. For `ssh` it runs `ssh -T -o BatchMode=yes -o StrictHostKeyChecking=yes git@github.com` (`GITHUB_SSH_PROBE`) with a timeout, then the same once per private key in `~/.ssh` with `-o IdentitiesOnly=yes -i `, and reports each answer's combined output; the check parses them , so the live layer can't misread one, and an empty list fails. A deploy key's greeting names `owner/repo`, which fails as `non_bot_identity`. Keys behind another `Host` alias in `~/.ssh/config` aren't probed. A source it can't read fails the probe, which fails the check closed (`identity_unknown`). +A merge token in its last 7 days passes with the `merge_token_expiring` warning, which drives the expiry card. `requirePassing` returns a `CredentialHygienePass`, which `OwnerMergeClient.token` requires (section 6). The merge token itself is probed once, at `save` (section 10); the hourly check reads only its stored login and expiry. GitHub lets the owner widen a fine-grained token's permissions without changing its value, so a token widened after `save` (for example to Pull requests: write) goes unnoticed until it's saved again. Rerunning the read-only probes in the hourly check is a follow-up. -The check only sees what the server's OS user is set up with. An agent running as that user can still read anything that user can: it can find an SSH agent socket on disk and set `SSH_AUTH_SOCK` itself, read the merge token and the App's private key from the keychain (open question 4), or use credentials outside these sources. The real fix is a separate executor OS user. +The identity probe is the `T3GitHubIdentity` port; `GitHubIdentityProbe.ts` is its live layer, and `HostConfigAllowlist.ts` holds the allowlists. The port's `identities` takes the owner logins to ask the git helper about and has a required field per source, so the live layer must report every one, and any source it can't read fails the probe. Allowing a phone never takes the check: it writes nothing to GitHub. Every process runs from `/` with argv only, a timeout, prompts off and `SSH_AUTH_SOCK` empty, and output past 64 KiB fails rather than being cut short. The home directory is the passwd entry's (`os.userInfo().homedir`), which is where ssh reads `~/.ssh`; a `$HOME` that differs fails, since git and gh would read another one. What each source reads: + +- `t3`: every credential T3 keeps for github.com, whichever it would use and whether or not the host is disabled in Settings: the token saved in Settings, the app sign-in (an empty one is signed out), `GH_TOKEN`/`GITHUB_TOKEN`, and `gh auth token` for the active account, the account Settings picks, and `KIN_GITHUB_OWNER_LOGIN` (a token there means the owner is signed in to gh; "no oauth token found" means none). `gh auth token` also returns keyring tokens that `gh auth status` no longer lists. The executor is built before the server's source control host, so the server installs a reader of T3's stored state afterwards (`T3GitHubCredential.ts`, once). The slot stays out of Kin's shared services, so nothing holding them can install a reader that hides T3's tokens first; a read waits up to 30 seconds for it, then fails. The reader never refreshes: the app session's refresh token is single-use, and spending it here would sign T3 out. An app session whose access token has expired but can still refresh fails closed, since its login can't be named without a refresh; an app sign-in left idle for about 8 hours gets there, which is why the Kin host uses a saved token or gh instead. +- `providerEnvironments`: the same reader names the variables each provider instance's environment adds to or changes from the server's, merged as `mergeProviderInstanceEnvironment` merges it, for every instance in Settings, enabled or not, since the terminal opens any instance's environment. A secret reference counts as set. Only names leave the reader. +- `ghAccounts`: every account `gh auth status --hostname github.com --json hosts` lists (gh 2.81 or later). A login that isn't a GitHub login (39 characters at most) fails. +- `gitCredential`: first, `~/.netrc` or `~/_netrc` under the passwd home fails ("~/.netrc exists"): with no helper, git's HTTPS transport sends it through curl, and `git credential fill` never sees it. Then one `git config --list --show-origin -z`, checked against the git allowlist below, then the login and account id behind `git credential fill` for `https://github.com`, by `GET /user` with the returned password, never logging it. `git credential fill` returns only the first helper's answer, so more than one helper fails, counted in config order with an empty value resetting the list as git does. With a helper configured, git's `terminal prompts disabled` means the helper crashed, was locked or refused, and fails; only with no helper does it mean none. One helper can hold several accounts and answer for the one asked, so `fill` runs again with `username=` each of `KIN_GITHUB_OWNER_LOGIN` and the merge token's login, as written and lower-cased. There, `terminal prompts disabled` means the helper has no credential for that login, and a returned password fails as `owner_identity`; that password is never sent anywhere or kept. +- `git_repo`: for each of T3's projects (64 at most; more fails), `git -C config --list --show-origin -z --local`, and `--worktree` too when `extensions.worktreeConfig` is on, checked against the repository allowlist below. A folder that's gone or isn't a repository has no config of its own; any other failure fails closed. +- `ssh`: `ssh` is resolved once on PATH to an absolute path, used for every probe; a relative PATH folder fails, since an agent elsewhere would find another ssh. `~/.ssh/config` and every file it `Include`s (32 files and 64 KiB each at most) are checked against the SSH allowlist below. Only then does ` -v -G git@github.com` run, and only its `Reading configuration data ` lines are kept: every other file it names is a system config for that build (Homebrew, MacPorts and Nix each read their own) and is checked, with its includes, against the system list. (`-F`, even `-F none`, makes ssh skip the system config, so this run can't use it.) A config line ssh's `argv_split` might read differently (single quotes, a backslash, or a quote or `#` inside an argument) fails, as does an `Include` without a glob that names a missing file. Every `IdentityFile` under any `Host` in any of these files is a candidate, along with every file in `~/.ssh` and two levels of folders below it. Each candidate goes to `ssh-keygen -y -P "" -f`, which loads it as ssh would: a key it loads is probed, and a file it can't load that still holds a private key (a passphrase, wrong permissions) fails. More than 32 keys fails. Then ` -F <~/.ssh/config, or none> -T -o BatchMode=yes -o StrictHostKeyChecking=yes -o ControlMaster=no -o ControlPath=none git@github.com` (`GITHUB_SSH_PROBE`) runs as is, and once per key with `-o IdentitiesOnly=yes -i `. `-F` reads exactly the user config the probe checked; keys named only in a system config are still probed by `-i`. The check parses each answer's combined output, so the live layer can't misread one, and an empty list fails. A deploy key's greeting names `owner/repo`, which fails as `non_bot_identity`. + +`check` shares one probe for 60 seconds across every org and held action with the same owner logins, and concurrent checks wait on the one running. `requirePassing`, which gates the side effect after the claim, always runs a fresh probe. The pre-claim check in `execute` uses the shared probe on purpose: it only decides whether to claim, so a stale pass can at worst spend one grant on a host that broke within the last minute (the fresh probe then refuses it), and a stale failure holds the action for at most another minute. The hourly check audits and tells the owner only when an org's result changes, including when it passes again; after a restart, a last `credential_check_failed` entry still gets its "passes again" notice, and a failure the last hourly entry already reported, with the same reasons, isn't posted or audited again. Orgs without a saved merge token haven't set GitHub up and get no notice. + +The Kin host runs the server as a dedicated OS user that holds only the bot's credentials, set up so the check can pass: + +- **Environment.** No variable named `GH_*`, `GITHUB_*`, `GIT_*`, `SSH_*` or `*_TOKEN`, in any case, is set to anything but blank, in the server's environment or any provider instance's in Settings, and no provider instance changes `PATH`, `HOME`, any `XDG_*`, `BASH_ENV`, `ENV` or `ZDOTDIR`, since agents would then run another ssh, git or gh than the ones the probe asked, read a git or gh config it never read, or source a startup file that can export a token. The exceptions are `GIT_TERMINAL_PROMPT`, which only turns git's prompt off, and `T3_MCP_BEARER_TOKEN` and `T3CODE_DEV_AUTH_TOKEN`, which T3 sets itself and which authenticate only to T3. `$HOME` is unset or the passwd entry's home, and every PATH folder is absolute. +- **No `~/.netrc`.** Neither `~/.netrc` nor `~/_netrc` exists in the passwd home. git's HTTPS transport reads it through curl when no helper answers, and the probe can't see what it holds. +- **Shell startup files.** `.zshenv`, `.zprofile`, `.zshrc`, `.bash_profile`, `.bashrc`, `.profile` and the like don't export any of those variables either. Agents' shells read them, and the probe can't see what they add. +- **`~/.ssh/config`** and the files it includes use only `Host`, `HostName`, `User`, `Port`, `IdentityFile`, `IdentitiesOnly`, `StrictHostKeyChecking`, `UserKnownHostsFile`, `Include`, `AddKeysToAgent no`, `IdentityAgent none`, `ControlMaster no`, `ControlPath none`, `ServerAliveInterval`, `ServerAliveCountMax`, `ConnectTimeout` and `Compression`, in any case, with `key value` or `key=value`, and arguments quoted only with whole double-quoted words. Anything else fails, `Match` in any form, `ProxyCommand`, `ProxyJump`, `PKCS11Provider`, `SecurityKeyProvider`, `CertificateFile`, `KnownHostsCommand`, `LocalCommand` and `PermitLocalCommand` included. An `IdentityFile` is an absolute path or starts with `~/`, and uses no tokens but `%d`, `%u`, `%i` and `%%` (no host tokens, `${VAR}` or `~user`). An `Include` names a path the same way and globs only its last segment. +- **The system SSH config** the ssh on PATH reads, and its includes, follow the same list, plus what distros ship there: `SendEnv` and the crypto policy's `Ciphers`, `KexAlgorithms`, `MACs`, `HostKeyAlgorithms`, `PubkeyAcceptedAlgorithms`, `CASignatureAlgorithms` and `HostbasedAcceptedAlgorithms` (macOS), and `HashKnownHosts` and `GSSAPIAuthentication` (Debian, Ubuntu). None of them can add an identity. +- **Git config** (every scope git reads from `/`) holds only keys under `user.`, `core.` (but not `core.askPass`, `core.sshCommand`, `core.gitProxy`, `core.fsmonitor` or `core.hooksPath`), `color.`, `alias.`, `pull.`, `push.`, `fetch.`, `merge.`, `diff.` and `filter.lfs.`, `init.defaultBranch`, plus `credential.helper` and `credential.https://github.com.helper`, with one helper in effect, holding only the bot's credential. Anything else fails, `init.templateDir`, `include.*`, `includeIf.*`, `http.*`, `url.*`, `remote.*`, every other `credential.*` and `gpg.*` included. `safe.bareRepository=explicit` from the command line is allowed, since T3's process runner sets it on every git run. Some allowed keys run a program the agent asks for (`core.pager`, `diff.external`, `merge.*.driver`, `!` aliases, the LFS filter): they're allowed because none picks a credential, and the agent could run that program anyway. The refused `core.` keys decide how git authenticates or connects, or run a program on commands the agent didn't ask for. +- **Each project's own config** (`--local`, and `--worktree` when on) holds only `core.*` (but not those five), `branch.*` (where `branch..remote` and `pushRemote` hold only a remote's name, `[A-Za-z0-9._-]+`, since git takes a URL there too), `user.*`, `extensions.objectFormat` and `extensions.refStorage`, `remote..url` and `pushurl` with no `::` helper (`ext::`, `fd::` and the rest), no userinfo on an HTTP or FTP URL and no password on any other (`git@github.com:o/r` and `ssh://git@github.com/o/r` are fine), `remote..fetch`, `push`, `mirror`, `skipDefaultUpdate`, `skipFetchAll`, `tagOpt`, `prune`, `pruneTags`, `promisor` and `partialCloneFilter`, `remote.pushDefault` (a remote's name the same way), `extensions.worktreeConfig`, `submodule..url` (checked the same way) and `active`, and `lfs.repositoryFormatVersion`. Anything else fails, `credential.*`, `http.*`, `url.*`, `include*` and every other `remote.*` or `submodule.*` key included. +- **Keys.** No passphrase-protected private keys in `~/.ssh` or named by `IdentityFile`, and github.com in `~/.ssh/known_hosts`, since the probe uses `StrictHostKeyChecking=yes`. +- **T3 and gh.** A saved token or gh for the bot, not the app sign-in, and gh 2.81 or newer. +- **`KIN_GITHUB_BOT_ID`** set to the bot account's numeric id. The check fails while it's unset. + +Default setups that always fail, and what to change: + +- macOS launchd sets `SSH_AUTH_SOCK` for every login session: unset it in the server's service definition. +- A server started from an SSH session inherits `SSH_CONNECTION`, `SSH_CLIENT` and `SSH_TTY`: run it as a service instead. +- An exported `GIT_EDITOR`, `GIT_PAGER`, `GH_*` or any `*_TOKEN` in the server's environment or a provider instance's: remove it. +- 1Password, Secretive or another agent set by `IdentityAgent`, and `ControlMaster auto` in `~/.ssh/config`: remove them, or set `IdentityAgent none` and `ControlMaster no`. +- `includeIf` for a work/personal split, `url..insteadOf` rewrites, `init.templateDir` and a second credential helper in git config: remove them. +- A project cloned with a token in its HTTPS remote URL, or with `user.email`, `credential.helper` or `http.extraHeader` in its own config: clone it again over SSH or plain HTTPS, and move per-repository identity to the host's config. +- A passphrase-protected key in `~/.ssh`, or T3's app sign-in left idle: use an unprotected bot key, and a saved token or gh. +- A `~/.netrc` from `curl` or an old tool, even for another host: delete it, and keep the bot's token in the credential helper. ## Threat model @@ -657,7 +692,7 @@ Acceptance for phase 1: on `GLDRoger/t3code`, the lead requests a merge of a bot **APPR-9 Executor.** `ActionExecutor.ts`, handler registry, `merge_pull_request` and `delete_branch` handlers via `OwnerMergeClient.ts` (REST with `sha`), stop-switch check, startup reconciliation, result posting. Depends on: APPR-4, APPR-8, LEAD-11. Done when: tests 10–12 and 19 pass against a recorded GitHub fake, and one real merge on a scratch repo succeeds. Owner: backend. -**APPR-16 Live GitHub identity probe.** The `T3GitHubIdentity` live layer (section 12) and the one-line `GitHubRequestPolicy` export from `@t3tools/source-control-github`, replacing `identityUnavailableLayer` in `approvals/layer.ts`; the hourly hygiene check with the owner's card. Depends on: APPR-8, APPR-9. Done when: on the Kin host, `CredentialHygiene.check` passes with only the bot's login and fails for each source given the owner's login. Owner: backend. +**APPR-16 Live GitHub identity probe.** The `T3GitHubIdentity` live layer (section 12) and the one-line `GitHubAppSession` export from `@t3tools/source-control-github`, replacing `identityUnavailableLayer` in `approvals/layer.ts`; the hourly hygiene check with the owner's card. Depends on: APPR-8, APPR-9. Done when: on the Kin host, `CredentialHygiene.check` passes with only the bot's login and fails for each source given the owner's login. Owner: backend. **APPR-10 Audit.** `Audit.ts` with hash chain, `kin.audit.list`/`verify`, entries from every step above. Depends on: APPR-2. Done when: test 20 passes and the acceptance merge shows a complete trail. Owner: backend. diff --git a/packages/shared/src/HostProcess.ts b/packages/shared/src/HostProcess.ts index e5e2f8e97f108..04c96413921a4 100644 --- a/packages/shared/src/HostProcess.ts +++ b/packages/shared/src/HostProcess.ts @@ -53,6 +53,20 @@ export const HomeDirectory = Context.Reference( }, ); +/** The passwd entry's home. ssh reads `~/.ssh` from here whatever `$HOME` says. */ +export const PasswdHomeDirectory = Context.Reference( + "@t3tools/shared/HostProcess/PasswdHomeDirectory", + { + defaultValue: () => { + try { + return NodeOS.userInfo().homedir || null; + } catch { + return null; + } + }, + }, +); + export const Environment = Context.Reference( "@t3tools/shared/HostProcess/Environment", { diff --git a/packages/source-control-github/src/server/GitHubAppSignIn.ts b/packages/source-control-github/src/server/GitHubAppSignIn.ts index e7e792068617e..4dac05ba41aec 100644 --- a/packages/source-control-github/src/server/GitHubAppSignIn.ts +++ b/packages/source-control-github/src/server/GitHubAppSignIn.ts @@ -34,7 +34,8 @@ const REQUEST_TIMEOUT = Duration.seconds(15); // Refresh ahead of expiry so a token never lapses between the check and a long request. const REFRESH_MARGIN_MS = Duration.toMillis(Duration.minutes(5)); -const GitHubAppSession = Schema.Struct({ +/** The stored app sign-in, as the `signIns` setting holds it. */ +export const GitHubAppSession = Schema.Struct({ accessToken: Schema.String, /** Null when the app hands out tokens that do not expire. */ accessTokenExpiresAt: Schema.NullOr(Schema.Number), diff --git a/packages/source-control-github/src/server/driver.ts b/packages/source-control-github/src/server/driver.ts index 2fb2dfb2899c7..fbfc55d86fcd2 100644 --- a/packages/source-control-github/src/server/driver.ts +++ b/packages/source-control-github/src/server/driver.ts @@ -120,3 +120,5 @@ export const driver = defineSourceControlDriver({ definition, make: makeInstance, }); + +export { GitHubAppSession } from "./GitHubAppSignIn.ts";