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
Check · Review · Land · Live done — Landed and live in production.
scout/forge:scout/6671-the-forge-s-ssh-terminal-is-off-after-ev → main 0 commits fork
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 | ||
| 98 | 98 | repos, and the SSH host key, in plain layout. Tar it up and it moves. |
| 99 | 99 | |
| 100 | 100 | 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 | |
| 102 | 102 | home page shows the fingerprint beside the ssh command, to compare with what `ssh` prints on |
| 103 | 103 | a first connection. The SSH door refuses, before any key work, past 256 open connections, 32 from one address |
| 104 | 104 | 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 { | ||
| 271 | 271 | return new Writer(64).string("ssh-ed25519").string(raw).build(); |
| 272 | 272 | } |
| 273 | 273 | |
| 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 | ||
| 274 | 289 | /** |
| 275 | 290 | * Load `<dir>/host_ed25519`, generating it on first boot. |
| 276 | 291 | * |
| @@ -280,13 +295,16 @@ function ed25519Blob(privateKey: KeyObject): Uint8Array { | ||
| 280 | 295 | * in `authorized_keys` form, which is what `ssh-keygen -lf` actually reads, |
| 281 | 296 | * so an operator can confirm the fingerprint with the standard tool. |
| 282 | 297 | */ |
| 283 | export function loadOrCreateHostKey(dir: string): HostKey { | |
| 298 | export function loadOrCreateHostKey(dir: string, ownerUid: number | undefined = process.getuid?.()): HostKey { | |
| 284 | 299 | mkdirSync(dir, { recursive: true }); |
| 285 | 300 | const path = join(dir, "host_ed25519"); |
| 286 | 301 | let privateKey: KeyObject; |
| 287 | 302 | try { |
| 288 | 303 | 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); | |
| 290 | 308 | const mode = statSync(path).mode & 0o777; |
| 291 | 309 | if ((mode & 0o077) !== 0) { |
| 292 | 310 | defect( |
test/file-modes.test.ts +11 −2
| @@ -20,12 +20,21 @@ afterAll(() => { | ||
| 20 | 20 | }); |
| 21 | 21 | const mode = (p: string) => statSync(p).mode & 0o777; |
| 22 | 22 | |
| 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", () => { | |
| 24 | 24 | const dir = tmp(); |
| 25 | 25 | loadOrCreateHostKey(dir); |
| 26 | 26 | 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); | |
| 27 | 35 | 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/); | |
| 29 | 38 | expect(mode(join(dir, "host_ed25519"))).toBe(0o640); |
| 30 | 39 | }); |
| 31 | 40 |
new above
offline