From 5604bb9f66d4fd55f65577dbe0c652f95d6bb756 Mon Sep 17 00:00:00 2001 From: GLDRoger <130458023+GLDRoger@users.noreply.github.com> Date: Mon, 9 Nov 2026 12:34:46 +0530 Subject: [PATCH 1/2] fix(kin): memory repo credentials for the live server Memory git ignores global and system git config, and MemoryConfig.layer never set a credential helper or SSH command, so cloning a private memory repo over HTTPS failed and every wake-up failed with it. MemoryConfig.layer now reads KIN_MEMORY_CREDENTIAL_HELPER and KIN_MEMORY_SSH_COMMAND, validates them with purgePush's reference checks, and hands them to memory git only. Co-Authored-By: Claude Opus 5.5 --- apps/server/src/kin/memory/Memory.test.ts | 50 ++++++++++++++++--- apps/server/src/kin/memory/MemoryConfig.ts | 33 ++++++++++++ .../src/kin/memory/MemoryWriter.test.ts | 20 ++++++++ apps/server/src/kin/memory/purgePush.ts | 4 +- docs/kin/specs/lead.md | 4 +- docs/kin/specs/triggers.md | 2 +- ops/kin/host/README.md | 24 +++++++++ 7 files changed, 124 insertions(+), 13 deletions(-) diff --git a/apps/server/src/kin/memory/Memory.test.ts b/apps/server/src/kin/memory/Memory.test.ts index 1979af9f9c1e..4aaa345cbdf7 100644 --- a/apps/server/src/kin/memory/Memory.test.ts +++ b/apps/server/src/kin/memory/Memory.test.ts @@ -1,9 +1,11 @@ import * as NodeZlib from "node:zlib"; import * as NodeServices from "@effect/platform-node/NodeServices"; -import { expect, it } from "@effect/vitest"; +import { assert, expect, it } from "@effect/vitest"; import { KinAgentId, KinWakeupId, KinMemoryItemId, KinOrgId } from "@t3tools/contracts"; import * as DateTime from "effect/DateTime"; import * as Clock from "effect/Clock"; +import * as ConfigProvider from "effect/ConfigProvider"; +import * as Context from "effect/Context"; import * as SqlClient from "effect/sql/SqlClient"; import * as Effect from "effect/Effect"; import * as FileSystem from "effect/FileSystem"; @@ -588,19 +590,51 @@ it.effect.each([ }).pipe(Effect.provide(Layer.merge(NodeServices.layer, databaseLayer)), Effect.scoped), ); -it.effect("derives production memory paths from the isolated server state directory", () => +const helperScript = "!f() { echo password=fixture; }; f"; +const sshInjection = "ssh -i /fixture/key; cat /etc/passwd"; +it.effect.each([ + { env: {}, expected: {} }, + { + env: { + KIN_MEMORY_CREDENTIAL_HELPER: "/fixture/bin/git-credential-kin", + KIN_MEMORY_SSH_COMMAND: "ssh -i /fixture/kin-memory-key -o IdentitiesOnly=yes", + }, + expected: { + credentialHelper: "/fixture/bin/git-credential-kin", + sshCommand: "ssh -i /fixture/kin-memory-key -o IdentitiesOnly=yes", + }, + }, + { env: { KIN_MEMORY_CREDENTIAL_HELPER: helperScript }, refused: helperScript }, + { env: { KIN_MEMORY_SSH_COMMAND: sshInjection }, refused: sshInjection }, +])("derives production memory config from the server state directory and env %#", (row) => Effect.gen(function* () { const server = yield* ServerConfig.ServerConfig; - const config = yield* MemoryConfig.MemoryConfig; + const result = yield* Effect.result( + Layer.build(MemoryConfig.layer([{ orgId: orgA, repoUrl: "unused" }])).pipe( + Effect.map((context) => Context.get(context, MemoryConfig.MemoryConfig)), + Effect.provide(ConfigProvider.layer(ConfigProvider.fromUnknown(row.env))), + ), + ); + if (row.refused !== undefined) { + assert(result._tag === "Failure"); + expect(result.failure._tag).toBe("MemoryCredentialConfigError"); + expect(result.failure.message).toContain(Object.keys(row.env)[0]); + expect(result.failure.message).not.toContain(row.refused); + return; + } + assert(result._tag === "Success"); + const config = result.success; expect(config.baseDirectory).toBe(`${server.stateDir}/kin`); expect(config.repositories[0]?.path).toBe(orgA); + expect({ + credentialHelper: config.credentialHelper, + sshCommand: config.sshCommand, + }).toStrictEqual({ credentialHelper: undefined, sshCommand: undefined, ...row.expected }); }).pipe( + Effect.scoped, Effect.provide( - MemoryConfig.layer([{ orgId: orgA, repoUrl: "unused" }]).pipe( - Layer.provideMerge( - ServerConfig.ServerConfig.layerTest(process.cwd(), { prefix: "kin-memory-config-" }), - ), - Layer.provide(NodeServices.layer), + ServerConfig.ServerConfig.layerTest(process.cwd(), { prefix: "kin-memory-config-" }).pipe( + Layer.provideMerge(NodeServices.layer), ), ), ), diff --git a/apps/server/src/kin/memory/MemoryConfig.ts b/apps/server/src/kin/memory/MemoryConfig.ts index 045cee154764..c9167ccf13de 100644 --- a/apps/server/src/kin/memory/MemoryConfig.ts +++ b/apps/server/src/kin/memory/MemoryConfig.ts @@ -1,10 +1,14 @@ import type { KinOrgId } from "@t3tools/contracts"; +import * as Config from "effect/Config"; 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 Path from "effect/Path"; +import * as Schema from "effect/Schema"; import { ServerConfig } from "../../config.ts"; +import { isCredentialHelperReference, isSshCommandReference } from "./purgePush.ts"; export class MemoryConfig extends Context.Service< MemoryConfig, @@ -23,6 +27,27 @@ export class MemoryConfig extends Context.Service< } >()("t3/kin/memory/MemoryConfig") {} +export class MemoryCredentialConfigError extends Schema.TaggedError()( + "MemoryCredentialConfigError", + { variable: Schema.Literals(["KIN_MEMORY_CREDENTIAL_HELPER", "KIN_MEMORY_SSH_COMMAND"]) }, +) { + override get message(): string { + return this.variable === "KIN_MEMORY_CREDENTIAL_HELPER" + ? "KIN_MEMORY_CREDENTIAL_HELPER must be a git credential helper name or absolute path, not an inline script." + : "KIN_MEMORY_SSH_COMMAND must be a plain ssh command naming its key file, without shell syntax."; + } +} + +const credential = Effect.fn("MemoryConfig.credential")(function* ( + variable: MemoryCredentialConfigError["variable"], + valid: (value: string) => boolean, +) { + const value = yield* Config.option(Config.String(variable)); + if (Option.isSome(value) && !valid(value.value)) + return yield* new MemoryCredentialConfigError({ variable }); + return Option.getOrUndefined(value); +}); + export const layer = ( repositories: ReadonlyArray<{ readonly orgId: KinOrgId; readonly repoUrl: string }>, ) => @@ -31,8 +56,16 @@ export const layer = ( Effect.gen(function* () { const config = yield* ServerConfig; const path = yield* Path.Path; + // Only memory git reads these; a server-wide GIT_SSH_COMMAND would reach every agent's git. + const credentialHelper = yield* credential( + "KIN_MEMORY_CREDENTIAL_HELPER", + isCredentialHelperReference, + ); + const sshCommand = yield* credential("KIN_MEMORY_SSH_COMMAND", isSshCommandReference); return MemoryConfig.of({ baseDirectory: path.join(config.stateDir, "kin"), + ...(credentialHelper === undefined ? {} : { credentialHelper }), + ...(sshCommand === undefined ? {} : { sshCommand }), repositories: repositories.map((repo) => ({ ...repo, path: encodeURIComponent(repo.orgId), diff --git a/apps/server/src/kin/memory/MemoryWriter.test.ts b/apps/server/src/kin/memory/MemoryWriter.test.ts index 0b72fe0cd22d..aa2eecd4f0ca 100644 --- a/apps/server/src/kin/memory/MemoryWriter.test.ts +++ b/apps/server/src/kin/memory/MemoryWriter.test.ts @@ -182,10 +182,12 @@ it.live.each([ >) | undefined; let failBranch = false; + const memoryRuns: Array = []; const interceptedGit = Layer.succeed( Vcs.VcsProcess, Vcs.VcsProcess.of({ run: Effect.fn("WriterTest.objectSwap")(function* (input) { + if (input.operation === "Memory.gitHardened") memoryRuns.push(input); if ( attack && input.args.includes("cat-file") && @@ -221,6 +223,7 @@ it.live.each([ const config = Layer.succeed(MemoryConfig, { baseDirectory: root, credentialHelper: "fixture-server-helper", + sshCommand: "ssh -i /fixture/kin-memory-key", repositories: [{ orgId, repoUrl: remote, path: "state" }], }); const synced: Array> = []; @@ -485,6 +488,23 @@ it.live.each([ }), ); expect(helper.trim()).toBe("fixture-server-helper"); + yield* writer.stage(orgId, "wake1", { + path: "claims/credentials.md", + content: fixtureItem({ kind: "claim" }), + }); + yield* writer.commitWakeup(orgId, "wake1", "completed"); + const remoteRuns = memoryRuns.filter((run) => run.args.includes(remote)); + expect( + new Set( + remoteRuns.map((run) => + run.args.find((arg) => /^(clone|fetch|ls-remote|push)$/.test(arg)), + ), + ), + ).toStrictEqual(new Set(["clone", "fetch", "ls-remote", "push"])); + for (const run of remoteRuns) { + expect(run.args).toContain("credential.helper=fixture-server-helper"); + expect(run.env?.GIT_SSH_COMMAND).toBe("ssh -i /fixture/kin-memory-key"); + } return; } if (scenario === "review-default" || scenario === "review-null") { diff --git a/apps/server/src/kin/memory/purgePush.ts b/apps/server/src/kin/memory/purgePush.ts index 2c80689fad72..d46da9de0660 100644 --- a/apps/server/src/kin/memory/purgePush.ts +++ b/apps/server/src/kin/memory/purgePush.ts @@ -82,9 +82,9 @@ const pushFailure = (stdout: string, stderr: string): KinPurgePushError["reason" }; // A helper name or absolute path, never an inline `!` script that could carry the secret itself. -const isCredentialHelperReference = (helper: string) => +export const isCredentialHelperReference = (helper: string) => /^(?:[A-Za-z0-9][A-Za-z0-9._-]*|\/[A-Za-z0-9._/-]+)$/.test(helper); -const isSshCommandReference = (command: string) => /^[A-Za-z0-9 ._/=,:@+-]+$/.test(command); +export const isSshCommandReference = (command: string) => /^[A-Za-z0-9 ._/=,:@+-]+$/.test(command); /** `file://` carries no credential; https needs the helper and SSH the owner's command. */ const remoteKind = (url: string) => /^https:\/\//i.test(url) diff --git a/docs/kin/specs/lead.md b/docs/kin/specs/lead.md index cd1ef77b3613..c731617fab42 100644 --- a/docs/kin/specs/lead.md +++ b/docs/kin/specs/lead.md @@ -604,7 +604,7 @@ Upstream files touched, one line each: - The lead's authority is its Kin tools. Tools resolve organization and lead from the calling thread, never from input. - Trust and `said_by` are set from server-verified provenance, never by the model. - `rules.md` cannot be written by any lead or dream. -- Leads and workers run in `full-access` mode, so they can run any shell command the server's OS user can. They must not run under the owner's account: that account holds the owner's `gh` login and keychain, which DESIGN 9.5 says agents never hold. Phase 1 requires the Kin server on the Kin host to run as a dedicated `kin` user whose only GitHub credential is the GitHub App's (triggers spec, "Running on the Kin host"). Until then, keep every lead at L0. +- Leads and workers run in `full-access` mode, so they can run any shell command the server's OS user can. They must not run under the owner's account: that account holds the owner's `gh` login and keychain, which DESIGN 9.5 says agents never hold. Phase 1 requires the Kin server on the Kin host to run as a dedicated `kin` user whose only GitHub credentials are the GitHub App's and the memory repo deploy key (triggers spec, "Running on the Kin host"). Until then, keep every lead at L0. - Release gate: workers are ordinary T3 threads holding a full `/mcp` credential. The `orchestration` capability that grants `delegate_task` also grants `schedule_task`, top-level `t3_thread_launch`, and `t3_thread_send` / `t3_thread_interrupt` on threads outside the worker's subtree, so a prompt-injected worker can reach other work, and its scheduled tasks outlive the worker budget and the stop switch. Scoping a credential to "delegate_task, its own children, and PR tools" needs per-tool capabilities upstream, which do not exist. Until they do, `workerLaunchReady` in `apps/server/src/kin/lead/Workers.ts` stays `false`: `kin_delegate` and `kin_follow_up` refuse with `not_allowed` at L2 and above, and below L2 they keep recording shadow entries. Flip it only when per-tool scoping lands. - Events carry references, so webhook bodies and message text don't sit in the queue. - Stopping is open to the owner at any time without a signature. Raising a level needs a signed grant. @@ -696,7 +696,7 @@ Operators or owners can call server-only `Memory.recover(orgId)` after losing a Each wake-up must start a fresh provider process with cwd set to the newly replaced read clone; LEAD-5 creates a fresh provider session for every wake-up. -**Running on the Kin host.** Git pushes use the server's credentials. Since system/global/repository git config is excluded, supply a server-owned `MemoryConfig.credentialHelper` command for HTTPS or `MemoryConfig.sshCommand` for SSH. These are passed through command config/environment only; repository credential helpers cannot override them. Configure the Kin host's dedicated server user and credentials before enabling writes; never put credentials in memory, repository config, or lead instructions. See also “Running on the Kin host” in `triggers.md`. +**Running on the Kin host.** System, global and repository git config are excluded, so memory git never sees the host's credentials. `MemoryConfig.layer` reads `KIN_MEMORY_CREDENTIAL_HELPER` (a helper name or absolute path, for HTTPS) and `KIN_MEMORY_SSH_COMMAND` (an ssh command naming its key file, for SSH) and every memory clone, fetch and push uses them; a value that fails `purgePush.ts`'s reference checks fails Kin startup, naming the variable but not the value. They go to memory git only, as `-c credential.helper` and `GIT_SSH_COMMAND`; a server-wide `GIT_SSH_COMMAND` would reach every agent's git. Repository credential helpers cannot override them. Configure the Kin host's dedicated server user and credentials before enabling writes; never put credentials in memory, repository config, or lead instructions. See also “Running on the Kin host” in `triggers.md`. The existing CLI has no Kin repository configuration or service layer, so a recovery subcommand would require more than a 1–2 line upstream hook. The operator entry point remains the typed `Memory.recover(orgId)`, never RPC or MCP. Stop the runner first. From `apps/server`, run `node --input-type=module < /absolute/path/to/operator-recover.mjs` with this operator-owned script, substituting explicit database, repository and directory values (no default live-state path): diff --git a/docs/kin/specs/triggers.md b/docs/kin/specs/triggers.md index 080c88b2c403..e3b3ef7476f8 100644 --- a/docs/kin/specs/triggers.md +++ b/docs/kin/specs/triggers.md @@ -487,7 +487,7 @@ Kin runs on one always-on machine, the **Kin host**. The laptop and phone are cl - `git`, `gh`, `git-filter-repo` and `gitleaks` from the distribution or their release binaries. - Node 26 (the repo's `engines`) through `mise`, which the host already uses, and `vp`. -- A dedicated `kin` user (`sudo useradd -m kin`). The Kin server runs as this user, never as the owner's account. Its only GitHub credential is the GitHub App's, so agents never see the owner's `gh` login (lead spec, security notes). +- A dedicated `kin` user (`sudo useradd -m kin`). The Kin server runs as this user, never as the owner's account. Its only GitHub credentials are the GitHub App's and a memory repo deploy key kept outside `~/.ssh` and passed to memory git alone through `KIN_MEMORY_SSH_COMMAND` ([host README](../../../ops/kin/host/README.md#memory-repo-access)), so agents never see the owner's `gh` login (lead spec, security notes). - The providers' CLIs (Claude Code, Codex) installed for the `kin` user and signed in to the owner's plans. - As `kin`: clone `GLDRoger/t3code` to `~/t3code`, `git switch kin`, `cp .env.example .env` (it holds the production relay URL), then `ops/kin/host/update.sh`, which makes the first build. - `sudo ops/kin/host/setup.sh` creates the user, turns on linger, masks sleep, installs and enables the units, and prints a pass/fail checklist of everything above. It is safe to re-run, and `--dry-run` shows every action. Steps: [`ops/kin/host/README.md`](../../../ops/kin/host/README.md). diff --git a/ops/kin/host/README.md b/ops/kin/host/README.md index 12d779f9e6bc..86a68c292a53 100644 --- a/ops/kin/host/README.md +++ b/ops/kin/host/README.md @@ -39,6 +39,30 @@ export XDG_RUNTIME_DIR=/run/user/$(id -u) Owner overrides such as `T3CODE_LAN=true` go in `~/.t3/service.env`, which the unit reads and the backup copies. The unit reads it before `kin-server.env`, so it can't change `PATH`, the build or the Node version. +## Memory repo access + +Memory git ignores the system and global git config, so the host's credential helpers and `GIT_SSH_COMMAND` don't reach it. Give it a credential through `~/.t3/service.env`, which only memory git reads. Without one, cloning a private memory repo fails and every wake-up fails with it. + +**SSH (use this for now).** Add a deploy key with write access to the memory repo only, and keep the private key outside `~/.ssh`: + +```bash +install -d -m 700 ~/.config/kin +ssh-keygen -t ed25519 -N '' -C kin-memory -f ~/.config/kin/kin-memory-deploy-key +chmod 600 ~/.config/kin/kin-memory-deploy-key +``` + +Then in `~/.t3/service.env`: + +```bash +KIN_MEMORY_SSH_COMMAND="ssh -i /home/kin/.config/kin/kin-memory-deploy-key -o IdentitiesOnly=yes" +``` + +Point `KIN_MEMORY_REPOSITORIES` at the SSH URL (`git@github.com:/.git`), make sure `~/.ssh/known_hosts` holds GitHub's host key, and restart `kin-server`. The key stays out of `~/.ssh` because the approvals credential check tries every key there against GitHub, and a deploy key's greeting names a repo instead of the bot, so the check fails with `non_bot_identity` and privileged actions stop. Agents run as `kin` and can still read the file, which is why the key gets access to the memory repo and nothing else. + +**HTTPS.** Set `KIN_MEMORY_CREDENTIAL_HELPER` to a git credential helper's name or absolute path, with an `https://` repo URL. There is no GitHub App installation-token helper yet, so this route has nothing to point at until one is written. + +Both values must name a credential, never hold it: the server refuses to start Kin when the helper is an inline `!` script or the SSH command contains shell syntax, and it never logs either value. + ## Link the laptop and phone Use `connect link`, not `up --connect`. On Linux with systemd, `up` installs the upstream background service: it downloads the pinned upstream release that matches the version and writes `~/.config/systemd/user/t3.service`. That would be a second server on kin's state. From 4718e80ca65fc13e05483c92a6deaf2d39dba8fc Mon Sep 17 00:00:00 2001 From: GLDRoger <130458023+GLDRoger@users.noreply.github.com> Date: Mon, 9 Nov 2026 12:58:16 +0530 Subject: [PATCH 2/2] fix(kin): harden memory ssh and say plainly the deploy key isn't contained Memory git appends purge's ssh hardening (-F none, BatchMode, IdentitiesOnly, IdentityAgent=none). The SSH validator now requires ssh with plain arguments and refuses ProxyCommand, LocalCommand, -F, -J and host-check overrides. The host README and lead spec state that agents can read the key, require a no-bypass ruleset on the memory repo's main while leads are at L0, and gate L1 on a separate memory OS user. Co-Authored-By: Claude Opus 5.5 --- apps/server/src/kin/memory/Memory.test.ts | 10 +++++++++- apps/server/src/kin/memory/MemoryWriter.test.ts | 4 +++- apps/server/src/kin/memory/git.ts | 5 ++++- apps/server/src/kin/memory/purgePush.ts | 15 +++++++++++---- docs/kin/specs/lead.md | 2 +- ops/kin/host/README.md | 9 ++++++--- 6 files changed, 34 insertions(+), 11 deletions(-) diff --git a/apps/server/src/kin/memory/Memory.test.ts b/apps/server/src/kin/memory/Memory.test.ts index 4aaa345cbdf7..56a19d00d01f 100644 --- a/apps/server/src/kin/memory/Memory.test.ts +++ b/apps/server/src/kin/memory/Memory.test.ts @@ -605,7 +605,15 @@ it.effect.each([ }, }, { env: { KIN_MEMORY_CREDENTIAL_HELPER: helperScript }, refused: helperScript }, - { env: { KIN_MEMORY_SSH_COMMAND: sshInjection }, refused: sshInjection }, + ...[ + sshInjection, + "ssh -o ProxyCommand=/fixture/pwn.sh", + "ssh -oLocalCommand=/x -o PermitLocalCommand=yes", + "ssh -F /fixture/hostile-config", + "ssh -o StrictHostKeyChecking=no", + "sshpass -p fixture ssh", + "/fixture/any-program", + ].map((value) => ({ env: { KIN_MEMORY_SSH_COMMAND: value }, refused: value })), ])("derives production memory config from the server state directory and env %#", (row) => Effect.gen(function* () { const server = yield* ServerConfig.ServerConfig; diff --git a/apps/server/src/kin/memory/MemoryWriter.test.ts b/apps/server/src/kin/memory/MemoryWriter.test.ts index aa2eecd4f0ca..9da9751c3ea0 100644 --- a/apps/server/src/kin/memory/MemoryWriter.test.ts +++ b/apps/server/src/kin/memory/MemoryWriter.test.ts @@ -503,7 +503,9 @@ it.live.each([ ).toStrictEqual(new Set(["clone", "fetch", "ls-remote", "push"])); for (const run of remoteRuns) { expect(run.args).toContain("credential.helper=fixture-server-helper"); - expect(run.env?.GIT_SSH_COMMAND).toBe("ssh -i /fixture/kin-memory-key"); + expect(run.env?.GIT_SSH_COMMAND).toBe( + "ssh -i /fixture/kin-memory-key -F none -o BatchMode=yes -o IdentitiesOnly=yes -o IdentityAgent=none", + ); } return; } diff --git a/apps/server/src/kin/memory/git.ts b/apps/server/src/kin/memory/git.ts index 5bf977ec9052..f6ceefd43785 100644 --- a/apps/server/src/kin/memory/git.ts +++ b/apps/server/src/kin/memory/git.ts @@ -1,6 +1,7 @@ import * as Schema from "effect/Schema"; import * as Option from "effect/Option"; import { MemoryConfig } from "./MemoryConfig.ts"; +import { SSH_HARDENING } from "./purgePush.ts"; import * as Effect from "effect/Effect"; import * as FileSystem from "effect/FileSystem"; import { VcsProcess } from "../../vcs/VcsProcess.ts"; @@ -96,7 +97,9 @@ export const memoryGit = Effect.fn("Memory.gitHardened")(function* ( GIT_ASKPASS: "", GCM_INTERACTIVE: "never", ...env, - ...(config?.sshCommand ? { GIT_SSH_COMMAND: config.sshCommand } : {}), + ...(config?.sshCommand + ? { GIT_SSH_COMMAND: `${config.sshCommand} ${SSH_HARDENING}` } + : {}), }, ...(stdin === undefined ? {} : { stdin }), ...(onStdoutChunk === undefined ? {} : { onStdoutChunk }), diff --git a/apps/server/src/kin/memory/purgePush.ts b/apps/server/src/kin/memory/purgePush.ts index d46da9de0660..e8703efbef5a 100644 --- a/apps/server/src/kin/memory/purgePush.ts +++ b/apps/server/src/kin/memory/purgePush.ts @@ -84,7 +84,16 @@ const pushFailure = (stdout: string, stderr: string): KinPurgePushError["reason" // A helper name or absolute path, never an inline `!` script that could carry the secret itself. export const isCredentialHelperReference = (helper: string) => /^(?:[A-Za-z0-9][A-Za-z0-9._-]*|\/[A-Za-z0-9._/-]+)$/.test(helper); -export const isSshCommandReference = (command: string) => /^[A-Za-z0-9 ._/=,:@+-]+$/.test(command); +// Options that run commands, load another config or weaken host checks win over the hardening below. +const sshOverride = + /(?:^|\s)-(?:[FJSEWw]|o\s*(?:ProxyCommand|ProxyJump|ProxyUseFdpass|LocalCommand|PermitLocalCommand|KnownHostsCommand|Include|StrictHostKeyChecking|UserKnownHostsFile|GlobalKnownHostsFile|IdentityAgent|ControlPath|ControlMaster|RemoteCommand|BatchMode)\b)/i; +/** `ssh` (or an absolute path to it) with plain arguments, so the command names a key, never holds one. */ +export const isSshCommandReference = (command: string) => + /^[A-Za-z0-9 ._/=,:@+-]+$/.test(command) && + /^(?:ssh|\/[A-Za-z0-9._/-]*\/ssh)(?:\s|$)/.test(command) && + !sshOverride.test(command); +/** Appended to every owner SSH command: no host config, no prompts, no agent keys. */ +export const SSH_HARDENING = "-F none -o BatchMode=yes -o IdentitiesOnly=yes -o IdentityAgent=none"; /** `file://` carries no credential; https needs the helper and SSH the owner's command. */ const remoteKind = (url: string) => /^https:\/\//i.test(url) @@ -165,9 +174,7 @@ export const gitPushLayer = (owner: { SSH_AUTH_SOCK: undefined, GCM_INTERACTIVE: "never", // Without an owner SSH command, refuse SSH instead of using the host's keys or agent. - GIT_SSH_COMMAND: sshCommand - ? `${sshCommand} -F none -o BatchMode=yes -o IdentitiesOnly=yes -o IdentityAgent=none` - : "false", + GIT_SSH_COMMAND: sshCommand ? `${sshCommand} ${SSH_HARDENING}` : "false", }, allowNonZeroExit: true, maxOutputBytes: 16 * 1024 * 1024, diff --git a/docs/kin/specs/lead.md b/docs/kin/specs/lead.md index c731617fab42..cfd94110efe9 100644 --- a/docs/kin/specs/lead.md +++ b/docs/kin/specs/lead.md @@ -604,7 +604,7 @@ Upstream files touched, one line each: - The lead's authority is its Kin tools. Tools resolve organization and lead from the calling thread, never from input. - Trust and `said_by` are set from server-verified provenance, never by the model. - `rules.md` cannot be written by any lead or dream. -- Leads and workers run in `full-access` mode, so they can run any shell command the server's OS user can. They must not run under the owner's account: that account holds the owner's `gh` login and keychain, which DESIGN 9.5 says agents never hold. Phase 1 requires the Kin server on the Kin host to run as a dedicated `kin` user whose only GitHub credentials are the GitHub App's and the memory repo deploy key (triggers spec, "Running on the Kin host"). Until then, keep every lead at L0. +- Leads and workers run in `full-access` mode, so they can run any shell command the server's OS user can. They must not run under the owner's account: that account holds the owner's `gh` login and keychain, which DESIGN 9.5 says agents never hold. Phase 1 requires the Kin server on the Kin host to run as a dedicated `kin` user whose only GitHub credentials are the GitHub App's and the memory repo deploy key (triggers spec, "Running on the Kin host"). Until then, keep every lead at L0. Agents can still read that deploy key and push to the memory repo around the writer, so while leads are at L0 a ruleset on the memory repo's `main` (pull request and `memory-check`, no deploy-key bypass) keeps the key to `kin/shadow/*`, and no lead goes to L1 until memory git runs as a separate OS user (`ops/kin/host/README.md`). - Release gate: workers are ordinary T3 threads holding a full `/mcp` credential. The `orchestration` capability that grants `delegate_task` also grants `schedule_task`, top-level `t3_thread_launch`, and `t3_thread_send` / `t3_thread_interrupt` on threads outside the worker's subtree, so a prompt-injected worker can reach other work, and its scheduled tasks outlive the worker budget and the stop switch. Scoping a credential to "delegate_task, its own children, and PR tools" needs per-tool capabilities upstream, which do not exist. Until they do, `workerLaunchReady` in `apps/server/src/kin/lead/Workers.ts` stays `false`: `kin_delegate` and `kin_follow_up` refuse with `not_allowed` at L2 and above, and below L2 they keep recording shadow entries. Flip it only when per-tool scoping lands. - Events carry references, so webhook bodies and message text don't sit in the queue. - Stopping is open to the owner at any time without a signature. Raising a level needs a signed grant. diff --git a/ops/kin/host/README.md b/ops/kin/host/README.md index 86a68c292a53..533bc6de14f6 100644 --- a/ops/kin/host/README.md +++ b/ops/kin/host/README.md @@ -41,7 +41,7 @@ Owner overrides such as `T3CODE_LAN=true` go in `~/.t3/service.env`, which the u ## Memory repo access -Memory git ignores the system and global git config, so the host's credential helpers and `GIT_SSH_COMMAND` don't reach it. Give it a credential through `~/.t3/service.env`, which only memory git reads. Without one, cloning a private memory repo fails and every wake-up fails with it. +Memory git ignores the system and global git config, so the host's credential helpers and `GIT_SSH_COMMAND` don't reach it. Give it a credential through `~/.t3/service.env`. The whole server and its agents see these variables, but only memory git turns them into `GIT_SSH_COMMAND` or a credential helper. Without one, cloning a private memory repo fails and every wake-up fails with it. **SSH (use this for now).** Add a deploy key with write access to the memory repo only, and keep the private key outside `~/.ssh`: @@ -57,11 +57,14 @@ Then in `~/.t3/service.env`: KIN_MEMORY_SSH_COMMAND="ssh -i /home/kin/.config/kin/kin-memory-deploy-key -o IdentitiesOnly=yes" ``` -Point `KIN_MEMORY_REPOSITORIES` at the SSH URL (`git@github.com:/.git`), make sure `~/.ssh/known_hosts` holds GitHub's host key, and restart `kin-server`. The key stays out of `~/.ssh` because the approvals credential check tries every key there against GitHub, and a deploy key's greeting names a repo instead of the bot, so the check fails with `non_bot_identity` and privileged actions stop. Agents run as `kin` and can still read the file, which is why the key gets access to the memory repo and nothing else. +Point `KIN_MEMORY_REPOSITORIES` at the SSH URL (`git@github.com:/.git`), make sure `~/.ssh/known_hosts` holds GitHub's host key, and restart `kin-server`. The key stays out of `~/.ssh` because the approvals credential check tries every key there against GitHub, and a deploy key's greeting names a repo instead of the bot, so the check fails with `non_bot_identity` and privileged actions stop. **The key is not contained.** Agents run as `kin` and can read the file, so a prompt-injected lead or worker can push to the memory repo directly, around Kin's memory writer and its rules (owner-only `rules.md`, server-set trust, secret scanning, L0 writes only on `kin/shadow/*`). Until Kin runs memory git as a separate OS user, contain it on GitHub: + +- While every lead is at L0, protect `main` in the memory repo with a ruleset that requires a pull request and the `memory-check` workflow, with no bypass for this deploy key. The key can then only update `kin/shadow/*` and `kin/conflict/*`. +- Don't raise any lead to L1 (which writes `main` directly) until memory git runs as a separate OS user whose key agents can't read. This is the same gap as the approval key (approvals spec, section 12) and has the same fix. **HTTPS.** Set `KIN_MEMORY_CREDENTIAL_HELPER` to a git credential helper's name or absolute path, with an `https://` repo URL. There is no GitHub App installation-token helper yet, so this route has nothing to point at until one is written. -Both values must name a credential, never hold it: the server refuses to start Kin when the helper is an inline `!` script or the SSH command contains shell syntax, and it never logs either value. +Both values must name a credential, never hold it: the server refuses to start Kin when the helper is an inline `!` script, or when the SSH command isn't `ssh` with plain arguments (shell syntax, `ProxyCommand`, `LocalCommand`, `-F`, `-J` and options that weaken host-key checks are refused). It appends `-F none -o BatchMode=yes -o IdentitiesOnly=yes -o IdentityAgent=none`, so `~/.ssh/config` and the agent are ignored and nothing prompts, and it never logs either value. ## Link the laptop and phone