Files
AgentHub/server/__tests__/server-s3-tools.test.js
T
nntrivi2001 84e2a9f0ac feat(server-s3): one Linux user per account (S3) — experimental
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>
2026-10-09 14:57:10 +07:00

149 lines
6.0 KiB
JavaScript

/**
* @file server-s3-tools.test.js
* @description S3/S4 plan, P5: what a publish copies under the shared tools
* root — a native executable alone, an npm package's whole folder, a script's
* own version folder — and never a shared `bin` folder; the `bin/<command>`
* link; an unchanged tool is not copied twice; everything readable by every
* account; the app scripts and the `agenthub` launcher for members' panes.
* @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(), "server-s3-tools-")));
process.env.AGENTHUB_TOOLS_ROOT = path.join(TMP, "opt", "tools");
process.env.AGENTHUB_APP_ROOT = path.join(TMP, "opt", "app");
const shared = require("../lib/shared-tools");
const write = (p, body, mode = 0o755) => {
fs.mkdirSync(path.dirname(p), { recursive: true });
fs.writeFileSync(p, body, { mode });
return p;
};
const ELF = Buffer.concat([Buffer.from([0x7f, 0x45, 0x4c, 0x46]), Buffer.alloc(64)]);
after(() => fs.rmSync(TMP, { recursive: true, force: true }));
describe("what a publish copies", () => {
it("a native executable: that file alone", () => {
const f = write(path.join(TMP, "home", ".local", "share", "x", "versions", "1.0"), ELF);
assert.deepEqual(shared.planFor(f), { src: f, entry: "" });
});
it("a file in an npm package: the package folder (scoped too)", () => {
const plain = write(
path.join(TMP, "npm", "lib", "node_modules", "opencode-ai", "bin", "oc.js"),
"#!/usr/bin/env node\n"
);
assert.deepEqual(shared.planFor(plain), {
src: path.join(TMP, "npm", "lib", "node_modules", "opencode-ai"),
entry: path.join("bin", "oc.js"),
});
const scoped = write(
path.join(TMP, "npm", "lib", "node_modules", "@github", "copilot", "index.js"),
"x"
);
assert.equal(
shared.planFor(scoped).src,
path.join(TMP, "npm", "lib", "node_modules", "@github", "copilot")
);
});
it("a script in a version folder: that folder; in a shared bin: refused", () => {
const s = write(
path.join(TMP, "home", ".local", "share", "cursor-agent", "versions", "v1", "cursor-agent"),
"#!/bin/sh\n"
);
assert.deepEqual(shared.planFor(s), { src: path.dirname(s), entry: "cursor-agent" });
const b = write(path.join(TMP, "home", ".local", "bin", "tool.sh"), "#!/bin/sh\n");
assert.throws(() => shared.planFor(b), { code: "ESHAREDLAYOUT" });
// ~/.tool/tool: its login usually sits beside it — never the whole folder.
const realHome = process.env.HOME;
process.env.HOME = path.join(TMP, "home");
try {
const dot = write(path.join(TMP, "home", ".tool", "tool"), "#!/bin/sh\n");
assert.throws(() => shared.planFor(dot), { code: "ESHAREDLAYOUT" });
} finally {
process.env.HOME = realHome;
}
});
});
describe("publishing an installed tool", () => {
let real;
before(() => {
real = write(
path.join(TMP, "home", ".local", "share", "claude", "versions", "2.1.0"),
ELF,
0o700
);
write(path.join(TMP, "home", ".local", "bin", "claude"), ""); // replaced by a link below
fs.rmSync(path.join(TMP, "home", ".local", "bin", "claude"));
fs.symlinkSync(real, path.join(TMP, "home", ".local", "bin", "claude"));
const aiTools = require("../lib/ai-tools");
aiTools.detect = () => ({
installed: true,
path: path.join(TMP, "home", ".local", "bin", "claude"),
});
aiTools.get = (id) => (id === "claude" ? { id: "claude", detect: ["claude"] } : null);
});
it("copies it once, links bin/<command>, readable by everyone", async () => {
const r = await shared.publish("claude");
assert.equal(r.copied, true);
const link = path.join(process.env.AGENTHUB_TOOLS_ROOT, "bin", "claude");
assert.equal(fs.realpathSync(link), r.path);
assert.equal(shared.sharedPath("claude"), link);
assert.equal(fs.statSync(r.path).mode & 0o777, 0o755);
const again = await shared.publish("claude");
assert.equal(again.copied, false);
assert.equal(again.path, r.path);
});
it("never copies a file whose name says it holds a login", async () => {
const dir = path.join(TMP, "home", ".local", "share", "cur", "versions", "v1");
const exe = write(path.join(dir, "cur"), "#!/bin/sh\n");
write(path.join(dir, "lib.js"), "x", 0o644);
write(path.join(dir, "auth.json"), "{}", 0o600);
const aiTools = require("../lib/ai-tools");
const [get, detect] = [aiTools.get, aiTools.detect];
aiTools.get = (id) => (id === "cur" ? { id: "cur", detect: ["cur"] } : get(id));
aiTools.detect = (id, o) => (id === "cur" ? { installed: true, path: exe } : detect(id, o));
try {
const r = await shared.publish("cur");
const copy = path.dirname(r.path);
assert.ok(fs.existsSync(path.join(copy, "lib.js")));
assert.ok(!fs.existsSync(path.join(copy, "auth.json")));
} finally {
[aiTools.get, aiTools.detect] = [get, detect];
}
});
it("never names a command outside a plain file name", () => {
assert.equal(shared.sharedPath("../etc/passwd"), null);
assert.equal(shared.sharedPath("nope"), null);
});
});
describe("the app scripts members run", () => {
it("are copied with an agenthub launcher", async () => {
const root = path.join(TMP, "install");
write(path.join(root, "scripts", "hook-handler.js"), "// hook\n", 0o600);
write(path.join(root, "bin", "agenthub.js"), "// cli\n", 0o700);
await shared.publishApp(root);
const hook = path.join(process.env.AGENTHUB_APP_ROOT, "scripts", "hook-handler.js");
assert.equal(fs.readFileSync(hook, "utf8"), "// hook\n");
assert.equal(fs.statSync(hook).mode & 0o777, 0o644);
const launcher = fs.readFileSync(
path.join(process.env.AGENTHUB_TOOLS_ROOT, "bin", "agenthub"),
"utf8"
);
assert.match(launcher, /bin\/node .*\/bin\/agenthub\.js "\$@"/);
});
});