Files
plugins/packages/agent-console/daemon/src/security.ts
T
qingnian108 a431798fe1 agent-console 1.0.0: a page plugin that drives the local Agent CLIs (#535)
Adds Agent Console (@magpie-community/page-agent-console 1.0.0), a community page plugin that drives the Agent CLIs on this computer (Claude Code, Codex, ACP agents) through a local daemon on 127.0.0.1, with its source in packages/agent-console and a registry entry. Claude Code is the user's own `claude` CLI driven over stream-json (daemon/src/providers/claude/cli.ts); no Anthropic SDK is installed, bundled or in the branch's history. Cold install ~45 MB (node-pty ~26 MB), stated in the README and registry summary in en and zh.

By qingnian108 (#535).

What was run at 8e8f910 (mote, macOS, throwaway HOME, no real accounts):
- packages/agent-console: `npm ci --ignore-scripts --workspaces=false`; `node scripts/verify-build.mjs`: all five bundles byte-identical (daemon/dist/main.js 08c95aa3…, app/app.js 9a6911f3…); `vitest run`: 45 files, 254 passed, 17 skipped; `npm run check` clean.
- Real Claude Code 2.1.316 (npm @anthropic-ai/claude-code, sandbox HOME/CLAUDE_CONFIG_DIR, local fake Messages API): `AC_REAL_CLAUDE=… vitest run --config vitest.real.config.ts test/real/claude-cli.real.test.ts`: 4 of 4 (initialize lists 51 slash commands, streaming turn, Bash permission approved and denied, interrupt, --resume with set_model).
- Probe: the driver's exact argv with `--permission-mode bypassPermissions --allow-dangerously-skip-permissions`: the tool runs with no can_use_tool request.
- Repo root: `HOME=$(mktemp -d) bun test`: 4006 tests, 0 fail; `bun scripts/check.mjs` exit 0.
- Install as magpie does: `npm pack --ignore-scripts` (552 KB) then `bun add --ignore-scripts`: 45 MB node_modules, no @anthropic-ai/*, index.mjs imports.
- Read every changed line since 41c7a94: argv values limited to SAFE_ARG tokens (no cmd.exe metacharacters reach the shim path), no new network hosts, no install scripts, no eval; no commit in main..8e8f910 contains claude-agent-sdk.

Not tried: magpie's desktop window loading the page; a Windows .cmd/.ps1 claude shim end to end.
2026-10-31 20:11:22 +08:00

45 lines
2.5 KiB
TypeScript

// Host / Origin / token checks (timingSafeEqual).
//
// Security model (after the PR #535 review):
// - Host is checked on every HTTP request and WS upgrade and must be 127.0.0.1:<actual port> or localhost:<actual port>,
// to block DNS rebinding (requests from a rebound page carry no Origin, so only Host identifies them).
// - Origin: null (opaque origin) is not trusted. Any website can send Origin: null from a sandboxed iframe, a data: or a file: page,
// so null by itself means nothing. The token is the only authorization:
// * token routes (/api/*, /attachments/*, WS hello) let a null origin "present" the token,
// because Magpie's plugin page is a sandbox="allow-scripts" iframe and can only request as null;
// * static routes (/, app.js etc.) always 403 for null and send no CORS headers.
// - Any other website's Origin is refused, even with the correct token.
import { timingSafeEqual, createHash } from "node:crypto";
export type CheckResult = { ok: true } | { ok: false; code: number; message: string };
// none: no Origin (ordinary navigation, <script src>, non-browser local clients)
// self: this daemon's own origin (opened directly in a browser during development)
// opaque: Origin: null — untrusted, may only present the token on token routes
// foreign: any other origin — always refused
export type OriginClass = "none" | "self" | "opaque" | "foreign";
// Host must be 127.0.0.1:<port> or localhost:<port> (the IPv6 loopback form [::1] is not allowed, per the requirements).
// port must be the actual listening port (with --port 0, main.ts writes it back after listen).
export function checkHost(host: string | undefined, port: number): CheckResult {
if (!host) return { ok: false, code: 421, message: "Host 不对" };
const h = host.toLowerCase();
if (h === `127.0.0.1:${port}` || h === `localhost:${port}`) return { ok: true };
return { ok: false, code: 421, message: "Host 不对" };
}
export function classifyOrigin(origin: string | undefined, port: number): OriginClass {
if (origin === undefined || origin === "") return "none";
if (origin === "null") return "opaque";
const o = origin.toLowerCase();
if (o === `http://127.0.0.1:${port}` || o === `http://localhost:${port}`) return "self";
return "foreign";
}
export function tokenEqual(a: string, b: string): boolean {
// Constant-time compare against timing attacks; different lengths are hashed to the same length first.
const ha = createHash("sha256").update(a).digest();
const hb = createHash("sha256").update(b).digest();
return timingSafeEqual(ha, hb);
}