Skip to content

Commit cdabf8b

Browse files
Share a complete signing key across concurrent first starts (#125)
1 parent 76c01e7 commit cdabf8b

2 files changed

Lines changed: 61 additions & 2 deletions

File tree

‎apps/server/src/auth.ts‎

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { createHash, createHmac, randomBytes, timingSafeEqual } from "node:crypto";
2-
import { mkdir, readFile, writeFile } from "node:fs/promises";
2+
import { link, mkdir, readFile, unlink, writeFile } from "node:fs/promises";
33
import { join } from "node:path";
44
import type { Config } from "./config.ts";
55
import type { Store } from "./db.ts";
@@ -74,7 +74,17 @@ export async function createAuth(db: Store, config: Config) {
7474
} catch (error) {
7575
if (!(error instanceof Error && "code" in error && error.code === "ENOENT")) throw error;
7676
key = randomBytes(32).toString("base64");
77-
await writeFile(path, key, { mode: 0o600, flag: "wx" });
77+
const temporary = `${path}.${randomBytes(16).toString("hex")}.tmp`;
78+
await writeFile(temporary, key, { mode: 0o600, flag: "wx" });
79+
try {
80+
// Publish only complete bytes, without replacing another process's key.
81+
await link(temporary, path);
82+
} catch (error) {
83+
if (!(error instanceof Error && "code" in error && error.code === "EEXIST")) throw error;
84+
key = await readFile(path, "utf8");
85+
} finally {
86+
await unlink(temporary);
87+
}
7888
}
7989
return new Auth(db, config, key);
8090
}

‎tests/auth-startup.test.ts‎

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import assert from "node:assert/strict";
2+
import { mkdtemp, readdir, readFile, rm, writeFile } from "node:fs/promises";
3+
import { tmpdir } from "node:os";
4+
import { join } from "node:path";
5+
import test from "node:test";
6+
import { createAuth } from "../apps/server/src/auth.ts";
7+
import type { Config } from "../apps/server/src/config.ts";
8+
import type { Store } from "../apps/server/src/db.ts";
9+
10+
function config(dataDir: string): Config {
11+
return {
12+
mode: "sample",
13+
port: 8787,
14+
host: "127.0.0.1",
15+
publicUrl: "http://localhost:8787",
16+
dataDir,
17+
agentBackend: "sample",
18+
allowedOrigins: [],
19+
googleRedirectUri: "http://localhost:8787/api/google/callback",
20+
};
21+
}
22+
23+
test("concurrent first startups share one complete signing key", async (t) => {
24+
const directory = await mkdtemp(join(tmpdir(), "openmuse-auth-race-"));
25+
t.after(() => rm(directory, { recursive: true, force: true }));
26+
const instances = await Promise.all(
27+
Array.from({ length: 16 }, () => createAuth({} as Store, config(directory))),
28+
);
29+
const key = await readFile(join(directory, "session-signing-key"), "utf8");
30+
assert.equal(Buffer.from(key, "base64").length, 32);
31+
for (const signer of instances) {
32+
const url = new URL(signer.sign("owner", "/api/files/example/content"));
33+
for (const verifier of instances) assert.equal(verifier.verify(url), "owner");
34+
}
35+
assert.deepEqual(await readdir(directory), ["session-signing-key"]);
36+
});
37+
38+
test("startup preserves an existing signing key and its links", async (t) => {
39+
const directory = await mkdtemp(join(tmpdir(), "openmuse-auth-existing-"));
40+
t.after(() => rm(directory, { recursive: true, force: true }));
41+
const key = Buffer.alloc(32, 7).toString("base64");
42+
await writeFile(join(directory, "session-signing-key"), key, { mode: 0o600 });
43+
const first = await createAuth({} as Store, config(directory));
44+
const url = new URL(first.sign("owner", "/api/files/example/content"));
45+
const second = await createAuth({} as Store, config(directory));
46+
assert.equal(second.verify(url), "owner");
47+
assert.equal(await readFile(join(directory, "session-signing-key"), "utf8"), key);
48+
assert.deepEqual(await readdir(directory), ["session-signing-key"]);
49+
});

0 commit comments

Comments
 (0)