From caeca80a72c47835659f5ea13005932c0724768a Mon Sep 17 00:00:00 2001 From: Surajsm60720 <1ds22is166@dsce.edu.in> Date: Mon, 9 Nov 2026 08:50:07 +0530 Subject: [PATCH 1/7] feat(kin): live GitHub identity probe and hourly credential check The credential check now reads every GitHub login the server's OS user can reach: T3's own credential, every gh account, the git credential helper's login and each SSH key's greeting. A source it can't read fails the check closed. The executor runs the check at startup and hourly, audits a failing result and tells the owner when the result changes. source-control-github exports GitHubRequestPolicy and its layer so the server can read T3's credential; the reader is installed once the server's source control host exists, and the probe fails until then. Co-Authored-By: Claude Opus 5.5 --- apps/server/src/kin/KinServer.ts | 2 + .../src/kin/approvals/ActionExecutor.test.ts | 57 ++- .../src/kin/approvals/ActionExecutor.ts | 64 +++- .../src/kin/approvals/CredentialHygiene.ts | 17 +- .../kin/approvals/GitHubIdentityProbe.test.ts | 333 ++++++++++++++++++ .../src/kin/approvals/GitHubIdentityProbe.ts | 296 ++++++++++++++++ .../src/kin/approvals/T3GitHubCredential.ts | 88 +++++ apps/server/src/kin/approvals/layer.ts | 13 +- apps/server/src/kin/chat/executionNotices.ts | 60 +++- apps/server/src/kin/layer.ts | 25 +- apps/server/src/kin/mcp/layer.test.ts | 10 + docs/kin/specs/approvals.md | 2 +- .../src/server/driver.ts | 3 + 13 files changed, 945 insertions(+), 25 deletions(-) create mode 100644 apps/server/src/kin/approvals/GitHubIdentityProbe.test.ts create mode 100644 apps/server/src/kin/approvals/GitHubIdentityProbe.ts create mode 100644 apps/server/src/kin/approvals/T3GitHubCredential.ts diff --git a/apps/server/src/kin/KinServer.ts b/apps/server/src/kin/KinServer.ts index b0b51a9513e97..56470a771072b 100644 --- a/apps/server/src/kin/KinServer.ts +++ b/apps/server/src/kin/KinServer.ts @@ -9,6 +9,7 @@ import { KinBudget } from "./lead/KinBudget.ts"; import { KinEventQueue } from "./lead/KinEventQueue.ts"; import { ApproverKeys } from "./approvals/ApproverKeys.ts"; import { GrantVerifier } from "./approvals/GrantVerifier.ts"; +import type { T3GitHubCredential } from "./approvals/T3GitHubCredential.ts"; import { KinStop } from "./lead/KinStop.ts"; import type { KinGithubWebhookLock } from "./triggers/githubWebhooks.ts"; import type { KinPushDevices } from "./push/KinPushDevices.ts"; @@ -25,6 +26,7 @@ export type Services = | KinGithubWebhookLock | ApproverKeys | GrantVerifier + | T3GitHubCredential | KinPushDevices | KinPushPresence | KinPushSender; diff --git a/apps/server/src/kin/approvals/ActionExecutor.test.ts b/apps/server/src/kin/approvals/ActionExecutor.test.ts index c27360038dbfc..c0c24f9b71451 100644 --- a/apps/server/src/kin/approvals/ActionExecutor.test.ts +++ b/apps/server/src/kin/approvals/ActionExecutor.test.ts @@ -20,6 +20,7 @@ import * as Effect from "effect/Effect"; 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"; @@ -153,6 +154,11 @@ function harness() { }), }); const notices: Array<{ readonly actionId: string; readonly outcome: 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 }>; + 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: { @@ -214,6 +220,10 @@ function harness() { const notifications = Layer.succeed( ActionExecutor.KinExecutionNotices, ActionExecutor.KinExecutionNotices.of({ + credentialCheck: ({ orgId: org, report }) => + Effect.sync(() => + credentialNotices.sent.push({ orgId: org, failures: report.failures }), + ).pipe(Effect.andThen(credentialNotices.posted?.(report.failures) ?? Effect.void)), finished: ({ action, outcome }) => Effect.sync(() => void notices.push({ actionId: action.id, outcome })).pipe( Effect.andThen(Deferred.succeed(noticed, undefined)), @@ -235,7 +245,17 @@ function harness() { const layer = Layer.effect(ActionExecutor.KinActionExecutor, ActionExecutor.make).pipe( Layer.provideMerge(deps), ); - return { world, requests: github.requests, notices, noticed, checks, mints, layer, deps }; + return { + world, + requests: github.requests, + notices, + credentialNotices, + noticed, + checks, + mints, + layer, + deps, + }; } const seed = Effect.gen(function* () { @@ -630,6 +650,41 @@ 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); + assert.deepStrictEqual(credentialNotices.sent, [ + { orgId, failures: ["identity_unknown"] }, + { orgId, failures: [] }, + ]); + const failed = (yield* auditEntries).filter( + (entry) => entry.event === "credential_check_failed", + ); + assert.deepStrictEqual( + failed.map((entry) => [entry.actionId, entry.result]), + [[undefined, "identity_unknown"]], + ); + }).pipe(Effect.provide(deps)); + }, + ); + it.effect("the worker executes an approval as it lands", () => { const { requests, noticed, deps } = harness(); return Effect.gen(function* () { diff --git a/apps/server/src/kin/approvals/ActionExecutor.ts b/apps/server/src/kin/approvals/ActionExecutor.ts index 7739e70ff767f..8451a51f77c24 100644 --- a/apps/server/src/kin/approvals/ActionExecutor.ts +++ b/apps/server/src/kin/approvals/ActionExecutor.ts @@ -1,4 +1,4 @@ -import type { KinGrant, KinOrgId, KinPendingAction } from "@t3tools/contracts"; +import { KinOrgId, type KinGrant, type KinPendingAction } from "@t3tools/contracts"; import { decodeKinGrant } from "@t3tools/shared/kinGrant"; import * as Cause from "effect/Cause"; import * as Context from "effect/Context"; @@ -11,7 +11,11 @@ import * as Schedule from "effect/Schedule"; import * as Semaphore from "effect/Semaphore"; import * as SqlClient from "effect/sql/SqlClient"; -import type { PersistenceDecodeError, PersistenceSqlError } from "../../persistence/Errors.ts"; +import { + type PersistenceDecodeError, + type PersistenceSqlError, + toPersistenceSqlError, +} from "../../persistence/Errors.ts"; import { isOrgPaused } from "../lead/orgState.ts"; import { KinActionHandlers, @@ -20,7 +24,7 @@ import { type KinOutcome, } from "./ActionHandlers.ts"; import { KinAudit } from "./Audit.ts"; -import { CredentialHygiene } from "./CredentialHygiene.ts"; +import { CredentialHygiene, type CredentialHygieneReport } from "./CredentialHygiene.ts"; import { KinOrgRoles } from "./GrantPorts.ts"; import { KinPendingActions, type ActionRef } from "./PendingActions.ts"; @@ -28,6 +32,8 @@ import { KinPendingActions, type ActionRef } from "./PendingActions.ts"; const TICK_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"; export type KinExecutionOutcome = "succeeded" | "failed" | "interrupted"; @@ -41,6 +47,11 @@ export class KinExecutionNotices extends Context.Service< readonly mergeCommitSha?: string; readonly error?: 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; + }) => Effect.Effect; } >()("t3/kin/approvals/ActionExecutor/KinExecutionNotices") {} @@ -71,6 +82,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 + >; /** Expiry sweep, then every approved action. The worker runs it at startup and each minute. */ readonly tick: Effect.Effect< ReadonlyArray<{ readonly ref: ActionRef; readonly result: KinExecuteResult }>, @@ -101,6 +120,8 @@ export const make = Effect.gen(function* () { }; // One `credential_check_failed` entry per held action and failure set, not one per tick. const auditedHolds = new Map(); + // 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)); @@ -289,6 +310,34 @@ 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")), + ); + 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].join(","); + if ((reportedChecks.get(orgId) ?? "") !== result) { + reportedChecks.set(orgId, result); + if (!report.passed) { + yield* audit + .append({ + orgId, + event: "credential_check_failed", + result: report.failures.join(","), + }) + .pipe(Effect.catch((error) => Effect.logWarning("Kin audit not written", error))); + } + yield* notices.credentialCheck({ orgId, report }); + } + return { orgId, report }; + }), + ); + }).pipe(Effect.withSpan("KinActionExecutor.checkCredentials")); + const tick = Effect.gen(function* () { yield* pending.sweep(); const approved = yield* pending.approvedWithGrant(); @@ -302,12 +351,13 @@ export const make = Effect.gen(function* () { ); }).pipe(Effect.withSpan("KinActionExecutor.tick")); - return KinActionExecutor.of({ execute, reconcile, tick }); + return KinActionExecutor.of({ execute, reconcile, checkCredentials, tick }); }); /** * The executor with its worker: startup reconciliation of `executing` rows (retried with backoff - * while GitHub can't be read), then every approval as it lands, plus a minute tick for held ones. + * while GitHub can't be read), then every approval as it lands, plus a minute tick for held ones + * and the hourly credential check. */ export const layer = Layer.effect( KinActionExecutor, @@ -358,6 +408,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, + ); yield* logged("tick failed", executor.tick.pipe(Effect.flatMap(settle))).pipe( Effect.repeat(Schedule.spaced(TICK_INTERVAL)), Effect.forkScoped, diff --git a/apps/server/src/kin/approvals/CredentialHygiene.ts b/apps/server/src/kin/approvals/CredentialHygiene.ts index c7a0d48d07ca7..f872601b983c4 100644 --- a/apps/server/src/kin/approvals/CredentialHygiene.ts +++ b/apps/server/src/kin/approvals/CredentialHygiene.ts @@ -24,7 +24,7 @@ const ENV_TOKEN_NAMES = [ "SSH_AUTH_SOCK", ]; -/** @public The probe APPR-9's live layer runs for `T3GitHubIdentities.ssh`, without `SSH_AUTH_SOCK`. */ +/** The probe the live layer (`GitHubIdentityProbe.ts`) runs for `T3GitHubIdentities.ssh`, without `SSH_AUTH_SOCK`. */ export const GITHUB_SSH_PROBE = [ "ssh", "-T", @@ -73,7 +73,7 @@ export class T3GitHubIdentityError extends Schema.TaggedError } >()("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", 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..4151a4c71b122 --- /dev/null +++ b/apps/server/src/kin/approvals/GitHubIdentityProbe.test.ts @@ -0,0 +1,333 @@ +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 ConfigProvider from "effect/ConfigProvider"; +import * as Context from "effect/Context"; +import * as Effect from "effect/Effect"; +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 { ChildProcessSpawner } from "effect/process"; +import * as TestClock from "effect/testing/TestClock"; + +import { VcsProcess, type VcsProcessInput, type VcsProcessOutput } from "../../vcs/VcsProcess.ts"; +import * as OwnerMergeClient from "../github/OwnerMergeClient.ts"; +import { 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 T3_TOKEN = "gho_t3_token"; +const GIT_PASSWORD = "gho_git_helper_password_never_logged"; +const KEY = + "-----BEGIN OPENSSH PRIVATE KEY-----\nnot a real key\n-----END OPENSSH 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)."; + +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 missing = (command: string): Run => + Effect.fail( + new VcsProcessSpawnError({ + operation: "test", + command, + cwd: "/", + cause: PlatformError.systemError({ _tag: "NotFound", module: "test", method: "spawn" }), + }), + ); +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` }); + +/** The machine the probe looks at. Each test changes what one source answers. */ +interface Machine { + t3: Option.Option | "uninstalled"; + /** The login GitHub's `GET /user` names for each token; any other token gets a 401. */ + users: Record; + gh: Run; + git: Run; + /** Answers one SSH probe; `key` is the `-i` file, or undefined for the probe as is. */ + ssh: (key: string | undefined) => Run; + keys: Record; +} + +const botMachine = (): Machine => ({ + t3: Option.some(T3_TOKEN), + users: { [T3_TOKEN]: "kin-bot", [GIT_PASSWORD]: "kin-bot" }, + gh: ghStatus("kin-bot"), + git: gitFill(GIT_PASSWORD), + ssh: (key) => answer({ stderr: key === undefined ? sshHi("kin-bot") : SSH_DENIED }), + keys: { id_ed25519: KEY, "id_ed25519.pub": "ssh-ed25519 AAAA kin-bot", config: "Host *\n" }, +}); + +/** 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 home = yield* fs.makeTempDirectoryScoped(); + yield* fs.makeDirectory(path.join(home, ".ssh")); + yield* fs.makeDirectory(path.join(home, ".ssh", "sockets")); + for (const [name, text] of Object.entries(machine.keys)) { + yield* fs.writeFileString(path.join(home, ".ssh", name), text); + } + const runs: Array = []; + const vcs = VcsProcess.of({ + run: (input) => { + runs.push(input); + if (input.command === "gh") return machine.gh; + if (input.command === "git") return machine.git; + const key = input.args.indexOf("-i"); + return machine.ssh(key === -1 ? undefined : input.args[key + 1]); + }, + }); + const github = fakeGitHub((request) => { + const token = request.authorization?.replace(/^Bearer /, "") ?? ""; + const user = machine.users[token]; + return typeof user === "string" + ? { status: 200, body: { login: user } } + : { status: user ?? 401, body: { message: "Bad credentials" } }; + }); + const slot = Context.get( + yield* Layer.build(T3GitHubCredential.slotLayer), + T3GitHubCredential.T3GitHubCredential, + ); + if (machine.t3 !== "uninstalled") { + const t3 = machine.t3; + yield* slot.install( + Effect.succeed( + Option.map(t3, (token) => ({ token: Redacted.make(token), source: "gh" as const })), + ), + ); + } + const identity = yield* GitHubIdentityProbe.make.pipe( + Effect.provideService(VcsProcess, vcs), + Effect.provideService(HostProcess.HomeDirectory, home), + Effect.provideService(T3GitHubCredential.T3GitHubCredential, slot), + Effect.provide(github.layer), + ); + 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(ConfigProvider.layer(ConfigProvider.fromEnv({ env: {} }))), + Layer.provide(scopeLayer()), + ), + ); + const check = Context.get(hygiene, CredentialHygiene.CredentialHygiene).check(ORG); + return { identities: identity.identities, check, runs, home, requests: github.requests }; + }); + +const run = (effect: Effect.Effect) => + effect.pipe(Effect.scoped, Effect.provide(NodeServices.layer)); + +describe("GitHubIdentityProbe", () => { + it.effect("reports all four sources, and the check passes with only the bot's login", () => + run( + Effect.gen(function* () { + const path = yield* Path.Path; + const { identities, check, runs, home } = yield* probe(botMachine()); + const found = yield* identities; + const probed = [...runs]; + assert.deepStrictEqual(found, { + t3: Option.some({ login: "kin-bot", source: "gh" }), + ghAccounts: ["kin-bot"], + gitCredential: Option.some("kin-bot"), + ssh: [sshHi("kin-bot"), SSH_DENIED], + }); + assert.deepInclude(yield* check, { passed: true, failures: [] }); + + // One run per source, plus one per private key: not the public key, config or directory. + assert.sameDeepMembers( + probed.map((input) => [input.command, ...input.args]), + [ + ["gh", "auth", "status", "--hostname", "github.com", "--json", "hosts"], + ["git", "credential", "fill"], + [...CredentialHygiene.GITHUB_SSH_PROBE], + [ + ...CredentialHygiene.GITHUB_SSH_PROBE.slice(0, -1), + "-o", + "IdentitiesOnly=yes", + "-i", + path.join(home, ".ssh", "id_ed25519"), + "git@github.com", + ], + ], + ); + for (const input of probed) { + assert.strictEqual(input.cwd, path.parse(home).root); + assert.isAbove(input.timeoutMs ?? 0, 0); + assert.isAbove(input.maxOutputBytes ?? 0, 0); + } + const git = probed.find((input) => input.command === "git"); + assert.strictEqual(git?.stdin, "protocol=https\nhost=github.com\n\n"); + assert.deepInclude(git?.env, { GIT_TERMINAL_PROMPT: "0" }); + for (const input of probed.filter((input) => input.command === "ssh")) { + assert.deepInclude(input.env, { SSH_AUTH_SOCK: "" }); + } + }), + ), + ); + + it.effect("reads no login from sources that have none", () => + run( + Effect.gen(function* () { + const { identities, check } = yield* probe({ + ...botMachine(), + t3: Option.none(), + gh: missing("gh"), + git: answer({ + exitCode: 128, + stderr: + "fatal: could not read Username for 'https://github.com': terminal prompts disabled", + }), + ssh: () => answer({ stderr: SSH_DENIED }), + keys: {}, + }); + assert.deepStrictEqual(yield* identities, { + t3: Option.none(), + ghAccounts: [], + gitCredential: Option.none(), + ssh: [SSH_DENIED], + }); + assert.deepInclude(yield* check, { passed: true, identities: [] }); + }), + ), + ); + + it.effect.each([ + ["T3's credential", { users: { [T3_TOKEN]: "Nishchay", [GIT_PASSWORD]: "kin-bot" } }], + ["an inactive gh account", { gh: ghStatus("kin-bot", "Nishchay") }], + ["the git credential helper", { users: { [T3_TOKEN]: "kin-bot", [GIT_PASSWORD]: "nishchay" } }], + [ + "SSH without a key", + { + ssh: (key: string | undefined) => + answer({ stderr: key === undefined ? sshHi("Nishchay") : SSH_DENIED }), + }, + ], + [ + "one key in ~/.ssh", + { + ssh: (key: string | undefined) => + answer({ + stderr: key?.endsWith("owner_ed25519") === true ? sshHi("Nishchay") : SSH_DENIED, + }), + keys: { ...botMachine().keys, owner_ed25519: KEY }, + }, + ], + ] as const)("fails as owner_identity when %s is the owner", ([, change]) => + run( + Effect.gen(function* () { + const { check } = yield* probe({ ...botMachine(), ...change }); + assert.deepInclude(yield* check, { passed: false, failures: ["owner_identity"] }); + }), + ), + ); + + it.effect.each([ + ["T3's reader isn't installed", { t3: "uninstalled" }], + ["GitHub refuses T3's credential", { users: { [GIT_PASSWORD]: "kin-bot" } }], + ["gh is too old for --json", { gh: answer({ exitCode: 1, stderr: "unknown flag: --json" }) }], + ["gh answers something else", { gh: answer({ stdout: "github.com\n Logged in" }) }], + ["gh times out", { gh: Effect.fail(timeout("gh")) }], + [ + "git credential fill fails", + { git: answer({ exitCode: 1, stderr: "fatal: credential helper crashed" }) }, + ], + ["the helper's password is refused", { users: { [T3_TOKEN]: "kin-bot" } }], + ["GitHub is down for the helper", { users: { [T3_TOKEN]: "kin-bot", [GIT_PASSWORD]: 502 } }], + ["an SSH probe times out", { ssh: () => Effect.fail(timeout("ssh")) }], + ] as const)("fails closed when %s", ([, change]) => + run( + Effect.gen(function* () { + const { identities, check } = yield* probe({ ...botMachine(), ...change } as Machine); + const error = yield* identities.pipe(Effect.flip); + assert.strictEqual(error._tag, "T3GitHubIdentityError"); + assert.deepInclude(yield* check, { passed: false, failures: ["identity_unknown"] }); + }), + ), + ); + + it.effect("never puts the helper's password in a log, error or report", () => { + const logged: Array = []; + const logger = Logger.make(({ message, cause }) => { + logged.push(JSON.stringify(message), String(cause)); + }); + return run( + Effect.gen(function* () { + const seen: Array = []; + for (const users of [ + { [T3_TOKEN]: "kin-bot", [GIT_PASSWORD]: "kin-bot" }, + { [T3_TOKEN]: "kin-bot" }, + { [T3_TOKEN]: "kin-bot", [GIT_PASSWORD]: 500 }, + ]) { + const { identities, check, requests } = yield* probe({ ...botMachine(), users }); + const result = yield* Effect.result(identities); + seen.push(JSON.stringify(result), String(result), JSON.stringify(yield* check)); + yield* Effect.logWarning("probe", result); + // It does reach GitHub, as a bearer token and nowhere else. + assert.include( + requests.map((request) => request.authorization), + `Bearer ${GIT_PASSWORD}`, + ); + } + const haystack = [...seen, ...logged].join("\n"); + assert.include(haystack, "git_credential"); + assert.notInclude(haystack, GIT_PASSWORD); + }).pipe(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..1d2ae7d009f1d --- /dev/null +++ b/apps/server/src/kin/approvals/GitHubIdentityProbe.ts @@ -0,0 +1,296 @@ +import * as HostProcess from "@t3tools/shared/HostProcess"; +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 { GITHUB_SSH_PROBE, T3GitHubIdentity, T3GitHubIdentityError } from "./CredentialHygiene.ts"; +import { T3GitHubCredential } 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; +// Below `VcsProcess`'s four slots for non-git commands, which T3's own gh runs share. +const SSH_CONCURRENCY = 2; +const KEY_HEADER_BYTES = 64; +const PRIVATE_KEY_HEADER = /^-----BEGIN [A-Z0-9 ]*PRIVATE KEY-----/; + +// 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"; + +const GitHubUser = Schema.Struct({ login: Schema.String }); +const GhAuthStatus = Schema.fromJsonString( + Schema.Struct({ + hosts: Schema.Record(Schema.String, Schema.Array(Schema.Struct({ login: Schema.String }))), + }), +); +const decodeGhAuthStatus = Schema.decodeUnknownOption(GhAuthStatus); + +/** `GITHUB_SSH_PROBE` offering only `key`, so GitHub answers for that key alone. */ +const sshProbeWithKey = (key: string) => [ + ...GITHUB_SSH_PROBE.slice(0, -1), + "-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 home = yield* HostProcess.HomeDirectory; + + // From the filesystem root, outside any repository, so no repository config picks the helper. + const cwd = path.parse(home).root; + 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) => response.body.login.trim()), + Effect.filterOrFail( + (login) => login !== "", + () => unreadable(source, "GET /user named no login"), + ), + ); + + const t3 = t3Credential.read.pipe( + Effect.mapError((error) => unreadable("t3", error.reason)), + Effect.flatMap( + Option.match({ + onNone: () => Effect.succeedNone, + onSome: (held) => + loginOf("t3", held.token).pipe( + Effect.map((login) => Option.some({ login, source: held.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 /not logged in/i.test(output.stderr) + ? Effect.succeed>([]) + : 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())); + return logins.includes("") + ? Effect.fail(unreadable("gh", "an account has no login")) + : Effect.succeed>(logins); + }, + }); + }), + ); + + // The password stays in this pipeline: it is wrapped at once, sent to GitHub and dropped. + const gitCredential = run({ + operation: "KinGitHubIdentity.gitCredential", + command: "git", + args: ["credential", "fill"], + stdin: `protocol=https\nhost=${HOST}\n\n`, + 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(Option.none>()); + // With no helper answering, git falls back to a terminal prompt, which is turned off. + if (output.exitCode !== 0) { + return /terminal prompts disabled/i.test(output.stderr) + ? Effect.succeed(Option.none>()) + : Effect.fail( + unreadable("git_credential", `git credential fill exited ${output.exitCode}`), + ); + } + const line = output.stdout.split("\n").find((entry) => entry.startsWith("password=")); + const password = line?.slice("password=".length).replace(/\r$/, "") ?? ""; + return password === "" + ? Effect.fail(unreadable("git_credential", "the helper answered without a password")) + : Effect.succeedSome(Redacted.make(password)); + }), + Effect.flatMap( + Option.match({ + onNone: () => Effect.succeedNone, + onSome: (password) => loginOf("git_credential", password).pipe(Effect.asSome), + }), + ), + ); + + /** + * Regular files in `~/.ssh` that start like a private key, by their first bytes only. Keys + * named only by `IdentityFile` under another `Host` alias in `~/.ssh/config`, or kept outside + * `~/.ssh`, aren't found, so a login reachable only through them isn't probed. + */ + const privateKeys = Effect.gen(function* () { + const dir = path.join(home, ".ssh"); + const names = yield* fs + .readDirectory(dir) + .pipe( + Effect.catch((error) => + isNotFound(error) + ? Effect.succeed>([]) + : Effect.fail(unreadable("ssh", "~/.ssh can't be listed")), + ), + ); + const keys: Array = []; + for (const name of names.toSorted()) { + if (name.endsWith(".pub")) continue; + const file = path.join(dir, name); + const unreadableKey = (error: PlatformError.PlatformError) => + isNotFound(error) + ? Effect.succeed(false) + : Effect.fail(unreadable("ssh", `~/.ssh/${name} can't be read`)); + const isFile = yield* fs.stat(file).pipe( + Effect.map((info) => info.type === "File"), + Effect.catch(unreadableKey), + ); + if (!isFile) continue; + const isKey = yield* fs.open(file, { flag: "r" }).pipe( + Effect.flatMap((handle) => handle.readAlloc(KEY_HEADER_BYTES)), + Effect.map( + Option.match({ + onNone: () => false, + onSome: (bytes) => PRIVATE_KEY_HEADER.test(new TextDecoder().decode(bytes)), + }), + ), + Effect.scoped, + Effect.catch(unreadableKey), + ); + if (isKey) keys.push(file); + } + if (keys.length > MAX_SSH_KEYS) { + return yield* unreadable("ssh", `more than ${MAX_SSH_KEYS} private keys in ~/.ssh`); + } + return keys; + }); + + const sshAnswer = (argv: ReadonlyArray) => + run({ + operation: "KinGitHubIdentity.ssh", + command: argv[0] ?? "ssh", + 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"), + ), + ); + + const ssh = Effect.gen(function* () { + const keys = yield* privateKeys; + const plain = yield* sshAnswer(GITHUB_SSH_PROBE); + const perKey = yield* Effect.forEach(keys, (key) => sshAnswer(sshProbeWithKey(key)), { + concurrency: SSH_CONCURRENCY, + }); + return [plain, ...perKey]; + }); + + return T3GitHubIdentity.of({ + // T3's credential first: until the server installs its reader, nothing is spawned. + identities: Effect.gen(function* () { + const t3Identity = yield* t3; + const rest = yield* Effect.all( + { ghAccounts, gitCredential, ssh }, + { concurrency: "unbounded" }, + ); + return { t3: t3Identity, ...rest }; + }).pipe(Effect.withSpan("KinGitHubIdentity.identities")), + }); +}); + +export const layer = Layer.effect(T3GitHubIdentity, make); diff --git a/apps/server/src/kin/approvals/T3GitHubCredential.ts b/apps/server/src/kin/approvals/T3GitHubCredential.ts new file mode 100644 index 0000000000000..fd08483b484f2 --- /dev/null +++ b/apps/server/src/kin/approvals/T3GitHubCredential.ts @@ -0,0 +1,88 @@ +import * as GitHubDriver from "@t3tools/source-control-github/server/driver"; +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 type * as Redacted from "effect/Redacted"; +import * as Ref from "effect/Ref"; +import * as Schema from "effect/Schema"; + +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}).`; + } +} + +export interface T3HeldCredential { + readonly token: Redacted.Redacted; + readonly source: string; +} +type ReadT3Credential = Effect.Effect, T3GitHubCredentialError>; + +/** + * T3's own credential for github.com, as `GitHubRequestPolicy.credential` picks it. The executor + * is built before the server's source control host, so the server installs the reader later + * (`install`); until then every read fails, and so does the credential check. + */ +export class T3GitHubCredential extends Context.Service< + T3GitHubCredential, + { + readonly read: ReadT3Credential; + readonly install: (read: ReadT3Credential) => Effect.Effect; + } +>()("t3/kin/approvals/T3GitHubCredential") {} + +export const slotLayer = Layer.effect( + T3GitHubCredential, + Effect.gen(function* () { + const reader = yield* Ref.make(Option.none()); + return T3GitHubCredential.of({ + read: Ref.get(reader).pipe( + Effect.flatMap( + Option.match({ + onNone: () => + Effect.fail(new T3GitHubCredentialError({ reason: "the reader isn't installed" })), + onSome: (read) => read, + }), + ), + ), + install: Effect.fn("T3GitHubCredential.install")(function* (read: ReadT3Credential) { + yield* Ref.set(reader, Option.some(read)); + }), + }); + }), +); + +/** What `credential` fails with when T3 simply has no GitHub credential for the host. */ +const NO_CREDENTIAL = new Set([ + "GitHubCliMissingError", + "GitHubNotSignedInError", + "GitHubHostDisabledError", +]); + +/** + * Reads through a second `GitHubRequestPolicy`, since the driver builds its own privately. Both + * share the server's settings, secrets and rate-limit pause; only their token caches differ. + */ +export const installLayer = Layer.effectDiscard( + Effect.gen(function* () { + const slot = yield* T3GitHubCredential; + const policy = yield* GitHubDriver.GitHubRequestPolicy; + yield* slot.install( + policy.credential("github.com").pipe( + Effect.map((held) => + Option.some({ token: held.token, source: held.source ?? "unknown" }), + ), + Effect.catch((error) => + NO_CREDENTIAL.has(error._tag) + ? Effect.succeedNone + : Effect.fail(new T3GitHubCredentialError({ reason: error._tag })), + ), + ), + ); + }), +).pipe(Layer.provide(GitHubDriver.gitHubRequestPolicyLayer)); diff --git a/apps/server/src/kin/approvals/layer.ts b/apps/server/src/kin/approvals/layer.ts index 8737e3f55befc..cc20a9dcbfb81 100644 --- a/apps/server/src/kin/approvals/layer.ts +++ b/apps/server/src/kin/approvals/layer.ts @@ -4,6 +4,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"; @@ -16,6 +17,8 @@ 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 * 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"; @@ -42,7 +45,13 @@ const registerApproverKeyHandler = Effect.map( // The bot App reads, the owner merge token writes, and the hygiene check gates both. const github = Layer.mergeAll(KinBotReads.layer, CredentialHygiene.layer).pipe( Layer.provideMerge(OwnerMergeClient.layer), - Layer.provide(CredentialHygiene.identityUnavailableLayer), + // Its process runner and filesystem are the server's, by layer memoization. + Layer.provide( + GitHubIdentityProbe.layer.pipe( + Layer.provide(VcsProcess.layer), + Layer.provide(NodeServices.layer), + ), + ), Layer.provide(KinGitHubApp.layer), Layer.provideMerge( Layer.mergeAll( @@ -116,6 +125,8 @@ export const layer = Layer.mergeAll( ).pipe( Layer.provideMerge(ApproverKeys.layer), Layer.provideMerge(PendingActions.layer), + // In Kin's services too: the server installs T3's credential reader into this one slot. + Layer.provideMerge(T3GitHubCredential.slotLayer), Layer.provide( Layer.mergeAll( AttestationTrust.layer.pipe(Layer.provide(FetchHttpClient.layer)), diff --git a/apps/server/src/kin/chat/executionNotices.ts b/apps/server/src/kin/chat/executionNotices.ts index d359558d47254..fe8441286914d 100644 --- a/apps/server/src/kin/chat/executionNotices.ts +++ b/apps/server/src/kin/chat/executionNotices.ts @@ -1,9 +1,13 @@ -import { KinChatId, KinMessageId, type KinPendingAction } from "@t3tools/contracts"; +import { KinChatId, KinMessageId, type KinOrgId, type KinPendingAction } from "@t3tools/contracts"; import * as Effect from "effect/Effect"; import * as Layer from "effect/Layer"; import * as SqlClient from "effect/sql/SqlClient"; import { KinExecutionNotices, type KinExecutionOutcome } from "../approvals/ActionExecutor.ts"; +import type { + CredentialHygieneFailure, + CredentialHygieneReport, +} from "../approvals/CredentialHygiene.ts"; import { KinChatService } from "./KinChatService.ts"; const subject = (action: KinPendingAction) => { @@ -37,13 +41,67 @@ const executionNoticeBody = (input: { return `Didn't carry out ${what}: ${input.error ?? "no reason given"}. Approve a new request to try again.`; }; +const FAILURE_TEXT: Record string> = + { + env_token: (report) => + report.envTokenNames.length > 0 + ? `${report.envTokenNames.join(", ")} ${report.envTokenNames.length === 1 ? "is" : "are"} set in the server's environment` + : "T3 takes its GitHub token from the server's environment", + 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). */ +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 reasons = 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: ${reasons.join("; ")}.`, + ...(logins.length > 0 ? [`GitHub logins found: ${logins.join(", ")}.`] : []), + "Kin checks again before each action and every hour.", + ].join("\n"); +}; + /** Posts each result once (the message id is the action's) to the lead's direct chat. */ export const layer = Layer.effect( KinExecutionNotices, 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`; return KinExecutionNotices.of({ + credentialCheck: Effect.fn("KinExecutionNotices.credentialCheck")( + function* ({ orgId, report }) { + 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(`kin-hygiene:${orgId}:${report.checkedAt}`), + body: credentialCheckBody(orgId, report), + }); + }, + Effect.catchCause((cause) => Effect.logWarning("Kin credential notice not posted", cause)), + ), finished: Effect.fn("KinExecutionNotices.finished")( function* (input) { const { action } = input; diff --git a/apps/server/src/kin/layer.ts b/apps/server/src/kin/layer.ts index 200d560edde6e..d8dad7ab9b772 100644 --- a/apps/server/src/kin/layer.ts +++ b/apps/server/src/kin/layer.ts @@ -10,6 +10,7 @@ import * as HttpRouter from "effect/http/HttpRouter"; import * as Option from "effect/Option"; import * as KinChat from "./chat/layer.ts"; import * as KinApprovals from "./approvals/layer.ts"; +import * as T3GitHubCredential from "./approvals/T3GitHubCredential.ts"; import * as ExecutionNotices from "./chat/executionNotices.ts"; import * as KinPush from "./push/layer.ts"; import { KinChatHub } from "./chat/KinChatHub.ts"; @@ -116,8 +117,30 @@ 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) + ? T3GitHubCredential.installLayer.pipe( + Layer.provide(Layer.succeedContext(kin.services.value)), + 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 cde8800c485ca..b470990516c68 100644 --- a/apps/server/src/kin/mcp/layer.test.ts +++ b/apps/server/src/kin/mcp/layer.test.ts @@ -39,6 +39,9 @@ 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 SourceControlRateLimit from "@t3tools/source-control-core/server/SourceControlRateLimit"; +import * as FetchHttpClient from "effect/http/FetchHttpClient"; import * as Kin from "../layer.ts"; import { ServerActivation } from "../../serverActivation.ts"; import { KinIdentity } from "../chat/KinIdentity.ts"; @@ -112,6 +115,13 @@ const toolHandlerServices = Layer.mergeAll( Layer.mock(ServerSecretStore)({}), ServerSettings.layerTest(), Layer.mock(SourceControlRepositoryService)({}), + // Kin installs T3's GitHub credential reader on these; nothing here reads it. + Layer.succeed( + SourceControlHost.SourceControlHost, + {} as SourceControlHost.SourceControlHost["Service"], + ), + SourceControlRateLimit.layer, + FetchHttpClient.layer, Layer.mock(ThreadLaunchService)({}), Layer.mock(ThreadManagementService)({ getThreadShell: (id) => Effect.succeed(liveThreadShell(id)), diff --git a/docs/kin/specs/approvals.md b/docs/kin/specs/approvals.md index abe72e45b1555..9128191e91dba 100644 --- a/docs/kin/specs/approvals.md +++ b/docs/kin/specs/approvals.md @@ -531,7 +531,7 @@ At startup and hourly, the executor checks every GitHub identity reachable from 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 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. 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`). +The identity probe is the `T3GitHubIdentity` port; `GitHubIdentityProbe.ts` is its live layer. The GitHub driver builds its `GitHubRequestPolicy` privately, so `@t3tools/source-control-github` also exports the policy and its layer. The executor is built before the server's source control host, so the server installs the reader of T3's credential afterwards (`T3GitHubCredential.ts`), and until it does every probe fails. 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 --json hosts` (gh 2.81 or later), 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`). The hourly check audits and tells the owner only when its result changes, including when it passes again. 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. diff --git a/packages/source-control-github/src/server/driver.ts b/packages/source-control-github/src/server/driver.ts index 45af994eca26e..d5c9cabfe6792 100644 --- a/packages/source-control-github/src/server/driver.ts +++ b/packages/source-control-github/src/server/driver.ts @@ -109,3 +109,6 @@ export const driver = defineSourceControlDriver({ definition, make: withOwnServices(makeInstance, services), }); + +export { GitHubRequestPolicy } from "./GitHubRequestPolicy.ts"; +export { layerWithDependencies as gitHubRequestPolicyLayer } from "./GitHubRest.ts"; From dd225bcdb7bdbf2ee693242abd18fe4bfbabb8fa Mon Sep 17 00:00:00 2001 From: Surajsm60720 <1ds22is166@dsce.edu.in> Date: Mon, 9 Nov 2026 09:35:15 +0530 Subject: [PATCH 2/7] fix(kin): identity probe fails closed on every source it can't enumerate The APPR-16 security review found ways the probe could report "no owner login" while an owner credential stayed usable. Each source now fails closed when it can't be fully read: - ssh: `ssh -G` first; an IdentityAgent or PKCS#11 provider fails, and its IdentityFiles are probed. `ssh-keygen -y -P ""` decides what a key is; passphrase or unloadable private keys fail. ~/.ssh is scanned two folders deep. - t3: every stored credential (saved token, app session, GH_TOKEN, gh auth token) is reported, host disabled or not, read from Settings without refreshing. A session that needs a refresh fails. - git: more than one helper fails; a configured helper plus "terminal prompts disabled" fails. - The slot waits up to 30 s for the reader instead of failing at startup. - check shares one single-flight probe for 60 s; requirePassing after the claim always probes afresh. - gh logins are capped to GitHub logins, the dead "not logged in" branch is gone, install is once-only, orgs without a merge token get no notice, and a restart still sends "passes again". Co-Authored-By: Claude Opus 5.5 --- .../src/kin/approvals/ActionExecutor.test.ts | 56 +- .../src/kin/approvals/ActionExecutor.ts | 50 +- .../kin/approvals/CredentialHygiene.test.ts | 105 +++- .../src/kin/approvals/CredentialHygiene.ts | 61 +- .../kin/approvals/GitHubIdentityProbe.test.ts | 529 +++++++++++++++--- .../src/kin/approvals/GitHubIdentityProbe.ts | 381 +++++++++---- .../kin/approvals/T3GitHubCredential.test.ts | 131 +++++ .../src/kin/approvals/T3GitHubCredential.ts | 122 ++-- apps/server/src/kin/chat/executionNotices.ts | 8 +- .../src/kin/github/OwnerMergeClient.test.ts | 2 +- apps/server/src/kin/mcp/layer.test.ts | 6 +- docs/kin/specs/approvals.md | 25 +- .../src/server/GitHubAppSignIn.ts | 3 +- .../src/server/driver.ts | 3 +- 14 files changed, 1170 insertions(+), 312 deletions(-) create mode 100644 apps/server/src/kin/approvals/T3GitHubCredential.test.ts diff --git a/apps/server/src/kin/approvals/ActionExecutor.test.ts b/apps/server/src/kin/approvals/ActionExecutor.test.ts index c0c24f9b71451..c4e5c10477ec7 100644 --- a/apps/server/src/kin/approvals/ActionExecutor.test.ts +++ b/apps/server/src/kin/approvals/ActionExecutor.test.ts @@ -156,7 +156,11 @@ function harness() { const notices: Array<{ readonly actionId: string; readonly outcome: 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 sent: Array<{ + readonly orgId: string; + readonly failures: ReadonlyArray; + readonly recoveredFrom?: number; + }>; posted?: (failures: ReadonlyArray) => Effect.Effect; } = { sent: [] }; const noticed = Deferred.makeUnsafe(); @@ -198,7 +202,7 @@ function harness() { checks.failing ? Effect.fail(new CredentialHygiene.T3GitHubIdentityError({ cause: "probe failed" })) : Effect.succeed({ - t3: Option.some({ login: "kin-bot", source: "app" }), + t3: [{ login: "kin-bot", source: "app" }], ghAccounts: ["kin-bot"], gitCredential: Option.none(), ssh: ["git@github.com: Permission denied (publickey)."], @@ -220,9 +224,13 @@ function harness() { const notifications = Layer.succeed( ActionExecutor.KinExecutionNotices, ActionExecutor.KinExecutionNotices.of({ - credentialCheck: ({ orgId: org, report }) => + credentialCheck: ({ orgId: org, report, recoveredFrom }) => Effect.sync(() => - credentialNotices.sent.push({ orgId: org, failures: report.failures }), + credentialNotices.sent.push({ + orgId: org, + failures: report.failures, + ...(recoveredFrom === undefined ? {} : { recoveredFrom }), + }), ).pipe(Effect.andThen(credentialNotices.posted?.(report.failures) ?? Effect.void)), finished: ({ action, outcome }) => Effect.sync(() => void notices.push({ actionId: action.id, outcome })).pipe( @@ -534,8 +542,11 @@ describe("KinActionExecutor", () => { failed.map((entry) => entry.result), ["identity_unknown"], ); - // 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)); @@ -670,10 +681,6 @@ describe("KinActionExecutor", () => { assert.deepStrictEqual(yield* Queue.take(posted), []); }).pipe(Effect.provide(ActionExecutor.layer), Effect.scoped); assert.strictEqual(checks.count, 3); - assert.deepStrictEqual(credentialNotices.sent, [ - { orgId, failures: ["identity_unknown"] }, - { orgId, failures: [] }, - ]); const failed = (yield* auditEntries).filter( (entry) => entry.event === "credential_check_failed", ); @@ -681,10 +688,41 @@ describe("KinActionExecutor", () => { failed.map((entry) => [entry.actionId, entry.result]), [[undefined, "identity_unknown"]], ); + 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 says it passes again after a restart", () => { + 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); + const [failure] = (yield* auditEntries).filter( + (entry) => entry.event === "credential_check_failed", + ); + + // 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, noticed, deps } = harness(); return Effect.gen(function* () { diff --git a/apps/server/src/kin/approvals/ActionExecutor.ts b/apps/server/src/kin/approvals/ActionExecutor.ts index 8451a51f77c24..298423425095d 100644 --- a/apps/server/src/kin/approvals/ActionExecutor.ts +++ b/apps/server/src/kin/approvals/ActionExecutor.ts @@ -51,6 +51,8 @@ export class KinExecutionNotices extends Context.Service< 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; } >()("t3/kin/approvals/ActionExecutor/KinExecutionNotices") {} @@ -265,9 +267,9 @@ export const make = Effect.gen(function* () { return { _tag: "held", reason: "roles_unavailable" } as KinExecuteResult; 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. - const check = yield* hygiene.requirePassing(action.orgId).pipe(Effect.flip, Effect.option); - if (Option.isSome(check)) { - const failures = check.value.failures.join(","); + const check = yield* hygiene.check(action.orgId); + if (!check.passed) { + const failures = check.failures.join(","); if (auditedHolds.get(action.id) !== failures) { auditedHolds.set(action.id, failures); yield* audit @@ -315,23 +317,43 @@ export const make = Effect.gen(function* () { 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: an execution only starts on a pass. + const lastCredentialEvent = (orgId: KinOrgId) => + sql<{ readonly seq: number; readonly event: string }>` + SELECT seq, event 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.seq : 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].join(","); - if ((reportedChecks.get(orgId) ?? "") !== result) { - reportedChecks.set(orgId, result); - if (!report.passed) { - yield* audit - .append({ - orgId, - event: "credential_check_failed", - result: report.failures.join(","), - }) - .pipe(Effect.catch((error) => Effect.logWarning("Kin audit not written", error))); - } + const previous = reportedChecks.get(orgId); + 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: report.failures.join(",") }) + .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); + if (previous !== undefined || failedAt !== undefined) { + yield* notices.credentialCheck({ + orgId, + report, + ...(failedAt === undefined ? {} : { recoveredFrom: failedAt }), + }); + } } return { orgId, report }; }), diff --git a/apps/server/src/kin/approvals/CredentialHygiene.test.ts b/apps/server/src/kin/approvals/CredentialHygiene.test.ts index 7a3c26bccb0ea..5af597cd00900 100644 --- a/apps/server/src/kin/approvals/CredentialHygiene.test.ts +++ b/apps/server/src/kin/approvals/CredentialHygiene.test.ts @@ -2,6 +2,7 @@ import { assert, it } from "@effect/vitest"; import type { KinOrgId } from "@t3tools/contracts"; import * as ConfigProvider from "effect/ConfigProvider"; 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"; @@ -20,7 +21,7 @@ const SSH_DENIED = "git@github.com: Permission denied (publickey)."; type Identity = | { - readonly t3?: Option.Option<{ readonly login: string; readonly source: string }>; + readonly t3?: ReadonlyArray<{ readonly login: string; readonly source: string }>; readonly gh?: ReadonlyArray; readonly git?: Option.Option; readonly ssh?: ReadonlyArray; @@ -32,7 +33,7 @@ const setup = (input: { readonly env?: Record; readonly mergeToken?: { readonly login: string; readonly expiresAt: number } | null; }) => { - const state = { identity: input.identity }; + const state = { identity: input.identity, probes: 0 }; const mergeToken = input.mergeToken === undefined ? { login: "Nishchay", expiresAt: NOW + 30 * DAY } @@ -51,15 +52,21 @@ 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: Effect.suspend(() => { + state.probes += 1; + // Takes a second, so a check that starts meanwhile shares it. + return Effect.sleep("1 second"); + }).pipe( + Effect.andThen(() => + state.identity === "unreachable" + ? Effect.fail(new CredentialHygiene.T3GitHubIdentityError({ cause: "offline" })) + : Effect.succeed({ + t3: state.identity.t3 ?? [], + ghAccounts: state.identity.gh ?? [], + gitCredential: state.identity.git ?? Option.none(), + ssh: state.identity.ssh ?? [SSH_DENIED], + }), + ), ), }), ); @@ -75,7 +82,7 @@ const setup = (input: { }; 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 +91,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 +128,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,7 +211,7 @@ 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"], ], ["GH_TOKEN in the environment", { identity: bot, env: { GH_TOKEN: "ghp_x" } }, ["env_token"]], @@ -192,7 +222,7 @@ describe("CredentialHygiene", () => { ], [ "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"]], @@ -210,6 +240,39 @@ 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("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 f872601b983c4..5243236540a29 100644 --- a/apps/server/src/kin/approvals/CredentialHygiene.ts +++ b/apps/server/src/kin/approvals/CredentialHygiene.ts @@ -78,10 +78,11 @@ export class T3GitHubIdentityError extends Schema.TaggedError; + readonly t3: ReadonlyArray<{ readonly login: string; readonly source: string }>; /** Every account `gh auth status --hostname github.com` lists, active or not. */ readonly ghAccounts: ReadonlyArray; /** @@ -91,8 +92,9 @@ export interface T3GitHubIdentities { readonly gitCredential: Option.Option; /** * 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` `ssh -G` lists, 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; } @@ -118,6 +120,7 @@ export interface CredentialHygieneReport { 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<{ @@ -171,9 +174,13 @@ export class CredentialHygieneFailedError extends Schema.TaggedError(); const warnings: Array<"merge_token_expiring"> = []; @@ -221,17 +234,17 @@ const make = Effect.gen(function* () { if (status._tag === "expired") failures.add("merge_token_expired"); if (status._tag === "active" && status.expiringSoon) warnings.push("merge_token_expiring"); - const probed = yield* identity.identities.pipe(Effect.option); + const probed = yield* probe.pipe(Effect.option); let t3Login: string | null = null; const found: Array = []; if (Option.isNone(probed)) { failures.add("identity_unknown"); } 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 }); + t3Login = t3[0]?.login ?? null; + for (const { login, source } of t3) { + if (source === "env") failures.add("env_token"); + found.push({ source: "t3", login }); } for (const login of ghAccounts) found.push({ source: "gh", login }); if (Option.isSome(gitCredential)) { @@ -243,15 +256,16 @@ const make = Effect.gen(function* () { 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 }); - } - } + 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"); @@ -263,7 +277,7 @@ const make = Effect.gen(function* () { warnings, envTokenNames: envNames, t3Login, - identities: found, + identities, checkedAt: yield* Clock.currentTimeMillis, }; yield* Effect.annotateCurrentSpan({ @@ -273,8 +287,13 @@ const make = Effect.gen(function* () { return report; }); + const check = Effect.fn("CredentialHygiene.check")(function* (orgId: KinOrgId) { + return yield* evaluate(orgId, cachedIdentities); + }); + 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, identity.identities); if (!report.passed) { return yield* new CredentialHygieneFailedError({ failures: report.failures }); } diff --git a/apps/server/src/kin/approvals/GitHubIdentityProbe.test.ts b/apps/server/src/kin/approvals/GitHubIdentityProbe.test.ts index 4151a4c71b122..79d8b25e323b1 100644 --- a/apps/server/src/kin/approvals/GitHubIdentityProbe.test.ts +++ b/apps/server/src/kin/approvals/GitHubIdentityProbe.test.ts @@ -10,6 +10,7 @@ import * as HostProcess from "@t3tools/shared/HostProcess"; import * as ConfigProvider from "effect/ConfigProvider"; 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"; @@ -18,6 +19,7 @@ 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 { HttpClient, HttpClientResponse } from "effect/http"; import { ChildProcessSpawner } from "effect/process"; import * as TestClock from "effect/testing/TestClock"; @@ -31,13 +33,25 @@ 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 T3_TOKEN = "gho_t3_token"; +const APP_TOKEN = "ghu_t3_app_token"; +const GH_TOKEN = "gho_gh_keyring_token"; +const OWNER_TOKEN = "gho_owner_token"; const GIT_PASSWORD = "gho_git_helper_password_never_logged"; +// 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"; type Run = Effect.Effect; const answer = (input: { @@ -52,15 +66,16 @@ const answer = (input: { stdoutTruncated: false, stderrTruncated: false, }); -const missing = (command: string): Run => +const spawnFailure = (command: string, reason: "NotFound" | "PermissionDenied"): Run => Effect.fail( new VcsProcessSpawnError({ operation: "test", command, cwd: "/", - cause: PlatformError.systemError({ _tag: "NotFound", module: "test", method: "spawn" }), + 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) => @@ -79,28 +94,95 @@ const ghStatus = (...logins: ReadonlyArray) => }); const gitFill = (password: string) => answer({ stdout: `protocol=https\nhost=github.com\nusername=x\npassword=${password}\n` }); +/** `ssh -G git@github.com` as OpenSSH prints it: identity files unexpanded. */ +const sshG = (...extra: ReadonlyArray) => + answer({ + stdout: [ + "user git", + "hostname github.com", + "port 22", + ...extra, + "identityfile ~/.ssh/id_rsa", + "identityfile ~/.ssh/id_ed25519", + "addkeystoagent false", + ].join("\n"), + }); +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(), + ...input, +}); /** The machine the probe looks at. Each test changes what one source answers. */ interface Machine { - t3: Option.Option | "uninstalled"; - /** The login GitHub's `GET /user` names for each token; any other token gets a 401. */ + t3: T3GitHubCredential.T3StoredCredentials | "uninstalled"; + env: Record; + /** The login `GET /user` names for each token; a number is the status, `hang` never answers. */ users: Record; gh: Run; + /** `gh auth token`, for the `--user` account or the active one. */ + ghToken: (account: string | undefined) => Run; + gitHelpers: Run; git: Run; + sshConfig: Run; /** Answers one SSH probe; `key` is the `-i` file, or undefined for the probe as is. */ ssh: (key: string | undefined) => Run; - keys: Record; + /** Files under the home directory, by relative path. */ + files: Record; } const botMachine = (): Machine => ({ - t3: Option.some(T3_TOKEN), - users: { [T3_TOKEN]: "kin-bot", [GIT_PASSWORD]: "kin-bot" }, + t3: stored({ app: session(APP_TOKEN) }), + env: {}, + users: { [APP_TOKEN]: "kin-bot", [GH_TOKEN]: "kin-bot", [GIT_PASSWORD]: "kin-bot" }, gh: ghStatus("kin-bot"), + ghToken: () => answer({ stdout: `${GH_TOKEN}\n` }), + gitHelpers: answer({ stdout: "credential.helper osxkeychain\n" }), git: gitFill(GIT_PASSWORD), + sshConfig: sshG(), ssh: (key) => answer({ stderr: key === undefined ? sshHi("kin-bot") : SSH_DENIED }), - keys: { id_ed25519: KEY, "id_ed25519.pub": "ssh-ed25519 AAAA kin-bot", config: "Host *\n" }, + files: { + ".ssh/id_ed25519": KEY, + ".ssh/id_ed25519.pub": "ssh-ed25519 AAAA kin-bot", + ".ssh/config": "Host *\n", + ".ssh/known_hosts": "github.com ssh-ed25519 AAAA\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}` }); + }), + ); + /** Builds the live probe over `machine` in a fresh home, and the check over that probe. */ const probe = (machine: Machine) => Effect.gen(function* () { @@ -108,45 +190,60 @@ const probe = (machine: Machine) => const fs = yield* FileSystem.FileSystem; const path = yield* Path.Path; const home = yield* fs.makeTempDirectoryScoped(); - yield* fs.makeDirectory(path.join(home, ".ssh")); - yield* fs.makeDirectory(path.join(home, ".ssh", "sockets")); - for (const [name, text] of Object.entries(machine.keys)) { - yield* fs.writeFileString(path.join(home, ".ssh", name), text); + yield* fs.makeDirectory(path.join(home, ".ssh", "sockets"), { recursive: true }); + for (const [name, text] of Object.entries(machine.files)) { + yield* fs.makeDirectory(path.dirname(path.join(home, name)), { recursive: true }); + yield* fs.writeFileString(path.join(home, name), text); } const runs: Array = []; const vcs = VcsProcess.of({ run: (input) => { runs.push(input); - if (input.command === "gh") return machine.gh; - if (input.command === "git") return machine.git; + 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") return first === "config" ? machine.gitHelpers : machine.git; + if (input.command === "ssh-keygen") return keygen(fs, input.args.at(-1) ?? ""); + if (first === "-G") return machine.sshConfig; const key = input.args.indexOf("-i"); return machine.ssh(key === -1 ? undefined : input.args[key + 1]); }, }); - const github = fakeGitHub((request) => { - const token = request.authorization?.replace(/^Bearer /, "") ?? ""; - const user = machine.users[token]; - return typeof user === "string" - ? { status: 200, body: { login: user } } - : { status: user ?? 401, body: { message: "Bad credentials" } }; - }); + 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 } : { message: "Bad credentials" }; + return Effect.succeed( + HttpClientResponse.fromWeb( + request, + new Response(JSON.stringify(body), { + status: typeof user === "string" ? 200 : (user ?? 401), + headers: { "content-type": "application/json" }, + }), + ), + ); + }), + ); const slot = Context.get( yield* Layer.build(T3GitHubCredential.slotLayer), T3GitHubCredential.T3GitHubCredential, ); - if (machine.t3 !== "uninstalled") { - const t3 = machine.t3; - yield* slot.install( - Effect.succeed( - Option.map(t3, (token) => ({ token: Redacted.make(token), source: "gh" as const })), - ), - ); - } + if (machine.t3 !== "uninstalled") yield* slot.install(Effect.succeed(machine.t3)); const identity = yield* GitHubIdentityProbe.make.pipe( Effect.provideService(VcsProcess, vcs), Effect.provideService(HostProcess.HomeDirectory, home), + Effect.provideService(HostProcess.Environment, machine.env), + Effect.provideService(HostProcess.Username, "kin"), + Effect.provideService(HostProcess.UserId, 501), Effect.provideService(T3GitHubCredential.T3GitHubCredential, slot), - Effect.provide(github.layer), + Effect.provide(github), ); const keychain = memoryKeychain({ [`kin-owner-merge/${ORG}`]: JSON.stringify({ @@ -167,14 +264,16 @@ const probe = (machine: Machine) => ), ); const check = Context.get(hygiene, CredentialHygiene.CredentialHygiene).check(ORG); - return { identities: identity.identities, check, runs, home, requests: github.requests }; + return { identities: identity.identities, check, runs, home, requests, slot }; }); const run = (effect: Effect.Effect) => effect.pipe(Effect.scoped, Effect.provide(NodeServices.layer)); +const argv = (input: VcsProcessInput) => [input.command, ...input.args]; + describe("GitHubIdentityProbe", () => { - it.effect("reports all four sources, and the check passes with only the bot's login", () => + it.effect("reports every source, and the check passes with only the bot's login", () => run( Effect.gen(function* () { const path = yield* Path.Path; @@ -182,41 +281,63 @@ describe("GitHubIdentityProbe", () => { const found = yield* identities; const probed = [...runs]; assert.deepStrictEqual(found, { - t3: Option.some({ login: "kin-bot", source: "gh" }), + t3: [ + { login: "kin-bot", source: "app" }, + { login: "kin-bot", source: "gh" }, + ], ghAccounts: ["kin-bot"], gitCredential: Option.some("kin-bot"), ssh: [sshHi("kin-bot"), SSH_DENIED], }); assert.deepInclude(yield* check, { passed: true, failures: [] }); - // One run per source, plus one per private key: not the public key, config or directory. - assert.sameDeepMembers( - probed.map((input) => [input.command, ...input.args]), + const ssh = path.join(home, ".ssh"); + assert.sameDeepMembers(probed.map(argv), [ + ["gh", "auth", "token", "--hostname", "github.com"], + ["gh", "auth", "status", "--hostname", "github.com", "--json", "hosts"], + ["git", "config", "--get-regexp", "^credential(\\..+)?\\.helper$"], + ["git", "credential", "fill"], + ["ssh", "-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), + ]), + [...CredentialHygiene.GITHUB_SSH_PROBE], [ - ["gh", "auth", "status", "--hostname", "github.com", "--json", "hosts"], - ["git", "credential", "fill"], - [...CredentialHygiene.GITHUB_SSH_PROBE], - [ - ...CredentialHygiene.GITHUB_SSH_PROBE.slice(0, -1), - "-o", - "IdentitiesOnly=yes", - "-i", - path.join(home, ".ssh", "id_ed25519"), - "git@github.com", - ], + ...CredentialHygiene.GITHUB_SSH_PROBE.slice(0, -1), + "-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); - assert.isAbove(input.maxOutputBytes ?? 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[input.command as keyof typeof envs]); } - const git = probed.find((input) => input.command === "git"); + const git = probed.find((input) => input.args[0] === "credential"); assert.strictEqual(git?.stdin, "protocol=https\nhost=github.com\n\n"); - assert.deepInclude(git?.env, { GIT_TERMINAL_PROMPT: "0" }); - for (const input of probed.filter((input) => input.command === "ssh")) { - assert.deepInclude(input.env, { SSH_AUTH_SOCK: "" }); - } }), ), ); @@ -224,33 +345,65 @@ describe("GitHubIdentityProbe", () => { it.effect("reads no login from sources that have none", () => run( Effect.gen(function* () { - const { identities, check } = yield* probe({ + const { identities, check, requests } = yield* probe({ ...botMachine(), - t3: Option.none(), + // 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"), - git: answer({ - exitCode: 128, - stderr: - "fatal: could not read Username for 'https://github.com': terminal prompts disabled", - }), + gitHelpers: answer({ exitCode: 1 }), + git: answer({ exitCode: 128, stderr: NO_HELPER_PROMPTS_OFF }), ssh: () => answer({ stderr: SSH_DENIED }), - keys: {}, + files: {}, }); assert.deepStrictEqual(yield* identities, { - t3: Option.none(), + t3: [], ghAccounts: [], gitCredential: Option.none(), ssh: [SSH_DENIED], }); assert.deepInclude(yield* check, { passed: true, identities: [] }); + assert.deepStrictEqual(requests, []); + }), + ), + ); + + 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", source: "app" }); }), ), ); it.effect.each([ - ["T3's credential", { users: { [T3_TOKEN]: "Nishchay", [GIT_PASSWORD]: "kin-bot" } }], + [ + "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("Nishchay") }), + 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", { users: { [T3_TOKEN]: "kin-bot", [GIT_PASSWORD]: "nishchay" } }], + ["the git credential helper", { git: gitFill(OWNER_TOKEN) }], [ "SSH without a key", { @@ -265,38 +418,237 @@ describe("GitHubIdentityProbe", () => { answer({ stderr: key?.endsWith("owner_ed25519") === true ? sshHi("Nishchay") : SSH_DENIED, }), - keys: { ...botMachine().keys, owner_ed25519: KEY }, + files: { ...botMachine().files, ".ssh/owner_ed25519": KEY }, + }, + ], + [ + "a key with a note before its header", + { + ssh: (key: string | undefined) => + answer({ stderr: key?.endsWith("owner_rsa") === true ? sshHi("Nishchay") : SSH_DENIED }), + files: { ...botMachine().files, ".ssh/owner_rsa": KEY_AFTER_NOTE }, + }, + ], + [ + "a key in a folder of ~/.ssh", + { + ssh: (key: string | undefined) => + answer({ stderr: key?.endsWith("work/owner") === true ? sshHi("Nishchay") : SSH_DENIED }), + files: { ...botMachine().files, ".ssh/work/owner": KEY }, + }, + ], + [ + "a key outside ~/.ssh that ~/.ssh/config names for github.com", + { + sshConfig: sshG("identityfile %d/keys/github_%r_%u"), + ssh: (key: string | undefined) => + answer({ + stderr: key?.endsWith("keys/github_git_kin") === true ? sshHi("Nishchay") : SSH_DENIED, + }), + files: { ...botMachine().files, "keys/github_git_kin": KEY }, }, ], ] as const)("fails as owner_identity when %s is the owner", ([, change]) => run( Effect.gen(function* () { - const { check } = yield* probe({ ...botMachine(), ...change }); - assert.deepInclude(yield* check, { passed: false, failures: ["owner_identity"] }); + const { check } = yield* probe({ + ...botMachine(), + ...change, + users: { ...botMachine().users, [OWNER_TOKEN]: "Nishchay" }, + } as Machine); + const report = yield* check; + assert.isFalse(report.passed); + assert.include(report.failures, "owner_identity"); }), ), ); it.effect.each([ - ["T3's reader isn't installed", { t3: "uninstalled" }], - ["GitHub refuses T3's credential", { users: { [GIT_PASSWORD]: "kin-bot" } }], - ["gh is too old for --json", { gh: answer({ exitCode: 1, stderr: "unknown flag: --json" }) }], - ["gh answers something else", { gh: answer({ stdout: "github.com\n Logged in" }) }], - ["gh times out", { gh: Effect.fail(timeout("gh")) }], - [ - "git credential fill fails", - { git: answer({ exitCode: 1, stderr: "fatal: credential helper crashed" }) }, - ], - ["the helper's password is refused", { users: { [T3_TOKEN]: "kin-bot" } }], - ["GitHub is down for the helper", { users: { [T3_TOKEN]: "kin-bot", [GIT_PASSWORD]: 502 } }], - ["an SSH probe times out", { ssh: () => Effect.fail(timeout("ssh")) }], - ] as const)("fails closed when %s", ([, change]) => + ["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 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 can't be started", + { gitHelpers: spawnFailure("git", "PermissionDenied") }, + "git_credential: VcsProcessSpawnError", + ], + [ + "git config fails", + { gitHelpers: answer({ exitCode: 128, stderr: "fatal: bad config" }) }, + "git_credential: git config exited 128", + ], + [ + "more than one credential helper is configured", + { + gitHelpers: answer({ + stdout: "credential.helper store\ncredential.https://github.com.helper osxkeychain\n", + }), + }, + "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", + ], + [ + "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: ", + ], + [ + "~/.ssh/config sets an IdentityAgent", + { + sshConfig: sshG( + "identityagent /Users/kin/Library/Group Containers/2BUA8C4S2C.com.1password/t/agent.sock", + ), + }, + "ssh: an IdentityAgent is configured", + ], + [ + "~/.ssh/config sets a PKCS#11 provider", + { sshConfig: sshG("pkcs11provider /usr/lib/opensc.so") }, + "ssh: a PKCS#11 provider is configured", + ], + [ + "an IdentityFile has a token the probe can't expand", + { sshConfig: sshG("identityfile ~/.ssh/%C") }, + "ssh: an IdentityFile can't be expanded", + ], + [ + "an IdentityFile names another user's home", + { sshConfig: sshG("identityfile ~nishchay/.ssh/id") }, + "ssh: an IdentityFile can't be expanded", + ], + [ + "ssh -G fails", + { sshConfig: answer({ exitCode: 255, stderr: "line 2: Bad configuration option" }) }, + "ssh: ssh -G exited 255", + ], + [ + "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, reason]) => 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. + assert.include(String(error.cause), reason); + const checked = yield* Effect.forkChild(check); + yield* TestClock.adjust("1 minute"); + assert.deepInclude(yield* Fiber.join(checked), { + passed: false, + failures: ["identity_unknown"], + }); + }), + ), + ); + + it.effect.each([ + // With no identity file from `ssh -G`, only the listing touches ~/.ssh. + ["~/.ssh can't be listed", ".ssh", answer({ stdout: "user git\nhostname github.com\n" })], + ["a key can't be read", ".ssh/id_ed25519", sshG()], + ] as const)("fails closed when %s", ([reason, locked, sshConfig]) => + run( + Effect.gen(function* () { + const fs = yield* FileSystem.FileSystem; + const path = yield* Path.Path; + const { identities, home } = yield* probe({ ...botMachine(), sshConfig }); + yield* fs.chmod(path.join(home, locked), 0o000); const error = yield* identities.pipe(Effect.flip); + yield* fs.chmod(path.join(home, locked), 0o700); assert.strictEqual(error._tag, "T3GitHubIdentityError"); - assert.deepInclude(yield* check, { passed: false, failures: ["identity_unknown"] }); + assert.include(String(error.cause), reason); }), ), ); @@ -310,19 +662,16 @@ describe("GitHubIdentityProbe", () => { Effect.gen(function* () { const seen: Array = []; for (const users of [ - { [T3_TOKEN]: "kin-bot", [GIT_PASSWORD]: "kin-bot" }, - { [T3_TOKEN]: "kin-bot" }, - { [T3_TOKEN]: "kin-bot", [GIT_PASSWORD]: 500 }, + botMachine().users, + { [APP_TOKEN]: "kin-bot", [GH_TOKEN]: "kin-bot" }, + { ...botMachine().users, [GIT_PASSWORD]: 500 }, ]) { const { identities, check, requests } = yield* probe({ ...botMachine(), users }); const result = yield* Effect.result(identities); seen.push(JSON.stringify(result), String(result), JSON.stringify(yield* check)); yield* Effect.logWarning("probe", result); // It does reach GitHub, as a bearer token and nowhere else. - assert.include( - requests.map((request) => request.authorization), - `Bearer ${GIT_PASSWORD}`, - ); + assert.include(requests, `Bearer ${GIT_PASSWORD}`); } const haystack = [...seen, ...logged].join("\n"); assert.include(haystack, "git_credential"); diff --git a/apps/server/src/kin/approvals/GitHubIdentityProbe.ts b/apps/server/src/kin/approvals/GitHubIdentityProbe.ts index 1d2ae7d009f1d..625af59262498 100644 --- a/apps/server/src/kin/approvals/GitHubIdentityProbe.ts +++ b/apps/server/src/kin/approvals/GitHubIdentityProbe.ts @@ -1,4 +1,5 @@ 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"; @@ -11,7 +12,12 @@ import { HttpClient } from "effect/http"; import { VcsProcess, type VcsProcessInput } from "../../vcs/VcsProcess.ts"; import { gitHubJson } from "../github/githubRest.ts"; -import { GITHUB_SSH_PROBE, T3GitHubIdentity, T3GitHubIdentityError } from "./CredentialHygiene.ts"; +import { + GITHUB_SSH_PROBE, + T3GitHubIdentity, + T3GitHubIdentityError, + type T3GitHubIdentities, +} from "./CredentialHygiene.ts"; import { T3GitHubCredential } from "./T3GitHubCredential.ts"; const HOST = "github.com"; @@ -20,10 +26,17 @@ const PROCESS_TIMEOUT_MS = 15_000; 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; // Below `VcsProcess`'s four slots for non-git commands, which T3's own gh runs share. const SSH_CONCURRENCY = 2; -const KEY_HEADER_BYTES = 64; -const PRIVATE_KEY_HEADER = /^-----BEGIN [A-Z0-9 ]*PRIVATE KEY-----/; +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" }; @@ -65,6 +78,13 @@ const sshProbeWithKey = (key: string) => [ ...GITHUB_SSH_PROBE.slice(-1), ]; +/** `ssh -G` prints `key value` lines with lower-case keys. */ +const sshOptions = (text: string) => + text.split("\n").flatMap((line) => { + const match = /^(\S+) (.*)$/.exec(line.replace(/\r$/, "")); + return match === null ? [] : [{ key: match[1]!.toLowerCase(), value: match[2]! }]; + }); + /** * 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. @@ -76,6 +96,9 @@ export const make = Effect.gen(function* () { const client = yield* HttpClient.HttpClient; const t3Credential = yield* T3GitHubCredential; const home = yield* HostProcess.HomeDirectory; + 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(home).root; @@ -117,18 +140,72 @@ export const make = Effect.gen(function* () { ), ); - const t3 = t3Credential.read.pipe( - Effect.mapError((error) => unreadable("t3", error.reason)), - Effect.flatMap( - Option.match({ - onNone: () => Effect.succeedNone, - onSome: (held) => - loginOf("t3", held.token).pipe( - Effect.map((login) => Option.some({ login, source: held.source })), - ), + /** `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.gen(function* () { + const stored = yield* t3Credential.read.pipe( + Effect.mapError((error) => unreadable("t3", error.reason)), + ); + 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) }); + for (const account of [undefined, ...Option.toArray(stored.account)]) { + 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((login) => ({ login, source }))), + ); + }); // `--json` lists every account, active or not, and exits 0 whatever their state (gh 2.81+). const ghAccounts = run({ @@ -146,16 +223,14 @@ export const make = Effect.gen(function* () { Effect.flatMap((output) => { if (output === null) return Effect.succeed>([]); if (output.exitCode !== 0) { - return /not logged in/i.test(output.stderr) - ? Effect.succeed>([]) - : Effect.fail( - unreadable( - "gh", - /unknown flag: --json/.test(output.stderr) - ? "gh is older than 2.81" - : `gh auth status exited ${output.exitCode}`, - ), - ); + 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")), @@ -163,20 +238,20 @@ export const make = Effect.gen(function* () { const logins = Object.entries(status.hosts) .filter(([host]) => host.trim().toLowerCase() === HOST) .flatMap(([, accounts]) => accounts.map((account) => account.login.trim())); - return logins.includes("") - ? Effect.fail(unreadable("gh", "an account has no login")) - : Effect.succeed>(logins); + // 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")); }, }); }), ); - // The password stays in this pipeline: it is wrapped at once, sent to GitHub and dropped. - const gitCredential = run({ - operation: "KinGitHubIdentity.gitCredential", + /** Every configured helper, for any URL: null when git isn't installed. */ + const gitHelpers = run({ + operation: "KinGitHubIdentity.gitHelpers", command: "git", - args: ["credential", "fill"], - stdin: `protocol=https\nhost=${HOST}\n\n`, + args: ["config", "--get-regexp", "^credential(\\..+)?\\.helper$"], env: GIT_ENV, timeoutMs: PROCESS_TIMEOUT_MS, }).pipe( @@ -186,75 +261,182 @@ export const make = Effect.gen(function* () { : Effect.fail(unreadable("git_credential", error._tag)), ), Effect.flatMap((output) => { - if (output === null) return Effect.succeed(Option.none>()); - // With no helper answering, git falls back to a terminal prompt, which is turned off. + if (output === null) return Effect.succeed(null); + // `git config` exits 1 when nothing matches. + if (output.exitCode === 1 && output.stdout.trim() === "") return Effect.succeed([]); if (output.exitCode !== 0) { - return /terminal prompts disabled/i.test(output.stderr) - ? Effect.succeed(Option.none>()) - : Effect.fail( - unreadable("git_credential", `git credential fill exited ${output.exitCode}`), - ); + return Effect.fail(unreadable("git_credential", `git config exited ${output.exitCode}`)); } - const line = output.stdout.split("\n").find((entry) => entry.startsWith("password=")); - const password = line?.slice("password=".length).replace(/\r$/, "") ?? ""; - return password === "" - ? Effect.fail(unreadable("git_credential", "the helper answered without a password")) - : Effect.succeedSome(Redacted.make(password)); + // An empty value resets the list rather than naming a helper. + return Effect.succeed( + output.stdout + .split("\n") + .map((line) => line.replace(/^\S+/, "").trim()) + .filter((value) => value !== ""), + ); }), - Effect.flatMap( - Option.match({ - onNone: () => Effect.succeedNone, - onSome: (password) => loginOf("git_credential", password).pipe(Effect.asSome), - }), - ), ); - /** - * Regular files in `~/.ssh` that start like a private key, by their first bytes only. Keys - * named only by `IdentityFile` under another `Host` alias in `~/.ssh/config`, or kept outside - * `~/.ssh`, aren't found, so a login reachable only through them isn't probed. - */ - const privateKeys = Effect.gen(function* () { - const dir = path.join(home, ".ssh"); - const names = yield* fs - .readDirectory(dir) - .pipe( - Effect.catch((error) => - isNotFound(error) - ? Effect.succeed>([]) - : Effect.fail(unreadable("ssh", "~/.ssh can't be listed")), - ), - ); - const keys: Array = []; - for (const name of names.toSorted()) { - if (name.endsWith(".pub")) continue; - const file = path.join(dir, name); - const unreadableKey = (error: PlatformError.PlatformError) => + // The password stays in this pipeline: it is wrapped at once, sent to GitHub and dropped. + const gitCredential = Effect.gen(function* () { + const helpers = yield* gitHelpers; + if (helpers === null) return Option.none(); + // `git credential fill` answers with the first helper's credential and hides the rest. + if (helpers.length > 1) { + return yield* unreadable("git_credential", "more than one credential helper is configured"); + } + const output = yield* run({ + operation: "KinGitHubIdentity.gitCredential", + command: "git", + args: ["credential", "fill"], + stdin: `protocol=https\nhost=${HOST}\n\n`, + env: GIT_ENV, + timeoutMs: PROCESS_TIMEOUT_MS, + }).pipe(Effect.mapError((error) => unreadable("git_credential", error._tag))); + if (output.exitCode !== 0) { + // git falls back to a terminal prompt, which is off, when no helper answers. With a helper + // configured that also covers a crashed, locked or refusing one, so it can't mean none. + if (/terminal prompts disabled/i.test(output.stderr) && helpers.length === 0) { + return Option.none(); + } + return yield* unreadable("git_credential", `git credential fill exited ${output.exitCode}`); + } + const line = output.stdout.split("\n").find((entry) => entry.startsWith("password=")); + const password = line?.slice("password=".length).replace(/\r$/, "") ?? ""; + if (password === "") { + return yield* unreadable("git_credential", "the helper answered without a password"); + } + return Option.some(yield* loginOf("git_credential", Redacted.make(password))); + }); + + /** `ssh -G`'s answer for github.com: the identity files it lists, and no agent or token. */ + const sshConfig = Effect.gen(function* () { + const output = yield* run({ + operation: "KinGitHubIdentity.sshConfig", + command: "ssh", + args: ["-G", "git@github.com"], + env: SSH_ENV, + timeoutMs: PROCESS_TIMEOUT_MS, + }).pipe(Effect.mapError((error) => unreadable("ssh", error._tag))); + if (output.exitCode !== 0) { + return yield* unreadable("ssh", `ssh -G exited ${output.exitCode}`); + } + const options = sshOptions(output.stdout); + const option = (key: string) => options.find((entry) => entry.key === key)?.value; + // `SSH_AUTH_SOCK` is the variable the probe empties; any other agent holds unseen keys. + const agent = option("identityagent"); + if (agent !== undefined && agent !== "none" && agent !== "SSH_AUTH_SOCK") { + return yield* unreadable("ssh", "an IdentityAgent is configured for github.com"); + } + const pkcs11 = option("pkcs11provider"); + if (pkcs11 !== undefined && pkcs11 !== "none") { + return yield* unreadable("ssh", "a PKCS#11 provider is configured for github.com"); + } + // `ssh -G` prints identity files as written, with `~` and `%` tokens unexpanded. + const tokens: Record = { + "%": "%", + d: home, + h: option("hostname"), + n: HOST, + p: option("port"), + r: option("user"), + u: username ?? undefined, + i: userId === undefined ? undefined : String(userId), + }; + const files: Array = []; + for (const { key, value } of options) { + if (key !== "identityfile" || value === "none") continue; + const tilde = value === "~" || value.startsWith("~/") ? home + value.slice(1) : value; + let known = !tilde.startsWith("~") && !tilde.includes("${"); + const expanded = tilde.replace(/%(.?)/g, (_, token: string) => { + const replacement = tokens[token]; + if (replacement === undefined) known = false; + return replacement ?? ""; + }); + if (!known) return yield* unreadable("ssh", "an IdentityFile can't be expanded"); + files.push(path.resolve(cwd, expanded)); + } + return 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", `~/.ssh/${name} can't be read`)); - const isFile = yield* fs.stat(file).pipe( - Effect.map((info) => info.type === "File"), - Effect.catch(unreadableKey), - ); - if (!isFile) continue; - const isKey = yield* fs.open(file, { flag: "r" }).pipe( - Effect.flatMap((handle) => handle.readAlloc(KEY_HEADER_BYTES)), - Effect.map( - Option.match({ - onNone: () => false, - onSome: (bytes) => PRIVATE_KEY_HEADER.test(new TextDecoder().decode(bytes)), - }), - ), - Effect.scoped, - Effect.catch(unreadableKey), - ); - if (isKey) keys.push(file); - } - if (keys.length > MAX_SSH_KEYS) { - return yield* unreadable("ssh", `more than ${MAX_SSH_KEYS} private keys in ~/.ssh`); + : 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 keys; + return false; }); const sshAnswer = (argv: ReadonlyArray) => @@ -272,7 +454,12 @@ export const make = Effect.gen(function* () { ); const ssh = Effect.gen(function* () { - const keys = yield* privateKeys; + const files = [...new Set([...(yield* sshConfig), ...(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 plain = yield* sshAnswer(GITHUB_SSH_PROBE); const perKey = yield* Effect.forEach(keys, (key) => sshAnswer(sshProbeWithKey(key)), { concurrency: SSH_CONCURRENCY, @@ -281,14 +468,14 @@ export const make = Effect.gen(function* () { }); return T3GitHubIdentity.of({ - // T3's credential first: until the server installs its reader, nothing is spawned. + // T3's credentials first: until the server installs its reader, nothing is spawned. identities: Effect.gen(function* () { - const t3Identity = yield* t3; + const t3Identities = yield* t3; const rest = yield* Effect.all( { ghAccounts, gitCredential, ssh }, { concurrency: "unbounded" }, ); - return { t3: t3Identity, ...rest }; + return { t3: t3Identities, ...rest } satisfies T3GitHubIdentities; }).pipe(Effect.withSpan("KinGitHubIdentity.identities")), }); }); 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..bb1aad24d9e40 --- /dev/null +++ b/apps/server/src/kin/approvals/T3GitHubCredential.test.ts @@ -0,0 +1,131 @@ +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 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 * 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) => + Effect.gen(function* () { + const host = SourceControlHost.SourceControlHost.of({ + settings: { + getWithSecrets: () => + isSettingsError(github) + ? Effect.fail(github) + : Effect.succeed({ + ...DEFAULT_SERVER_SETTINGS, + 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)), + ), + ); + 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")); + assert.deepInclude(yield* (yield* slotOver({})).read, { + saved: Option.none(), + app: Option.none(), + account: Option.none(), + }); + }).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(), + } 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 index fd08483b484f2..fde1388786945 100644 --- a/apps/server/src/kin/approvals/T3GitHubCredential.ts +++ b/apps/server/src/kin/approvals/T3GitHubCredential.ts @@ -1,12 +1,19 @@ -import * as GitHubDriver from "@t3tools/source-control-github/server/driver"; +import { requireSettingsSecretLiteral } from "@t3tools/contracts"; +import * as SourceControlHost from "@t3tools/source-control-core/server/SourceControlHost"; +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 type * as Redacted from "effect/Redacted"; -import * as Ref from "effect/Ref"; +import * as Redacted from "effect/Redacted"; import * as Schema from "effect/Schema"; +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. @@ -17,21 +24,36 @@ export class T3GitHubCredentialError extends Schema.TaggedError; - readonly source: string; +/** + * 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; } -type ReadT3Credential = Effect.Effect, T3GitHubCredentialError>; +type ReadT3Credential = Effect.Effect; /** - * T3's own credential for github.com, as `GitHubRequestPolicy.credential` picks it. The executor - * is built before the server's source control host, so the server installs the reader later - * (`install`); until then every read fails, and so does the credential check. + * 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") {} @@ -39,50 +61,62 @@ export class T3GitHubCredential extends Context.Service< export const slotLayer = Layer.effect( T3GitHubCredential, Effect.gen(function* () { - const reader = yield* Ref.make(Option.none()); + const installed = yield* Deferred.make(); return T3GitHubCredential.of({ - read: Ref.get(reader).pipe( - Effect.flatMap( - Option.match({ - onNone: () => - Effect.fail(new T3GitHubCredentialError({ reason: "the reader isn't installed" })), - onSome: (read) => read, - }), - ), + 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* Ref.set(reader, Option.some(read)); + yield* Deferred.succeed(installed, read); }), }); }), ); -/** What `credential` fails with when T3 simply has no GitHub credential for the host. */ -const NO_CREDENTIAL = new Set([ - "GitHubCliMissingError", - "GitHubNotSignedInError", - "GitHubHostDisabledError", -]); +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"], +) { + 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"); + const { tokens, signIns, hosts } = github.value; + const token = yield* requireSettingsSecretLiteral(tokens[HOST], settings.secretProviders).pipe( + Effect.catch(() => fail("the saved token's secret can't be resolved")), + ); + const rawSession = signIns[HOST]; + const session = rawSession === undefined ? Option.none() : decodeSession(rawSession); + if (rawSession !== undefined && Option.isNone(session)) { + return yield* fail("the app sign-in can't be decoded"); + } + 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), + } satisfies T3StoredCredentials; +}); -/** - * Reads through a second `GitHubRequestPolicy`, since the driver builds its own privately. Both - * share the server's settings, secrets and rate-limit pause; only their token caches differ. - */ export const installLayer = Layer.effectDiscard( Effect.gen(function* () { const slot = yield* T3GitHubCredential; - const policy = yield* GitHubDriver.GitHubRequestPolicy; - yield* slot.install( - policy.credential("github.com").pipe( - Effect.map((held) => - Option.some({ token: held.token, source: held.source ?? "unknown" }), - ), - Effect.catch((error) => - NO_CREDENTIAL.has(error._tag) - ? Effect.succeedNone - : Effect.fail(new T3GitHubCredentialError({ reason: error._tag })), - ), - ), - ); + yield* slot.install(readStored(yield* SourceControlHost.SourceControlHost)); }), -).pipe(Layer.provide(GitHubDriver.gitHubRequestPolicyLayer)); +); diff --git a/apps/server/src/kin/chat/executionNotices.ts b/apps/server/src/kin/chat/executionNotices.ts index fe8441286914d..6301278f85c48 100644 --- a/apps/server/src/kin/chat/executionNotices.ts +++ b/apps/server/src/kin/chat/executionNotices.ts @@ -83,7 +83,7 @@ export const layer = Layer.effect( WHERE l.org_id = ${orgId} AND a.retired_at IS NULL ORDER BY l.agent_id LIMIT 1`; return KinExecutionNotices.of({ credentialCheck: Effect.fn("KinExecutionNotices.credentialCheck")( - function* ({ orgId, report }) { + function* ({ orgId, report, recoveredFrom }) { const [lead] = yield* ownerChat(orgId); if (lead === undefined) { yield* Effect.logWarning("Kin credential check changed with no owner chat", { @@ -96,7 +96,11 @@ export const layer = Layer.effect( yield* chat.notice({ orgId, chatId: KinChatId.make(lead.chatId), - messageId: KinMessageId.make(`kin-hygiene:${orgId}:${report.checkedAt}`), + messageId: KinMessageId.make( + recoveredFrom === undefined + ? `kin-hygiene:${orgId}:${report.checkedAt}` + : `kin-hygiene:${orgId}:recovered:${recoveredFrom}`, + ), body: credentialCheckBody(orgId, report), }); }, diff --git a/apps/server/src/kin/github/OwnerMergeClient.test.ts b/apps/server/src/kin/github/OwnerMergeClient.test.ts index 8edc9c6017af1..08051cbe9f342 100644 --- a/apps/server/src/kin/github/OwnerMergeClient.test.ts +++ b/apps/server/src/kin/github/OwnerMergeClient.test.ts @@ -81,7 +81,7 @@ const withHygiene = (effect: Effect.Effect) => Layer.provide( Layer.succeed(CredentialHygiene.T3GitHubIdentity, { identities: Effect.succeed({ - t3: Option.none(), + t3: [], ghAccounts: [], gitCredential: Option.none(), ssh: ["git@github.com: Permission denied (publickey)."], diff --git a/apps/server/src/kin/mcp/layer.test.ts b/apps/server/src/kin/mcp/layer.test.ts index b470990516c68..7b2a4d07b9af9 100644 --- a/apps/server/src/kin/mcp/layer.test.ts +++ b/apps/server/src/kin/mcp/layer.test.ts @@ -40,8 +40,6 @@ 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 SourceControlRateLimit from "@t3tools/source-control-core/server/SourceControlRateLimit"; -import * as FetchHttpClient from "effect/http/FetchHttpClient"; import * as Kin from "../layer.ts"; import { ServerActivation } from "../../serverActivation.ts"; import { KinIdentity } from "../chat/KinIdentity.ts"; @@ -115,13 +113,11 @@ const toolHandlerServices = Layer.mergeAll( Layer.mock(ServerSecretStore)({}), ServerSettings.layerTest(), Layer.mock(SourceControlRepositoryService)({}), - // Kin installs T3's GitHub credential reader on these; nothing here reads it. + // Kin installs T3's GitHub credential reader on this; nothing here reads it. Layer.succeed( SourceControlHost.SourceControlHost, {} as SourceControlHost.SourceControlHost["Service"], ), - SourceControlRateLimit.layer, - FetchHttpClient.layer, Layer.mock(ThreadLaunchService)({}), Layer.mock(ThreadManagementService)({ getThreadShell: (id) => Effect.succeed(liveThreadShell(id)), diff --git a/docs/kin/specs/approvals.md b/docs/kin/specs/approvals.md index 9128191e91dba..19426943be5dd 100644 --- a/docs/kin/specs/approvals.md +++ b/docs/kin/specs/approvals.md @@ -519,21 +519,36 @@ 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`), 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. `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. +- `identity_unknown`: the identity probe failed, or an SSH answer was neither `Hi !` nor `Permission denied (publickey)`. This fails closed: a source the probe can't fully enumerate is never read as having no login. - `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 identity probe is the `T3GitHubIdentity` port; `GitHubIdentityProbe.ts` is its live layer. The GitHub driver builds its `GitHubRequestPolicy` privately, so `@t3tools/source-control-github` also exports the policy and its layer. The executor is built before the server's source control host, so the server installs the reader of T3's credential afterwards (`T3GitHubCredential.ts`), and until it does every probe fails. 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 --json hosts` (gh 2.81 or later), 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`). The hourly check audits and tells the owner only when its result changes, including when it passes again. +The identity probe is the `T3GitHubIdentity` port; `GitHubIdentityProbe.ts` is its live layer. The port's `identities` has a required field per source (`t3`, `ghAccounts`, `gitCredential`, `ssh`), so the live layer must report all four, and any source it can't read fails the probe, which fails the check closed (`identity_unknown`). What each source reads: -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. +- `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, `GH_TOKEN`/`GITHUB_TOKEN`, and `gh auth token` (for the active account and for the account Settings picks). `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 settings afterwards (`T3GitHubCredential.ts`, once); 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. +- `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`: the login 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 configured `credential.*.helper` fails. 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. +- `ssh`: `ssh -G git@github.com` first. An `IdentityAgent` other than `none` or `SSH_AUTH_SOCK` (which the probe empties), or a `PKCS11Provider`, fails, since an agent or token can hold keys the probe can't list. The probe expands each `IdentityFile` it lists (`~`, `%d`, `%u`, `%h`, `%r`, `%p`, `%n`, `%i`; any other token fails) and adds 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 `ssh -T -o BatchMode=yes -o StrictHostKeyChecking=yes git@github.com` (`GITHUB_SSH_PROBE`) runs as is, and once per key with `-o IdentitiesOnly=yes -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, and concurrent checks wait on the one running. `requirePassing`, which gates the side effect after the claim, always runs a fresh probe. 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. Orgs without a saved merge token haven't set GitHub up and get no notice. + +The Kin host must be set up so the check can pass: + +- one git credential helper, holding only the bot's credential for github.com; +- no `IdentityAgent` (1Password, Secretive) or `PKCS11Provider` for github.com in `~/.ssh/config`; +- no passphrase-protected private keys in `~/.ssh` or named by `IdentityFile`; +- github.com in `~/.ssh/known_hosts`, since the probe uses `StrictHostKeyChecking=yes`; +- gh 2.81 or newer. + +The probe is defence in depth, not the boundary. It sees only the sources above, as the server's OS user is configured, and an agent running as that same user can always find more: an SSH agent socket on disk it points `SSH_AUTH_SOCK` at, a key outside these paths, a token in another tool's config, the merge token and the App's private key in the keychain (open question 4). The real boundary is keeping the owner's credentials off the Kin host's OS user entirely, and in time a separate executor OS user. ## Threat model @@ -646,7 +661,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/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 d5c9cabfe6792..0371b7b904aef 100644 --- a/packages/source-control-github/src/server/driver.ts +++ b/packages/source-control-github/src/server/driver.ts @@ -110,5 +110,4 @@ export const driver = defineSourceControlDriver({ make: withOwnServices(makeInstance, services), }); -export { GitHubRequestPolicy } from "./GitHubRequestPolicy.ts"; -export { layerWithDependencies as gitHubRequestPolicyLayer } from "./GitHubRest.ts"; +export { GitHubAppSession } from "./GitHubAppSignIn.ts"; From 51d70aabd3dbe93866ef954937905fa522c11a4d Mon Sep 17 00:00:00 2001 From: Surajsm60720 <1ds22is166@dsce.edu.in> Date: Mon, 9 Nov 2026 10:48:18 +0530 Subject: [PATCH 3/7] fix(kin): identity probe checks an allowlist of host SSH and git config Two review rounds each found new places an owner credential can hide from a probe that lists sources. The probe now requires a minimal, known host setup and fails closed as identity_unknown on anything else, naming the directive or key but never its value. - SSH: ~/.ssh/config, /etc/ssh/ssh_config and their Includes are checked against an allowlist; every IdentityFile under any Host is probed with -F , ControlMaster=no and ControlPath=none. - Git: one `git config --list --show-origin -z` checked against allowed key groups, with one credential helper. - Environment: askpass, GIT_SSH*, GIT_CONFIG_PARAMETERS/COUNT fail env_token; home comes from passwd and $HOME must match it. - gh auth token is also asked for the owner's login; an empty app sign-in reads as signed out; HostCredentialSettings keys are guarded. Co-Authored-By: Claude Opus 5.5 --- .../kin/approvals/CredentialHygiene.test.ts | 19 + .../src/kin/approvals/CredentialHygiene.ts | 25 +- .../kin/approvals/GitHubIdentityProbe.test.ts | 567 +++++++++++++++--- .../src/kin/approvals/GitHubIdentityProbe.ts | 257 +++++--- .../src/kin/approvals/HostConfigAllowlist.ts | 233 +++++++ .../kin/approvals/T3GitHubCredential.test.ts | 12 +- .../src/kin/approvals/T3GitHubCredential.ts | 15 +- apps/server/src/kin/chat/executionNotices.ts | 2 +- docs/kin/specs/approvals.md | 29 +- packages/shared/src/HostProcess.ts | 14 + 10 files changed, 967 insertions(+), 206 deletions(-) create mode 100644 apps/server/src/kin/approvals/HostConfigAllowlist.ts diff --git a/apps/server/src/kin/approvals/CredentialHygiene.test.ts b/apps/server/src/kin/approvals/CredentialHygiene.test.ts index 5af597cd00900..ea1626350780d 100644 --- a/apps/server/src/kin/approvals/CredentialHygiene.test.ts +++ b/apps/server/src/kin/approvals/CredentialHygiene.test.ts @@ -220,6 +220,25 @@ describe("CredentialHygiene", () => { { 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", + ] as const + ).map( + (name) => + [ + `${name} in the environment`, + { identity: bot, env: { [name]: "x" } }, + ["env_token"], + ] as const, + ), [ "T3 using an environment token", { identity: { t3: [{ login: "kin-bot", source: "env" }] } }, diff --git a/apps/server/src/kin/approvals/CredentialHygiene.ts b/apps/server/src/kin/approvals/CredentialHygiene.ts index 5243236540a29..5c9155e385fc6 100644 --- a/apps/server/src/kin/approvals/CredentialHygiene.ts +++ b/apps/server/src/kin/approvals/CredentialHygiene.ts @@ -12,9 +12,10 @@ import { KinGitHubScope } from "../github/KinGitHubScope.ts"; import * as OwnerMergeClient from "../github/OwnerMergeClient.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. + * Agent CLIs inherit the server's environment, so any of these hands them a GitHub credential or + * a way to one the probe can't see: a token, an SSH agent (the probe sees only the first key GitHub + * accepts), a program git or ssh asks for a password, an ssh command, or git config from outside + * the files the probe checks. */ const ENV_TOKEN_NAMES = [ "GH_TOKEN", @@ -22,9 +23,18 @@ const ENV_TOKEN_NAMES = [ "GH_ENTERPRISE_TOKEN", "GITHUB_ENTERPRISE_TOKEN", "SSH_AUTH_SOCK", + "GIT_ASKPASS", + "SSH_ASKPASS", + "GIT_SSH", + "GIT_SSH_COMMAND", + "GIT_CONFIG_PARAMETERS", + "GIT_CONFIG_COUNT", ]; -/** The probe the live layer (`GitHubIdentityProbe.ts`) runs for `T3GitHubIdentities.ssh`, without `SSH_AUTH_SOCK`. */ +/** + * The probe the live layer (`GitHubIdentityProbe.ts`) runs for `T3GitHubIdentities.ssh`, with + * `-F ` added and without `SSH_AUTH_SOCK`. + */ export const GITHUB_SSH_PROBE = [ "ssh", "-T", @@ -32,6 +42,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", @@ -92,7 +107,7 @@ export interface T3GitHubIdentities { readonly gitCredential: Option.Option; /** * The combined stdout and stderr of each SSH probe: `GITHUB_SSH_PROBE` as is, then once per - * private key (each `IdentityFile` `ssh -G` lists, and every key in `~/.ssh`) with + * 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. */ diff --git a/apps/server/src/kin/approvals/GitHubIdentityProbe.test.ts b/apps/server/src/kin/approvals/GitHubIdentityProbe.test.ts index 79d8b25e323b1..3275e95ea5019 100644 --- a/apps/server/src/kin/approvals/GitHubIdentityProbe.test.ts +++ b/apps/server/src/kin/approvals/GitHubIdentityProbe.test.ts @@ -19,11 +19,13 @@ 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 { fakeGitHub, memoryKeychain, scopeLayer } from "../github/testFakes.ts"; import * as CredentialHygiene from "./CredentialHygiene.ts"; @@ -34,6 +36,7 @@ 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"; @@ -53,6 +56,45 @@ const HELPER_PROMPTS_OFF = 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; @@ -94,19 +136,40 @@ const ghStatus = (...logins: ReadonlyArray) => }); const gitFill = (password: string) => answer({ stdout: `protocol=https\nhost=github.com\nusername=x\npassword=${password}\n` }); -/** `ssh -G git@github.com` as OpenSSH prints it: identity files unexpanded. */ -const sshG = (...extra: ReadonlyArray) => + +/** `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: [ - "user git", - "hostname github.com", - "port 22", - ...extra, - "identityfile ~/.ssh/id_rsa", - "identityfile ~/.ssh/id_ed25519", - "addkeystoagent false", - ].join("\n"), + 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 } = {}, @@ -129,37 +192,60 @@ const stored = ( /** 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; /** The login `GET /user` names for each token; a number is the status, `hang` never answers. */ users: Record; gh: Run; /** `gh auth token`, for the `--user` account or the active one. */ ghToken: (account: string | undefined) => Run; - gitHelpers: Run; + gitConfig: Run; git: Run; - sshConfig: 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: () => answer({ stdout: `${GH_TOKEN}\n` }), - gitHelpers: answer({ stdout: "credential.helper osxkeychain\n" }), + 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), - sshConfig: sshG(), 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": "Host *\n", + ".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), }); /** A fake `ssh-keygen -y -P ""`, answering by the file's contents as OpenSSH does. */ @@ -183,18 +269,28 @@ const keygen = (fs: FileSystem.FileSystem, file: string): Run => }), ); +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 home = yield* fs.makeTempDirectoryScoped(); + const root = yield* fs.makeTempDirectoryScoped(); + const home = path.join(root, "home"); + const etc = path.join(root, "etc", "ssh"); yield* fs.makeDirectory(path.join(home, ".ssh", "sockets"), { recursive: true }); - for (const [name, text] of Object.entries(machine.files)) { - yield* fs.makeDirectory(path.dirname(path.join(home, name)), { recursive: true }); - yield* fs.writeFileString(path.join(home, name), text); - } + yield* writeFiles(home, machine.files); + yield* writeFiles(etc, machine.system); const runs: Array = []; const vcs = VcsProcess.of({ run: (input) => { @@ -205,9 +301,8 @@ const probe = (machine: Machine) => const user = input.args.find((arg) => arg.startsWith("--user=")); return machine.ghToken(user?.slice("--user=".length)); } - if (input.command === "git") return first === "config" ? machine.gitHelpers : machine.git; + if (input.command === "git") return first === "config" ? machine.gitConfig : machine.git; if (input.command === "ssh-keygen") return keygen(fs, input.args.at(-1) ?? ""); - if (first === "-G") return machine.sshConfig; const key = input.args.indexOf("-i"); return machine.ssh(key === -1 ? undefined : input.args[key + 1]); }, @@ -236,14 +331,18 @@ const probe = (machine: Machine) => T3GitHubCredential.T3GitHubCredential, ); if (machine.t3 !== "uninstalled") yield* slot.install(Effect.succeed(machine.t3)); + const env = Object.fromEntries( + Object.entries(machine.env).map(([name, value]) => [name, value.replace("", home)]), + ); const identity = yield* GitHubIdentityProbe.make.pipe( Effect.provideService(VcsProcess, vcs), - Effect.provideService(HostProcess.HomeDirectory, home), - Effect.provideService(HostProcess.Environment, machine.env), + 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(GitHubIdentityProbe.SystemSshConfig, path.join(etc, "ssh_config")), Effect.provideService(T3GitHubCredential.T3GitHubCredential, slot), - Effect.provide(github), + Effect.provide(Layer.mergeAll(github, scopeLayer())), ); const keychain = memoryKeychain({ [`kin-owner-merge/${ORG}`]: JSON.stringify({ @@ -271,13 +370,31 @@ const run = (effect: Effect.Effect [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 } = yield* probe(botMachine()); + const { identities, check, runs, home } = yield* probe({ + ...botMachine(), + env: { HOME: "/" }, + }); const found = yield* identities; const probed = [...runs]; assert.deepStrictEqual(found, { @@ -292,12 +409,14 @@ describe("GitHubIdentityProbe", () => { assert.deepInclude(yield* check, { passed: true, failures: [] }); const ssh = path.join(home, ".ssh"); + const config = path.join(ssh, "config"); 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", "--get-regexp", "^credential(\\..+)?\\.helper$"], + ["git", "config", "--list", "--show-origin", "-z"], ["git", "credential", "fill"], - ["ssh", "-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", @@ -307,9 +426,13 @@ describe("GitHubIdentityProbe", () => { "-f", path.join(ssh, name), ]), - [...CredentialHygiene.GITHUB_SSH_PROBE], + // Only the validated config, with no shared connection and no agent (`SSH_AUTH_SOCK`). + ["ssh", "-F", config, ...SSH_PROBE_OPTIONS, "git@github.com"], [ - ...CredentialHygiene.GITHUB_SSH_PROBE.slice(0, -1), + "ssh", + "-F", + config, + ...SSH_PROBE_OPTIONS, "-o", "IdentitiesOnly=yes", "-i", @@ -345,16 +468,17 @@ describe("GitHubIdentityProbe", () => { it.effect("reads no login from sources that have none", () => run( Effect.gen(function* () { - const { identities, check, requests } = yield* probe({ + const { identities, check, requests, runs } = 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"), - gitHelpers: answer({ exitCode: 1 }), + gitConfig: gitList(["user.name", "kin-bot"]), git: answer({ exitCode: 128, stderr: NO_HELPER_PROMPTS_OFF }), ssh: () => answer({ stderr: SSH_DENIED }), files: {}, + system: {}, }); assert.deepStrictEqual(yield* identities, { t3: [], @@ -364,6 +488,11 @@ describe("GitHubIdentityProbe", () => { }); 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(" ")), + ["ssh", "-F", "none", ...SSH_PROBE_OPTIONS, "git@github.com"].join(" "), + ); }), ), ); @@ -380,6 +509,27 @@ describe("GitHubIdentityProbe", () => { ), ); + it.effect.each([ + ["git is not installed", { gitConfig: missing("git") }], + [ + "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", @@ -397,7 +547,14 @@ describe("GitHubIdentityProbe", () => { [ "the gh account Settings picks", { - t3: stored({ app: session(APP_TOKEN), account: Option.some("Nishchay") }), + 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 }), }, @@ -414,48 +571,69 @@ describe("GitHubIdentityProbe", () => { [ "one key in ~/.ssh", { - ssh: (key: string | undefined) => - answer({ - stderr: key?.endsWith("owner_ed25519") === true ? sshHi("Nishchay") : SSH_DENIED, - }), + ssh: ownerOn("owner_ed25519"), files: { ...botMachine().files, ".ssh/owner_ed25519": KEY }, }, ], [ "a key with a note before its header", { - ssh: (key: string | undefined) => - answer({ stderr: key?.endsWith("owner_rsa") === true ? sshHi("Nishchay") : SSH_DENIED }), + 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: (key: string | undefined) => - answer({ stderr: key?.endsWith("work/owner") === true ? sshHi("Nishchay") : SSH_DENIED }), - files: { ...botMachine().files, ".ssh/work/owner": KEY }, + 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 outside ~/.ssh that ~/.ssh/config names for github.com", + "a key an included file names", { - sshConfig: sshG("identityfile %d/keys/github_%r_%u"), - ssh: (key: string | undefined) => - answer({ - stderr: key?.endsWith("keys/github_git_kin") === true ? sshHi("Nishchay") : SSH_DENIED, - }), - files: { ...botMachine().files, "keys/github_git_kin": KEY }, + 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({ - ...botMachine(), - ...change, + ...machine, users: { ...botMachine().users, [OWNER_TOKEN]: "Nishchay" }, - } as Machine); + }); const report = yield* check; assert.isFalse(report.passed); assert.include(report.failures, "owner_identity"); @@ -464,6 +642,14 @@ describe("GitHubIdentityProbe", () => { ); 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", @@ -490,6 +676,12 @@ describe("GitHubIdentityProbe", () => { { 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", @@ -522,23 +714,25 @@ describe("GitHubIdentityProbe", () => { "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", - { gitHelpers: spawnFailure("git", "PermissionDenied") }, + { gitConfig: spawnFailure("git", "PermissionDenied") }, "git_credential: VcsProcessSpawnError", ], [ "git config fails", - { gitHelpers: answer({ exitCode: 128, stderr: "fatal: bad config" }) }, + { 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", - { - gitHelpers: answer({ - stdout: "credential.helper store\ncredential.https://github.com.helper osxkeychain\n", - }), - }, + withGit(["credential.https://github.com.helper", "store"]), "git_credential: more than one credential helper", ], [ @@ -561,35 +755,162 @@ describe("GitHubIdentityProbe", () => { { 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", + ], [ - "~/.ssh/config sets an IdentityAgent", + "a file included without a glob has a forbidden directive", { - sshConfig: sshG( - "identityagent /Users/kin/Library/Group Containers/2BUA8C4S2C.com.1password/t/agent.sock", - ), + files: { + ...withSshConfig("Include work").files, + ".ssh/work": "Host *\n IdentityAgent /tmp/agent\n", + }, }, - "ssh: an IdentityAgent is configured", + "ssh: ~/.ssh/config sets IdentityAgent", ], [ - "~/.ssh/config sets a PKCS#11 provider", - { sshConfig: sshG("pkcs11provider /usr/lib/opensc.so") }, - "ssh: a PKCS#11 provider is configured", + "the system SSH config has Match", + withSystemConfig("Match exec true"), + "ssh: the system SSH config sets Match", ], [ - "an IdentityFile has a token the probe can't expand", - { sshConfig: sshG("identityfile ~/.ssh/%C") }, - "ssh: an IdentityFile can't be expanded", + "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 IdentityFile names another user's home", - { sshConfig: sshG("identityfile ~nishchay/.ssh/id") }, - "ssh: an IdentityFile can't be expanded", + "an Include includes itself", + withSshConfig("Include config"), + "ssh: ~/.ssh/config includes too many files", ], [ - "ssh -G fails", - { sshConfig: answer({ exitCode: 255, stderr: "line 2: Bad configuration option" }) }, - "ssh: ssh -G exited 255", + "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: keys and probes. [ "a key needs a passphrase", { files: { ...botMachine().files, ".ssh/owner_ed25519": PASSPHRASE_KEY } }, @@ -624,6 +945,7 @@ describe("GitHubIdentityProbe", () => { assert.strictEqual(error._tag, "T3GitHubIdentityError"); // The fixed reason, so a case can't pass by failing for some other reason. assert.include(String(error.cause), reason); + assert.notInclude(String(error.cause), "ghp_"); const checked = yield* Effect.forkChild(check); yield* TestClock.adjust("1 minute"); assert.deepInclude(yield* Fiber.join(checked), { @@ -635,16 +957,16 @@ describe("GitHubIdentityProbe", () => { ); it.effect.each([ - // With no identity file from `ssh -G`, only the listing touches ~/.ssh. - ["~/.ssh can't be listed", ".ssh", answer({ stdout: "user git\nhostname github.com\n" })], - ["a key can't be read", ".ssh/id_ed25519", sshG()], - ] as const)("fails closed when %s", ([reason, locked, sshConfig]) => + // 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(), sshConfig }); - yield* fs.chmod(path.join(home, locked), 0o000); + 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"); @@ -653,30 +975,81 @@ describe("GitHubIdentityProbe", () => { ), ); - it.effect("never puts the helper's password in a log, error or report", () => { + 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]; return run( Effect.gen(function* () { const seen: Array = []; - for (const users of [ - botMachine().users, - { [APP_TOKEN]: "kin-bot", [GH_TOKEN]: "kin-bot" }, - { ...botMachine().users, [GIT_PASSWORD]: 500 }, - ]) { - const { identities, check, requests } = yield* probe({ ...botMachine(), users }); + 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 }); + 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, + ...change, + }); const result = yield* Effect.result(identities); - seen.push(JSON.stringify(result), String(result), JSON.stringify(yield* check)); + 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); - // It does reach GitHub, as a bearer token and nowhere else. - assert.include(requests, `Bearer ${GIT_PASSWORD}`); + // Each does reach GitHub, as a bearer token and nowhere else. + if (index === 0) { + for (const secret of secrets) 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, "git_credential"); - assert.notInclude(haystack, GIT_PASSWORD); - }).pipe(Effect.provide(Logger.layer([logger], { mergeWithExisting: false }))), + 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 index 625af59262498..f4428cfd57e02 100644 --- a/apps/server/src/kin/approvals/GitHubIdentityProbe.ts +++ b/apps/server/src/kin/approvals/GitHubIdentityProbe.ts @@ -1,5 +1,6 @@ import * as HostProcess from "@t3tools/shared/HostProcess"; import * as Clock from "effect/Clock"; +import * as Context from "effect/Context"; import * as Effect from "effect/Effect"; import * as FileSystem from "effect/FileSystem"; import * as Layer from "effect/Layer"; @@ -12,12 +13,20 @@ 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, + expandIdentityFile, + parseGitConfigList, + parseSshConfig, + sshViolation, +} from "./HostConfigAllowlist.ts"; import { T3GitHubCredential } from "./T3GitHubCredential.ts"; const HOST = "github.com"; @@ -30,6 +39,9 @@ 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 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-----/; @@ -68,22 +80,24 @@ const GhAuthStatus = Schema.fromJsonString( ); const decodeGhAuthStatus = Schema.decodeUnknownOption(GhAuthStatus); -/** `GITHUB_SSH_PROBE` offering only `key`, so GitHub answers for that key alone. */ -const sshProbeWithKey = (key: string) => [ - ...GITHUB_SSH_PROBE.slice(0, -1), - "-o", - "IdentitiesOnly=yes", - "-i", - key, +/** + * `GITHUB_SSH_PROBE` 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 = (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), ]; -/** `ssh -G` prints `key value` lines with lower-case keys. */ -const sshOptions = (text: string) => - text.split("\n").flatMap((line) => { - const match = /^(\S+) (.*)$/.exec(line.replace(/\r$/, "")); - return match === null ? [] : [{ key: match[1]!.toLowerCase(), value: match[2]! }]; - }); +/** The system-wide SSH config, which agents' ssh reads after `~/.ssh/config`. */ +export const SystemSshConfig = Context.Reference( + "t3/kin/approvals/GitHubIdentityProbe/SystemSshConfig", + { defaultValue: () => "/etc/ssh/ssh_config" }, +); /** * The live `T3GitHubIdentity` (spec section 12). It reports what each source answers and leaves @@ -95,13 +109,24 @@ export const make = Effect.gen(function* () { const path = yield* Path.Path; const client = yield* HttpClient.HttpClient; const t3Credential = yield* T3GitHubCredential; - const home = yield* HostProcess.HomeDirectory; + 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; + const systemSshConfig = yield* SystemSshConfig; // From the filesystem root, outside any repository, so no repository config picks the helper. - const cwd = path.parse(home).root; + 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, @@ -198,7 +223,12 @@ export const make = Effect.gen(function* () { (value) => value !== "", ); if (fromEnv !== undefined) tokens.push({ source: "env", token: Redacted.make(fromEnv) }); - for (const account of [undefined, ...Option.toArray(stored.account)]) { + // 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 }); } @@ -247,11 +277,14 @@ export const make = Effect.gen(function* () { }), ); - /** Every configured helper, for any URL: null when git isn't installed. */ + /** + * 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.gitHelpers", + operation: "KinGitHubIdentity.gitConfig", command: "git", - args: ["config", "--get-regexp", "^credential(\\..+)?\\.helper$"], + args: ["config", "--list", "--show-origin", "-z"], env: GIT_ENV, timeoutMs: PROCESS_TIMEOUT_MS, }).pipe( @@ -262,18 +295,17 @@ export const make = Effect.gen(function* () { ), Effect.flatMap((output) => { if (output === null) return Effect.succeed(null); - // `git config` exits 1 when nothing matches. - if (output.exitCode === 1 && output.stdout.trim() === "") return Effect.succeed([]); if (output.exitCode !== 0) { return Effect.fail(unreadable("git_credential", `git config exited ${output.exitCode}`)); } - // An empty value resets the list rather than naming a helper. - return Effect.succeed( - output.stdout - .split("\n") - .map((line) => line.replace(/^\S+/, "").trim()) - .filter((value) => value !== ""), - ); + 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); }), ); @@ -282,7 +314,7 @@ export const make = Effect.gen(function* () { const helpers = yield* gitHelpers; if (helpers === null) return Option.none(); // `git credential fill` answers with the first helper's credential and hides the rest. - if (helpers.length > 1) { + if (helpers > 1) { return yield* unreadable("git_credential", "more than one credential helper is configured"); } const output = yield* run({ @@ -296,7 +328,7 @@ export const make = Effect.gen(function* () { if (output.exitCode !== 0) { // git falls back to a terminal prompt, which is off, when no helper answers. With a helper // configured that also covers a crashed, locked or refusing one, so it can't mean none. - if (/terminal prompts disabled/i.test(output.stderr) && helpers.length === 0) { + if (/terminal prompts disabled/i.test(output.stderr) && helpers === 0) { return Option.none(); } return yield* unreadable("git_credential", `git credential fill exited ${output.exitCode}`); @@ -309,54 +341,112 @@ export const make = Effect.gen(function* () { return Option.some(yield* loginOf("git_credential", Redacted.make(password))); }); - /** `ssh -G`'s answer for github.com: the identity files it lists, and no agent or token. */ - const sshConfig = Effect.gen(function* () { - const output = yield* run({ - operation: "KinGitHubIdentity.sshConfig", - command: "ssh", - args: ["-G", "git@github.com"], - env: SSH_ENV, - timeoutMs: PROCESS_TIMEOUT_MS, - }).pipe(Effect.mapError((error) => unreadable("ssh", error._tag))); - if (output.exitCode !== 0) { - return yield* unreadable("ssh", `ssh -G exited ${output.exitCode}`); - } - const options = sshOptions(output.stdout); - const option = (key: string) => options.find((entry) => entry.key === key)?.value; - // `SSH_AUTH_SOCK` is the variable the probe empties; any other agent holds unseen keys. - const agent = option("identityagent"); - if (agent !== undefined && agent !== "none" && agent !== "SSH_AUTH_SOCK") { - return yield* unreadable("ssh", "an IdentityAgent is configured for github.com"); + /** 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"); } - const pkcs11 = option("pkcs11provider"); - if (pkcs11 !== undefined && pkcs11 !== "none") { - return yield* unreadable("ssh", "a PKCS#11 provider is configured for github.com"); - } - // `ssh -G` prints identity files as written, with `~` and `%` tokens unexpanded. - const tokens: Record = { - "%": "%", - d: home, - h: option("hostname"), - n: HOST, - p: option("port"), - r: option("user"), - u: username ?? undefined, - i: userId === undefined ? undefined : String(userId), - }; - const files: Array = []; - for (const { key, value } of options) { - if (key !== "identityfile" || value === "none") continue; - const tilde = value === "~" || value.startsWith("~/") ? home + value.slice(1) : value; - let known = !tilde.startsWith("~") && !tilde.includes("${"); - const expanded = tilde.replace(/%(.?)/g, (_, token: string) => { - const replacement = tokens[token]; - if (replacement === undefined) known = false; - return replacement ?? ""; - }); - if (!known) return yield* unreadable("ssh", "an IdentityFile can't be expanded"); - files.push(path.resolve(cwd, expanded)); - } - return files; + if (!GLOB.test(name)) return [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. + return names + .filter((entry) => (name.startsWith(".") || !entry.startsWith(".")) && matcher.test(entry)) + .toSorted() + .map((entry) => path.join(dir, entry)); + }); + + /** + * 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, + system: boolean, + ) { + const label = system ? "the system SSH config" : "~/.ssh/config"; + // Relative includes resolve against `~/.ssh` for the user's config and `/etc/ssh` for the system's. + const base = system ? path.dirname(top) : path.join(home, ".ssh"); + const identityFiles: Array = []; + let read = 0; + const visit = Effect.fn("KinGitHubIdentity.readSshConfigFile")(function* ( + file: string, + ): 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 an Include that matches nothing. + if (Option.isNone(info)) return false; + 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) { + for (const target of yield* includeTargets(pattern, base)) { + yield* visit(target); + } + } + } + } + return true; + }); + const exists = yield* visit(top); + return exists ? identityFiles : null; }); const isRegularFile = (file: string) => @@ -454,14 +544,20 @@ export const make = Effect.gen(function* () { ); const ssh = Effect.gen(function* () { - const files = [...new Set([...(yield* sshConfig), ...(yield* sshDirFiles)])]; + const userConfig = path.join(home, ".ssh", "config"); + const fromUser = yield* readSshConfig(userConfig, false); + // Agents' ssh reads this after the user's config; the probe's `-F` skips it, so its keys go by `-i`. + const fromSystem = yield* readSshConfig(systemSshConfig, true); + const named = [...(fromUser ?? []), ...(fromSystem ?? [])]; + 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 plain = yield* sshAnswer(GITHUB_SSH_PROBE); - const perKey = yield* Effect.forEach(keys, (key) => sshAnswer(sshProbeWithKey(key)), { + const config = fromUser === null ? "none" : userConfig; + const plain = yield* sshAnswer(sshProbe(config)); + const perKey = yield* Effect.forEach(keys, (key) => sshAnswer(sshProbe(config, key)), { concurrency: SSH_CONCURRENCY, }); return [plain, ...perKey]; @@ -470,6 +566,7 @@ export const make = Effect.gen(function* () { return T3GitHubIdentity.of({ // T3's credentials first: until the server installs its reader, nothing is spawned. identities: Effect.gen(function* () { + yield* homeMatches; const t3Identities = yield* t3; const rest = yield* Effect.all( { ghAccounts, gitCredential, ssh }, diff --git a/apps/server/src/kin/approvals/HostConfigAllowlist.ts b/apps/server/src/kin/approvals/HostConfigAllowlist.ts new file mode 100644 index 0000000000000..7a46448b32211 --- /dev/null +++ b/apps/server/src/kin/approvals/HostConfigAllowlist.ts @@ -0,0 +1,233 @@ +/** + * What the Kin host's SSH and git config 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 or key, 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 `/etc/ssh` 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", +]); + +/** Spellings for reasons; any other directive is named only if it can't be a pasted secret. */ +const SSH_NAMES = [ + "AddKeysToAgent", + "CertificateFile", + "ControlMaster", + "ControlPath", + "ControlPersist", + "IdentityAgent", + "KnownHostsCommand", + "LocalCommand", + "Match", + "PermitLocalCommand", + "PKCS11Provider", + "ProxyCommand", + "ProxyJump", + "RemoteCommand", + "SecurityKeyProvider", +]; +const sshName = (keyword: string) => + SSH_NAMES.find((name) => name.toLowerCase() === keyword) ?? + (/^[a-z]{1,32}$/.test(keyword) ? keyword : "an unrecognised directive"); + +/** + * Splits a config file as ssh's `readconf.c` does: `keyword args` or `keyword=args`, double quotes + * group an argument, and a `#` token ends the line. Null when a line has no keyword. + */ +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: Array = []; + for (const token of match[2]!.matchAll(/"([^"]*)"|(\S+)/g)) { + if (token[2]?.startsWith("#") === true) break; + args.push(token[1] ?? token[2]!); + } + 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`; +}; + +/** + * 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"]); +// Each runs a program for transport or on every repository command, outside what the probe sees. +const CORE_REFUSED = new Set([ + "core.askpass", + "core.sshcommand", + "core.gitproxy", + "core.fsmonitor", + "core.hookspath", +]); +/** Allowed keys, matched from the start of the key. */ +const GIT_ALLOWED: ReadonlyArray = [ + // Commit identity. + /^user\./, + // Local repository behaviour; its transport and hook keys are refused above. + /^core\./, + // New repository and output defaults. + /^init\./, + /^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"; +}; + +/** + * 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, +): + | { readonly _tag: "refused"; readonly reason: string } + | { readonly _tag: "allowed"; readonly helpers: number } => { + let helpers = 0; + for (const { origin, key, value } of entries) { + if (origin === "command line:") { + // `VcsProcess` sets this on every run; the check fails on any other `GIT_CONFIG_*` variable. + if (key === "safe.barerepository" && value === "explicit") continue; + return { _tag: "refused", reason: "git config is set from the environment" }; + } + if (!origin.startsWith("file:")) { + return { _tag: "refused", reason: "git config comes from an unexpected source" }; + } + 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 }; +}; diff --git a/apps/server/src/kin/approvals/T3GitHubCredential.test.ts b/apps/server/src/kin/approvals/T3GitHubCredential.test.ts index bb1aad24d9e40..540871003c141 100644 --- a/apps/server/src/kin/approvals/T3GitHubCredential.test.ts +++ b/apps/server/src/kin/approvals/T3GitHubCredential.test.ts @@ -72,11 +72,13 @@ describe("T3GitHubCredential", () => { }), ); assert.deepStrictEqual(stored.account, Option.some("kin-bot")); - assert.deepInclude(yield* (yield* slotOver({})).read, { - saved: Option.none(), - app: Option.none(), - account: Option.none(), - }); + 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), ); diff --git a/apps/server/src/kin/approvals/T3GitHubCredential.ts b/apps/server/src/kin/approvals/T3GitHubCredential.ts index fde1388786945..d1551cc40db5e 100644 --- a/apps/server/src/kin/approvals/T3GitHubCredential.ts +++ b/apps/server/src/kin/approvals/T3GitHubCredential.ts @@ -1,5 +1,6 @@ import { requireSettingsSecretLiteral } from "@t3tools/contracts"; 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"; @@ -93,13 +94,19 @@ const readStored = Effect.fn("T3GitHubCredential.read")(function* ( // 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"); - const { tokens, signIns, hosts } = github.value; + // 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")), ); - const rawSession = signIns[HOST]; - const session = rawSession === undefined ? Option.none() : decodeSession(rawSession); - if (rawSession !== undefined && Option.isNone(session)) { + // 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"); } return { diff --git a/apps/server/src/kin/chat/executionNotices.ts b/apps/server/src/kin/chat/executionNotices.ts index 6301278f85c48..af85015e23849 100644 --- a/apps/server/src/kin/chat/executionNotices.ts +++ b/apps/server/src/kin/chat/executionNotices.ts @@ -55,7 +55,7 @@ const FAILURE_TEXT: Record { +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.` diff --git a/docs/kin/specs/approvals.md b/docs/kin/specs/approvals.md index 19426943be5dd..08ebd5dc88703 100644 --- a/docs/kin/specs/approvals.md +++ b/docs/kin/specs/approvals.md @@ -519,34 +519,35 @@ 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: every credential T3 holds (each named by `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`), every account `gh` holds for github.com, active or not, the login behind the github.com git credential helper, 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 and environment to be minimal and known (an allowlist), and fails closed on anything outside it. If any login found isn't `KIN_GITHUB_BOT_LOGIN`, 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. `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). +- `env_token`: one of the environment variables listed below is set, or T3's credential came from the environment. The report names the variables, never their values. - `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: a source the probe can't fully enumerate is never read as having no login. +- `identity_unknown`: the identity probe failed, the host is outside the allowlist, or an SSH answer was neither `Hi !` nor `Permission denied (publickey)`. The probe's error carries a fixed reason naming the source and the offending directive or key, never its value; a git subsection, which can hold a URL with a token, is shown as `*`. - `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 identity probe is the `T3GitHubIdentity` port; `GitHubIdentityProbe.ts` is its live layer. The port's `identities` has a required field per source (`t3`, `ghAccounts`, `gitCredential`, `ssh`), so the live layer must report all four, and any source it can't read fails the probe, which fails the check closed (`identity_unknown`). What each source reads: +The identity probe is the `T3GitHubIdentity` port; `GitHubIdentityProbe.ts` is its live layer, and `HostConfigAllowlist.ts` holds the allowlists. The port's `identities` has a required field per source (`t3`, `ghAccounts`, `gitCredential`, `ssh`), so the live layer must report all four, and any source it can't read fails the probe. 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, `GH_TOKEN`/`GITHUB_TOKEN`, and `gh auth token` (for the active account and for the account Settings picks). `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 settings afterwards (`T3GitHubCredential.ts`, once); 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. +- `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 settings afterwards (`T3GitHubCredential.ts`, once); 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. - `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`: the login 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 configured `credential.*.helper` fails. 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. -- `ssh`: `ssh -G git@github.com` first. An `IdentityAgent` other than `none` or `SSH_AUTH_SOCK` (which the probe empties), or a `PKCS11Provider`, fails, since an agent or token can hold keys the probe can't list. The probe expands each `IdentityFile` it lists (`~`, `%d`, `%u`, `%h`, `%r`, `%p`, `%n`, `%i`; any other token fails) and adds 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 `ssh -T -o BatchMode=yes -o StrictHostKeyChecking=yes git@github.com` (`GITHUB_SSH_PROBE`) runs as is, and once per key with `-o IdentitiesOnly=yes -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`. +- `gitCredential`: one `git config --list --show-origin -z`, checked against the git allowlist below, then the login 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. +- `ssh`: `~/.ssh/config` and `/etc/ssh/ssh_config`, with every file they `Include` (32 files and 64 KiB each at most), checked against the SSH allowlist below. Every `IdentityFile` under any `Host` in either 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 `ssh -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 the 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, and concurrent checks wait on the one running. `requirePassing`, which gates the side effect after the claim, always runs a fresh probe. 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. Orgs without a saved merge token haven't set GitHub up and get no notice. +`check` shares one probe for 60 seconds across every org and held action, 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. Orgs without a saved merge token haven't set GitHub up and get no notice. -The Kin host must be set up so the check can pass: +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: -- one git credential helper, holding only the bot's credential for github.com; -- no `IdentityAgent` (1Password, Secretive) or `PKCS11Provider` for github.com in `~/.ssh/config`; -- no passphrase-protected private keys in `~/.ssh` or named by `IdentityFile`; -- github.com in `~/.ssh/known_hosts`, since the probe uses `StrictHostKeyChecking=yes`; -- gh 2.81 or newer. +- **Environment.** None of `GH_TOKEN`, `GITHUB_TOKEN`, `GH_ENTERPRISE_TOKEN`, `GITHUB_ENTERPRISE_TOKEN`, `SSH_AUTH_SOCK`, `GIT_ASKPASS`, `SSH_ASKPASS`, `GIT_SSH`, `GIT_SSH_COMMAND`, `GIT_CONFIG_PARAMETERS` or `GIT_CONFIG_COUNT` is set (macOS launchd sets `SSH_AUTH_SOCK` for every login session, so unset it in the service definition). `$HOME` is unset or the passwd entry's home. +- **`~/.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`. 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. +- **`/etc/ssh/ssh_config`** 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`), `init.`, `color.`, `alias.`, `pull.`, `push.`, `fetch.`, `merge.`, `diff.` and `filter.lfs.`, plus `credential.helper` and `credential.https://github.com.helper`, with one helper in effect, holding only the bot's credential. Anything else fails, `include.*`, `includeIf.*`, `http.*`, `url.*`, 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. +- **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. The probe is defence in depth, not the boundary. It sees only the sources above, as the server's OS user is configured, and an agent running as that same user can always find more: an SSH agent socket on disk it points `SSH_AUTH_SOCK` at, a key outside these paths, a token in another tool's config, the merge token and the App's private key in the keychain (open question 4). The real boundary is keeping the owner's credentials off the Kin host's OS user entirely, and in time a separate executor OS user. 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", { From fe59646402816232c2c836357b68816e24a81421 Mon Sep 17 00:00:00 2001 From: Surajsm60720 <1ds22is166@dsce.edu.in> Date: Mon, 9 Nov 2026 11:56:14 +0530 Subject: [PATCH 4/7] fix(kin): close the identity probe's round-3 review findings The round-3 review of #87 found holes the allowlist probe still let through. Each now fails closed with a fixed, value-free reason: - SSH: arguments ssh's argv_split reads differently (single quotes, backslashes, a quote or # inside a word) fail, as does an Include without a glob naming a missing file. Unknown directives are named only when they're OpenSSH keywords. - ssh is resolved once on PATH; every probe uses that path, and the system configs it reads come from ` -v -G` path lines, run only after the user config passes (-F would skip the system config). - Git: only init.defaultBranch under init.*. Each project's own config (--local, and --worktree when on) is checked against a repository allowlist, 64 projects at most. - One helper holding several accounts: git credential fill also runs with the owner's and the merge token's logins as the username. - Environment: patterns (GH_*, GITHUB_*, GIT_*, SSH_*, *_TOKEN) with three documented exceptions, over the server's environment and every provider instance's as mergeProviderInstanceEnvironment builds it. - The probe's reason is no longer dropped: the report, the audit row and the owner's notice carry it. Spec section 12 now says the probe guards against accidental use only, that a separate executor OS user is the real boundary, adds the shell startup files requirement, and lists default setups that always fail. Co-Authored-By: Claude Opus 5.5 --- .../src/kin/approvals/ActionExecutor.test.ts | 40 +- .../src/kin/approvals/ActionExecutor.ts | 10 +- .../kin/approvals/CredentialHygiene.test.ts | 123 +++++- .../src/kin/approvals/CredentialHygiene.ts | 142 ++++-- .../kin/approvals/CredentialHygieneReport.ts | 14 + .../kin/approvals/GitHubIdentityProbe.test.ts | 415 ++++++++++++++++-- .../src/kin/approvals/GitHubIdentityProbe.ts | 292 +++++++++--- .../src/kin/approvals/HostConfigAllowlist.ts | 240 ++++++++-- .../kin/approvals/T3GitHubCredential.test.ts | 53 ++- .../src/kin/approvals/T3GitHubCredential.ts | 47 +- apps/server/src/kin/approvals/layer.ts | 3 + apps/server/src/kin/chat/executionNotices.ts | 7 +- .../src/kin/github/OwnerMergeClient.test.ts | 19 +- apps/server/src/kin/mcp/layer.test.ts | 2 + docs/kin/specs/approvals.md | 44 +- 15 files changed, 1200 insertions(+), 251 deletions(-) diff --git a/apps/server/src/kin/approvals/ActionExecutor.test.ts b/apps/server/src/kin/approvals/ActionExecutor.test.ts index 7f51d8a042a04..f7ee17e2d4505 100644 --- a/apps/server/src/kin/approvals/ActionExecutor.test.ts +++ b/apps/server/src/kin/approvals/ActionExecutor.test.ts @@ -14,7 +14,7 @@ import { } from "@t3tools/contracts"; import { encodeGrantSigningInput } from "@t3tools/shared/kinGrant"; import * as Clock from "effect/Clock"; -import * as ConfigProvider from "effect/ConfigProvider"; +import * as HostProcess from "@t3tools/shared/HostProcess"; import * as Deferred from "effect/Deferred"; import * as Effect from "effect/Effect"; import * as Fiber from "effect/Fiber"; @@ -265,25 +265,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: [{ 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", 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( @@ -790,7 +793,8 @@ 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"], ); // Both holds within the minute shared one probe. assert.strictEqual(checks.count, 1); @@ -1026,7 +1030,7 @@ describe("KinActionExecutor", () => { ); assert.deepStrictEqual( failed.map((entry) => [entry.actionId, entry.result]), - [[undefined, "identity_unknown"]], + [[undefined, "identity_unknown: probe failed"]], ); assert.deepStrictEqual(credentialNotices.sent, [ { orgId, failures: ["identity_unknown"] }, diff --git a/apps/server/src/kin/approvals/ActionExecutor.ts b/apps/server/src/kin/approvals/ActionExecutor.ts index 606c57a8021ac..d2053b069df72 100644 --- a/apps/server/src/kin/approvals/ActionExecutor.ts +++ b/apps/server/src/kin/approvals/ActionExecutor.ts @@ -26,7 +26,7 @@ import { } from "./ActionHandlers.ts"; import { KinAudit } from "./Audit.ts"; import { CredentialHygiene } from "./CredentialHygiene.ts"; -import type { CredentialHygieneReport } from "./CredentialHygieneReport.ts"; +import { type CredentialHygieneReport, failureResult } from "./CredentialHygieneReport.ts"; import { KinExecutionNotices, type KinExecutionOutcome } from "./ExecutionNotices.ts"; import { KinOrgRoles } from "./GrantPorts.ts"; import { KinExecutionLedger, KinPendingActions, type ActionRef } from "./PendingActions.ts"; @@ -226,7 +226,7 @@ export const make = Effect.gen(function* () { (error) => new KinExecError({ reason: "credential_check_failed", - detail: error.failures.join(","), + detail: failureResult(error), }), ), ); @@ -298,7 +298,7 @@ export const make = Effect.gen(function* () { // Checked before the claim too, so a misconfigured host holds the grant instead of spending it. const check = yield* hygiene.check(action.orgId); if (!check.passed) { - const failures = check.failures.join(","); + const failures = failureResult(check); if (auditedHolds.get(action.id) !== failures) { auditedHolds.set(action.id, failures); yield* audit @@ -368,7 +368,7 @@ export const make = Effect.gen(function* () { Effect.gen(function* () { const orgId = KinOrgId.make(row.orgId); const report = yield* hygiene.check(orgId); - const result = [...report.failures, ...report.warnings].join(","); + const result = [...report.failures, ...report.warnings, ...report.reasons].join(","); const previous = reportedChecks.get(orgId); reportedChecks.set(orgId, result); // An org with no merge token hasn't set GitHub up for Kin, so there is no one to tell. @@ -377,7 +377,7 @@ export const make = Effect.gen(function* () { } if (!report.passed) { yield* audit - .append({ orgId, event: "credential_check_failed", result: report.failures.join(",") }) + .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) { diff --git a/apps/server/src/kin/approvals/CredentialHygiene.test.ts b/apps/server/src/kin/approvals/CredentialHygiene.test.ts index ea1626350780d..2401b93be6947 100644 --- a/apps/server/src/kin/approvals/CredentialHygiene.test.ts +++ b/apps/server/src/kin/approvals/CredentialHygiene.test.ts @@ -1,6 +1,6 @@ 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"; @@ -25,6 +25,11 @@ type Identity = readonly gh?: ReadonlyArray; readonly git?: Option.Option; readonly ssh?: ReadonlyArray; + readonly gitFor?: ReadonlyArray; + readonly providers?: ReadonlyArray<{ + readonly instance: string; + readonly names: ReadonlyArray; + }>; } | "unreachable"; @@ -33,7 +38,11 @@ const setup = (input: { readonly env?: Record; readonly mergeToken?: { readonly login: string; readonly expiresAt: number } | null; }) => { - const state = { identity: input.identity, probes: 0 }; + const state = { + identity: input.identity, + probes: 0, + ownerLogins: [] as ReadonlyArray, + }; const mergeToken = input.mergeToken === undefined ? { login: "Nishchay", expiresAt: NOW + 30 * DAY } @@ -52,22 +61,26 @@ const setup = (input: { const identity = Layer.succeed( CredentialHygiene.T3GitHubIdentity, CredentialHygiene.T3GitHubIdentity.of({ - identities: Effect.suspend(() => { - state.probes += 1; - // Takes a second, so a check that starts meanwhile shares it. - return Effect.sleep("1 second"); - }).pipe( - Effect.andThen(() => - state.identity === "unreachable" - ? Effect.fail(new CredentialHygiene.T3GitHubIdentityError({ cause: "offline" })) - : Effect.succeed({ - t3: state.identity.t3 ?? [], - 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(() => + state.identity === "unreachable" + ? Effect.fail(new CredentialHygiene.T3GitHubIdentityError({ cause: "offline" })) + : Effect.succeed({ + t3: state.identity.t3 ?? [], + ghAccounts: state.identity.gh ?? [], + gitCredential: state.identity.git ?? Option.none(), + gitCredentialFor: state.identity.gitFor ?? [], + ssh: state.identity.ssh ?? [SSH_DENIED], + providerEnvironments: state.identity.providers ?? [], + }), + ), ), - ), }), ); const layer = CredentialHygiene.layer.pipe( @@ -75,7 +88,7 @@ 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(Layer.succeed(HostProcess.Environment, input.env ?? {})), Layer.provide(scopeLayer()), ); return { state, layer }; @@ -230,6 +243,13 @@ describe("CredentialHygiene", () => { "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) => @@ -245,6 +265,16 @@ describe("CredentialHygiene", () => { ["env_token"], ], ["the identity unreachable", { identity: "unreachable" }, ["identity_unknown"]], + [ + "a provider instance's environment with a token", + { identity: { ...bot, providers: [{ instance: "codex", names: ["PATH", "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", @@ -292,6 +322,63 @@ describe("CredentialHygiene", () => { }).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", "PATH"] }, + { instance: "claude", names: ["PATH"] }, + ], + }, + env: { GH_TOKEN: "ghp_secret_value", PATH: "/bin" }, + }); + const report = yield* check.pipe(Effect.provide(layer)); + assert.deepStrictEqual(report.failures, ["env_token", "owner_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`, + ]); + 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 6249eb10a1c8c..fd58b7cddf068 100644 --- a/apps/server/src/kin/approvals/CredentialHygiene.ts +++ b/apps/server/src/kin/approvals/CredentialHygiene.ts @@ -1,12 +1,12 @@ 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"; @@ -14,26 +14,7 @@ import { CredentialHygieneFailure, type CredentialHygieneReport, } from "./CredentialHygieneReport.ts"; - -/** - * Agent CLIs inherit the server's environment, so any of these hands them a GitHub credential or - * a way to one the probe can't see: a token, an SSH agent (the probe sees only the first key GitHub - * accepts), a program git or ssh asks for a password, an ssh command, or git config from outside - * the files the probe checks. - */ -const ENV_TOKEN_NAMES = [ - "GH_TOKEN", - "GITHUB_TOKEN", - "GH_ENTERPRISE_TOKEN", - "GITHUB_ENTERPRISE_TOKEN", - "SSH_AUTH_SOCK", - "GIT_ASKPASS", - "SSH_ASKPASS", - "GIT_SSH", - "GIT_SSH_COMMAND", - "GIT_CONFIG_PARAMETERS", - "GIT_CONFIG_COUNT", -]; +import { forbiddenEnvNames } from "./HostConfigAllowlist.ts"; /** * The probe the live layer (`GitHubIdentityProbe.ts`) runs for `T3GitHubIdentities.ssh`, with @@ -88,6 +69,10 @@ export class T3GitHubIdentityError extends Schema.TaggedError; + /** 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 (each `IdentityFile` the SSH configs name, and every key in `~/.ssh`) with @@ -116,11 +103,24 @@ export interface T3GitHubIdentities { * check parses them; any answer it can't read fails it. */ readonly ssh: ReadonlyArray; + /** + * The variables each provider instance's environment sets, 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") {} const ISSUE_PASS: unique symbol = Symbol("CredentialHygienePass.issue"); @@ -160,7 +160,11 @@ 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(", ")}).`; @@ -190,31 +194,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 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), - ); + const environment = yield* HostProcess.Environment; - // Single-flight too: concurrent checks wait on the probe already running. - const cachedIdentities = yield* Effect.cachedWithTTL(identity.identities, PROBE_CACHE_TTL); + // 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 evaluate = Effect.fnUntraced(function* ( orgId: KinOrgId, - probe: typeof identity.identities, + 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) @@ -226,14 +241,24 @@ 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()], + ), + ), + ]; - const probed = yield* probe.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; + const { t3, ghAccounts, gitCredential, gitCredentialFor, ssh, providerEnvironments } = + probed.success; t3Login = t3[0]?.login ?? null; for (const { login, source } of t3) { if (source === "env") failures.add("env_token"); @@ -243,11 +268,29 @@ const make = Effect.gen(function* () { if (Option.isSome(gitCredential)) { found.push({ source: "git_credential", login: 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 own = forbiddenEnvNames(Object.fromEntries(names.map((name) => [name, "set"]))); + for (const name of own.filter((name) => !envNames.includes(name))) { + failures.add("env_token"); + const shown = /^[A-Za-z0-9_-]{1,64}$/.test(instance) ? `"${instance}"` : "an instance"; + reasons.push(`provider instance ${shown}'s environment sets ${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 === "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 }); } @@ -267,6 +310,7 @@ const make = Effect.gen(function* () { const report: CredentialHygieneReport = { passed: failures.size === 0, failures: [...failures], + reasons: [...new Set(reasons)], warnings, envTokenNames: envNames, t3Login, @@ -283,12 +327,16 @@ const make = Effect.gen(function* () { 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) { // A fresh probe, started after the caller's claim, so a cached pass never gates a side effect. - const report = yield* evaluate(orgId, identity.identities); + 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 index cf8135973730a..ce99c7f27583f 100644 --- a/apps/server/src/kin/approvals/CredentialHygieneReport.ts +++ b/apps/server/src/kin/approvals/CredentialHygieneReport.ts @@ -17,6 +17,11 @@ 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; @@ -29,3 +34,12 @@ export interface CredentialHygieneReport { }>; 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/GitHubIdentityProbe.test.ts b/apps/server/src/kin/approvals/GitHubIdentityProbe.test.ts index 3275e95ea5019..3d3f41a825103 100644 --- a/apps/server/src/kin/approvals/GitHubIdentityProbe.test.ts +++ b/apps/server/src/kin/approvals/GitHubIdentityProbe.test.ts @@ -7,7 +7,6 @@ import { VcsProcessTimeoutError, } from "@t3tools/contracts"; import * as HostProcess from "@t3tools/shared/HostProcess"; -import * as ConfigProvider from "effect/ConfigProvider"; import * as Context from "effect/Context"; import * as Effect from "effect/Effect"; import * as Fiber from "effect/Fiber"; @@ -40,6 +39,7 @@ 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"; @@ -186,8 +186,12 @@ const stored = ( 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 { @@ -203,6 +207,12 @@ interface Machine { 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. */ @@ -226,6 +236,8 @@ const botMachine = (): Machine => ({ }), 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, @@ -247,6 +259,31 @@ const withSystemConfig = (...lines: ReadonlyArray): Partial => 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 => @@ -288,9 +325,30 @@ const probe = (machine: Machine) => 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) => { @@ -301,8 +359,18 @@ const probe = (machine: Machine) => const user = input.args.find((arg) => arg.startsWith("--user=")); return machine.ghToken(user?.slice("--user=".length)); } - if (input.command === "git") return first === "config" ? machine.gitConfig : machine.git; + 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]); }, @@ -330,9 +398,16 @@ const probe = (machine: Machine) => yield* Layer.build(T3GitHubCredential.slotLayer), T3GitHubCredential.T3GitHubCredential, ); - if (machine.t3 !== "uninstalled") yield* slot.install(Effect.succeed(machine.t3)); + 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(machine.env).map(([name, value]) => [name, value.replace("", home)]), + 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), @@ -340,7 +415,6 @@ const probe = (machine: Machine) => Effect.provideService(HostProcess.Environment, env), Effect.provideService(HostProcess.Username, "kin"), Effect.provideService(HostProcess.UserId, 501), - Effect.provideService(GitHubIdentityProbe.SystemSshConfig, path.join(etc, "ssh_config")), Effect.provideService(T3GitHubCredential.T3GitHubCredential, slot), Effect.provide(Layer.mergeAll(github, scopeLayer())), ); @@ -358,12 +432,21 @@ const probe = (machine: Machine) => Layer.provide(OwnerMergeClient.layer), Layer.provide(keychain.layer), Layer.provide(fakeGitHub(() => ({ status: 500 })).layer), - Layer.provide(ConfigProvider.layer(ConfigProvider.fromEnv({ env: {} }))), + Layer.provide(Layer.succeed(HostProcess.Environment, {})), Layer.provide(scopeLayer()), ), ); const check = Context.get(hygiene, CredentialHygiene.CredentialHygiene).check(ORG); - return { identities: identity.identities, check, runs, home, requests, slot }; + return { + identities: identity.identities({ ownerLogins: OWNER_LOGINS }), + check, + runs, + home, + root, + bin, + requests, + slot, + }; }); const run = (effect: Effect.Effect) => @@ -391,9 +474,30 @@ describe("GitHubIdentityProbe", () => { run( Effect.gen(function* () { const path = yield* Path.Path; - const { identities, check, runs, home } = yield* probe({ + 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: ["PATH", "CODEX_HOME"] }], + }), + repos: { + repo: { + local: gitList( + ["core.repositoryformatversion", "0", "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]; @@ -404,19 +508,41 @@ describe("GitHubIdentityProbe", () => { ], ghAccounts: ["kin-bot"], gitCredential: Option.some("kin-bot"), + gitCredentialFor: [], ssh: [sshHi("kin-bot"), SSH_DENIED], + providerEnvironments: [{ instance: "codex", names: ["PATH", "CODEX_HOME"] }], }); - assert.deepInclude(yield* check, { passed: true, failures: [] }); + 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", @@ -427,9 +553,9 @@ describe("GitHubIdentityProbe", () => { path.join(ssh, name), ]), // Only the validated config, with no shared connection and no agent (`SSH_AUTH_SOCK`). - ["ssh", "-F", config, ...SSH_PROBE_OPTIONS, "git@github.com"], + [sshBin, "-F", config, ...SSH_PROBE_OPTIONS, "git@github.com"], [ - "ssh", + sshBin, "-F", config, ...SSH_PROBE_OPTIONS, @@ -457,10 +583,20 @@ describe("GitHubIdentityProbe", () => { // 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[input.command as keyof typeof envs]); + assert.deepStrictEqual( + input.env, + envs[path.basename(input.command) as keyof typeof envs], + ); } - const git = probed.find((input) => input.args[0] === "credential"); - assert.strictEqual(git?.stdin, "protocol=https\nhost=github.com\n\n"); + 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", + ], + ); }), ), ); @@ -468,7 +604,8 @@ describe("GitHubIdentityProbe", () => { it.effect("reads no login from sources that have none", () => run( Effect.gen(function* () { - const { identities, check, requests, runs } = yield* probe({ + 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 }) }), @@ -478,20 +615,22 @@ describe("GitHubIdentityProbe", () => { git: answer({ exitCode: 128, stderr: NO_HELPER_PROMPTS_OFF }), ssh: () => answer({ stderr: SSH_DENIED }), files: {}, - system: {}, + 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(" ")), - ["ssh", "-F", "none", ...SSH_PROBE_OPTIONS, "git@github.com"].join(" "), + [path.join(bin, "ssh"), "-F", "none", ...SSH_PROBE_OPTIONS, "git@github.com"].join(" "), ); }), ), @@ -511,6 +650,46 @@ describe("GitHubIdentityProbe", () => { 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.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( @@ -561,6 +740,15 @@ describe("GitHubIdentityProbe", () => { ], ["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", { @@ -808,8 +996,8 @@ describe("GitHubIdentityProbe", () => { ["KnownHostsCommand /bin/cat", "KnownHostsCommand"], ["LocalCommand id", "LocalCommand"], ["PermitLocalCommand yes", "PermitLocalCommand"], - ["CanonicalizeHostname yes", "canonicalizehostname"], - ["SendEnv LANG", "sendenv"], + ["CanonicalizeHostname yes", "CanonicalizeHostname"], + ["SendEnv LANG", "SendEnv"], ["ghp_0123456789abcdef0123456789abcdef01234567", "an unrecognised directive"], ] as const ).map( @@ -910,6 +1098,152 @@ describe("GitHubIdentityProbe", () => { "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", + ], + // 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"], + ["user.email", "owner@example.com", "user.email"], + ["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", @@ -935,7 +1269,7 @@ describe("GitHubIdentityProbe", () => { { ssh: () => Effect.fail(timeout("ssh")) }, "ssh: VcsProcessTimeoutError", ], - ] as const)("fails closed when %s", ([, change, reason]) => + ] as const)("fails closed when %s", ([, change, ...reasons]) => run( Effect.gen(function* () { const { identities, check } = yield* probe({ ...botMachine(), ...change } as Machine); @@ -944,14 +1278,28 @@ describe("GitHubIdentityProbe", () => { 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. - assert.include(String(error.cause), 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"); - assert.deepInclude(yield* Fiber.join(checked), { - passed: false, - failures: ["identity_unknown"], + 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("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"))); }), ), ); @@ -988,7 +1336,7 @@ describe("GitHubIdentityProbe", () => { return span; }, }); - const secrets = [GIT_PASSWORD, GH_TOKEN, APP_TOKEN, SAVED_TOKEN, OWNER_TOKEN]; + const secrets = [GIT_PASSWORD, GH_TOKEN, APP_TOKEN, SAVED_TOKEN, OWNER_TOKEN, SSH_G_SECRET]; return run( Effect.gen(function* () { const seen: Array = []; @@ -1003,6 +1351,17 @@ describe("GitHubIdentityProbe", () => { }; 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. @@ -1016,6 +1375,8 @@ describe("GitHubIdentityProbe", () => { t3, users, ghToken, + gitFor, + sshG, ...change, }); const result = yield* Effect.result(identities); @@ -1031,7 +1392,9 @@ describe("GitHubIdentityProbe", () => { yield* Effect.logWarning("probe", result); // Each does reach GitHub, as a bearer token and nowhere else. if (index === 0) { - for (const secret of secrets) assert.include(requests, `Bearer ${secret}`); + for (const secret of secrets.filter((value) => value !== SSH_G_SECRET)) { + assert.include(requests, `Bearer ${secret}`); + } } } for (const span of spans) { diff --git a/apps/server/src/kin/approvals/GitHubIdentityProbe.ts b/apps/server/src/kin/approvals/GitHubIdentityProbe.ts index f4428cfd57e02..9da2996d7af11 100644 --- a/apps/server/src/kin/approvals/GitHubIdentityProbe.ts +++ b/apps/server/src/kin/approvals/GitHubIdentityProbe.ts @@ -1,6 +1,5 @@ import * as HostProcess from "@t3tools/shared/HostProcess"; import * as Clock from "effect/Clock"; -import * as Context from "effect/Context"; import * as Effect from "effect/Effect"; import * as FileSystem from "effect/FileSystem"; import * as Layer from "effect/Layer"; @@ -22,12 +21,14 @@ import { } from "./CredentialHygiene.ts"; import { checkGitConfig, + checkRepoGitConfig, expandIdentityFile, parseGitConfigList, parseSshConfig, + sshConfigPaths, sshViolation, } from "./HostConfigAllowlist.ts"; -import { T3GitHubCredential } from "./T3GitHubCredential.ts"; +import { T3GitHubCredential, type T3StoredCredentials } from "./T3GitHubCredential.ts"; const HOST = "github.com"; const PROCESS_TIMEOUT_MS = 15_000; @@ -41,6 +42,7 @@ 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; @@ -81,11 +83,11 @@ const GhAuthStatus = Schema.fromJsonString( const decodeGhAuthStatus = Schema.decodeUnknownOption(GhAuthStatus); /** - * `GITHUB_SSH_PROBE` 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. + * `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 = (config: string, key?: string) => [ - "ssh", +const sshProbe = (ssh: string, config: string, key?: string) => [ + ssh, "-F", config, ...GITHUB_SSH_PROBE.slice(1, -1), @@ -93,12 +95,6 @@ const sshProbe = (config: string, key?: string) => [ ...GITHUB_SSH_PROBE.slice(-1), ]; -/** The system-wide SSH config, which agents' ssh reads after `~/.ssh/config`. */ -export const SystemSshConfig = Context.Reference( - "t3/kin/approvals/GitHubIdentityProbe/SystemSshConfig", - { defaultValue: () => "/etc/ssh/ssh_config" }, -); - /** * 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. @@ -115,7 +111,6 @@ export const make = Effect.gen(function* () { const environment = yield* HostProcess.Environment; const username = yield* HostProcess.Username; const userId = yield* HostProcess.UserId; - const systemSshConfig = yield* SystemSshConfig; // From the filesystem root, outside any repository, so no repository config picks the helper. const cwd = path.parse(path.resolve(home || "/")).root; @@ -199,10 +194,7 @@ export const make = Effect.gen(function* () { ); /** Every credential T3 holds, whichever it would pick and whether or not the host is enabled. */ - const t3 = Effect.gen(function* () { - const stored = yield* t3Credential.read.pipe( - Effect.mapError((error) => unreadable("t3", error.reason)), - ); + const t3 = Effect.fn("KinGitHubIdentity.t3")(function* (stored: T3StoredCredentials) { const now = yield* Clock.currentTimeMillis; const tokens: Array<{ readonly source: string; readonly token: Redacted.Redacted }> = []; @@ -309,36 +301,126 @@ export const make = Effect.gen(function* () { }), ); - // The password stays in this pipeline: it is wrapped at once, sent to GitHub and dropped. - const gitCredential = Effect.gen(function* () { + 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, + ) { const helpers = yield* gitHelpers; - if (helpers === null) return Option.none(); + 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 output = yield* run({ - operation: "KinGitHubIdentity.gitCredential", + 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: ["credential", "fill"], - stdin: `protocol=https\nhost=${HOST}\n\n`, + args: ["-C", root, "config", "--list", "--show-origin", "-z", scope], env: GIT_ENV, timeoutMs: PROCESS_TIMEOUT_MS, - }).pipe(Effect.mapError((error) => unreadable("git_credential", error._tag))); - if (output.exitCode !== 0) { - // git falls back to a terminal prompt, which is off, when no helper answers. With a helper - // configured that also covers a crashed, locked or refusing one, so it can't mean none. - if (/terminal prompts disabled/i.test(output.stderr) && helpers === 0) { - return Option.none(); - } - return yield* unreadable("git_credential", `git credential fill exited ${output.exitCode}`); + }).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`); } - const line = output.stdout.split("\n").find((entry) => entry.startsWith("password=")); - const password = line?.slice("password=".length).replace(/\r$/, "") ?? ""; - if (password === "") { - return yield* unreadable("git_credential", "the helper answered without a password"); + 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; + } } - return Option.some(yield* loginOf("git_credential", Redacted.make(password))); }); /** The files an `Include` pattern names, globbing its last path segment as ssh does. */ @@ -353,7 +435,7 @@ export const make = Effect.gen(function* () { 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 [full]; + if (!GLOB.test(name)) return { glob: false, files: [full] }; const matcher = new RegExp( `^${name .replace(/[.+^$(){}|\\\]]/g, "\\$&") @@ -370,10 +452,11 @@ export const make = Effect.gen(function* () { ), ); // Like glob(3), `*` doesn't match a leading dot. - return names + const files = names .filter((entry) => (name.startsWith(".") || !entry.startsWith(".")) && matcher.test(entry)) .toSorted() .map((entry) => path.join(dir, entry)); + return { glob: true, files }; }); /** @@ -382,15 +465,21 @@ export const make = Effect.gen(function* () { */ const readSshConfig = Effect.fn("KinGitHubIdentity.readSshConfig")(function* ( top: string, - system: boolean, + 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"; - // Relative includes resolve against `~/.ssh` for the user's config and `/etc/ssh` for the system's. - const base = system ? path.dirname(top) : path.join(home, ".ssh"); 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, @@ -400,8 +489,13 @@ export const make = Effect.gen(function* () { : Effect.fail(unreadable("ssh", `${label} can't be read`)), ), ); - // ssh skips an Include that matches nothing. - if (Option.isNone(info)) return false; + // 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) { @@ -437,16 +531,15 @@ export const make = Effect.gen(function* () { } if (directive.keyword === "include") { for (const pattern of directive.args) { - for (const target of yield* includeTargets(pattern, base)) { - yield* visit(target); - } + const targets = yield* includeTargets(pattern, base); + for (const target of targets.files) yield* visit(target, !targets.glob); } } } return true; }); - const exists = yield* visit(top); - return exists ? identityFiles : null; + const exists = yield* visit(top, options.mustExist); + return { exists, identityFiles, files }; }); const isRegularFile = (file: string) => @@ -532,7 +625,7 @@ export const make = Effect.gen(function* () { const sshAnswer = (argv: ReadonlyArray) => run({ operation: "KinGitHubIdentity.ssh", - command: argv[0] ?? "ssh", + command: argv[0]!, args: argv.slice(1), env: SSH_ENV, timeoutMs: SSH_TIMEOUT_MS, @@ -543,21 +636,81 @@ export const make = Effect.gen(function* () { ), ); + /** + * 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, false); - // Agents' ssh reads this after the user's config; the probe's `-F` skips it, so its keys go by `-i`. - const fromSystem = yield* readSshConfig(systemSshConfig, true); - const named = [...(fromUser ?? []), ...(fromSystem ?? [])]; + 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 === null ? "none" : userConfig; - const plain = yield* sshAnswer(sshProbe(config)); - const perKey = yield* Effect.forEach(keys, (key) => sshAnswer(sshProbe(config, key)), { + 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]; @@ -565,15 +718,30 @@ export const make = Effect.gen(function* () { return T3GitHubIdentity.of({ // T3's credentials first: until the server installs its reader, nothing is spawned. - identities: Effect.gen(function* () { + identities: Effect.fn("KinGitHubIdentity.identities")(function* ({ ownerLogins }) { yield* homeMatches; - const t3Identities = yield* t3; + const stored = yield* t3Credential.read.pipe( + Effect.mapError((error) => unreadable("t3", error.reason)), + ); + const t3Identities = yield* t3(stored); const rest = yield* Effect.all( - { ghAccounts, gitCredential, ssh }, + { + ghAccounts, + git: gitCredential(ownerLogins), + repositories: repositories(stored.projectRoots), + ssh, + }, { concurrency: "unbounded" }, ); - return { t3: t3Identities, ...rest } satisfies T3GitHubIdentities; - }).pipe(Effect.withSpan("KinGitHubIdentity.identities")), + return { + t3: t3Identities, + ghAccounts: rest.ghAccounts, + gitCredential: rest.git.login, + gitCredentialFor: rest.git.ownerHeld, + ssh: rest.ssh, + providerEnvironments: stored.providerEnvironments, + } satisfies T3GitHubIdentities; + }), }); }); diff --git a/apps/server/src/kin/approvals/HostConfigAllowlist.ts b/apps/server/src/kin/approvals/HostConfigAllowlist.ts index 7a46448b32211..2aafcb8ab2d99 100644 --- a/apps/server/src/kin/approvals/HostConfigAllowlist.ts +++ b/apps/server/src/kin/approvals/HostConfigAllowlist.ts @@ -1,7 +1,8 @@ /** - * What the Kin host's SSH and git config 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 or key, never its value. + * 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 { @@ -41,7 +42,7 @@ const SSH_ALLOWED: Record) => boolean> = { }; /** - * What distro system configs ship, allowed in `/etc/ssh` only. macOS: `SendEnv` and the crypto + * 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. */ @@ -58,31 +59,62 @@ const SSH_SYSTEM_ALLOWED = new Set([ "gssapiauthentication", ]); -/** Spellings for reasons; any other directive is named only if it can't be a pasted secret. */ -const SSH_NAMES = [ - "AddKeysToAgent", - "CertificateFile", - "ControlMaster", - "ControlPath", - "ControlPersist", - "IdentityAgent", - "KnownHostsCommand", - "LocalCommand", - "Match", - "PermitLocalCommand", - "PKCS11Provider", - "ProxyCommand", - "ProxyJump", - "RemoteCommand", - "SecurityKeyProvider", -]; +/** 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_NAMES.find((name) => name.toLowerCase() === keyword) ?? - (/^[a-z]{1,32}$/.test(keyword) ? keyword : "an unrecognised directive"); + 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`, double quotes - * group an argument, and a `#` token ends the line. Null when a line has no keyword. + * 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 = []; @@ -91,11 +123,8 @@ export const parseSshConfig = (text: string): ReadonlyArray | null if (line === "" || line.startsWith("#")) continue; const match = /^([^\s="]+)(?:\s*=\s*|\s+|$)(.*)$/.exec(line); if (match === null) return null; - const args: Array = []; - for (const token of match[2]!.matchAll(/"([^"]*)"|(\S+)/g)) { - if (token[2]?.startsWith("#") === true) break; - args.push(token[1] ?? token[2]!); - } + const args = splitSshArgs(match[2]!); + if (args === null) return null; directives.push({ keyword: match[1]!.toLowerCase(), args }); } return directives; @@ -113,6 +142,10 @@ export const sshViolation = (directive: SshDirective, system: boolean): string | : `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 @@ -167,7 +200,11 @@ export const parseGitConfigList = (stdout: string): ReadonlyArray = [ // Commit identity. /^user\./, - // Local repository behaviour; its transport and hook keys are refused above. + // Local repository behaviour; the keys above are refused. /^core\./, - // New repository and output defaults. - /^init\./, + // `init.templateDir` would copy its config and hooks into every new repository. + /^init\.defaultbranch$/, /^color\./, // Shorthands for commands the agent could run anyway. /^alias\./, @@ -202,25 +244,34 @@ const gitKeyName = (key: string) => { 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, -): - | { readonly _tag: "refused"; readonly reason: string } - | { readonly _tag: "allowed"; readonly helpers: number } => { +): GitConfigVerdict<{ readonly helpers: number }> => { let helpers = 0; - for (const { origin, key, value } of entries) { - if (origin === "command line:") { - // `VcsProcess` sets this on every run; the check fails on any other `GIT_CONFIG_*` variable. - if (key === "safe.barerepository" && value === "explicit") continue; - return { _tag: "refused", reason: "git config is set from the environment" }; - } - if (!origin.startsWith("file:")) { - return { _tag: "refused", reason: "git config comes from an unexpected source" }; - } + 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; @@ -231,3 +282,96 @@ export const checkGitConfig = ( } 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; + +/** + * 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") { + // `remote.pushDefault` names a remote and has no subsection. + const url = name === "url" || name === "pushurl"; + if (subsection && (url ? urlCarriesCredential(value) : !REMOTE_PLAIN.has(name))) + return refused; + } else if (section === "submodule" && subsection) { + if (name === "url" ? urlCarriesCredential(value) : name !== "active") return refused; + } else if (section !== "branch" && 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", +]); + +/** 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]) => (/^[A-Za-z0-9_]{1,64}$/.test(name) ? name : "a variable with an unusual name")) + .toSorted(); diff --git a/apps/server/src/kin/approvals/T3GitHubCredential.test.ts b/apps/server/src/kin/approvals/T3GitHubCredential.test.ts index 540871003c141..7ae14d8ad487b 100644 --- a/apps/server/src/kin/approvals/T3GitHubCredential.test.ts +++ b/apps/server/src/kin/approvals/T3GitHubCredential.test.ts @@ -5,6 +5,7 @@ import { 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"; @@ -14,6 +15,7 @@ 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) => @@ -27,8 +29,20 @@ const session = (accessToken: string) => const isSettingsError = Schema.is(ServerSettingsError); /** The slot with the server's reader installed over Settings that read as `github`. */ -const slotOver = (github: unknown | ServerSettingsError) => +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: () => @@ -36,6 +50,9 @@ const slotOver = (github: unknown | ServerSettingsError) => ? Effect.fail(github) : Effect.succeed({ ...DEFAULT_SERVER_SETTINGS, + ...(rest.providerInstances === undefined + ? {} + : { providerInstances: rest.providerInstances }), sourceControlHosts: { github }, } as unknown as ServerSettings), }, @@ -44,6 +61,7 @@ const slotOver = (github: unknown | ServerSettingsError) => 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); @@ -82,6 +100,37 @@ describe("T3GitHubCredential", () => { }).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: ["PATH", "GITHUB_PERSONAL_ACCESS_TOKEN", "SSH_AUTH_SOCK"] }, + // Disabled instances count: the terminal opens any instance's environment. + { instance: "claude", names: ["PATH", "HOME_TOOL"] }, + ]); + 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", @@ -111,6 +160,8 @@ describe("T3GitHubCredential", () => { 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); diff --git a/apps/server/src/kin/approvals/T3GitHubCredential.ts b/apps/server/src/kin/approvals/T3GitHubCredential.ts index d1551cc40db5e..0fc20ff2b964f 100644 --- a/apps/server/src/kin/approvals/T3GitHubCredential.ts +++ b/apps/server/src/kin/approvals/T3GitHubCredential.ts @@ -1,4 +1,6 @@ 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"; @@ -11,6 +13,8 @@ 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"; @@ -42,6 +46,17 @@ export interface T3StoredCredentials { }>; /** The gh account Settings picks for T3's personal credential. */ readonly account: Option.Option; + /** + * Each provider instance's environment as `mergeProviderInstanceEnvironment` builds it over the + * server's, by the names it sets to something (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; @@ -85,6 +100,8 @@ const decodeSession = Schema.decodeUnknownOption(Schema.fromJsonString(GitHubApp /** 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 @@ -109,6 +126,26 @@ const readStored = Effect.fn("T3GitHubCredential.read")(function* ( 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, + names: Object.keys(env).filter((name) => (env[name]?.trim() ?? "") !== ""), + })), + ), + ); + 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) => ({ @@ -118,12 +155,20 @@ const readStored = Effect.fn("T3GitHubCredential.read")(function* ( 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* 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 e39d266694ef7..263546158b0d8 100644 --- a/apps/server/src/kin/approvals/layer.ts +++ b/apps/server/src/kin/approvals/layer.ts @@ -76,6 +76,7 @@ const handlers = Layer.effect( // 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 nothing else runs either. +const UNCONFIGURED = "the GitHub App and scope aren't configured"; const unconfigured = Layer.mergeAll( Layer.effect( ActionHandlers.KinActionHandlers, @@ -90,6 +91,7 @@ const unconfigured = Layer.mergeAll( return yield* Effect.succeed({ passed: false, failures: ["identity_unknown", "merge_token_missing"], + reasons: [UNCONFIGURED], warnings: [], envTokenNames: [], t3Login: null, @@ -100,6 +102,7 @@ const unconfigured = Layer.mergeAll( requirePassing: Effect.fn("CredentialHygiene.unconfiguredRequirePassing")(function* () { return yield* new CredentialHygiene.CredentialHygieneFailedError({ failures: ["identity_unknown", "merge_token_missing"], + reasons: [UNCONFIGURED], }); }), }), diff --git a/apps/server/src/kin/chat/executionNotices.ts b/apps/server/src/kin/chat/executionNotices.ts index 21a66ee912d30..2224a43817dbf 100644 --- a/apps/server/src/kin/chat/executionNotices.ts +++ b/apps/server/src/kin/chat/executionNotices.ts @@ -50,7 +50,7 @@ const FAILURE_TEXT: Record report.envTokenNames.length > 0 ? `${report.envTokenNames.join(", ")} ${report.envTokenNames.length === 1 ? "is" : "are"} set in the server's environment` - : "T3 takes its GitHub token from 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", @@ -65,10 +65,11 @@ export const credentialCheckBody = (orgId: KinOrgId, report: CredentialHygieneRe ? `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 reasons = report.failures.map((failure) => FAILURE_TEXT[failure](report)); + 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: ${reasons.join("; ")}.`, + `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"); diff --git a/apps/server/src/kin/github/OwnerMergeClient.test.ts b/apps/server/src/kin/github/OwnerMergeClient.test.ts index 08051cbe9f342..23731119896e7 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 Layer from "effect/Layer"; @@ -80,15 +80,18 @@ const withHygiene = (effect: Effect.Effect) => CredentialHygiene.layer.pipe( Layer.provide( Layer.succeed(CredentialHygiene.T3GitHubIdentity, { - identities: Effect.succeed({ - t3: [], - 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/mcp/layer.test.ts b/apps/server/src/kin/mcp/layer.test.ts index 7b2a4d07b9af9..00e9a6ee6b022 100644 --- a/apps/server/src/kin/mcp/layer.test.ts +++ b/apps/server/src/kin/mcp/layer.test.ts @@ -54,6 +54,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"; @@ -98,6 +99,7 @@ const toolHandlerServices = Layer.mergeAll( Layer.mock(ProjectCloneTracker)({}), Layer.mock(ProjectService)({}), Layer.mock(ProjectSetupScriptRunner)({}), + Layer.mock(ProjectStoreV2)({}), Layer.mock(ProviderAdapterRegistryV2)({}), Layer.mock(ProviderInstanceRegistry)({}), Layer.mock(ProviderRegistry)({}), diff --git a/docs/kin/specs/approvals.md b/docs/kin/specs/approvals.md index 67423f6ff241f..c70f4e955d53c 100644 --- a/docs/kin/specs/approvals.md +++ b/docs/kin/specs/approvals.md @@ -519,37 +519,53 @@ 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: every credential T3 holds (each named by `GET /user`), every account `gh` holds for github.com, active or not, the login behind the github.com git credential helper, 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 and environment to be minimal and known (an allowlist), and fails closed on anything outside it. If any login found isn't `KIN_GITHUB_BOT_LOGIN`, 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. +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`), 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`, 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`: one of the environment variables listed below is set, or T3's credential came from the environment. The report names the variables, never their values. -- `owner_identity`: a login found equals the merge token's login or `KIN_GITHUB_OWNER_LOGIN`, case-insensitively. +- `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`. -- `identity_unknown`: the identity probe failed, the host is outside the allowlist, or an SSH answer was neither `Hi !` nor `Permission denied (publickey)`. The probe's error carries a fixed reason naming the source and the offending directive or key, never its value; a git subsection, which can hold a URL with a token, is shown as `*`. +- `identity_unknown`: the identity probe failed, the host is outside the allowlist, or an SSH answer was neither `Hi !` nor `Permission denied (publickey)`. - `merge_token_missing` or `merge_token_expired`: no owner login to compare against, and nothing to merge with. +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. + 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 identity probe is the `T3GitHubIdentity` port; `GitHubIdentityProbe.ts` is its live layer, and `HostConfigAllowlist.ts` holds the allowlists. The port's `identities` has a required field per source (`t3`, `ghAccounts`, `gitCredential`, `ssh`), so the live layer must report all four, and any source it can't read fails the probe. `register_approver_key` passes the same check, so a second device can't be enrolled on a host whose check fails, or without GitHub configured. 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: +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. `register_approver_key` passes the same check, so a second device can't be enrolled on a host whose check fails, or without GitHub configured. 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 settings afterwards (`T3GitHubCredential.ts`, once); 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. +- `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); 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 sets, merged over the server's 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`: one `git config --list --show-origin -z`, checked against the git allowlist below, then the login 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. -- `ssh`: `~/.ssh/config` and `/etc/ssh/ssh_config`, with every file they `Include` (32 files and 64 KiB each at most), checked against the SSH allowlist below. Every `IdentityFile` under any `Host` in either 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 `ssh -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 the 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`. +- `gitCredential`: one `git config --list --show-origin -z`, checked against the git allowlist below, then the login 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, 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. Orgs without a saved merge token haven't set GitHub up and get no notice. +`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. 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.** None of `GH_TOKEN`, `GITHUB_TOKEN`, `GH_ENTERPRISE_TOKEN`, `GITHUB_ENTERPRISE_TOKEN`, `SSH_AUTH_SOCK`, `GIT_ASKPASS`, `SSH_ASKPASS`, `GIT_SSH`, `GIT_SSH_COMMAND`, `GIT_CONFIG_PARAMETERS` or `GIT_CONFIG_COUNT` is set (macOS launchd sets `SSH_AUTH_SOCK` for every login session, so unset it in the service definition). `$HOME` is unset or the passwd entry's home. -- **`~/.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`. 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. -- **`/etc/ssh/ssh_config`** 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`), `init.`, `color.`, `alias.`, `pull.`, `push.`, `fetch.`, `merge.`, `diff.` and `filter.lfs.`, plus `credential.helper` and `credential.https://github.com.helper`, with one helper in effect, holding only the bot's credential. Anything else fails, `include.*`, `includeIf.*`, `http.*`, `url.*`, 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. +- **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. 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. +- **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.*`, `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`, `extensions.worktreeConfig`, `submodule..url` (checked the same way) and `active`, and `lfs.repositoryFormatVersion`. Anything else fails, `credential.*`, `http.*`, `url.*`, `include*`, `user.*` 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. -The probe is defence in depth, not the boundary. It sees only the sources above, as the server's OS user is configured, and an agent running as that same user can always find more: an SSH agent socket on disk it points `SSH_AUTH_SOCK` at, a key outside these paths, a token in another tool's config, the merge token and the App's private key in the keychain (open question 4). The real boundary is keeping the owner's credentials off the Kin host's OS user entirely, and in time a separate executor OS user. +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. ## Threat model From c4b1134b523d9adbac761e27b4d179336d08ef98 Mon Sep 17 00:00:00 2001 From: Surajsm60720 <1ds22is166@dsce.edu.in> Date: Mon, 9 Nov 2026 12:07:58 +0530 Subject: [PATCH 5/7] test(kin): kill the probe's surviving PATH and owner-fill mutants The mutation run left two survivors: a non-executable ssh earlier on PATH, and a helper answering the owner's username without a password. Each now has a case that fails without the check. Co-Authored-By: Claude Opus 5.5 --- .../kin/approvals/GitHubIdentityProbe.test.ts | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/apps/server/src/kin/approvals/GitHubIdentityProbe.test.ts b/apps/server/src/kin/approvals/GitHubIdentityProbe.test.ts index 3d3f41a825103..9c64e263497bc 100644 --- a/apps/server/src/kin/approvals/GitHubIdentityProbe.test.ts +++ b/apps/server/src/kin/approvals/GitHubIdentityProbe.test.ts @@ -1162,6 +1162,11 @@ describe("GitHubIdentityProbe", () => { { 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. ...( [ @@ -1291,6 +1296,24 @@ describe("GitHubIdentityProbe", () => { ), ); + 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* () { From a4562bea2a9b45b6fa2d012a448b8a74c002a2d6 Mon Sep 17 00:00:00 2001 From: Surajsm60720 <1ds22is166@dsce.edu.in> Date: Mon, 9 Nov 2026 12:13:22 +0530 Subject: [PATCH 6/7] fix(kin): a provider's PATH fails the check; projects may set their own identity A provider instance that changes PATH would run another ssh, git or gh than the ones the probe asked, so it now fails as env_token. Provider environments are named by what they add to or change from the server's. A project's own user.*, extensions.objectFormat and extensions.refStorage pick no credential and are allowed. Co-Authored-By: Claude Opus 5.5 --- .../src/kin/approvals/CredentialHygiene.test.ts | 5 +++-- apps/server/src/kin/approvals/CredentialHygiene.ts | 10 ++++++++-- .../src/kin/approvals/GitHubIdentityProbe.test.ts | 7 ++++--- apps/server/src/kin/approvals/HostConfigAllowlist.ts | 9 ++++++++- .../src/kin/approvals/T3GitHubCredential.test.ts | 7 ++++--- apps/server/src/kin/approvals/T3GitHubCredential.ts | 11 +++++++---- docs/kin/specs/approvals.md | 6 +++--- 7 files changed, 37 insertions(+), 18 deletions(-) diff --git a/apps/server/src/kin/approvals/CredentialHygiene.test.ts b/apps/server/src/kin/approvals/CredentialHygiene.test.ts index 2401b93be6947..11c1e17c960c4 100644 --- a/apps/server/src/kin/approvals/CredentialHygiene.test.ts +++ b/apps/server/src/kin/approvals/CredentialHygiene.test.ts @@ -267,7 +267,7 @@ describe("CredentialHygiene", () => { ["the identity unreachable", { identity: "unreachable" }, ["identity_unknown"]], [ "a provider instance's environment with a token", - { identity: { ...bot, providers: [{ instance: "codex", names: ["PATH", "GH_TOKEN"] }] } }, + { identity: { ...bot, providers: [{ instance: "codex", names: ["GH_TOKEN"] }] } }, ["env_token"], ], [ @@ -330,7 +330,7 @@ describe("CredentialHygiene", () => { gitFor: ["Nishchay"], providers: [ // The server's own GH_TOKEN is named once, under the server. - { instance: "codex", names: ["GH_TOKEN", "SSH_AUTH_SOCK", "PATH"] }, + { instance: "codex", names: ["GH_TOKEN", "SSH_AUTH_SOCK"] }, { instance: "claude", names: ["PATH"] }, ], }, @@ -342,6 +342,7 @@ describe("CredentialHygiene", () => { "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`, ]); 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. diff --git a/apps/server/src/kin/approvals/CredentialHygiene.ts b/apps/server/src/kin/approvals/CredentialHygiene.ts index fd58b7cddf068..a6abb6b3feeb9 100644 --- a/apps/server/src/kin/approvals/CredentialHygiene.ts +++ b/apps/server/src/kin/approvals/CredentialHygiene.ts @@ -104,8 +104,8 @@ export interface T3GitHubIdentities { */ readonly ssh: ReadonlyArray; /** - * The variables each provider instance's environment sets, by name: agents and the terminal - * inherit these on top of the server's. + * 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; @@ -279,6 +279,12 @@ const make = Effect.gen(function* () { const shown = /^[A-Za-z0-9_-]{1,64}$/.test(instance) ? `"${instance}"` : "an instance"; reasons.push(`provider instance ${shown}'s environment sets ${name}`); } + // Agents would then run another ssh, git or gh than the ones this probe asked. + if (names.includes("PATH")) { + failures.add("env_token"); + const shown = /^[A-Za-z0-9_-]{1,64}$/.test(instance) ? `"${instance}"` : "an instance"; + reasons.push(`provider instance ${shown}'s environment changes PATH`); + } } // The probe as is always runs, so no answer means the live layer skipped SSH. if (ssh.length === 0) { diff --git a/apps/server/src/kin/approvals/GitHubIdentityProbe.test.ts b/apps/server/src/kin/approvals/GitHubIdentityProbe.test.ts index 9c64e263497bc..561f6470e8a2d 100644 --- a/apps/server/src/kin/approvals/GitHubIdentityProbe.test.ts +++ b/apps/server/src/kin/approvals/GitHubIdentityProbe.test.ts @@ -480,12 +480,14 @@ describe("GitHubIdentityProbe", () => { t3: stored({ app: session(APP_TOKEN), projectRoots: ["/repo", "/plain", "/gone"], - providerEnvironments: [{ instance: "codex", names: ["PATH", "CODEX_HOME"] }], + 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"], ), @@ -510,7 +512,7 @@ describe("GitHubIdentityProbe", () => { gitCredential: Option.some("kin-bot"), gitCredentialFor: [], ssh: [sshHi("kin-bot"), SSH_DENIED], - providerEnvironments: [{ instance: "codex", names: ["PATH", "CODEX_HOME"] }], + providerEnvironments: [{ instance: "codex", names: ["CODEX_HOME"] }], }); assert.deepInclude(yield* check, { passed: true, failures: [], reasons: [] }); @@ -1204,7 +1206,6 @@ describe("GitHubIdentityProbe", () => { ["remote.origin.vcs", "owner", "remote.*.vcs"], ["submodule.lib.url", "https://ghp_secret@github.com/kin/lib", "submodule.*.url"], ["submodule.lib.update", "!owner", "submodule.*.update"], - ["user.email", "owner@example.com", "user.email"], ["gc.auto", "0", "gc.auto"], ] as const ).map( diff --git a/apps/server/src/kin/approvals/HostConfigAllowlist.ts b/apps/server/src/kin/approvals/HostConfigAllowlist.ts index 2aafcb8ab2d99..7937de56a484e 100644 --- a/apps/server/src/kin/approvals/HostConfigAllowlist.ts +++ b/apps/server/src/kin/approvals/HostConfigAllowlist.ts @@ -345,7 +345,14 @@ export const checkRepoGitConfig = ( return refused; } else if (section === "submodule" && subsection) { if (name === "url" ? urlCarriesCredential(value) : name !== "active") return refused; - } else if (section !== "branch" && key !== "lfs.repositoryformatversion") { + } 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; } } diff --git a/apps/server/src/kin/approvals/T3GitHubCredential.test.ts b/apps/server/src/kin/approvals/T3GitHubCredential.test.ts index 7ae14d8ad487b..4e287557206da 100644 --- a/apps/server/src/kin/approvals/T3GitHubCredential.test.ts +++ b/apps/server/src/kin/approvals/T3GitHubCredential.test.ts @@ -122,9 +122,10 @@ describe("T3GitHubCredential", () => { ).pipe(Effect.provideService(HostProcess.Environment, { PATH: "/bin", HOME_TOOL: "x" })); const stored = yield* slot.read; assert.deepStrictEqual(stored.providerEnvironments, [ - { instance: "codex", names: ["PATH", "GITHUB_PERSONAL_ACCESS_TOKEN", "SSH_AUTH_SOCK"] }, - // Disabled instances count: the terminal opens any instance's environment. - { instance: "claude", names: ["PATH", "HOME_TOOL"] }, + { 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"); diff --git a/apps/server/src/kin/approvals/T3GitHubCredential.ts b/apps/server/src/kin/approvals/T3GitHubCredential.ts index 0fc20ff2b964f..530dc7da3c688 100644 --- a/apps/server/src/kin/approvals/T3GitHubCredential.ts +++ b/apps/server/src/kin/approvals/T3GitHubCredential.ts @@ -47,9 +47,9 @@ export interface T3StoredCredentials { /** The gh account Settings picks for T3's personal credential. */ readonly account: Option.Option; /** - * Each provider instance's environment as `mergeProviderInstanceEnvironment` builds it over the - * server's, by the names it sets to something (a secret reference counts as set). Agents and the - * terminal, which opens any instance's environment, enabled or not, inherit these. + * 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; @@ -138,7 +138,10 @@ const readStored = Effect.fn("T3GitHubCredential.read")(function* ( ).pipe( Effect.map((env) => ({ instance, - names: Object.keys(env).filter((name) => (env[name]?.trim() ?? "") !== ""), + // 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], + ), })), ), ); diff --git a/docs/kin/specs/approvals.md b/docs/kin/specs/approvals.md index c70f4e955d53c..9b7c0dfb4e7d4 100644 --- a/docs/kin/specs/approvals.md +++ b/docs/kin/specs/approvals.md @@ -538,7 +538,7 @@ A merge token in its last 7 days passes with the `merge_token_expiring` warning, 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. `register_approver_key` passes the same check, so a second device can't be enrolled on a host whose check fails, or without GitHub configured. 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); 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 sets, merged over the server's 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. +- `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`: one `git config --list --show-origin -z`, checked against the git allowlist below, then the login 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. @@ -548,12 +548,12 @@ The identity probe is the `T3GitHubIdentity` port; `GitHubIdentityProbe.ts` is i 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. 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. +- **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`, since agents would then run another ssh, git or gh than the ones the probe asked. 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. - **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.*`, `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`, `extensions.worktreeConfig`, `submodule..url` (checked the same way) and `active`, and `lfs.repositoryFormatVersion`. Anything else fails, `credential.*`, `http.*`, `url.*`, `include*`, `user.*` and every other `remote.*` or `submodule.*` key included. +- **Each project's own config** (`--local`, and `--worktree` when on) holds only `core.*` (but not those five), `branch.*`, `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`, `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. From 5d21e63f10db4e5f31f9087bf6098db59ca55a3e Mon Sep 17 00:00:00 2001 From: Surajsm60720 <1ds22is166@dsce.edu.in> Date: Mon, 9 Nov 2026 20:05:11 +0530 Subject: [PATCH 7/7] fix(kin): close round-4 credential check gaps - Fail identity_unknown when ~/.netrc or ~/_netrc exists under the passwd home: git's HTTPS transport sends it through curl, unseen by the probe. - branch.*.remote, branch.*.pushremote and remote.pushdefault accept only a remote name in a project's own config, since git takes a URL there too. - A provider instance may not change HOME, XDG_*, BASH_ENV, ENV or ZDOTDIR, besides PATH. - Token-backed identities (T3, gh auth token, the git helper) must name KIN_GITHUB_BOT_ID as well as the bot's login; the check fails closed while it's unset. SSH greetings stay login-only. - A restart while failing no longer repeats the notice and audit entry. - T3's credential slot stays out of kin.services; KinServer carries only the layer that installs T3's own reader. Co-Authored-By: Claude Opus 5.5 --- apps/server/src/kin/KinServer.ts | 13 ++- .../src/kin/approvals/ActionExecutor.test.ts | 11 ++- .../src/kin/approvals/ActionExecutor.ts | 26 +++-- .../kin/approvals/CredentialHygiene.test.ts | 72 +++++++++++--- .../src/kin/approvals/CredentialHygiene.ts | 55 ++++++++--- .../kin/approvals/GitHubIdentityProbe.test.ts | 95 +++++++++++++++++-- .../src/kin/approvals/GitHubIdentityProbe.ts | 35 +++++-- .../src/kin/approvals/HostConfigAllowlist.ts | 30 +++++- apps/server/src/kin/github/KinGitHubScope.ts | 9 ++ apps/server/src/kin/github/testFakes.ts | 6 ++ apps/server/src/kin/layer.ts | 14 ++- apps/server/src/kin/mcp/layer.test.ts | 7 ++ docs/kin/specs/approvals.md | 21 ++-- 13 files changed, 321 insertions(+), 73 deletions(-) diff --git a/apps/server/src/kin/KinServer.ts b/apps/server/src/kin/KinServer.ts index 44a994352a9e4..288ae9717b6aa 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"; @@ -9,7 +11,6 @@ import { KinBudget } from "./lead/KinBudget.ts"; import { KinEventQueue } from "./lead/KinEventQueue.ts"; import { ApproverKeys } from "./approvals/ApproverKeys.ts"; import { GrantVerifier } from "./approvals/GrantVerifier.ts"; -import type { T3GitHubCredential } from "./approvals/T3GitHubCredential.ts"; import { KinStop } from "./lead/KinStop.ts"; import type { KinShadowSummary } from "./lead/KinLevels.ts"; import type { KinLevelGate } from "./lead/LevelGate.ts"; @@ -34,7 +35,6 @@ export type Services = | KinGithubWebhookLock | ApproverKeys | GrantVerifier - | T3GitHubCredential | KinPushDevices | KinPushPresence | KinPushSender; @@ -43,6 +43,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 caf93eadea274..03c1b7bd0de5b 100644 --- a/apps/server/src/kin/approvals/ActionExecutor.test.ts +++ b/apps/server/src/kin/approvals/ActionExecutor.test.ts @@ -34,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, @@ -311,7 +312,7 @@ function harness( checks.failing ? Effect.fail(new CredentialHygiene.T3GitHubIdentityError({ cause: "probe failed" })) : Effect.succeed({ - t3: [{ login: "kin-bot", source: "app" }], + t3: [{ login: "kin-bot", id: BOT_ID, source: "app" }], ghAccounts: ["kin-bot"], gitCredential: Option.none(), gitCredentialFor: [], @@ -1314,7 +1315,7 @@ describe("KinActionExecutor", () => { }, ); - it.effect("tells only orgs with a merge token, and says it passes again after a restart", () => { + 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; @@ -1326,9 +1327,13 @@ describe("KinActionExecutor", () => { 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); - const [failure] = (yield* auditEntries).filter( + // 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; diff --git a/apps/server/src/kin/approvals/ActionExecutor.ts b/apps/server/src/kin/approvals/ActionExecutor.ts index c9516439ffaa4..e1e4bdd44f05d 100644 --- a/apps/server/src/kin/approvals/ActionExecutor.ts +++ b/apps/server/src/kin/approvals/ActionExecutor.ts @@ -375,13 +375,20 @@ export const make = Effect.gen(function* () { 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: an execution only starts on a pass. + // 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 }>` - SELECT seq, event FROM kin_audit + 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.seq : undefined)), + Effect.map(([row]) => (row?.event === "credential_check_failed" ? row : undefined)), Effect.mapError(toPersistenceSqlError("KinActionExecutor.checkCredentials")), ); return yield* Effect.forEach(orgs, (row) => @@ -389,7 +396,14 @@ export const make = Effect.gen(function* () { const orgId = KinOrgId.make(row.orgId); const report = yield* hygiene.check(orgId); const result = [...report.failures, ...report.warnings, ...report.reasons].join(","); - const previous = reportedChecks.get(orgId); + 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")) { @@ -404,7 +418,7 @@ export const make = Effect.gen(function* () { 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); + const failedAt = (yield* lastCredentialEvent(orgId))?.seq; if (previous !== undefined || failedAt !== undefined) { yield* notices.credentialCheck({ orgId, diff --git a/apps/server/src/kin/approvals/CredentialHygiene.test.ts b/apps/server/src/kin/approvals/CredentialHygiene.test.ts index 11c1e17c960c4..5d94610b6a56d 100644 --- a/apps/server/src/kin/approvals/CredentialHygiene.test.ts +++ b/apps/server/src/kin/approvals/CredentialHygiene.test.ts @@ -9,7 +9,7 @@ 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; @@ -21,9 +21,15 @@ const SSH_DENIED = "git@github.com: Permission denied (publickey)."; type Identity = | { - readonly t3?: ReadonlyArray<{ 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<{ @@ -37,6 +43,8 @@ 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, @@ -68,18 +76,26 @@ const setup = (input: { // Takes a second, so a check that starts meanwhile shares it. return Effect.sleep("1 second"); }).pipe( - Effect.andThen(() => - state.identity === "unreachable" + Effect.andThen(() => { + const found = state.identity; + return found === "unreachable" ? Effect.fail(new CredentialHygiene.T3GitHubIdentityError({ cause: "offline" })) : Effect.succeed({ - t3: state.identity.t3 ?? [], - ghAccounts: state.identity.gh ?? [], - gitCredential: state.identity.git ?? Option.none(), - gitCredentialFor: state.identity.gitFor ?? [], - ssh: state.identity.ssh ?? [SSH_DENIED], - providerEnvironments: state.identity.providers ?? [], - }), - ), + 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 ?? [], + }); + }), ), }), ); @@ -89,11 +105,13 @@ const setup = (input: { Layer.provide(keychain.layer), Layer.provide(fakeGitHub(() => ({ status: 500 })).layer), Layer.provide(Layer.succeed(HostProcess.Environment, input.env ?? {})), - Layer.provide(scopeLayer()), + 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: [{ login: "kin-bot", source: "gh" }], gh: ["kin-bot"], @@ -227,6 +245,18 @@ describe("CredentialHygiene", () => { { 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", @@ -265,6 +295,15 @@ describe("CredentialHygiene", () => { ["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"] }] } }, @@ -331,18 +370,21 @@ describe("CredentialHygiene", () => { 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"] }, + { 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"]); + 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. diff --git a/apps/server/src/kin/approvals/CredentialHygiene.ts b/apps/server/src/kin/approvals/CredentialHygiene.ts index a6abb6b3feeb9..ca2bb7682487c 100644 --- a/apps/server/src/kin/approvals/CredentialHygiene.ts +++ b/apps/server/src/kin/approvals/CredentialHygiene.ts @@ -14,7 +14,7 @@ import { CredentialHygieneFailure, type CredentialHygieneReport, } from "./CredentialHygieneReport.ts"; -import { forbiddenEnvNames } from "./HostConfigAllowlist.ts"; +import { forbiddenEnvNames, refusedProviderEnvChanges } from "./HostConfigAllowlist.ts"; /** * The probe the live layer (`GitHubIdentityProbe.ts`) runs for `T3GitHubIdentities.ssh`, with @@ -82,18 +82,23 @@ 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; /** @@ -250,6 +255,17 @@ const make = Effect.gen(function* () { ), ]; + // 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* probe(askAbout).pipe(Effect.result); let t3Login: string | null = null; const found: Array = []; @@ -260,30 +276,31 @@ const make = Effect.gen(function* () { const { t3, ghAccounts, gitCredential, gitCredentialFor, ssh, providerEnvironments } = probed.success; t3Login = t3[0]?.login ?? null; - for (const { login, source } of t3) { + 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"); - const shown = /^[A-Za-z0-9_-]{1,64}$/.test(instance) ? `"${instance}"` : "an instance"; reasons.push(`provider instance ${shown}'s environment sets ${name}`); } - // Agents would then run another ssh, git or gh than the ones this probe asked. - if (names.includes("PATH")) { + // Agents would then run another program, config or startup file than the probe read. + for (const name of refusedProviderEnvChanges(names)) { failures.add("env_token"); - const shown = /^[A-Za-z0-9_-]{1,64}$/.test(instance) ? `"${instance}"` : "an instance"; - reasons.push(`provider instance ${shown}'s environment changes PATH`); + reasons.push(`provider instance ${shown}'s environment changes ${name}`); } } // The probe as is always runs, so no answer means the live layer skipped SSH. @@ -312,6 +329,14 @@ const make = Effect.gen(function* () { 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, diff --git a/apps/server/src/kin/approvals/GitHubIdentityProbe.test.ts b/apps/server/src/kin/approvals/GitHubIdentityProbe.test.ts index 561f6470e8a2d..7ae9b50879406 100644 --- a/apps/server/src/kin/approvals/GitHubIdentityProbe.test.ts +++ b/apps/server/src/kin/approvals/GitHubIdentityProbe.test.ts @@ -26,7 +26,7 @@ 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 { fakeGitHub, memoryKeychain, scopeLayer } from "../github/testFakes.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"; @@ -200,8 +200,13 @@ interface Machine { env: Record; /** Whether the passwd entry names the home directory. */ passwdHome: boolean; - /** The login `GET /user` names for each token; a number is the status, `hang` never answers. */ - users: Record; + /** + * 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; @@ -382,12 +387,17 @@ const probe = (machine: Machine) => 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 } : { message: "Bad credentials" }; + 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 === "string" ? 200 : (user ?? 401), + status: typeof user === "number" ? user : user === undefined ? 401 : 200, headers: { "content-type": "application/json" }, }), ), @@ -433,7 +443,7 @@ const probe = (machine: Machine) => Layer.provide(keychain.layer), Layer.provide(fakeGitHub(() => ({ status: 500 })).layer), Layer.provide(Layer.succeed(HostProcess.Environment, {})), - Layer.provide(scopeLayer()), + Layer.provide(scopeLayer(machine.botId === undefined ? {} : { botId: machine.botId })), ), ); const check = Context.get(hygiene, CredentialHygiene.CredentialHygiene).check(ORG); @@ -505,11 +515,11 @@ describe("GitHubIdentityProbe", () => { const probed = [...runs]; assert.deepStrictEqual(found, { t3: [ - { login: "kin-bot", source: "app" }, - { login: "kin-bot", source: "gh" }, + { login: "kin-bot", id: BOT_ID, source: "app" }, + { login: "kin-bot", id: BOT_ID, source: "gh" }, ], ghAccounts: ["kin-bot"], - gitCredential: Option.some("kin-bot"), + gitCredential: Option.some({ login: "kin-bot", id: BOT_ID }), gitCredentialFor: [], ssh: [sshHi("kin-bot"), SSH_DENIED], providerEnvironments: [{ instance: "codex", names: ["CODEX_HOME"] }], @@ -645,7 +655,11 @@ describe("GitHubIdentityProbe", () => { 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", source: "app" }); + assert.deepInclude((yield* Fiber.join(early)).t3, { + login: "kin-bot", + id: BOT_ID, + source: "app", + }); }), ), ); @@ -663,6 +677,7 @@ describe("GitHubIdentityProbe", () => { ["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"], @@ -831,6 +846,50 @@ describe("GitHubIdentityProbe", () => { ), ); + 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"], @@ -935,6 +994,15 @@ describe("GitHubIdentityProbe", () => { { 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" } }, @@ -1206,6 +1274,13 @@ describe("GitHubIdentityProbe", () => { ["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( diff --git a/apps/server/src/kin/approvals/GitHubIdentityProbe.ts b/apps/server/src/kin/approvals/GitHubIdentityProbe.ts index 9da2996d7af11..06d21c8fd0f0a 100644 --- a/apps/server/src/kin/approvals/GitHubIdentityProbe.ts +++ b/apps/server/src/kin/approvals/GitHubIdentityProbe.ts @@ -74,12 +74,14 @@ const isMissingCommand = (error: { readonly _tag: string; readonly cause?: unkno const isNotFound = (error: PlatformError.PlatformError) => error.reason._tag === "NotFound"; -const GitHubUser = Schema.Struct({ login: Schema.String }); +// 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); /** @@ -153,9 +155,9 @@ export const make = Effect.gen(function* () { Effect.mapError((error) => error._tag === "T3GitHubIdentityError" ? error : unreadable(source, error.message), ), - Effect.map((response) => response.body.login.trim()), + Effect.map((response) => ({ login: response.body.login.trim(), id: response.body.id })), Effect.filterOrFail( - (login) => login !== "", + (user) => user.login !== "", () => unreadable(source, "GET /user named no login"), ), ); @@ -225,7 +227,7 @@ export const make = Effect.gen(function* () { if (Option.isSome(token)) tokens.push({ source: "gh", token: token.value }); } return yield* Effect.forEach(tokens, ({ source, token }) => - loginOf("t3", token).pipe(Effect.map((login) => ({ login, source }))), + loginOf("t3", token).pipe(Effect.map((user) => ({ ...user, source }))), ); }); @@ -301,6 +303,26 @@ export const make = Effect.gen(function* () { }), ); + /** + * 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", @@ -331,14 +353,15 @@ export const make = Effect.gen(function* () { const gitCredential = Effect.fn("KinGitHubIdentity.gitCredential")(function* ( ownerLogins: ReadonlyArray, ) { + yield* netrcAbsent; const helpers = yield* gitHelpers; - if (helpers === null) return { login: Option.none(), ownerHeld: [] }; + 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(); + 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) { diff --git a/apps/server/src/kin/approvals/HostConfigAllowlist.ts b/apps/server/src/kin/approvals/HostConfigAllowlist.ts index 7937de56a484e..8d3c78d01e647 100644 --- a/apps/server/src/kin/approvals/HostConfigAllowlist.ts +++ b/apps/server/src/kin/approvals/HostConfigAllowlist.ts @@ -313,6 +313,9 @@ const REMOTE_PLAIN = new Set([ "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 @@ -338,11 +341,13 @@ export const checkRepoGitConfig = ( 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") { - // `remote.pushDefault` names a remote and has no subsection. const url = name === "url" || name === "pushurl"; - if (subsection && (url ? urlCarriesCredential(value) : !REMOTE_PLAIN.has(name))) - return refused; + 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 ( @@ -371,6 +376,9 @@ const ENV_EXCEPTIONS = new Set([ "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) @@ -380,5 +388,19 @@ export const forbiddenEnvNames = (env: Readonly (/^[A-Za-z0-9_]{1,64}$/.test(name) ? name : "a variable with an unusual name")) + .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/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/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 9237abc8f71cd..921573dc311e9 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"; @@ -27,6 +28,7 @@ const enabled = Layer.effect( KinServer, Effect.gen(function* () { const services = yield* Effect.context(); + const slot = yield* T3GitHubCredential.T3GitHubCredential; const hub = yield* KinChatHub; const updaterServices = yield* Effect.context< KinChatRepository | TaskCardIndex | KinChatHub | SqlClient.SqlClient @@ -35,7 +37,11 @@ const enabled = Layer.effect( 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), }; }), @@ -58,6 +64,7 @@ const enabled = Layer.effect( ); const disabled = Layer.succeed(KinServer, { taskCardUpdater: Layer.empty, + t3GitHubCredential: Layer.empty, services: Option.none(), subscribeForViewer: Option.none(), }); @@ -73,6 +80,7 @@ const runtime = Layer.unwrap( config.reportFailure(cause).pipe( Effect.as({ taskCardUpdater: Layer.empty, + t3GitHubCredential: Layer.empty, services: Option.none(), subscribeForViewer: Option.none(), }), @@ -90,6 +98,7 @@ const runtime = Layer.unwrap( Effect.logError("Kin disabled: invalid configuration.", Cause.pretty(cause)).pipe( Effect.as({ taskCardUpdater: Layer.empty, + t3GitHubCredential: Layer.empty, services: Option.none(), subscribeForViewer: Option.none(), }), @@ -125,8 +134,7 @@ const t3GitHubCredential = Layer.unwrap( Effect.gen(function* () { const kin = yield* KinServer; return Option.isSome(kin.services) - ? T3GitHubCredential.installLayer.pipe( - Layer.provide(Layer.succeedContext(kin.services.value)), + ? kin.t3GitHubCredential.pipe( Layer.catchCause((cause) => Layer.effectDiscard( Effect.logWarning( diff --git a/apps/server/src/kin/mcp/layer.test.ts b/apps/server/src/kin/mcp/layer.test.ts index 8be25615dcf72..c3e7a555f970a 100644 --- a/apps/server/src/kin/mcp/layer.test.ts +++ b/apps/server/src/kin/mcp/layer.test.ts @@ -80,6 +80,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({ @@ -270,6 +271,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), + ), + ); const [person] = yield* identity.resolve({ sessionId: AuthSessionId.make("owner"), subject: "browser", diff --git a/docs/kin/specs/approvals.md b/docs/kin/specs/approvals.md index 64a4b214438cd..c479204c178ae 100644 --- a/docs/kin/specs/approvals.md +++ b/docs/kin/specs/approvals.md @@ -392,7 +392,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. @@ -527,7 +527,7 @@ 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: every credential T3 holds (each named by `GET /user`), 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`, 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. +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. @@ -535,8 +535,8 @@ What the check is for: it guards against _accidental_ owner-credential use, such - `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`. -- `identity_unknown`: the identity probe failed, the host is outside the allowlist, or an SSH answer was neither `Hi !` nor `Permission denied (publickey)`. +- `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. 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. @@ -545,25 +545,27 @@ A merge token in its last 7 days passes with the `merge_token_expiring` warning, 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. `register_approver_key` passes the same check, so a second device can't be enrolled on a host whose check fails, or without GitHub configured. 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); 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. +- `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`: one `git config --list --show-origin -z`, checked against the git allowlist below, then the login 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. +- `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. Orgs without a saved merge token haven't set GitHub up and get no notice. +`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`, since agents would then run another ssh, git or gh than the ones the probe asked. 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. +- **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.*`, `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`, `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. +- **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: @@ -574,6 +576,7 @@ Default setups that always fail, and what to change: - `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