Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/core/AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
29 changes: 27 additions & 2 deletions src/core/registry.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,13 @@ const spec: VirtualSpec = {
class ObservingFilesystem extends MemoryFilesystem {
readonly operations: string[] = [];

override async writeFileAtomic(path: string, contents: string): Promise<void> {
override async writeFileAtomic(
path: string,
contents: string,
options?: { mode?: number },
): Promise<void> {
this.operations.push("save");
await super.writeFileAtomic(path, contents);
await super.writeFileAtomic(path, contents, options);
}
}

Expand Down Expand Up @@ -193,6 +197,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();
Expand Down
1 change: 1 addition & 0 deletions src/core/registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -968,6 +968,7 @@ export class Registry implements LeaseRequestStore<LeaseGrant> {
})),
components,
}),
{ mode: 0o600 },
);
this.#devices = devices;
this.#leases = leases;
Expand Down
2 changes: 1 addition & 1 deletion src/gateway/drain-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
2 changes: 1 addition & 1 deletion src/http/AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
8 changes: 8 additions & 0 deletions src/http/token-store.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down
4 changes: 3 additions & 1 deletion src/http/token-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,9 @@ export class TokenStore {

async #writeAll(records: readonly TokenRecord[]): Promise<void> {
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,
});
}
}

Expand Down
Loading