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
14 changes: 12 additions & 2 deletions apps/server/src/auth.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { createHash, createHmac, randomBytes, timingSafeEqual } from "node:crypto";
import { mkdir, readFile, writeFile } from "node:fs/promises";
import { link, mkdir, readFile, unlink, writeFile } from "node:fs/promises";
import { join } from "node:path";
import type { Config } from "./config.ts";
import type { Store } from "./db.ts";
Expand Down Expand Up @@ -74,7 +74,17 @@ export async function createAuth(db: Store, config: Config) {
} catch (error) {
if (!(error instanceof Error && "code" in error && error.code === "ENOENT")) throw error;
key = randomBytes(32).toString("base64");
await writeFile(path, key, { mode: 0o600, flag: "wx" });
const temporary = `${path}.${randomBytes(16).toString("hex")}.tmp`;
await writeFile(temporary, key, { mode: 0o600, flag: "wx" });
try {
// Publish only complete bytes, without replacing another process's key.
await link(temporary, path);
} catch (error) {
if (!(error instanceof Error && "code" in error && error.code === "EEXIST")) throw error;
key = await readFile(path, "utf8");
} finally {
await unlink(temporary);
}
}
return new Auth(db, config, key);
}
49 changes: 49 additions & 0 deletions tests/auth-startup.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import assert from "node:assert/strict";
import { mkdtemp, readdir, readFile, rm, writeFile } from "node:fs/promises";
import { tmpdir } from "node:os";
import { join } from "node:path";
import test from "node:test";
import { createAuth } from "../apps/server/src/auth.ts";
import type { Config } from "../apps/server/src/config.ts";
import type { Store } from "../apps/server/src/db.ts";

function config(dataDir: string): Config {
return {
mode: "sample",
port: 8787,
host: "127.0.0.1",
publicUrl: "http://localhost:8787",
dataDir,
agentBackend: "sample",
allowedOrigins: [],
googleRedirectUri: "http://localhost:8787/api/google/callback",
};
}

test("concurrent first startups share one complete signing key", async (t) => {
const directory = await mkdtemp(join(tmpdir(), "openmuse-auth-race-"));
t.after(() => rm(directory, { recursive: true, force: true }));
const instances = await Promise.all(
Array.from({ length: 16 }, () => createAuth({} as Store, config(directory))),
);
const key = await readFile(join(directory, "session-signing-key"), "utf8");
assert.equal(Buffer.from(key, "base64").length, 32);
for (const signer of instances) {
const url = new URL(signer.sign("owner", "/api/files/example/content"));
for (const verifier of instances) assert.equal(verifier.verify(url), "owner");
}
assert.deepEqual(await readdir(directory), ["session-signing-key"]);
});

test("startup preserves an existing signing key and its links", async (t) => {
const directory = await mkdtemp(join(tmpdir(), "openmuse-auth-existing-"));
t.after(() => rm(directory, { recursive: true, force: true }));
const key = Buffer.alloc(32, 7).toString("base64");
await writeFile(join(directory, "session-signing-key"), key, { mode: 0o600 });
const first = await createAuth({} as Store, config(directory));
const url = new URL(first.sign("owner", "/api/files/example/content"));
const second = await createAuth({} as Store, config(directory));
assert.equal(second.verify(url), "owner");
assert.equal(await readFile(join(directory, "session-signing-key"), "utf8"), key);
assert.deepEqual(await readdir(directory), ["session-signing-key"]);
});
Loading