@botiverse/stamp-cli 0.3.2 → 0.3.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -58,3 +58,18 @@ export function runGitClone({ url, directory, gitFlags, env, spawnImpl = spawn }
58
58
  child.on("close", (code) => resolve({ code: code ?? 1, stdout, stderr }));
59
59
  });
60
60
  }
61
+
62
+ // runGitConfig pins the enrolled identity in the cloned repository itself, so
63
+ // every later `git push`/`git fetch` from that checkout uses the same key and
64
+ // the same pinned host key without any GIT_SSH_COMMAND export.
65
+ export function runGitSetSSHCommand({ directory, sshCommand, spawnImpl = spawn }) {
66
+ return new Promise((resolve) => {
67
+ const child = spawnImpl("git", ["-C", directory, "config", "--local", "core.sshCommand", sshCommand], {
68
+ stdio: ["ignore", "ignore", "pipe"],
69
+ });
70
+ let stderr = "";
71
+ child.stderr?.on("data", (b) => { stderr += b; });
72
+ child.on("error", () => resolve({ ok: false }));
73
+ child.on("close", (code) => resolve({ ok: code === 0, stderr }));
74
+ });
75
+ }
@@ -0,0 +1,96 @@
1
+ import { chmodSync, existsSync, mkdirSync, readFileSync, writeFileSync } from "node:fs";
2
+ import { homedir } from "node:os";
3
+ import { dirname, join } from "node:path";
4
+
5
+ // `stamp repo clone` must not ask a human to export GIT_SSH_COMMAND: after
6
+ // `stamp key enroll` the CLI already knows which key it enrolled and the
7
+ // gateway already publishes its host key over HTTPS
8
+ // (/.well-known/ssh-known-hosts, task #463). So the clone carries both: the
9
+ // private key as `-i`, and the published line as the ONLY trusted host key
10
+ // (StrictHostKeyChecking=yes + IdentitiesOnly=yes), which is a pin, not TOFU.
11
+
12
+ // defaultIdentityPath is where `stamp key enroll` looks for the public key; the
13
+ // private half is the same path without `.pub`.
14
+ export function defaultIdentityPath(env) {
15
+ return join(homedir(), ".ssh", "id_ed25519");
16
+ }
17
+
18
+ export function resolveIdentityPath(env, flagValue) {
19
+ const fromFlag = String(flagValue ?? "").trim();
20
+ if (fromFlag) return fromFlag;
21
+ const fromEnv = String(env?.STAMP_SSH_IDENTITY ?? "").trim();
22
+ return fromEnv || defaultIdentityPath(env);
23
+ }
24
+
25
+ // defaultKnownHostsPath is one stable file for every Stamp instance (a
26
+ // known_hosts file holds many lines); lines are merged per host pattern below.
27
+ export function resolveKnownHostsPath(env, flagValue) {
28
+ const fromFlag = String(flagValue ?? "").trim();
29
+ if (fromFlag) return fromFlag;
30
+ const fromEnv = String(env?.STAMP_KNOWN_HOSTS ?? "").trim();
31
+ return fromEnv || join(homedir(), ".stamp", "known_hosts");
32
+ }
33
+
34
+ function shellQuote(value) {
35
+ const s = String(value);
36
+ if (s === "" || /[^A-Za-z0-9._@%+,:=\/-]/.test(s)) {
37
+ return `'${s.replaceAll("'", `'\\''`)}'`;
38
+ }
39
+ return s;
40
+ }
41
+
42
+ // sshCommandFor is the value git needs (GIT_SSH_COMMAND / core.sshCommand). It
43
+ // names the identity and the pinned host key file explicitly, and never falls
44
+ // back to ~/.ssh/known_hosts or to the agent's other identities.
45
+ export function sshCommandFor({ identityPath, knownHostsPath }) {
46
+ return [
47
+ "ssh",
48
+ "-i", shellQuote(identityPath),
49
+ "-o", "IdentitiesOnly=yes",
50
+ "-o", `UserKnownHostsFile=${shellQuote(knownHostsPath)}`,
51
+ "-o", "StrictHostKeyChecking=yes",
52
+ ].join(" ");
53
+ }
54
+
55
+ const KNOWN_HOSTS_KEY_TYPE = /^(ssh-ed25519|ssh-rsa|ecdsa-sha2-nistp(256|384|521)|sk-ssh-ed25519@openssh\.com|sk-ecdsa-sha2-nistp256@openssh\.com)$/;
56
+ const KNOWN_HOSTS_PATTERN = /^(\[[^\]:]+]:\d{1,5}|[A-Za-z0-9][A-Za-z0-9.-]*)$/;
57
+
58
+ // parseKnownHostsLine accepts exactly the one-line c2sp form the gateway
59
+ // publishes and rejects anything else: pinning a malformed line would either
60
+ // fail every connection or silently trust the wrong pattern.
61
+ export function parseKnownHostsLine(text) {
62
+ const line = String(text ?? "").trim();
63
+ if (!line || line.includes("\n")) return null;
64
+ const [pattern, keyType, base64] = line.split(/\s+/);
65
+ if (!pattern || !keyType || !base64) return null;
66
+ if (!KNOWN_HOSTS_PATTERN.test(pattern) || !KNOWN_HOSTS_KEY_TYPE.test(keyType)) return null;
67
+ if (!/^[A-Za-z0-9+/]+={0,3}$/.test(base64) || base64.length < 40) return null;
68
+ return { pattern, line };
69
+ }
70
+
71
+ // writePinnedKnownHosts merges one line into the known_hosts file, replacing any
72
+ // earlier line for the same pattern so a rotated host key is superseded rather
73
+ // than left to collide (two lines for one pattern make ssh refuse the host).
74
+ export function writePinnedKnownHosts(path, pattern, line) {
75
+ const existing = existsSync(path) ? readFileSync(path, "utf8") : "";
76
+ const kept = existing
77
+ .split("\n")
78
+ .filter((l) => l.trim() !== "" && l.trim().split(/\s+/)[0] !== pattern);
79
+ kept.push(line);
80
+ mkdirSync(dirname(path), { recursive: true });
81
+ writeFileSync(path, `${kept.join("\n")}\n`, { mode: 0o600 });
82
+ chmodSync(path, 0o600);
83
+ }
84
+
85
+ // fetchPublishedKnownHosts reads the host key from the instance itself over
86
+ // HTTPS — a channel independent of the SSH connection — so the pin is not
87
+ // first-use. "Not available here" (404) is reported, never substituted with a
88
+ // keyscan of the very connection we are about to trust.
89
+ export async function fetchPublishedKnownHosts({ apiBase, fetchImpl = fetch }) {
90
+ const res = await fetchImpl(`${apiBase}/.well-known/ssh-known-hosts`, { headers: { accept: "text/plain" } });
91
+ const text = await res.text().catch(() => "");
92
+ if (!res.ok) return { ok: false, status: res.status };
93
+ const parsed = parseKnownHostsLine(text);
94
+ if (!parsed) return { ok: false, invalid: true };
95
+ return { ok: true, pattern: parsed.pattern, line: parsed.line };
96
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@botiverse/stamp-cli",
3
- "version": "0.3.2",
3
+ "version": "0.3.4",
4
4
  "description": "CLI for Stamp — Agent Git gateway in the Raft loop (bin stamp; future origin stamp.build)",
5
5
  "license": "MIT",
6
6
  "type": "module",