Public: anyone can read this; the crew can change it.

The forge's SSH terminal is off after every boot: the host key comes up mode 660; the forge should tighten its own key to 0600 at start #16984

Merged opened this in answer to

Check · Review · Land · Live done — Landed and live in production.

Merged

scout/forge:scout/6671-the-forge-s-ssh-terminal-is-off-after-ev → main 0 commits fork

  • Merged. scout/6671-the-forge-s-ssh-terminal-is-off-after-ev was merged into main as c86b931.

Review

Sign in to review this change.
3 files changed +32 −5
docs/OPERATING.md +1 −1
@@ -98,7 +98,7 @@ instance _is_ lives under the data directory — the SQLite index, the bare
9898 repos, and the SSH host key, in plain layout. Tar it up and it moves.
9999
100100 The SSH host key is `<data>/ssh/host_ed25519`, and the snapshot carries it (`ssh/`), so a restore
101 keeps the fingerprint people have trusted; lose both and every client sees "host key changed". The
101 keeps the fingerprint people have trusted; lose both and every client sees "host key changed". The key must be mode 0600; a volume's group bits can widen it on every mount, so the forge tightens its own key at start (a key it does not own, or cannot chmod, is refused and the terminal stays off, with the reason logged). The
102102 home page shows the fingerprint beside the ssh command, to compare with what `ssh` prints on
103103 a first connection. The SSH door refuses, before any key work, past 256 open connections, 32 from one address
104104 or 20 new handshakes from one address (120 overall) per 10 s (`src/ssh/limits.ts`); the address is the
src/ssh/keys.ts +20 −2
@@ -271,6 +271,21 @@ function ed25519Blob(privateKey: KeyObject): Uint8Array {
271271 return new Writer(64).string("ssh-ed25519").string(raw).build();
272272 }
273273
274 /**
275 * chmod 0600 the host key when the forge's own uid owns it. A key someone
276 * else owns is left alone (the mode check refuses it); a chmod that fails
277 * is logged with its reason, then the mode check refuses with the mode.
278 */
279 function tightenHostKey(path: string, uid: number | undefined): void {
280 try {
281 const st = statSync(path);
282 if (st.uid !== uid || (st.mode & 0o077) === 0) return;
283 chmodSync(path, 0o600);
284 } catch (e) {
285 console.error(`ssh: cannot tighten ${path} to 0600: ${(e as Error).message}`);
286 }
287 }
288
274289 /**
275290 * Load `<dir>/host_ed25519`, generating it on first boot.
276291 *
@@ -280,13 +295,16 @@ function ed25519Blob(privateKey: KeyObject): Uint8Array {
280295 * in `authorized_keys` form, which is what `ssh-keygen -lf` actually reads,
281296 * so an operator can confirm the fingerprint with the standard tool.
282297 */
283 export function loadOrCreateHostKey(dir: string): HostKey {
298 export function loadOrCreateHostKey(dir: string, ownerUid: number | undefined = process.getuid?.()): HostKey {
284299 mkdirSync(dir, { recursive: true });
285300 const path = join(dir, "host_ed25519");
286301 let privateKey: KeyObject;
287302 try {
288303 const pem = readFileSync(path, "utf8");
289 // Same refusal as secret.key: fail closed, never chmod it quietly.
304 // A volume's group bits (fsGroup) re-widen the key on every mount. The
305 // forge tightens a key it owns; one owned by someone else, or one it
306 // cannot chmod, is still refused (fail closed).
307 tightenHostKey(path, ownerUid);
290308 const mode = statSync(path).mode & 0o777;
291309 if ((mode & 0o077) !== 0) {
292310 defect(
test/file-modes.test.ts +11 −2
@@ -20,12 +20,21 @@ afterAll(() => {
2020 });
2121 const mode = (p: string) => statSync(p).mode & 0o777;
2222
23 test("a fresh host key is 0600, and a 0640 one is refused with the path and mode", () => {
23 test("a fresh host key is 0600, and a 0660 one is tightened at load", () => {
2424 const dir = tmp();
2525 loadOrCreateHostKey(dir);
2626 expect(mode(join(dir, "host_ed25519"))).toBe(0o600);
27 chmodSync(join(dir, "host_ed25519"), 0o660);
28 loadOrCreateHostKey(dir);
29 expect(mode(join(dir, "host_ed25519"))).toBe(0o600);
30 });
31
32 test("a too-wide host key the forge does not own is refused with the path and mode, and left alone", () => {
33 const dir = tmp();
34 loadOrCreateHostKey(dir);
2735 chmodSync(join(dir, "host_ed25519"), 0o640);
28 expect(() => loadOrCreateHostKey(dir)).toThrow(/host_ed25519 is mode 640; it must be 0600/);
36 const stranger = (process.getuid?.() ?? 0) + 1;
37 expect(() => loadOrCreateHostKey(dir, stranger)).toThrow(/host_ed25519 is mode 640; it must be 0600/);
2938 expect(mode(join(dir, "host_ed25519"))).toBe(0o640);
3039 });
3140

Search
↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs