mirror of
https://github.com/paperclipai/paperclip.git
synced 2026-11-21 02:43:25 +01:00
## Thinking Path > - Paperclip is the open source app people use to manage AI agents for work > - In `local_trusted` mode, the operator's own local processes get implicit board access without credentials > - #21181 added a local board boundary. It refuses implicit board access to requests that come from an agent run's process tree > - On Linux CI hosts, the boundary sometimes did not decide a loopback request. It logged a warning and kept the earlier permissive behavior for that request > - The cause was not IPv6. IPv4 and IPv6 loopback lookups worked. The gap was a connection that belonged to a same-user process that the server cannot inspect > - This pull request makes the Linux boundary decide those connections from the process ancestry that the server can read, and refuse them when that ancestry is not clearly the operator's > - The benefit is that the local board boundary now holds on Linux for IPv4 and IPv6 loopback, and the operator's browser and CLI keep working ## Linked Issues or Issue Description Refs #21181 **What happened?** On Linux CI hosts, the local board boundary sometimes could not attribute a loopback request to a process. It then logged that it could not identify the local process and kept the earlier permissive behavior for that request. In one chat eval cohort, 37 of 191 server logs contained this warning. The warning is logged once per server process. **Expected behavior** The boundary decides every uncredentialed loopback request on Linux. A request from an agent run gets no implicit board access. Requests from the operator's browser and CLI keep implicit board access. **Actual behavior** Some requests on Linux were not decided by the boundary, and they got implicit board access. ## What Changed - The Linux inspector now records which same-user processes it cannot inspect. Before, it only recorded that one existed. - The Linux TCP table lookup now also returns the socket's owner UID. It covers `/proc/net/tcp` and `/proc/net/tcp6`, including IPv4-mapped addresses. - If no inspectable process owns the connection, the server reads the TCP tables again. If the connection is gone, has changed, or belongs to a different user, the request is refused. - If the connection is still open and same-user, the server checks every process it cannot inspect. Implicit board access stays only if each one has a readable parent chain that does not belong to an agent run. Otherwise the request is refused. - The "cannot identify" fallback now applies only when the platform has no inspector, or when Linux cannot read the TCP tables. The server keeps logging a warning in that case. - `doc/DEPLOYMENT-MODES.md` describes the new Linux behavior. **How the operator is recognized** - Browser and CLI processes are normal same-user processes. The server reads their open sockets and environment, so their requests are classified as before. - A browser helper process that the server cannot inspect is still the operator when its readable parent chain leads to the browser and has no agent-run markers. - The server's own requests and requests from the server's ancestors stay the operator, as before. - Connections owned by other users were refused before, and they are still refused. ## Verification - `pnpm --dir server exec vitest run src/__tests__/local-agent-process-boundary.test.ts` passes on macOS (15 passed, 10 Linux-only skipped). - The same file ran on Linux (Docker, `node:24-bookworm`, non-root user, IPv6 loopback on). Result: 25 passed. - Red check on Linux with the previous boundary and the new tests: the agent-run cases for processes the server cannot inspect failed for both `127.0.0.1` and `::1`. The operator cases passed. - New Linux-only tests run real processes over `127.0.0.1` and `::1`: - an operator process keeps board access - an agent-run process is refused - an operator process the server cannot inspect keeps board access - an agent-run process the server cannot inspect is refused - New platform-independent tests cover the classification rules and the `/proc/net/tcp6` fixture parsing. - A paid chat-feature run on Linux CI, with temporary diagnostic logging on a separate branch, checks the server logs for the warning and for refused operator requests. The results will be in a PR comment. ## Risks - A rare operator tool that the server cannot inspect, and whose parent chain the server cannot read, now loses implicit board access. Such a tool can use a board API key. - The extra TCP table read happens only when a connection has no inspectable owner. Normal requests do no extra work. - Platforms without process inspection keep the previous behavior. > For core feature work, check [`ROADMAP.md`](ROADMAP.md) first and discuss it in `#dev` before opening the PR. Feature PRs that overlap with planned core work may need to be redirected — check the roadmap first. See `CONTRIBUTING.md`. ## Model Used - Anthropic Claude Opus 5.5 (`claude-opus-5-5`) in Claude Code, with tool use (shell, file edits, Docker on Linux) and extended reasoning. ## Checklist - [x] I have included a thinking path that traces from project context to this change - [x] I have specified the model used (with version and capability details) - [x] I have checked ROADMAP.md and confirmed this PR does not duplicate planned core work - [x] I have searched GitHub for duplicate or related PRs and linked them above - [x] I have either (a) linked existing issues with `Fixes: #` / `Closes #` / `Refs #` OR (b) described the issue in-PR following the relevant issue template - [x] I have not referenced internal/instance-local Paperclip issues or links (only public GitHub `#NNN` / `github.com/paperclipai/paperclip` URLs) - [x] My branch name describes the change (e.g. `docs/...`, `fix/...`) and contains no internal Paperclip ticket id or instance-derived details - [x] I have run tests locally and they pass - [x] I have added or updated tests where applicable - [x] I have updated relevant documentation to reflect my changes - [x] I have considered and documented any risks above - [x] All Paperclip CI gates are green - [x] Greptile is 5/5 with no open P2s, recommendations, or follow-ups - [x] I will address all Greptile and reviewer comments before requesting merge 🤖 Generated with [Claude Code](https://claude.com/claude-code) --------- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
452 lines
18 KiB
TypeScript
452 lines
18 KiB
TypeScript
import { execFile } from "node:child_process";
|
|
import { readdir, readFile, readlink } from "node:fs/promises";
|
|
import path from "node:path";
|
|
import { logger } from "./logger.js";
|
|
|
|
export const AGENT_RUN_ENVIRONMENT_MARKERS = [
|
|
"PAPERCLIP_RUN_ID",
|
|
"PAPERCLIP_AGENT_ID",
|
|
"PAPERCLIP_AGENT_KEY_ID",
|
|
"PAPERCLIP_RUNNER_INSTANCE_ID",
|
|
] as const;
|
|
|
|
const RUNNER_DAEMON_EXECUTABLE = "paperclip-runnerd";
|
|
const MAX_ANCESTRY_DEPTH = 64;
|
|
const COMMAND_TIMEOUT_MS = 5_000;
|
|
|
|
export type LocalPeerClassification = "operator" | "agent_run" | "unsupported";
|
|
|
|
export interface LocalPeerSocket {
|
|
remoteAddress?: string | null;
|
|
remotePort?: number | null;
|
|
localAddress?: string | null;
|
|
localPort?: number | null;
|
|
}
|
|
|
|
export interface LocalPeerEndpoints {
|
|
remoteAddress: string;
|
|
remotePort: number;
|
|
localAddress: string;
|
|
localPort: number;
|
|
}
|
|
|
|
export interface LocalProcessInfo {
|
|
pid: number;
|
|
ppid: number;
|
|
executable: string | null;
|
|
environment: Map<string, string>;
|
|
}
|
|
|
|
export type LocalPeerOwner = number | null | "unsupported" | { candidates: number[] };
|
|
|
|
export interface LocalProcessInspector {
|
|
findPeerPids(sockets: readonly LocalPeerEndpoints[]): Promise<LocalPeerOwner[] | "unsupported">;
|
|
ancestries(pids: readonly number[], stopAt: ReadonlySet<number>): Promise<Map<number, LocalProcessInfo[] | null>>;
|
|
ancestorPids(pid: number): Promise<number[] | null>;
|
|
}
|
|
|
|
export function normalizeLoopbackAddress(address: string): string {
|
|
const lower = address.toLowerCase();
|
|
if (lower.startsWith("::ffff:") && lower.includes(".")) return lower.slice("::ffff:".length);
|
|
if (lower === "0:0:0:0:0:0:0:1") return "::1";
|
|
return lower;
|
|
}
|
|
|
|
function decodeProcNetAddress(hex: string): string | null {
|
|
if (hex.length === 8) {
|
|
const bytes = Buffer.from(hex, "hex");
|
|
return `${bytes[3]}.${bytes[2]}.${bytes[1]}.${bytes[0]}`;
|
|
}
|
|
if (hex.length !== 32) return null;
|
|
const bytes = Buffer.alloc(16);
|
|
for (let word = 0; word < 4; word += 1) {
|
|
const value = Buffer.from(hex.slice(word * 8, word * 8 + 8), "hex");
|
|
bytes[word * 4] = value[3]!;
|
|
bytes[word * 4 + 1] = value[2]!;
|
|
bytes[word * 4 + 2] = value[1]!;
|
|
bytes[word * 4 + 3] = value[0]!;
|
|
}
|
|
if (bytes.subarray(0, 10).every((byte) => byte === 0) && bytes[10] === 0xff && bytes[11] === 0xff) {
|
|
return `${bytes[12]}.${bytes[13]}.${bytes[14]}.${bytes[15]}`;
|
|
}
|
|
if (bytes.subarray(0, 15).every((byte) => byte === 0) && bytes[15] === 1) return "::1";
|
|
const groups: string[] = [];
|
|
for (let index = 0; index < 16; index += 2) groups.push(bytes.readUInt16BE(index).toString(16));
|
|
return groups.join(":");
|
|
}
|
|
|
|
export function findSocketInProcNet(
|
|
table: string,
|
|
peer: { address: string; port: number },
|
|
server: { address: string; port: number },
|
|
): { inode: string; uid: number } | null {
|
|
for (const line of table.split("\n").slice(1)) {
|
|
const fields = line.trim().split(/\s+/);
|
|
if (fields.length < 10) continue;
|
|
const [localHex, localPortHex] = fields[1]!.split(":");
|
|
const [remoteHex, remotePortHex] = fields[2]!.split(":");
|
|
if (!localHex || !localPortHex || !remoteHex || !remotePortHex) continue;
|
|
if (Number.parseInt(localPortHex, 16) !== peer.port || Number.parseInt(remotePortHex, 16) !== server.port) continue;
|
|
if (decodeProcNetAddress(localHex) !== peer.address || decodeProcNetAddress(remoteHex) !== server.address) continue;
|
|
const inode = fields[9];
|
|
if (inode && inode !== "0") return { inode, uid: Number(fields[7]) };
|
|
}
|
|
return null;
|
|
}
|
|
|
|
export function findPeerPidInDarwinNetstat(
|
|
output: string,
|
|
peer: { address: string; port: number },
|
|
server: { address: string; port: number },
|
|
): number | null | "unsupported" {
|
|
const lines = output.split("\n");
|
|
const header = lines.find((line) => line.includes("Local Address"));
|
|
if (!header || !header.includes("process:pid")) return "unsupported";
|
|
const local = `${peer.address}.${peer.port}`;
|
|
const remote = `${server.address}.${server.port}`;
|
|
for (const line of lines) {
|
|
const fields = line.trim().split(/\s+/);
|
|
if (fields.length < 7 || !fields[0]!.startsWith("tcp")) continue;
|
|
if (normalizeLoopbackAddress(fields[3]!) !== local || normalizeLoopbackAddress(fields[4]!) !== remote) continue;
|
|
for (const field of fields.slice(6)) {
|
|
const match = /:(\d+)$/.exec(field);
|
|
if (match) return Number(match[1]);
|
|
}
|
|
}
|
|
return null;
|
|
}
|
|
|
|
function parseEnvironmentBlock(entries: string[]): Map<string, string> {
|
|
const environment = new Map<string, string>();
|
|
for (const entry of entries) {
|
|
const separator = entry.indexOf("=");
|
|
if (separator <= 0) continue;
|
|
environment.set(entry.slice(0, separator), entry.slice(separator + 1));
|
|
}
|
|
return environment;
|
|
}
|
|
|
|
function runCommand(command: string, args: string[]): Promise<string | null> {
|
|
return new Promise((resolve) => {
|
|
execFile(command, args, { encoding: "utf8", timeout: COMMAND_TIMEOUT_MS, maxBuffer: 64 * 1024 * 1024 },
|
|
(error, stdout) => resolve(error ? null : stdout));
|
|
});
|
|
}
|
|
|
|
function linuxParentPid(stat: string): number {
|
|
return Number(stat.slice(stat.lastIndexOf(")") + 2).split(" ")[1]);
|
|
}
|
|
|
|
async function ownedByServerUser(pid: string): Promise<boolean> {
|
|
const status = await readFile(`/proc/${pid}/status`, "utf8").catch(() => "");
|
|
const realUid = /^Uid:\s+(\d+)/m.exec(status)?.[1];
|
|
return realUid !== undefined && Number(realUid) === process.getuid?.();
|
|
}
|
|
|
|
async function readProcNetTables(): Promise<string[] | null> {
|
|
const tables = await Promise.all(["/proc/net/tcp", "/proc/net/tcp6"].map((file) =>
|
|
readFile(file, "utf8").catch(() => null)));
|
|
const readable = tables.filter((table): table is string => table !== null);
|
|
return readable.length ? readable : null;
|
|
}
|
|
|
|
function findSocketInTables(tables: readonly string[], socket: LocalPeerEndpoints) {
|
|
const { peer, server } = endpoints(socket);
|
|
for (const table of tables) {
|
|
const found = findSocketInProcNet(table, peer, server);
|
|
if (found) return found;
|
|
}
|
|
return null;
|
|
}
|
|
|
|
function endpoints(socket: LocalPeerEndpoints) {
|
|
return {
|
|
peer: { address: normalizeLoopbackAddress(socket.remoteAddress), port: socket.remotePort },
|
|
server: { address: normalizeLoopbackAddress(socket.localAddress), port: socket.localPort },
|
|
};
|
|
}
|
|
|
|
async function linuxProcessInfo(pid: number): Promise<LocalProcessInfo | null> {
|
|
const stat = await readFile(`/proc/${pid}/stat`, "utf8").catch(() => null);
|
|
if (!stat) return null;
|
|
const [cmdline, environ] = await Promise.all([
|
|
readFile(`/proc/${pid}/cmdline`, "utf8").catch(() => ""),
|
|
readFile(`/proc/${pid}/environ`, "utf8").catch(() => ""),
|
|
]);
|
|
return {
|
|
pid,
|
|
ppid: linuxParentPid(stat),
|
|
executable: cmdline.split("\0")[0] || null,
|
|
environment: parseEnvironmentBlock(environ.split("\0")),
|
|
};
|
|
}
|
|
|
|
const linuxInspector: LocalProcessInspector = {
|
|
async findPeerPids(sockets) {
|
|
const tables = await readProcNetTables();
|
|
if (!tables) return "unsupported";
|
|
const found = sockets.map((socket) => findSocketInTables(tables, socket));
|
|
const wanted = new Set(found.flatMap((entry) => entry ? [`socket:[${entry.inode}]`] : []));
|
|
const owners = new Map<string, number>();
|
|
const hidden: number[] = [];
|
|
if (wanted.size) {
|
|
const others = (await readdir("/proc").catch(() => [] as string[]))
|
|
.filter((entry) => /^\d+$/.test(entry) && Number(entry) !== process.pid);
|
|
for (const pid of [String(process.pid), ...others]) {
|
|
if (owners.size === wanted.size) break;
|
|
const fds = await readdir(`/proc/${pid}/fd`).catch(async (error: NodeJS.ErrnoException) => {
|
|
if ((error.code === "EACCES" || error.code === "EPERM") && await ownedByServerUser(pid)) hidden.push(Number(pid));
|
|
return [] as string[];
|
|
});
|
|
for (const fd of fds) {
|
|
const link = await readlink(`/proc/${pid}/fd/${fd}`).catch(() => null);
|
|
if (link && wanted.has(link) && !owners.has(link)) owners.set(link, Number(pid));
|
|
}
|
|
}
|
|
}
|
|
const unowned = found.some((entry) => entry && !owners.has(`socket:[${entry.inode}]`));
|
|
const after = unowned && hidden.length ? await readProcNetTables() : null;
|
|
return found.map((entry, index): LocalPeerOwner => {
|
|
if (!entry) return null;
|
|
const owner = owners.get(`socket:[${entry.inode}]`);
|
|
if (owner !== undefined) return owner;
|
|
const current = after ? findSocketInTables(after, sockets[index]!) : null;
|
|
if (!current || current.inode !== entry.inode || entry.uid !== process.getuid?.()) return null;
|
|
return { candidates: hidden };
|
|
});
|
|
},
|
|
async ancestorPids(pid) {
|
|
const pids: number[] = [];
|
|
let current = pid;
|
|
for (let depth = 0; depth < MAX_ANCESTRY_DEPTH && current > 1; depth += 1) {
|
|
pids.push(current);
|
|
const stat = await readFile(`/proc/${current}/stat`, "utf8").catch(() => null);
|
|
if (!stat) return null;
|
|
current = linuxParentPid(stat);
|
|
if (!Number.isInteger(current)) return null;
|
|
}
|
|
return pids;
|
|
},
|
|
async ancestries(pids, stopAt) {
|
|
const known = new Map<number, LocalProcessInfo | null>();
|
|
const result = new Map<number, LocalProcessInfo[] | null>();
|
|
for (const pid of pids) {
|
|
const chain: LocalProcessInfo[] = [];
|
|
let current = pid;
|
|
for (let depth = 0; depth < MAX_ANCESTRY_DEPTH && current > 1 && !stopAt.has(current); depth += 1) {
|
|
if (!known.has(current)) known.set(current, await linuxProcessInfo(current));
|
|
const info = known.get(current);
|
|
if (!info) break;
|
|
chain.push(info);
|
|
current = info.ppid;
|
|
}
|
|
result.set(pid, chain.length || stopAt.has(pid) ? chain : null);
|
|
}
|
|
return result;
|
|
},
|
|
};
|
|
|
|
async function darwinParentTable(): Promise<Map<number, number> | null> {
|
|
const output = await runCommand("ps", ["-axo", "pid=,ppid="]);
|
|
if (output === null) return null;
|
|
const parents = new Map<number, number>();
|
|
for (const line of output.split("\n")) {
|
|
const [pid, ppid] = line.trim().split(/\s+/).map(Number);
|
|
if (Number.isInteger(pid) && Number.isInteger(ppid)) parents.set(pid!, ppid!);
|
|
}
|
|
return parents;
|
|
}
|
|
|
|
function darwinChain(parents: Map<number, number>, pid: number, stopAt: ReadonlySet<number>): number[] {
|
|
const chain: number[] = [];
|
|
let current = pid;
|
|
for (let depth = 0; depth < MAX_ANCESTRY_DEPTH && current > 1 && !stopAt.has(current); depth += 1) {
|
|
chain.push(current);
|
|
const parent = parents.get(current);
|
|
if (parent === undefined) break;
|
|
current = parent;
|
|
}
|
|
return chain;
|
|
}
|
|
|
|
export function parseDarwinProcessEnvironmentLine(command: string): {
|
|
executable: string | null;
|
|
environment: Map<string, string>;
|
|
} {
|
|
const tokens = command.trim().split(/\s+/);
|
|
const environment = new Map<string, string>();
|
|
for (const token of tokens.slice(1)) {
|
|
const match = /^([A-Za-z_][A-Za-z0-9_]*)=(.*)$/.exec(token);
|
|
if (match) environment.set(match[1]!, match[2]!);
|
|
}
|
|
return { executable: tokens[0] || null, environment };
|
|
}
|
|
|
|
const darwinInspector: LocalProcessInspector = {
|
|
async findPeerPids(sockets) {
|
|
const output = await runCommand("netstat", ["-anv", "-p", "tcp"]);
|
|
if (output === null) return "unsupported";
|
|
return sockets.map((socket) => {
|
|
const { peer, server } = endpoints(socket);
|
|
return findPeerPidInDarwinNetstat(output, peer, server);
|
|
});
|
|
},
|
|
async ancestorPids(pid) {
|
|
const parents = await darwinParentTable();
|
|
return parents ? darwinChain(parents, pid, new Set()) : null;
|
|
},
|
|
async ancestries(pids, stopAt) {
|
|
const result = new Map<number, LocalProcessInfo[] | null>();
|
|
const parents = await darwinParentTable();
|
|
if (!parents) {
|
|
for (const pid of pids) result.set(pid, null);
|
|
return result;
|
|
}
|
|
const chains = new Map(pids.map((pid) => [pid, darwinChain(parents, pid, stopAt)]));
|
|
const members = [...new Set([...chains.values()].flat())];
|
|
const details = new Map<number, ReturnType<typeof parseDarwinProcessEnvironmentLine>>();
|
|
if (members.length) {
|
|
const output = await runCommand("ps", ["-E", "-ww", "-o", "pid=,command=", "-p", members.join(",")]);
|
|
if (output === null) {
|
|
for (const pid of pids) result.set(pid, null);
|
|
return result;
|
|
}
|
|
for (const line of output.split("\n")) {
|
|
const match = /^\s*(\d+)\s+(.*)$/.exec(line);
|
|
if (match) details.set(Number(match[1]), parseDarwinProcessEnvironmentLine(match[2]!));
|
|
}
|
|
}
|
|
for (const [pid, chain] of chains) {
|
|
result.set(pid, chain.map((entry) => ({
|
|
pid: entry,
|
|
ppid: parents.get(entry) ?? 0,
|
|
executable: details.get(entry)?.executable ?? null,
|
|
environment: details.get(entry)?.environment ?? new Map(),
|
|
})));
|
|
}
|
|
return result;
|
|
},
|
|
};
|
|
|
|
export function defaultLocalProcessInspector(platform: NodeJS.Platform = process.platform): LocalProcessInspector | null {
|
|
if (platform === "linux") return linuxInspector;
|
|
if (platform === "darwin") return darwinInspector;
|
|
return null;
|
|
}
|
|
|
|
export function chainBelongsToAgentRun(
|
|
chain: readonly LocalProcessInfo[],
|
|
serverEnvironment: NodeJS.ProcessEnv,
|
|
): boolean {
|
|
return chain.some((entry, index) => {
|
|
if (entry.executable !== null && path.basename(entry.executable) === RUNNER_DAEMON_EXECUTABLE) return index > 0;
|
|
return AGENT_RUN_ENVIRONMENT_MARKERS.some((key) => {
|
|
const value = entry.environment.get(key);
|
|
return value !== undefined && value !== "" && value !== serverEnvironment[key];
|
|
});
|
|
});
|
|
}
|
|
|
|
export interface LocalAgentProcessBoundaryOptions {
|
|
inspector?: LocalProcessInspector | null;
|
|
serverPid?: number;
|
|
serverEnvironment?: NodeJS.ProcessEnv;
|
|
}
|
|
|
|
export function createLocalAgentProcessBoundary(options: LocalAgentProcessBoundaryOptions = {}) {
|
|
const inspector = options.inspector === undefined ? defaultLocalProcessInspector() : options.inspector;
|
|
const serverPid = options.serverPid ?? process.pid;
|
|
const serverEnvironment = options.serverEnvironment ?? process.env;
|
|
const classifications = new WeakMap<object, Promise<LocalPeerClassification>>();
|
|
let serverAncestors: Promise<ReadonlySet<number>> | null = null;
|
|
let warnedUnsupported = false;
|
|
let queued: { socket: LocalPeerEndpoints; resolve: (result: LocalPeerClassification) => void }[] = [];
|
|
let scanning = false;
|
|
|
|
function stopSet(): Promise<ReadonlySet<number>> {
|
|
serverAncestors ??= (inspector?.ancestorPids(serverPid) ?? Promise.resolve(null))
|
|
.then((pids) => new Set([serverPid, ...(pids ?? [])]));
|
|
return serverAncestors;
|
|
}
|
|
|
|
async function classifyBatch(sockets: readonly LocalPeerEndpoints[]): Promise<LocalPeerClassification[]> {
|
|
if (!inspector) return sockets.map(() => "unsupported");
|
|
const owners = await inspector.findPeerPids(sockets);
|
|
if (owners === "unsupported") return sockets.map(() => "unsupported");
|
|
const stopAt = await stopSet();
|
|
const pids = owners.flatMap((owner) =>
|
|
typeof owner === "number" ? [owner] : owner !== null && typeof owner === "object" ? owner.candidates : []);
|
|
const outside = [...new Set(pids.filter((pid) => !stopAt.has(pid)))];
|
|
const chains = outside.length ? await inspector.ancestries(outside, stopAt) : new Map<number, LocalProcessInfo[] | null>();
|
|
const operatorPid = (pid: number, minimumChain: number) => {
|
|
if (stopAt.has(pid)) return true;
|
|
const chain = chains.get(pid);
|
|
return Boolean(chain) && chain!.length >= minimumChain && !chainBelongsToAgentRun(chain!, serverEnvironment);
|
|
};
|
|
return owners.map((owner): LocalPeerClassification => {
|
|
if (owner === "unsupported") return "unsupported";
|
|
if (owner === null) return "agent_run";
|
|
if (typeof owner === "number") return operatorPid(owner, 0) ? "operator" : "agent_run";
|
|
return owner.candidates.length && owner.candidates.every((pid) => operatorPid(pid, 2)) ? "operator" : "agent_run";
|
|
});
|
|
}
|
|
|
|
async function drain() {
|
|
if (scanning) return;
|
|
scanning = true;
|
|
try {
|
|
while (queued.length) {
|
|
const batch = queued;
|
|
queued = [];
|
|
const results = await classifyBatch(batch.map((entry) => entry.socket)).catch((err) => {
|
|
logger.warn({ err }, "Could not identify the local process behind a request; refusing implicit board access");
|
|
return batch.map(() => "agent_run" as const);
|
|
});
|
|
batch.forEach((entry, index) => entry.resolve(results[index]!));
|
|
}
|
|
} finally {
|
|
scanning = false;
|
|
}
|
|
}
|
|
|
|
function classifyUncached(socket: LocalPeerSocket): Promise<LocalPeerClassification> {
|
|
if (!inspector) return Promise.resolve("unsupported");
|
|
const { remoteAddress, remotePort, localAddress, localPort } = socket;
|
|
if (!remoteAddress || !remotePort || !localAddress || !localPort) return Promise.resolve("agent_run");
|
|
return new Promise((resolve) => {
|
|
queued.push({ socket: { remoteAddress, remotePort, localAddress, localPort }, resolve });
|
|
void drain();
|
|
});
|
|
}
|
|
|
|
async function classify(socket: LocalPeerSocket & object): Promise<LocalPeerClassification> {
|
|
let pending = classifications.get(socket);
|
|
if (!pending) {
|
|
pending = classifyUncached(socket);
|
|
classifications.set(socket, pending);
|
|
}
|
|
const result = await pending;
|
|
if (result === "unsupported" && !warnedUnsupported) {
|
|
warnedUnsupported = true;
|
|
logger.warn("This platform cannot identify the local process behind a request, so implicit local board access is not limited to operator processes");
|
|
}
|
|
return result;
|
|
}
|
|
|
|
async function grantsImplicitBoard(socket: (LocalPeerSocket & object) | null | undefined): Promise<boolean> {
|
|
if (!socket) return false;
|
|
return await classify(socket) !== "agent_run";
|
|
}
|
|
|
|
return { classify, grantsImplicitBoard };
|
|
}
|
|
|
|
export type LocalAgentProcessBoundary = ReturnType<typeof createLocalAgentProcessBoundary>;
|
|
|
|
let sharedBoundary: LocalAgentProcessBoundary | null = null;
|
|
|
|
export function localAgentProcessBoundary(): LocalAgentProcessBoundary {
|
|
sharedBoundary ??= createLocalAgentProcessBoundary();
|
|
return sharedBoundary;
|
|
}
|