311 lines
9.5 KiB
TypeScript
311 lines
9.5 KiB
TypeScript
import { afterEach, expect, it } from "vitest";
|
|
import {
|
|
mkdtemp,
|
|
readFile,
|
|
rm,
|
|
stat,
|
|
writeFile,
|
|
mkdir,
|
|
copyFile,
|
|
} from "node:fs/promises";
|
|
import { tmpdir } from "node:os";
|
|
import { join } from "node:path";
|
|
import type { FastifyInstance } from "fastify";
|
|
import { buildApp } from "../src/app";
|
|
import { setPassword } from "../src/auth/credentials";
|
|
import { claudeStatus } from "../src/settings";
|
|
const dirs: string[] = [],
|
|
apps: FastifyInstance[] = [];
|
|
async function dir() {
|
|
const path = await mkdtemp(join(tmpdir(), "settings-"));
|
|
dirs.push(path);
|
|
return path;
|
|
}
|
|
async function setup(maxAttempts = 5) {
|
|
const dataDir = await dir();
|
|
const app = await buildApp({ config: { dataDir }, auth: { maxAttempts } });
|
|
apps.push(app);
|
|
return { app, dataDir };
|
|
}
|
|
const password = "a long test password";
|
|
const cookieOf = (r: { headers: Record<string, unknown> }) =>
|
|
String(r.headers["set-cookie"]).split(";")[0]!;
|
|
afterEach(async () => {
|
|
for (const app of apps.splice(0)) await app.close();
|
|
for (const path of dirs.splice(0))
|
|
await rm(path, { recursive: true, force: true });
|
|
});
|
|
it("creates a persistent private token and consumes it once, including parallel setup", async () => {
|
|
const { app, dataDir } = await setup();
|
|
const token = (await readFile(join(dataDir, "setup-token"), "utf8")).trim();
|
|
expect(token).toMatch(/^[a-f0-9]{64}$/);
|
|
if (process.platform !== "win32")
|
|
expect((await stat(join(dataDir, "setup-token"))).mode & 0o777).toBe(0o600);
|
|
expect((await app.inject("/api/auth/setup")).json()).toEqual({
|
|
required: true,
|
|
});
|
|
expect(
|
|
(
|
|
await app.inject({
|
|
method: "POST",
|
|
url: "/api/auth/setup",
|
|
payload: {
|
|
token: "wrong",
|
|
newPassword: password,
|
|
confirmPassword: password,
|
|
},
|
|
})
|
|
).statusCode,
|
|
).toBe(401);
|
|
const payload = { token, newPassword: password, confirmPassword: password };
|
|
const results = await Promise.all(
|
|
[1, 2].map(() =>
|
|
app.inject({ method: "POST", url: "/api/auth/setup", payload }),
|
|
),
|
|
);
|
|
expect(results.map((r) => r.statusCode).sort()).toEqual([204, 409]);
|
|
await expect(stat(join(dataDir, "setup-token"))).rejects.toMatchObject({
|
|
code: "ENOENT",
|
|
});
|
|
expect((await app.inject("/api/auth/setup")).json()).toEqual({
|
|
required: false,
|
|
});
|
|
const cookie = cookieOf(results.find((r) => r.statusCode === 204)!);
|
|
expect(
|
|
(await app.inject({ url: "/api/auth/me", headers: { cookie } })).statusCode,
|
|
).toBe(200);
|
|
if (process.platform !== "win32")
|
|
expect((await stat(join(dataDir, "auth.json"))).mode & 0o777).toBe(0o600);
|
|
});
|
|
it("retains the setup token across restart and removes stale tokens with configured password", async () => {
|
|
const { app, dataDir } = await setup();
|
|
const token = await readFile(join(dataDir, "setup-token"), "utf8");
|
|
await app.close();
|
|
apps.splice(apps.indexOf(app), 1);
|
|
const second = await buildApp({ config: { dataDir } });
|
|
apps.push(second);
|
|
expect(await readFile(join(dataDir, "setup-token"), "utf8")).toBe(token);
|
|
await setPassword(dataDir, password);
|
|
const third = await buildApp({ config: { dataDir } });
|
|
apps.push(third);
|
|
await expect(stat(join(dataDir, "setup-token"))).rejects.toMatchObject({
|
|
code: "ENOENT",
|
|
});
|
|
});
|
|
it("shares setup/login limits and rejects weak or mismatched passwords", async () => {
|
|
const { app, dataDir } = await setup(3);
|
|
const token = (await readFile(join(dataDir, "setup-token"), "utf8")).trim();
|
|
for (const [newPassword, confirmPassword] of [
|
|
["short", "short"],
|
|
[password, "different"],
|
|
]) {
|
|
expect(
|
|
(
|
|
await app.inject({
|
|
method: "POST",
|
|
url: "/api/auth/setup",
|
|
payload: { token, newPassword, confirmPassword },
|
|
})
|
|
).statusCode,
|
|
).toBe(400);
|
|
}
|
|
expect(
|
|
(
|
|
await app.inject({
|
|
method: "POST",
|
|
url: "/api/auth/login",
|
|
payload: { password },
|
|
})
|
|
).statusCode,
|
|
).toBe(401);
|
|
const response = await app.inject({
|
|
method: "POST",
|
|
url: "/api/auth/setup",
|
|
payload: { token, newPassword: password, confirmPassword: password },
|
|
});
|
|
expect(response.statusCode).toBe(429);
|
|
expect(response.headers["retry-after"]).toBeDefined();
|
|
});
|
|
it("keeps every settings route private and setup under the origin check", async () => {
|
|
const { app } = await setup();
|
|
for (const [method, url] of [
|
|
["GET", "/api/settings/system"],
|
|
["GET", "/api/settings/claude"],
|
|
["GET", "/api/settings/session"],
|
|
["POST", "/api/settings/password"],
|
|
["POST", "/api/settings/sessions/revoke"],
|
|
["GET", "/api/auth/setup/"],
|
|
["GET", "/api/auth/setup/extra"],
|
|
] as const) {
|
|
expect((await app.inject({ method, url })).statusCode, url).toBe(401);
|
|
}
|
|
expect(
|
|
(
|
|
await app.inject({
|
|
method: "POST",
|
|
url: "/api/auth/setup",
|
|
headers: { origin: "https://evil.test" },
|
|
payload: {},
|
|
})
|
|
).statusCode,
|
|
).toBe(403);
|
|
});
|
|
it("checks current password, revokes cookies on password change and rotation, survives restart", async () => {
|
|
const { app, dataDir } = await setup();
|
|
await setPassword(dataDir, password);
|
|
const login = async () =>
|
|
cookieOf(
|
|
await app.inject({
|
|
method: "POST",
|
|
url: "/api/auth/login",
|
|
payload: { password },
|
|
}),
|
|
);
|
|
const cookie = await login();
|
|
const payload = {
|
|
currentPassword: "wrong",
|
|
newPassword: "new long password",
|
|
confirmPassword: "new long password",
|
|
};
|
|
const change = () =>
|
|
app.inject({
|
|
method: "POST",
|
|
url: "/api/settings/password",
|
|
headers: { cookie },
|
|
payload,
|
|
});
|
|
expect((await change()).statusCode).toBe(401);
|
|
payload.currentPassword = password;
|
|
payload.confirmPassword = "mismatch";
|
|
expect((await change()).statusCode).toBe(400);
|
|
payload.confirmPassword = payload.newPassword;
|
|
const changed = await change();
|
|
expect(changed.statusCode).toBe(204);
|
|
const fresh = cookieOf(changed);
|
|
expect(
|
|
(await app.inject({ url: "/api/auth/me", headers: { cookie } })).statusCode,
|
|
).toBe(401);
|
|
expect(
|
|
(
|
|
await app.inject({
|
|
url: "/api/settings/session",
|
|
headers: { cookie: fresh },
|
|
})
|
|
).json(),
|
|
).toEqual({ lifetimeSeconds: 2592000 });
|
|
const oldSecret = await readFile(join(dataDir, "session-secret"));
|
|
const revoked = await app.inject({
|
|
method: "POST",
|
|
url: "/api/settings/sessions/revoke",
|
|
headers: { cookie: fresh },
|
|
});
|
|
expect(revoked.statusCode).toBe(204);
|
|
expect(await readFile(join(dataDir, "session-secret"))).not.toEqual(
|
|
oldSecret,
|
|
);
|
|
expect(
|
|
(await app.inject({ url: "/api/auth/me", headers: { cookie: fresh } }))
|
|
.statusCode,
|
|
).toBe(401);
|
|
const renewed = cookieOf(revoked);
|
|
const second = await buildApp({ config: { dataDir } });
|
|
apps.push(second);
|
|
expect(
|
|
(
|
|
await second.inject({
|
|
url: "/api/settings/system",
|
|
headers: { cookie: renewed },
|
|
})
|
|
).json(),
|
|
).toMatchObject({
|
|
dataDirPath: dataDir,
|
|
nodeVersion: process.version,
|
|
version: "0.0.0",
|
|
});
|
|
await setPassword(dataDir, "CLI changed password");
|
|
expect(
|
|
(await second.inject({ url: "/api/auth/me", headers: { cookie: renewed } }))
|
|
.statusCode,
|
|
).toBe(401);
|
|
});
|
|
it("reports absent CLI/credentials without reading contents", async () => {
|
|
const home = await dir();
|
|
expect(await claudeStatus(home, { PATH: home })).toEqual({
|
|
available: false,
|
|
version: null,
|
|
loginAppearsPresent: false,
|
|
});
|
|
await mkdir(join(home, ".claude"));
|
|
await mkdir(join(home, ".claude", ".credentials.json"));
|
|
expect((await claudeStatus(home, { PATH: home })).loginAppearsPresent).toBe(
|
|
true,
|
|
);
|
|
});
|
|
it("runs a fake Claude executable from PATH with argv", async () => {
|
|
const home = await dir();
|
|
if (process.platform === "win32") {
|
|
await copyFile(process.execPath, join(home, "claude.exe"));
|
|
expect(
|
|
await claudeStatus(home, {
|
|
PATH: home,
|
|
SystemRoot: process.env.SystemRoot,
|
|
}),
|
|
).toEqual({
|
|
available: true,
|
|
version: process.version,
|
|
loginAppearsPresent: false,
|
|
});
|
|
return;
|
|
}
|
|
await writeFile(
|
|
join(home, "claude"),
|
|
'#!/bin/sh\n[ "$1" = --version ] || exit 1\nprintf "fake-claude 1.0\\n"\n',
|
|
{ mode: 0o700 },
|
|
);
|
|
expect(await claudeStatus(home, { PATH: home })).toEqual({
|
|
available: true,
|
|
version: "fake-claude 1.0",
|
|
loginAppearsPresent: false,
|
|
});
|
|
});
|
|
|
|
it("closes active WebSockets on password changes and sign-out-all", async () => {
|
|
const { app, dataDir } = await setup();
|
|
await setPassword(dataDir, password);
|
|
app.get("/api/test-socket", { websocket: true }, (socket) => {
|
|
socket.send("ready");
|
|
});
|
|
let cookie = cookieOf(
|
|
await app.inject({
|
|
method: "POST",
|
|
url: "/api/auth/login",
|
|
payload: { password },
|
|
}),
|
|
);
|
|
for (const url of [
|
|
"/api/settings/password",
|
|
"/api/settings/sessions/revoke",
|
|
]) {
|
|
const ws = await app.injectWS("/api/test-socket", { headers: { cookie } });
|
|
const closed = new Promise((resolve) => ws.on("close", resolve));
|
|
const response = await app.inject({
|
|
method: "POST",
|
|
url,
|
|
headers: { cookie },
|
|
payload: url.endsWith("password")
|
|
? {
|
|
currentPassword: password,
|
|
newPassword: "changed test password",
|
|
confirmPassword: "changed test password",
|
|
}
|
|
: {},
|
|
});
|
|
expect(response.statusCode).toBe(204);
|
|
await closed;
|
|
await expect(
|
|
app.injectWS("/api/test-socket", { headers: { cookie } }),
|
|
).rejects.toThrow("401");
|
|
cookie = cookieOf(response);
|
|
}
|
|
});
|