UNPKG

@code-yeongyu/senpi

Version:

Coding agent CLI with read, bash, edit, write tools and session management

174 lines • 6.59 kB
/** * The loopback credential proxy (senpi#6820). A child calls `http://127.0.0.1:<port>/<capability>/<env name>/<path>`; * the proxy forwards to that credential's fixed origin with the header set from the value it holds. Security rules: * the upstream host never comes from the request, redirects are returned rather than followed (a followed redirect * would carry the header to another host), and no error text the child can read includes the value. */ import { randomBytes, timingSafeEqual } from "node:crypto"; import { createServer } from "node:http"; import { Readable } from "node:stream"; import { hasUnsafePathSegment } from "./declarations.js"; const MAX_REQUEST_BYTES = 64 * 1024 * 1024; const DROPPED_REQUEST_HEADERS = new Set([ "accept-encoding", "connection", "content-length", "host", "keep-alive", "proxy-authorization", "proxy-connection", "te", "trailer", "transfer-encoding", "upgrade", ]); const DROPPED_RESPONSE_HEADERS = new Set([ "connection", "content-encoding", "content-length", "keep-alive", "transfer-encoding", ]); function refuse(response, status, reason) { if (response.headersSent) { response.destroy(); return; } response.writeHead(status, { "content-type": "text/plain; charset=utf-8" }); response.end(`senpi credential proxy: ${reason}\n`); } function sameCapability(given, expected) { const bytes = Buffer.from(given); return bytes.length === expected.length && timingSafeEqual(bytes, expected); } async function readBody(request) { if (request.method === "GET" || request.method === "HEAD") return undefined; const chunks = []; let size = 0; for await (const chunk of request) { const buffer = Buffer.isBuffer(chunk) ? chunk : Buffer.from(String(chunk)); size += buffer.length; if (size > MAX_REQUEST_BYTES) throw new RangeError("request body too large"); chunks.push(buffer); } const body = new Uint8Array(size); let offset = 0; for (const chunk of chunks) { body.set(chunk, offset); offset += chunk.length; } return body; } function forwardedHeaders(request, credential) { const headers = new Headers(); for (const [name, value] of Object.entries(request.headers)) { const lower = name.toLowerCase(); if (value === undefined || DROPPED_REQUEST_HEADERS.has(lower) || lower === credential.declaration.header) continue; for (const item of Array.isArray(value) ? value : [value]) headers.append(lower, item); } const { scheme, header } = credential.declaration; headers.set(header, scheme === undefined ? credential.value : `${scheme} ${credential.value}`); headers.set("accept-encoding", "identity"); return headers; } function allowedPath(path, credential) { if (hasUnsafePathSegment(path)) return false; const prefixes = credential.declaration.pathPrefixes; return (prefixes === undefined || prefixes.some((prefix) => path === prefix || path.startsWith(prefix.endsWith("/") ? prefix : `${prefix}/`))); } async function relay(upstream, response) { const headers = {}; upstream.headers.forEach((value, name) => { if (DROPPED_RESPONSE_HEADERS.has(name)) return; (headers[name] ??= []).push(value); }); response.writeHead(upstream.status, headers); if (upstream.body === null) { response.end(); return; } await new Promise((resolve, reject) => { const body = Readable.fromWeb(upstream.body); body.on("error", reject); response.on("close", () => { body.destroy(); resolve(); }); body.pipe(response).on("finish", resolve); }); } function handler(capability, byEnv) { return async (request, response) => { const target = request.url ?? ""; if (!target.startsWith("/")) return refuse(response, 400, "only origin-form requests are accepted"); const queryAt = target.indexOf("?"); const path = queryAt === -1 ? target : target.slice(0, queryAt); const query = queryAt === -1 ? "" : target.slice(queryAt); const [, given = "", env = "", ...rest] = path.split("/"); const credential = byEnv.get(env); if (!sameCapability(given, capability) || credential === undefined) return refuse(response, 403, "unknown route"); const upstreamPath = `/${rest.join("/")}`; if (!allowedPath(upstreamPath, credential)) return refuse(response, 403, "path not allowed for this credential"); let body; try { body = await readBody(request); } catch { return refuse(response, 413, "request body too large"); } let upstream; try { upstream = await fetch(`${credential.declaration.origin}${upstreamPath === "/" ? "" : upstreamPath}${query}`, { method: request.method ?? "GET", headers: forwardedHeaders(request, credential), redirect: "manual", ...(body === undefined ? {} : { body }), }); } catch { return refuse(response, 502, "upstream request failed"); } try { await relay(upstream, response); } catch { response.destroy(); } }; } /** Binds the proxy on loopback; rejects when it cannot listen, so the caller never falls back to passing values. */ export async function startCredentialProxy(credentials) { const capabilityText = randomBytes(32).toString("base64url"); const capability = Buffer.from(capabilityText); const byEnv = new Map(credentials.map((credential) => [credential.declaration.env, credential])); const server = createServer((request, response) => { void handler(capability, byEnv)(request, response); }); await new Promise((resolve, reject) => { server.once("error", reject); server.listen(0, "127.0.0.1", () => { server.off("error", reject); resolve(); }); }); server.unref(); const { port } = server.address(); return { routeFor: (env) => (byEnv.has(env) ? `http://127.0.0.1:${port}/${capabilityText}/${env}` : undefined), close: () => new Promise((resolve) => { server.close(() => resolve()); server.closeAllConnections(); }), }; } //# sourceMappingURL=proxy.js.map