From 746e29ca4f841283b1f55bd2f540deca1564ade2 Mon Sep 17 00:00:00 2001 From: Szymon Chmal Date: Fri, 16 Oct 2026 19:46:08 +0200 Subject: [PATCH 1/3] test(http,core): tokens.json and state.json are written 0644, not the owner-only mode the comments promise (#258) --- src/core/registry.test.ts | 21 +++++++++++++++++++++ src/http/token-store.test.ts | 8 ++++++++ 2 files changed, 29 insertions(+) diff --git a/src/core/registry.test.ts b/src/core/registry.test.ts index 1f860a1aa..4b07abe1b 100644 --- a/src/core/registry.test.ts +++ b/src/core/registry.test.ts @@ -193,6 +193,27 @@ describe("Registry", () => { expect(reloaded.snapshot).toEqual(registry.snapshot); }); + it("writes state.json owner-only, mode 0o600, as the daemon's local-state guarantee says", async () => { + const clock = new FakeClock(1_000); + const filesystem = new MemoryFilesystem(); + const registry = await Registry.load({ + clock, + eventBus: new EventBus(clock), + filesystem, + idGenerator: { generate: () => "test" }, + statePath, + }); + + await registry.registerDevice({ + driverData: {}, + driverDeviceId: "driver_test", + provisionDuration: 25, + spec, + }); + + expect((await filesystem.stat(statePath)).mode).toBe(0o600); + }); + it("loads a legacy warm record as busy reclaiming rather than eligible ready inventory", async () => { const clock = new FakeClock(1_000); const filesystem = new MemoryFilesystem(); diff --git a/src/http/token-store.test.ts b/src/http/token-store.test.ts index d77a86315..6c42b478b 100644 --- a/src/http/token-store.test.ts +++ b/src/http/token-store.test.ts @@ -67,6 +67,14 @@ describe("TokenStore", () => { expect(JSON.stringify(persisted)).not.toContain("fake-secret"); }); + it("writes tokens.json owner-only, mode 0o600, as the daemon's local-state guarantee says", async () => { + const { store, filesystem } = tokenStore(); + + await store.create("agent", "ci-runner"); + + expect((await filesystem.stat(tokensPath)).mode).toBe(0o600); + }); + it("creates a token without a label when none is given", async () => { const { store } = tokenStore(); From 7af68ebae663064ccec86468551805b59f99d89e Mon Sep 17 00:00:00 2001 From: Szymon Chmal Date: Fri, 16 Oct 2026 19:56:39 +0200 Subject: [PATCH 2/3] fix(http,core): write tokens.json and state.json 0600 (#258) 2 failing -> 0 failing --- src/core/registry.test.ts | 8 ++++++-- src/core/registry.ts | 1 + src/http/token-store.ts | 4 +++- 3 files changed, 10 insertions(+), 3 deletions(-) diff --git a/src/core/registry.test.ts b/src/core/registry.test.ts index 4b07abe1b..a0ad42db7 100644 --- a/src/core/registry.test.ts +++ b/src/core/registry.test.ts @@ -23,9 +23,13 @@ const spec: VirtualSpec = { class ObservingFilesystem extends MemoryFilesystem { readonly operations: string[] = []; - override async writeFileAtomic(path: string, contents: string): Promise { + override async writeFileAtomic( + path: string, + contents: string, + options?: { mode?: number }, + ): Promise { this.operations.push("save"); - await super.writeFileAtomic(path, contents); + await super.writeFileAtomic(path, contents, options); } } diff --git a/src/core/registry.ts b/src/core/registry.ts index 328374a2f..769f9562a 100644 --- a/src/core/registry.ts +++ b/src/core/registry.ts @@ -968,6 +968,7 @@ export class Registry implements LeaseRequestStore { })), components, }), + { mode: 0o600 }, ); this.#devices = devices; this.#leases = leases; diff --git a/src/http/token-store.ts b/src/http/token-store.ts index f6cf46241..ecfcf130d 100644 --- a/src/http/token-store.ts +++ b/src/http/token-store.ts @@ -144,7 +144,9 @@ export class TokenStore { async #writeAll(records: readonly TokenRecord[]): Promise { await this.#filesystem.mkdirp(dirname(this.#path)); - await this.#filesystem.writeFileAtomic(this.#path, `${JSON.stringify(records, null, 2)}\n`); + await this.#filesystem.writeFileAtomic(this.#path, `${JSON.stringify(records, null, 2)}\n`, { + mode: 0o600, + }); } } From 1f1689c77003407a0a430ee18f9e8edfb924f330 Mon Sep 17 00:00:00 2001 From: Szymon Chmal Date: Fri, 16 Oct 2026 19:58:35 +0200 Subject: [PATCH 3/3] docs(gateway,http,core): scope drain-store mode comment, note 0600 in manuals (#258) --- src/core/AGENTS.md | 2 +- src/gateway/drain-store.ts | 2 +- src/http/AGENTS.md | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/core/AGENTS.md b/src/core/AGENTS.md index ba8a76899..1ff5901fa 100644 --- a/src/core/AGENTS.md +++ b/src/core/AGENTS.md @@ -129,7 +129,7 @@ Events: → `absent` `device.absence-detected`; `absent` → `reclaiming` `devic ## Rules -- Only `Registry` writes `state.json`, inside `SerializedDecision.run`. Events after the commit. +- Only `Registry` writes `state.json`, inside `SerializedDecision.run`, mode `0600`. Events after the commit. - Destroy only what the registry holds. One exception: `doctor --purge-orphans`, opt-in. - Nothing in core acts on a leased device, except `recoverLeased`, under the lease. - Grant paths select by exact state; `mayBeGranted` keeps a spent `fresh` device out. diff --git a/src/gateway/drain-store.ts b/src/gateway/drain-store.ts index c02ee8752..fafa67455 100644 --- a/src/gateway/drain-store.ts +++ b/src/gateway/drain-store.ts @@ -13,7 +13,7 @@ import { dirname } from "node:path"; import type { Filesystem, Logger } from "../ports/index.js"; import { NoopLogger } from "../ports/index.js"; -/** Owner-only, like every other file the daemon keeps under `SIMLOCK_HOME` (`tokens.json`, +/** why: Owner-only, like the other files that hold secrets or state (`tokens.json`, * `admin.token`, `state.json`): the trust boundary is the OS user. */ const WORKERS_FILE_MODE = 0o600; diff --git a/src/http/AGENTS.md b/src/http/AGENTS.md index b087752e3..cdcc16095 100644 --- a/src/http/AGENTS.md +++ b/src/http/AGENTS.md @@ -89,7 +89,7 @@ until `POST /v1/leases/{id}/renew` drains them into `notices`. `lease_lost` clea convergence. Early requests park in the dispatcher. On a gateway the worker uplink upgrades on the same listener (`onServerCreated`). - `tokens.json` (data directory): `{ id: "tok_…", hash, role, label?, createdAt }`, written - atomically. The dispatcher's `token.*` handlers write it through the same `TokenStore`. + atomically, mode `0600`. The dispatcher's `token.*` handlers write it through the same `TokenStore`. `verify` reads the file on every call. The secret is shown once, by `token.create`. - `LeaseNoticeBuffer` is memory only. `HttpGateway.stop` closes the server and destroys every open socket.