84e2a9f0ac
Server mode can now run each account as its own Linux user (ah-<name>) when `agenthub server install-system` has set S3 up. The dashboard reaches a member only through sudo -u / write-as / pane-launch, reads a member's files only through owner- and type-checked opens, and hooks are authenticated by peer uid. Local mode and server mode without S3 are unchanged. EXPERIMENTAL: tested with fake sudo and fake accounts only. It has not yet been verified on a real install with real member accounts, sudo or a real Codex login. Do not offer it to real users yet. - S3 core: provisioning, per-member tmux servers, the proxy key gateway, shared tools in /opt, dropped capabilities for children (P0–P5) - P4: plan and quota per member home - P6: ~/.claude.json seed; opt-in plugin baseline installed as the member; managed-settings spike script (not run) - P7: Codex account pool (`agenthub accounts add --tool codex`) - Roles merged with main's admin / analyst / developer: analysts write only their own rows, under S3 as well - Per-account prefs: one implementation (Chat UI + onboarding) - Security reviews F1–F49 (five rounds), each fix with a regression test: symlink/TOCTOU-safe member reads, no host writes in a member home, credentials and the data dir never served, owner and folder account must agree, pane tokens bound to the pane's uid, bounded sudo/tmux/git calls and member reads (no hang or crash from a member), async route rejections answer 500 instead of exiting - Docs: docs/SERVER-MODE.md (S3 model, accepted ceilings, operator runbook), docs/plans/server-mode/S3-PLAN.md, CLAUDE.md S3 rules Tests: server 2838/2841 (3 need 127.0.0.1:3000, held on this box); client 1162/1162; tsc clean; headers check passes. Co-Authored-By: Claude <noreply@anthropic.com>
137 lines
5.5 KiB
JavaScript
137 lines
5.5 KiB
JavaScript
/**
|
|
* @file me-prefs.test.js
|
|
* @description `/api/me` (docs/plans/onboarding): an account's own UI
|
|
* preferences and first-run flag — local mode is the one account `local`;
|
|
* in server mode each account reads and writes only its own row, only known
|
|
* keys and short strings are kept, `null` deletes one; an S3 member's
|
|
* dark/light mode becomes their Claude Code theme, written as them.
|
|
* @author Nguyễn Ngọc Trí Vĩ <vinnt@smartgift.vn>
|
|
*/
|
|
|
|
const { describe, it, before, after } = require("node:test");
|
|
const assert = require("node:assert/strict");
|
|
const path = require("node:path");
|
|
const fs = require("node:fs");
|
|
const os = require("node:os");
|
|
|
|
const TMP = fs.realpathSync(fs.mkdtempSync(path.join(os.tmpdir(), "me-prefs-")));
|
|
const USERS = path.join(TMP, "users");
|
|
process.env.DASHBOARD_DB_PATH = path.join(TMP, "dashboard.db");
|
|
process.env.DASHBOARD_LIVENESS_PROBE = "0";
|
|
process.env.LANES_ROOT = path.join(TMP, "lanes");
|
|
process.env.AGENTHUB_SERVER_CONF = path.join(TMP, "server.conf");
|
|
process.env.AGENTHUB_PASSWD = path.join(TMP, "passwd");
|
|
|
|
const { createApp, startServer } = require("../index");
|
|
const auth = require("../lib/auth");
|
|
const linux = require("../lib/linux-accounts");
|
|
const runAs = require("../lib/run-as");
|
|
const { fakeSudo } = require("./fixtures/fake-sudo");
|
|
|
|
let server;
|
|
let BASE;
|
|
|
|
const call = (method, p, { body, cookie } = {}) =>
|
|
fetch(BASE + p, {
|
|
method,
|
|
headers: { "Content-Type": "application/json", ...(cookie ? { cookie } : {}) },
|
|
body: body ? JSON.stringify(body) : undefined,
|
|
}).then(async (r) => ({ status: r.status, body: await r.json().catch(() => null) }));
|
|
|
|
async function login(username, password) {
|
|
const r = await fetch(`${BASE}/api/auth/login`, {
|
|
method: "POST",
|
|
headers: { "Content-Type": "application/json" },
|
|
body: JSON.stringify({ username, password }),
|
|
});
|
|
return r.headers.get("set-cookie").split(";")[0];
|
|
}
|
|
|
|
before(async () => {
|
|
server = await startServer(createApp(), 0);
|
|
BASE = `http://127.0.0.1:${server.address().port}`;
|
|
});
|
|
|
|
after(() => {
|
|
delete process.env.AGENTHUB_MODE;
|
|
server?.close();
|
|
fs.rmSync(TMP, { recursive: true, force: true });
|
|
});
|
|
|
|
describe("local mode: one account", () => {
|
|
it("starts empty, keeps known keys, deletes on null, refuses the rest", async () => {
|
|
assert.deepEqual((await call("GET", "/api/me/prefs")).body, { prefs: {}, onboardedAt: null });
|
|
let r = await call("PUT", "/api/me/prefs", {
|
|
body: { prefs: { theme: "light", "agenthub.theme.light": "canvas" } },
|
|
});
|
|
assert.equal(r.status, 200);
|
|
r = await call("PUT", "/api/me/prefs", { body: { prefs: { theme: null } } });
|
|
assert.deepEqual(r.body.prefs, { "agenthub.theme.light": "canvas" });
|
|
for (const prefs of [{ evil: "x" }, { theme: 1 }, { theme: "x".repeat(9000) }, []]) {
|
|
r = await call("PUT", "/api/me/prefs", { body: { prefs } });
|
|
assert.equal(r.status, 400, JSON.stringify(prefs));
|
|
assert.equal(r.body.error.code, "EBADPREFS");
|
|
}
|
|
});
|
|
|
|
it("marks the first-run setup done, once", async () => {
|
|
const r = await call("POST", "/api/me/onboarded");
|
|
assert.match(r.body.onboardedAt, /^\d{4}-/);
|
|
const again = await call("POST", "/api/me/onboarded");
|
|
assert.equal(again.body.onboardedAt, r.body.onboardedAt);
|
|
});
|
|
});
|
|
|
|
describe("server mode: per account", () => {
|
|
let mia;
|
|
let max;
|
|
before(async () => {
|
|
const uid = process.getuid();
|
|
fs.writeFileSync(process.env.AGENTHUB_PASSWD, `ah-mia:x:${uid}:${uid}::/:/bin/sh\n`);
|
|
fs.writeFileSync(
|
|
process.env.AGENTHUB_SERVER_CONF,
|
|
`HOST_USER=h\nHOST_UID=1\nUSERS_ROOT=${USERS}\nPORT=4820\n`
|
|
);
|
|
linux.__resetConfCache();
|
|
runAs.__resetCache();
|
|
linux.__setRunImpl(() => "ok");
|
|
process.env.AGENTHUB_MODE = "server";
|
|
await auth.createUser("mia", "member-pass-1", "developer");
|
|
await auth.createUser("max", "member-pass-2", "developer");
|
|
fs.mkdirSync(path.join(USERS, "mia"), { recursive: true });
|
|
mia = await login("mia", "member-pass-1");
|
|
max = await login("max", "member-pass-2");
|
|
});
|
|
|
|
it("needs a login", async () => {
|
|
assert.equal((await call("GET", "/api/me/prefs")).status, 401);
|
|
});
|
|
|
|
it("keeps each account's own, apart from local mode's and each other's", async () => {
|
|
await call("PUT", "/api/me/prefs", { cookie: mia, body: { prefs: { i18nextLng: "vi" } } });
|
|
await call("PUT", "/api/me/prefs", { cookie: max, body: { prefs: { i18nextLng: "en" } } });
|
|
assert.deepEqual((await call("GET", "/api/me/prefs", { cookie: mia })).body.prefs, {
|
|
i18nextLng: "vi",
|
|
});
|
|
const m = (await call("GET", "/api/me/prefs", { cookie: max })).body;
|
|
assert.deepEqual(m, { prefs: { i18nextLng: "en" }, onboardedAt: null });
|
|
});
|
|
|
|
it("puts an S3 member's mode into their Claude Code theme, as them", async () => {
|
|
const sudo = fakeSudo();
|
|
runAs.__setExecImpl(sudo.exec);
|
|
const settings = path.join(USERS, "mia", ".claude", "settings.json");
|
|
fs.mkdirSync(path.dirname(settings), { recursive: true });
|
|
fs.writeFileSync(settings, JSON.stringify({ model: "opus" }));
|
|
await call("PUT", "/api/me/prefs", { cookie: mia, body: { prefs: { theme: "light" } } });
|
|
assert.deepEqual(JSON.parse(fs.readFileSync(settings, "utf8")), {
|
|
model: "opus",
|
|
theme: "light",
|
|
});
|
|
assert.ok(sudo.calls.some((c) => c.includes("ah-mia") && c.includes(settings)));
|
|
// "system" is the browser's to resolve: Claude's file is left as it is.
|
|
await call("PUT", "/api/me/prefs", { cookie: mia, body: { prefs: { theme: "system" } } });
|
|
assert.equal(JSON.parse(fs.readFileSync(settings, "utf8")).theme, "light");
|
|
});
|
|
});
|