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>
258 lines
10 KiB
JavaScript
258 lines
10 KiB
JavaScript
/**
|
|
* @file server-mode-proxies.test.js
|
|
* @description Personal AI Proxy profiles (S3 plan, P1): each user keeps
|
|
* their own beside the admin's shared ones. A member lists, edits, deletes and
|
|
* tests only their own; nobody else sees, edits or starts a session on them;
|
|
* only an admin writes a shared one; local mode is unchanged (shared only).
|
|
* Also `resolveProxy`/`resumeProxy`: a personal profile resolves only for a
|
|
* workspace its owner owns.
|
|
* @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.mkdtempSync(path.join(os.tmpdir(), "server-mode-proxies-"));
|
|
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.CLAUDE_HOME = path.join(TMP, ".claude");
|
|
process.env.CODEX_HOME = path.join(TMP, ".codex");
|
|
process.env.DASHBOARD_DATA_DIR = path.join(TMP, "data");
|
|
|
|
const { createApp, startServer } = require("../index");
|
|
const auth = require("../lib/auth");
|
|
const proxies = require("../lib/ai-proxies");
|
|
const { resolveProxy, resumeProxy } = require("../lib/proxy-apply");
|
|
const { db } = require("../db");
|
|
|
|
let server;
|
|
let BASE;
|
|
let mia;
|
|
let max;
|
|
|
|
async function call(method, p, { cookie, body } = {}) {
|
|
const res = await fetch(BASE + p, {
|
|
method,
|
|
headers: { "Content-Type": "application/json", ...(cookie ? { cookie } : {}) },
|
|
body: body ? JSON.stringify(body) : undefined,
|
|
});
|
|
return { status: res.status, body: await res.json().catch(() => null) };
|
|
}
|
|
|
|
async function login(username, password) {
|
|
const res = await fetch(BASE + "/api/auth/login", {
|
|
method: "POST",
|
|
headers: { "Content-Type": "application/json" },
|
|
body: JSON.stringify({ username, password }),
|
|
});
|
|
assert.equal(res.status, 200);
|
|
return res.headers.get("set-cookie").split(";")[0];
|
|
}
|
|
|
|
const profile = (label, extra = {}) => ({
|
|
label,
|
|
baseUrl: "http://127.0.0.1:9/v1",
|
|
apiKey: "sk-secret-" + label.replace(/\W/g, ""),
|
|
model: "m1",
|
|
...extra,
|
|
});
|
|
|
|
const namesOf = (r) => r.body.items.map((i) => `${i.scope}:${i.name}`);
|
|
|
|
before(async () => {
|
|
server = await startServer(createApp(), 0);
|
|
BASE = `http://127.0.0.1:${server.address().port}`;
|
|
mia = await auth.createUser("mia", "member-pass-1", "developer");
|
|
max = await auth.createUser("max", "member-pass-2", "developer");
|
|
await auth.createUser("boss", "admin-pass-1", "admin");
|
|
await auth.createUser("ana", "analyst-pass-1", "analyst");
|
|
});
|
|
|
|
after(() => {
|
|
delete process.env.AGENTHUB_MODE;
|
|
server?.close();
|
|
fs.rmSync(TMP, { recursive: true, force: true });
|
|
});
|
|
|
|
describe("local mode keeps one shared store", () => {
|
|
it("creates a shared profile with the plain slug name", async () => {
|
|
delete process.env.AGENTHUB_MODE;
|
|
const r = await call("POST", "/api/ai-proxies", { body: profile("Team Router") });
|
|
assert.equal(r.status, 201);
|
|
assert.equal(r.body.name, "team-router");
|
|
assert.equal(r.body.scope, "shared");
|
|
assert.equal(r.body.hasKey, true);
|
|
assert.equal(r.body.apiKey, undefined);
|
|
});
|
|
});
|
|
|
|
describe("server mode: personal profiles", () => {
|
|
before(() => {
|
|
process.env.AGENTHUB_MODE = "server";
|
|
});
|
|
|
|
it("gives a member's new profile a personal name and keeps it out of others' lists", async () => {
|
|
const cm = await login("mia", "member-pass-1");
|
|
const r = await call("POST", "/api/ai-proxies", { cookie: cm, body: profile("Mia Proxy") });
|
|
assert.equal(r.status, 201);
|
|
assert.equal(r.body.name, `u${mia.id}_mia-proxy`);
|
|
assert.equal(r.body.scope, "personal");
|
|
assert.equal(r.body.apiKey, undefined);
|
|
|
|
const mine = await call("GET", "/api/ai-proxies", { cookie: cm });
|
|
assert.deepEqual(namesOf(mine), [
|
|
"shared:native",
|
|
"shared:team-router",
|
|
`personal:u${mia.id}_mia-proxy`,
|
|
]);
|
|
assert.ok(!JSON.stringify(mine.body).includes("sk-secret"));
|
|
|
|
const cx = await login("max", "member-pass-2");
|
|
const theirs = await call("GET", "/api/ai-proxies", { cookie: cx });
|
|
assert.ok(!namesOf(theirs).some((n) => n.includes("mia")));
|
|
const boss = await login("boss", "admin-pass-1");
|
|
const admins = await call("GET", "/api/ai-proxies", { cookie: boss });
|
|
assert.ok(!namesOf(admins).some((n) => n.includes("mia")), "not even an admin lists it");
|
|
});
|
|
|
|
it("refuses a member a shared profile, and anyone else's personal one", async () => {
|
|
const cm = await login("mia", "member-pass-1");
|
|
const shared = await call("POST", "/api/ai-proxies", {
|
|
cookie: cm,
|
|
body: profile("Sneaky", { scope: "shared" }),
|
|
});
|
|
assert.equal(shared.status, 403);
|
|
assert.equal(shared.body.error.code, "EADMIN");
|
|
const edit = await call("PATCH", "/api/ai-proxies/team-router", {
|
|
cookie: cm,
|
|
body: { model: "m2" },
|
|
});
|
|
assert.equal(edit.status, 403);
|
|
const del = await call("DELETE", "/api/ai-proxies/team-router", { cookie: cm });
|
|
assert.equal(del.status, 403);
|
|
|
|
const cx = await login("max", "member-pass-2");
|
|
const name = `u${mia.id}_mia-proxy`;
|
|
const e2 = await call("PATCH", `/api/ai-proxies/${name}`, { cookie: cx, body: { model: "x" } });
|
|
assert.equal(e2.status, 404);
|
|
const d2 = await call("DELETE", `/api/ai-proxies/${name}`, { cookie: cx });
|
|
assert.equal(d2.status, 404);
|
|
const boss = await login("boss", "admin-pass-1");
|
|
const d3 = await call("DELETE", `/api/ai-proxies/${name}`, { cookie: boss });
|
|
assert.equal(d3.status, 404, "an admin does not manage someone's personal profile");
|
|
assert.ok(proxies.getProxy(name), "still there");
|
|
});
|
|
|
|
it("lets the owner edit and delete their own", async () => {
|
|
const cm = await login("mia", "member-pass-1");
|
|
const made = await call("POST", "/api/ai-proxies", { cookie: cm, body: profile("Temp One") });
|
|
const name = made.body.name;
|
|
const e = await call("PATCH", `/api/ai-proxies/${name}`, { cookie: cm, body: { model: "m9" } });
|
|
assert.equal(e.status, 200);
|
|
assert.equal(e.body.model, "m9");
|
|
const d = await call("DELETE", `/api/ai-proxies/${name}`, { cookie: cm });
|
|
assert.equal(d.status, 200);
|
|
});
|
|
|
|
it("never uses another user's stored key in a test", async () => {
|
|
const cx = await login("max", "member-pass-2");
|
|
const r = await call("POST", "/api/ai-proxies/test", {
|
|
cookie: cx,
|
|
body: { name: `u${mia.id}_mia-proxy`, baseUrl: "http://127.0.0.1:9", model: "m1" },
|
|
});
|
|
assert.equal(r.status, 400);
|
|
assert.equal(r.body.error.code, "EBADKEY");
|
|
});
|
|
|
|
it("lets an admin write shared profiles, and keep a personal one of their own", async () => {
|
|
const boss = await login("boss", "admin-pass-1");
|
|
const s = await call("POST", "/api/ai-proxies", { cookie: boss, body: profile("Ops Gateway") });
|
|
assert.equal(s.status, 201);
|
|
assert.equal(s.body.scope, "shared");
|
|
const p = await call("POST", "/api/ai-proxies", {
|
|
cookie: boss,
|
|
body: profile("Boss Own", { scope: "personal" }),
|
|
});
|
|
assert.equal(p.status, 201);
|
|
assert.equal(p.body.scope, "personal");
|
|
assert.match(p.body.name, /^u\d+_boss-own$/);
|
|
const cm = await login("mia", "member-pass-1");
|
|
const list = await call("GET", "/api/ai-proxies", { cookie: cm });
|
|
assert.ok(namesOf(list).includes("shared:ops-gateway"));
|
|
assert.ok(!namesOf(list).some((n) => n.includes("boss-own")));
|
|
});
|
|
|
|
it("lets an analyst keep a personal profile, never a shared one", async () => {
|
|
const ca = await login("ana", "analyst-pass-1");
|
|
const r = await call("POST", "/api/ai-proxies", { cookie: ca, body: profile("Ana Proxy") });
|
|
assert.equal(r.status, 201);
|
|
assert.equal(r.body.scope, "personal");
|
|
const s = await call("POST", "/api/ai-proxies", {
|
|
cookie: ca,
|
|
body: { ...profile("Ana Shared"), scope: "shared" },
|
|
});
|
|
assert.equal(s.status, 403);
|
|
assert.equal(s.body.error.code, "EADMIN");
|
|
});
|
|
});
|
|
|
|
describe("resolveProxy / resumeProxy: a personal profile only for its owner's workspace", () => {
|
|
it("resolves for the owner, refuses anyone else and an ownerless workspace", () => {
|
|
const name = `u${mia.id}_mia-proxy`;
|
|
assert.equal(resolveProxy("claude", name, mia.id).name, name);
|
|
assert.throws(() => resolveProxy("claude", name, max.id), { code: "EBADPROXY" });
|
|
assert.throws(() => resolveProxy("claude", name, null), { code: "EBADPROXY" });
|
|
assert.equal(resolveProxy("claude", "team-router", max.id).name, "team-router");
|
|
assert.equal(resolveProxy("claude", "team-router", null).name, "team-router");
|
|
});
|
|
|
|
it("resumes on a personal profile only for its owner, else on native", () => {
|
|
const name = `u${mia.id}_mia-proxy`;
|
|
db.prepare("INSERT INTO lanes (cwd, tool, owner_id) VALUES ('/w/p', 'claude', ?)").run(mia.id);
|
|
const laneId = db.prepare("SELECT id FROM lanes WHERE cwd = '/w/p'").get().id;
|
|
db.prepare(
|
|
"INSERT INTO dashboard_runs (id, lane_id, cwd, status, session_id, proxy) VALUES ('agenthub-lane-p', ?, '/w/p', 'exited', 'sess-proxy-1', ?)"
|
|
).run(laneId, name);
|
|
assert.equal(resumeProxy("sess-proxy-1", mia.id), name);
|
|
assert.equal(resumeProxy("sess-proxy-1", max.id), null);
|
|
});
|
|
});
|
|
|
|
describe("an edited profile reaches a live gateway pane (review fix)", () => {
|
|
it("rewrites a running pane's own settings file and deletes a dead one", () => {
|
|
process.env.AGENTHUB_MODE = "server";
|
|
const crypto = require("node:crypto");
|
|
const { proxyForSpawn, refreshProfileFiles } = require("../lib/proxy-apply");
|
|
const { name } = proxies.createProxy(profile("Refresh me"));
|
|
const live = "ahpane_live_token";
|
|
const fileLive = proxyForSpawn(
|
|
"claude",
|
|
proxies.getProxy(name),
|
|
live,
|
|
null,
|
|
"agenthub-lane-91"
|
|
).argv[0].slice("--settings=".length);
|
|
const fileDead = proxyForSpawn(
|
|
"claude",
|
|
proxies.getProxy(name),
|
|
"ahpane_dead",
|
|
null,
|
|
"agenthub-lane-92"
|
|
).argv[0].slice("--settings=".length);
|
|
const hash = crypto.createHash("sha256").update(live).digest("hex");
|
|
db.prepare(
|
|
"INSERT INTO dashboard_runs (id, cwd, mode, status, pane_token_hash) VALUES ('agenthub-lane-91', ?, 'interactive', 'running', ?)"
|
|
).run(TMP, hash);
|
|
proxies.updateProxy(name, { model: "m2" });
|
|
refreshProfileFiles(name);
|
|
const env = JSON.parse(fs.readFileSync(fileLive, "utf8")).env;
|
|
assert.equal(env.ANTHROPIC_MODEL, "m2");
|
|
assert.equal(env.ANTHROPIC_AUTH_TOKEN, live, "the pane keeps its own token");
|
|
assert.equal(fs.existsSync(fileDead), false, "a file whose pane is gone holds a dead token");
|
|
});
|
|
});
|