mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-26 22:57:32 +00:00
feat(plugins): sandbox hardening — OS permission model, egress choke point, bound acting user, author signatures
Closes the four gaps the security review surfaced: - OS permission model on the prod plugin child (Node --permission with fs-read scoped to the compiled server dir + the plugin's own code dir, no fs-write/child_process/worker/native). A plugin can no longer read trek.db or the .jwt_secret/.encryption_key files, nor shell out — the direct-fs and RCE escapes that bypassed the RPC layer. Opt-out via TREK_PLUGIN_PERMISSIONS=off. - Egress guard extended from fetch to the net.Socket connect choke point, so node:http/https/net/tls obey the declared-egress allowlist too (no declared egress = no outbound). Under the permission model there is no clean escape to an unwrapped runtime. Kernel/network-namespace containment remains the container step. - Trip reads are membership-checked against the acting user the HOST binds from the authenticated invocation, not an asUserId the plugin supplies; a job/onLoad (no user) can't read user-scoped trips. - Optional minisign (Ed25519) author signatures verified offline, TOFU-pinned (migration 158). Unsigned plugins install on sha256 alone; a signed plugin can't silently drop its signature or swap its author key. Server suite green (permission-model activation verified against the Koffi reference plugin on dev1).
This commit is contained in:
@@ -3370,6 +3370,13 @@ function runMigrations(db: Database.Database): void {
|
||||
() => {
|
||||
try { db.exec("ALTER TABLE plugins ADD COLUMN capabilities TEXT NOT NULL DEFAULT '{}';"); } catch (err) { console.warn('[migrations] Non-fatal migration step failed:', err); }
|
||||
},
|
||||
// Migration 158: TOFU pin for a plugin's author signing key (#plugins, #4).
|
||||
// Set on first install of a signed plugin; a later install whose registry key
|
||||
// differs is a hard stop (author change / key rotation / attack) unless an
|
||||
// admin re-trusts. NULL for unsigned plugins (signing is opt-in).
|
||||
() => {
|
||||
try { db.exec("ALTER TABLE plugins ADD COLUMN author_pubkey TEXT;"); } catch (err) { console.warn('[migrations] Non-fatal migration step failed:', err); }
|
||||
},
|
||||
];
|
||||
|
||||
if (currentVersion < migrations.length) {
|
||||
|
||||
@@ -41,7 +41,7 @@ export interface HostDeps {
|
||||
broadcastToUser(userId: number, payload: Record<string, unknown>): void;
|
||||
}
|
||||
|
||||
type Handler = (params: Record<string, unknown>) => unknown;
|
||||
type Handler = (params: Record<string, unknown>, actingUserId: number | undefined) => unknown;
|
||||
|
||||
const num = (v: unknown, name: string): number => {
|
||||
const n = typeof v === 'string' ? Number(v) : v;
|
||||
@@ -71,14 +71,14 @@ export class PluginRpcHost {
|
||||
}
|
||||
|
||||
if (has('db:read:trips')) {
|
||||
this.methods.set('trips.getById', (p) =>
|
||||
this.tripRead(p, () => deps.db.prepare('SELECT * FROM trips WHERE id = ?').get(num(p.tripId, 'tripId'))),
|
||||
this.methods.set('trips.getById', (p, uid) =>
|
||||
this.tripRead(p, uid, () => deps.db.prepare('SELECT * FROM trips WHERE id = ?').get(num(p.tripId, 'tripId'))),
|
||||
);
|
||||
this.methods.set('trips.getPlaces', (p) =>
|
||||
this.tripRead(p, () => deps.db.prepare('SELECT * FROM places WHERE trip_id = ? ORDER BY day_id, position').all(num(p.tripId, 'tripId'))),
|
||||
this.methods.set('trips.getPlaces', (p, uid) =>
|
||||
this.tripRead(p, uid, () => deps.db.prepare('SELECT * FROM places WHERE trip_id = ? ORDER BY day_id, position').all(num(p.tripId, 'tripId'))),
|
||||
);
|
||||
this.methods.set('trips.getReservations', (p) =>
|
||||
this.tripRead(p, () => deps.db.prepare('SELECT * FROM reservations WHERE trip_id = ? ORDER BY reservation_time').all(num(p.tripId, 'tripId'))),
|
||||
this.methods.set('trips.getReservations', (p, uid) =>
|
||||
this.tripRead(p, uid, () => deps.db.prepare('SELECT * FROM reservations WHERE trip_id = ? ORDER BY reservation_time').all(num(p.tripId, 'tripId'))),
|
||||
);
|
||||
}
|
||||
|
||||
@@ -104,17 +104,25 @@ export class PluginRpcHost {
|
||||
}
|
||||
}
|
||||
|
||||
/** Membership-check every trip read against the acting user; forbid otherwise. */
|
||||
private tripRead(p: Record<string, unknown>, read: () => unknown): unknown {
|
||||
/**
|
||||
* Membership-check every trip read against the acting user. The acting user is
|
||||
* bound by the HOST from the authenticated invocation (see the supervisor's
|
||||
* invocation map) — NOT taken from a plugin-supplied `asUserId`, which a plugin
|
||||
* could set to any id to read another user's trips. If no acting user is bound
|
||||
* (a job / onLoad, or a forged call), the read is forbidden.
|
||||
*/
|
||||
private tripRead(p: Record<string, unknown>, actingUserId: number | undefined, read: () => unknown): unknown {
|
||||
const tripId = num(p.tripId, 'tripId');
|
||||
const asUserId = num(p.asUserId, 'asUserId');
|
||||
if (!this.deps.canAccessTrip(tripId, asUserId)) {
|
||||
if (actingUserId === undefined) {
|
||||
throw new ForbiddenResource('trip reads require an authenticated user context');
|
||||
}
|
||||
if (!this.deps.canAccessTrip(tripId, actingUserId)) {
|
||||
throw new ForbiddenResource(`no access to trip ${tripId}`);
|
||||
}
|
||||
return read();
|
||||
}
|
||||
|
||||
async dispatch(req: RpcRequest): Promise<RpcResponse | RpcError> {
|
||||
async dispatch(req: RpcRequest, actingUserId?: number): Promise<RpcResponse | RpcError> {
|
||||
const handler = this.methods.get(req.method);
|
||||
if (!handler) {
|
||||
const known = (KNOWN_METHODS as readonly string[]).includes(req.method as KnownMethod);
|
||||
@@ -128,7 +136,7 @@ export class PluginRpcHost {
|
||||
}
|
||||
const params = (req.params ?? {}) as Record<string, unknown>;
|
||||
try {
|
||||
const result = await handler(params);
|
||||
const result = await handler(params, actingUserId);
|
||||
return { k: 'res', id: req.id, ok: true, result };
|
||||
} catch (e) {
|
||||
if (e instanceof BadParams) return this.err(req.id, 'BAD_PARAMS', e.message);
|
||||
|
||||
@@ -0,0 +1,87 @@
|
||||
import crypto from 'node:crypto';
|
||||
|
||||
/**
|
||||
* Author-signature verification for the plugin installer (#plugins, #4).
|
||||
*
|
||||
* sha256 pinning proves the bytes match what the REGISTRY vouches for; a whoever
|
||||
* controls the registry repo controls that. A minisign (Ed25519) author
|
||||
* signature proves the bytes were signed by the holder of the plugin AUTHOR's
|
||||
* private key — so a compromised registry cannot ship attacker code under an
|
||||
* author's name without also stealing that key. TREK verifies offline with
|
||||
* node:crypto (no Fulcio/Rekor phone-home), consistent with the "no telemetry
|
||||
* beyond the update check" stance.
|
||||
*
|
||||
* Signing is OPT-IN: an entry without an author key + signature installs exactly
|
||||
* as before (sha256 only). When both are present, verification is mandatory and
|
||||
* a mismatch aborts the install.
|
||||
*/
|
||||
|
||||
// SPKI DER prefix for a raw 32-byte Ed25519 public key (RFC 8410).
|
||||
const ED25519_SPKI_PREFIX = Buffer.from('302a300506032b6570032100', 'hex');
|
||||
|
||||
export class SignatureError extends Error {}
|
||||
|
||||
/** Wrap a raw 32-byte Ed25519 public key as a node KeyObject. */
|
||||
function ed25519KeyFromRaw(raw: Buffer): crypto.KeyObject {
|
||||
if (raw.length !== 32) throw new SignatureError('public key is not a 32-byte Ed25519 key');
|
||||
return crypto.createPublicKey({ key: Buffer.concat([ED25519_SPKI_PREFIX, raw]), format: 'der', type: 'spki' });
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse a minisign public key. Accepts either the two-line minisign format
|
||||
* (comment line + base64 payload) or a bare base64 payload. The 10-byte payload
|
||||
* header is `Ed` + an 8-byte key id; the trailing 32 bytes are the Ed25519 key.
|
||||
* A bare 32-byte base64 key (44 chars) is also accepted for simple deployments.
|
||||
*/
|
||||
function parseMinisignPubKey(pub: string): { key: crypto.KeyObject; keyId: Buffer | null } {
|
||||
const line = pub.trim().split(/\r?\n/).map((l) => l.trim()).filter((l) => l && !l.startsWith('untrusted comment')).pop();
|
||||
if (!line) throw new SignatureError('empty public key');
|
||||
const buf = Buffer.from(line, 'base64');
|
||||
if (buf.length === 32) return { key: ed25519KeyFromRaw(buf), keyId: null };
|
||||
if (buf.length === 42 && buf.subarray(0, 2).toString('latin1') === 'Ed') {
|
||||
return { key: ed25519KeyFromRaw(buf.subarray(10)), keyId: buf.subarray(2, 10) };
|
||||
}
|
||||
throw new SignatureError('unrecognized public key format');
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse a minisign signature (.minisig). The relevant line is the first base64
|
||||
* payload: 2-byte algorithm (`Ed` legacy | `ED` prehashed) + 8-byte key id +
|
||||
* 64-byte Ed25519 signature. A bare 64-byte base64 signature (legacy over the
|
||||
* raw bytes) is also accepted.
|
||||
*/
|
||||
function parseMinisignSignature(sig: string): { algo: 'Ed' | 'ED' | 'raw'; keyId: Buffer | null; signature: Buffer } {
|
||||
const line = sig.trim().split(/\r?\n/).map((l) => l.trim()).filter((l) => l && !l.startsWith('untrusted comment')).shift();
|
||||
if (!line) throw new SignatureError('empty signature');
|
||||
const buf = Buffer.from(line, 'base64');
|
||||
if (buf.length === 64) return { algo: 'raw', keyId: null, signature: buf };
|
||||
if (buf.length === 74) {
|
||||
const algo = buf.subarray(0, 2).toString('latin1');
|
||||
if (algo !== 'Ed' && algo !== 'ED') throw new SignatureError(`unsupported signature algorithm ${algo}`);
|
||||
return { algo, keyId: buf.subarray(2, 10), signature: buf.subarray(10) };
|
||||
}
|
||||
throw new SignatureError('unrecognized signature format');
|
||||
}
|
||||
|
||||
/**
|
||||
* Verify that `bytes` were signed by the holder of `publicKeyB64`'s private key.
|
||||
* Supports minisign legacy (`Ed`, signs the raw file) and prehashed (`ED`, signs
|
||||
* BLAKE2b-512 of the file), plus bare raw Ed25519 over the file bytes. Returns
|
||||
* true on a valid signature, throws SignatureError on a malformed input, and
|
||||
* returns false on a well-formed but non-matching signature.
|
||||
*/
|
||||
export function verifyAuthorSignature(bytes: Buffer, signatureB64: string, publicKeyB64: string): boolean {
|
||||
const { key, keyId } = parseMinisignPubKey(publicKeyB64);
|
||||
const { algo, keyId: sigKeyId, signature } = parseMinisignSignature(signatureB64);
|
||||
|
||||
// If both carry a key id, they must match the same key (catches a wrong-key mixup).
|
||||
if (keyId && sigKeyId && !keyId.equals(sigKeyId)) return false;
|
||||
|
||||
const message =
|
||||
algo === 'ED' ? crypto.createHash('blake2b512').update(bytes).digest() : bytes;
|
||||
try {
|
||||
return crypto.verify(null, message, key, signature);
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -45,7 +45,7 @@ export function pluginDbFile(id: string): string {
|
||||
* so no `.js` sibling exists — fall back to the `.ts` source loaded via tsx (a
|
||||
* prod dependency). This keeps the fork path identical in tests and production.
|
||||
*/
|
||||
export function resolveChildEntry(): { entry: string; execArgv: string[]; forkCwd?: string } {
|
||||
export function resolveChildEntry(): { entry: string; execArgv: string[]; forkCwd?: string; jsMode: boolean } {
|
||||
const js = path.join(__dirname, 'runtime', 'plugin-host-entry.js');
|
||||
const ts = path.join(__dirname, 'runtime', 'plugin-host-entry.ts');
|
||||
if (!fs.existsSync(js) && fs.existsSync(ts)) {
|
||||
@@ -53,8 +53,43 @@ export function resolveChildEntry(): { entry: string; execArgv: string[]; forkCw
|
||||
// must run from a dir where `tsx` is on the node_modules chain (the server
|
||||
// root) — NOT the plugin dir. The plugin itself is still loaded by absolute
|
||||
// path via createRequire, so this doesn't weaken the prod isolation, where
|
||||
// the .js branch below keeps cwd at the plugin dir.
|
||||
return { entry: ts, execArgv: ['--import', 'tsx'], forkCwd: process.cwd() };
|
||||
// the .js branch below keeps cwd at the plugin dir. tsx compiles TS on the
|
||||
// fly and walks node_modules, so the OS-level permission model can't be
|
||||
// applied here — it is a prod-only hardening (jsMode).
|
||||
return { entry: ts, execArgv: ['--import', 'tsx'], forkCwd: process.cwd(), jsMode: false };
|
||||
}
|
||||
return { entry: js, execArgv: ['--max-old-space-size=192'] };
|
||||
return { entry: js, execArgv: ['--max-old-space-size=192'], jsMode: true };
|
||||
}
|
||||
|
||||
/**
|
||||
* The compiled server code root (…/dist), computed from this module's location.
|
||||
* The plugin child needs read access to it to load its bootstrap + SDK; it must
|
||||
* NOT be granted the data root (trek.db, .jwt_secret, .encryption_key live there).
|
||||
*/
|
||||
export function serverCodeRoot(): string {
|
||||
// __dirname = …/dist/nest/plugins → …/dist
|
||||
return path.resolve(__dirname, '../..');
|
||||
}
|
||||
|
||||
/**
|
||||
* Node OS-level permission-model flags for a plugin child (#plugins, #2 security
|
||||
* hardening). `--permission` denies filesystem write, child_process, worker
|
||||
* threads and native addons outright; `--allow-fs-read` is scoped to exactly the
|
||||
* code the child must load (the compiled server dir + this plugin's own code
|
||||
* dir). Result: the child can no longer read trek.db / the secret files or shell
|
||||
* out, closing the direct-filesystem and RCE escapes that bypass the RPC layer.
|
||||
* Empty (opt-out) when TREK_PLUGIN_PERMISSIONS=off.
|
||||
*/
|
||||
export function pluginPermissionArgs(pluginId: string): string[] {
|
||||
if ((process.env.TREK_PLUGIN_PERMISSIONS ?? 'on').toLowerCase() === 'off') return [];
|
||||
const codeRoot = serverCodeRoot();
|
||||
return [
|
||||
'--permission',
|
||||
`--allow-fs-read=${codeRoot}`,
|
||||
// The server package.json sits one level above dist and Node reads it to
|
||||
// resolve the child's module type. Grant just that file — never its dir,
|
||||
// which also holds the `data` symlink to trek.db and the secret files.
|
||||
`--allow-fs-read=${path.join(codeRoot, '..', 'package.json')}`,
|
||||
`--allow-fs-read=${pluginCodeDir(pluginId)}`,
|
||||
];
|
||||
}
|
||||
|
||||
@@ -128,8 +128,8 @@ export class PluginRuntimeService implements OnModuleInit, OnModuleDestroy {
|
||||
routesOf(id: string): PluginRouteInfo[] {
|
||||
return this.supervisor.routesOf(id);
|
||||
}
|
||||
invoke(id: string, method: string, params: Record<string, unknown>): Promise<unknown> {
|
||||
return this.supervisor.invoke(id, method, params);
|
||||
invoke(id: string, method: string, params: Record<string, unknown>, actingUserId?: number): Promise<unknown> {
|
||||
return this.supervisor.invoke(id, method, params, { actingUserId });
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -48,16 +48,23 @@ export class PluginsProxyController {
|
||||
}
|
||||
|
||||
try {
|
||||
const reply = (await this.runtime.invoke(pluginId, 'invoke.route', {
|
||||
routeId: route.i,
|
||||
req: {
|
||||
method: req.method,
|
||||
path: sub,
|
||||
query: req.query,
|
||||
body: req.body ?? null,
|
||||
user: user ? { id: user.id, username: user.username, isAdmin: !!user.is_admin } : null,
|
||||
const reply = (await this.runtime.invoke(
|
||||
pluginId,
|
||||
'invoke.route',
|
||||
{
|
||||
routeId: route.i,
|
||||
req: {
|
||||
method: req.method,
|
||||
path: sub,
|
||||
query: req.query,
|
||||
body: req.body ?? null,
|
||||
user: user ? { id: user.id, username: user.username, isAdmin: !!user.is_admin } : null,
|
||||
},
|
||||
},
|
||||
})) as { status?: number; headers?: Record<string, string>; body?: unknown };
|
||||
// Bind the authenticated session user as the acting user for any trip
|
||||
// reads this invocation makes — the plugin cannot override it.
|
||||
user?.id,
|
||||
)) as { status?: number; headers?: Record<string, string>; body?: unknown };
|
||||
|
||||
res.status(reply?.status ?? 200);
|
||||
for (const [k, v] of Object.entries(reply?.headers ?? {})) {
|
||||
|
||||
@@ -4,6 +4,7 @@ import path from 'node:path';
|
||||
import { db } from '../../../db/database';
|
||||
import { pluginCodeDir, pluginsCodeRoot, pluginsDataRoot } from '../paths';
|
||||
import { safeDownload, sha256Matches } from '../install/safe-fetch';
|
||||
import { verifyAuthorSignature } from '../install/verify-signature';
|
||||
import { extractArchive } from '../install/safe-extract';
|
||||
import { scanForNativeBinaries } from '../install/native-scan';
|
||||
import { parseManifest } from '../install/manifest';
|
||||
@@ -36,6 +37,8 @@ interface RegistryVersion {
|
||||
apiVersion?: number;
|
||||
nativeModules?: boolean;
|
||||
publishedAt?: string;
|
||||
/** base64 minisign signature (.minisig payload) over the artifact bytes. */
|
||||
signature?: string;
|
||||
}
|
||||
export interface RegistryEntry {
|
||||
id: string;
|
||||
@@ -47,6 +50,8 @@ export interface RegistryEntry {
|
||||
tags?: string[];
|
||||
type: string;
|
||||
reviewedAt?: string | null;
|
||||
/** base64 minisign author public key — stable across versions, TOFU-pinned. */
|
||||
authorPublicKey?: string;
|
||||
versions: RegistryVersion[];
|
||||
}
|
||||
interface Registry {
|
||||
@@ -160,6 +165,11 @@ export class PluginRegistryService {
|
||||
const { bytes, sha256 } = await safeDownload(ver.downloadUrl, (ver.size ?? 50 * 1024 * 1024) + 4096);
|
||||
if (!sha256Matches(sha256, ver.sha256)) throw new RegistryError('integrity check failed (sha256 mismatch)');
|
||||
|
||||
// 2b. author signature (opt-in) + TOFU key pin. Unsigned plugins skip this
|
||||
// and install on sha256 alone (unchanged behaviour); a signed plugin must
|
||||
// verify, and its author key must match the one pinned on first install.
|
||||
this.verifySignatureAndTofu(id, bytes, entry, ver);
|
||||
|
||||
// 3. zip/tar-slip-safe extract to staging
|
||||
const staging = path.join(pluginsDataRoot(), '.staging', `${id}-${ver.version}-${Date.now()}`);
|
||||
try {
|
||||
@@ -183,11 +193,44 @@ export class PluginRegistryService {
|
||||
db.prepare('UPDATE plugins SET source_repo = ?, source_commit = ?, sha256 = ?, reviewed_at = ? WHERE id = ?').run(
|
||||
entry.repo, ver.commitSha, ver.sha256, entry.reviewedAt ?? null, id,
|
||||
);
|
||||
// Pin the author key on first successful install of a signed plugin (TOFU).
|
||||
if (entry.authorPublicKey) {
|
||||
db.prepare('UPDATE plugins SET author_pubkey = ? WHERE id = ?').run(entry.authorPublicKey, id);
|
||||
}
|
||||
return { id, version: ver.version };
|
||||
} finally {
|
||||
fs.rmSync(staging, { recursive: true, force: true });
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Verify the author signature (if the entry declares a key + the version a
|
||||
* signature) and enforce Trust-On-First-Use on the author key.
|
||||
*
|
||||
* - Neither key nor signature → unsigned plugin, skip (opt-in).
|
||||
* - Key present but no signature (or vice versa) → hard stop; a half-signed
|
||||
* entry is a misconfiguration we refuse rather than silently downgrade.
|
||||
* - Signature invalid → hard stop.
|
||||
* - Registry key differs from the key pinned on a prior install → hard stop
|
||||
* (author change / rotation / attack; needs an explicit admin re-trust).
|
||||
*/
|
||||
private verifySignatureAndTofu(id: string, bytes: Buffer, entry: RegistryEntry, ver: RegistryVersion): void {
|
||||
const pinned = (db.prepare('SELECT author_pubkey FROM plugins WHERE id = ?').get(id) as { author_pubkey?: string } | undefined)?.author_pubkey ?? null;
|
||||
|
||||
if (!entry.authorPublicKey && !ver.signature) {
|
||||
if (pinned) throw new RegistryError('this plugin was signed before but the update is unsigned — refusing');
|
||||
return; // unsigned throughout: sha256 is the only pin
|
||||
}
|
||||
if (!entry.authorPublicKey || !ver.signature) {
|
||||
throw new RegistryError('incomplete signature: an author key and a version signature must both be present');
|
||||
}
|
||||
if (pinned && pinned !== entry.authorPublicKey) {
|
||||
throw new RegistryError("the plugin's author signing key changed since it was installed — re-trust it explicitly to continue");
|
||||
}
|
||||
if (!verifyAuthorSignature(bytes, ver.signature, entry.authorPublicKey)) {
|
||||
throw new RegistryError('author signature verification failed');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function rawFileUrl(repo: string, commitSha: string, file: string): string {
|
||||
|
||||
@@ -11,6 +11,7 @@
|
||||
*/
|
||||
|
||||
import path from 'node:path';
|
||||
import net from 'node:net';
|
||||
import { createRequire } from 'node:module';
|
||||
import { createPluginContext, type ChildTransport, type PluginContext, type PluginDefinition } from './plugin-sdk';
|
||||
import type { Envelope, RpcError } from '../protocol/envelope';
|
||||
@@ -18,6 +19,8 @@ import type { Envelope, RpcError } from '../protocol/envelope';
|
||||
const pluginId = process.argv[2] || process.env.TREK_PLUGIN_ID || 'unknown';
|
||||
const pluginDir = process.argv[3] || '';
|
||||
|
||||
let pluginConfig: Record<string, unknown> = {};
|
||||
|
||||
const pending = new Map<string, { resolve: (v: unknown) => void; reject: (e: Error) => void }>();
|
||||
let seq = 0;
|
||||
|
||||
@@ -73,18 +76,21 @@ async function handleInvoke(req: { id: string; method: string; params: Record<st
|
||||
);
|
||||
try {
|
||||
if (!def || !ctx) throw new Error('plugin not loaded');
|
||||
// A per-invocation ctx tagged with this invoke's id, so the host binds trip
|
||||
// reads to the invocation's authenticated user (routes) / refuses them (jobs).
|
||||
const invCtx = createPluginContext(pluginId, pluginConfig, transport, req.id);
|
||||
if (req.method === 'invoke.route') {
|
||||
const routeId = req.params.routeId as number;
|
||||
const route = def.routes?.[routeId];
|
||||
if (!route) throw new Error(`no route ${routeId}`);
|
||||
const pluginReq = req.params.req as Parameters<NonNullable<typeof route.handler>>[0];
|
||||
const result = await route.handler(pluginReq, ctx);
|
||||
const result = await route.handler(pluginReq, invCtx);
|
||||
respond(true, result);
|
||||
} else if (req.method === 'invoke.job') {
|
||||
const jobId = req.params.jobId as string;
|
||||
const job = def.jobs?.find((j) => j.id === jobId);
|
||||
if (!job) throw new Error(`no job ${jobId}`);
|
||||
await job.handler(ctx);
|
||||
await job.handler(invCtx);
|
||||
respond(true, { ok: true });
|
||||
} else {
|
||||
respond(false, `unknown invoke ${req.method}`);
|
||||
@@ -127,17 +133,26 @@ process.on('message', (raw: unknown) => {
|
||||
if (msg.k === 'evt') {
|
||||
if (msg.topic === 'init') {
|
||||
const d = msg.data as { config?: Record<string, unknown>; egress?: string[] };
|
||||
pluginConfig = d.config ?? {};
|
||||
installEgressGuard(d.egress ?? []);
|
||||
void boot(d.config ?? {});
|
||||
void boot(pluginConfig);
|
||||
} else if (msg.topic === 'shutdown') void shutdown();
|
||||
}
|
||||
});
|
||||
|
||||
/**
|
||||
* Restrict the plugin's outbound network to its declared egress hosts. With no
|
||||
* declared egress, ALL outbound fetch is blocked. Wildcards like `*.host` match
|
||||
* any subdomain. This is process-level defense in depth; the container runtime
|
||||
* (v2) enforces it at the network layer.
|
||||
* declared egress, ALL outbound is blocked. Wildcards like `*.host` match any
|
||||
* subdomain.
|
||||
*
|
||||
* Two layers, so a plugin can't just sidestep `fetch` with a raw socket:
|
||||
* 1. `globalThis.fetch` is wrapped (undici path).
|
||||
* 2. `net.Socket.prototype.connect` is wrapped — the single TCP choke point that
|
||||
* node:http / node:https / node:net / node:tls all funnel through — so
|
||||
* `require('node:https').request(...)` is subject to the same allowlist.
|
||||
* Under the OS permission model the child also cannot spawn a fresh process or
|
||||
* load a native addon to escape these wrappers. This is strong defense in depth;
|
||||
* a kernel/network-namespace guarantee still belongs to the container runtime.
|
||||
*/
|
||||
function installEgressGuard(egress: string[]): void {
|
||||
const patterns = egress.map((h) => h.trim().toLowerCase()).filter(Boolean);
|
||||
@@ -145,19 +160,42 @@ function installEgressGuard(egress: string[]): void {
|
||||
const h = hostname.toLowerCase();
|
||||
return patterns.some((p) => (p.startsWith('*.') ? h === p.slice(2) || h.endsWith(p.slice(1)) : h === p));
|
||||
};
|
||||
|
||||
const realFetch = globalThis.fetch;
|
||||
if (typeof realFetch !== 'function') return;
|
||||
globalThis.fetch = ((input: unknown, init?: unknown) => {
|
||||
const url = typeof input === 'string' ? input : (input as { url?: string })?.url ?? String(input);
|
||||
let host: string;
|
||||
try {
|
||||
host = new URL(url).hostname;
|
||||
} catch {
|
||||
return Promise.reject(new Error('egress: invalid url'));
|
||||
if (typeof realFetch === 'function') {
|
||||
globalThis.fetch = ((input: unknown, init?: unknown) => {
|
||||
const url = typeof input === 'string' ? input : (input as { url?: string })?.url ?? String(input);
|
||||
let host: string;
|
||||
try {
|
||||
host = new URL(url).hostname;
|
||||
} catch {
|
||||
return Promise.reject(new Error('egress: invalid url'));
|
||||
}
|
||||
if (!allowed(host)) return Promise.reject(new Error(`egress: ${host} is not in the plugin's declared hosts`));
|
||||
return (realFetch as (i: unknown, n: unknown) => Promise<unknown>)(input, init);
|
||||
}) as typeof fetch;
|
||||
}
|
||||
|
||||
// TCP choke point: http/https/net/tls all end up here. A unix-socket (path)
|
||||
// connect is local IPC, not network egress, so it's left alone.
|
||||
const proto = net.Socket.prototype as unknown as { connect: (...a: unknown[]) => unknown };
|
||||
const realConnect = proto.connect;
|
||||
proto.connect = function (this: unknown, ...args: unknown[]): unknown {
|
||||
const first = args[0];
|
||||
let host: string | undefined;
|
||||
if (first && typeof first === 'object') {
|
||||
const o = first as { host?: string; path?: string };
|
||||
if (o.path) return realConnect.apply(this, args); // unix socket — local
|
||||
host = o.host ?? 'localhost';
|
||||
} else if (typeof first === 'number' || typeof first === 'string') {
|
||||
// connect(port[, host]) — a numeric-only path connects to localhost.
|
||||
host = typeof args[1] === 'string' ? (args[1] as string) : 'localhost';
|
||||
}
|
||||
if (!allowed(host)) return Promise.reject(new Error(`egress: ${host} is not in the plugin's declared hosts`));
|
||||
return (realFetch as (i: unknown, n: unknown) => Promise<unknown>)(input, init);
|
||||
}) as typeof fetch;
|
||||
if (host && !allowed(host)) {
|
||||
throw new Error(`egress: ${host} is not in the plugin's declared hosts`);
|
||||
}
|
||||
return realConnect.apply(this, args);
|
||||
};
|
||||
}
|
||||
|
||||
// Ask the host for the init payload (instance config), then wait for it.
|
||||
|
||||
@@ -17,9 +17,14 @@ export interface PluginContext {
|
||||
migrate(id: string, sql: string): Promise<{ applied: boolean }>;
|
||||
};
|
||||
trips: {
|
||||
getById(tripId: number, asUserId: number): Promise<unknown>;
|
||||
getPlaces(tripId: number, asUserId: number): Promise<unknown[]>;
|
||||
getReservations(tripId: number, asUserId: number): Promise<unknown[]>;
|
||||
// `asUserId` is accepted for source compatibility but IGNORED by the host —
|
||||
// trip reads are always membership-checked against the authenticated user of
|
||||
// the current invocation (the request's `req.user`), which the plugin cannot
|
||||
// override. Only reachable from a route handler (a user context); a job has
|
||||
// no user and its trip reads are refused.
|
||||
getById(tripId: number, asUserId?: number): Promise<unknown>;
|
||||
getPlaces(tripId: number, asUserId?: number): Promise<unknown[]>;
|
||||
getReservations(tripId: number, asUserId?: number): Promise<unknown[]>;
|
||||
};
|
||||
users: {
|
||||
getById(id: number): Promise<unknown>;
|
||||
@@ -77,11 +82,18 @@ export interface ChildTransport {
|
||||
emit(topic: string, data: unknown): void;
|
||||
}
|
||||
|
||||
/** Build the ctx the plugin's handlers receive — every method is an RPC call. */
|
||||
/**
|
||||
* Build the ctx the plugin's handlers receive — every method is an RPC call.
|
||||
* `invocationId` (the host's reqId for the route/job currently being handled) is
|
||||
* attached to trip reads as `_inv` so the host can bind the acting user to THIS
|
||||
* invocation. It is undefined for the load-time ctx (onLoad), where trip reads
|
||||
* have no user and are refused.
|
||||
*/
|
||||
export function createPluginContext(
|
||||
id: string,
|
||||
config: Record<string, unknown>,
|
||||
t: ChildTransport,
|
||||
invocationId?: string,
|
||||
): PluginContext {
|
||||
return {
|
||||
id,
|
||||
@@ -92,9 +104,9 @@ export function createPluginContext(
|
||||
migrate: (mid, sql) => t.rpc('db.migrate', { id: mid, sql }) as Promise<{ applied: boolean }>,
|
||||
},
|
||||
trips: {
|
||||
getById: (tripId, asUserId) => t.rpc('trips.getById', { tripId, asUserId }),
|
||||
getPlaces: (tripId, asUserId) => t.rpc('trips.getPlaces', { tripId, asUserId }) as Promise<unknown[]>,
|
||||
getReservations: (tripId, asUserId) => t.rpc('trips.getReservations', { tripId, asUserId }) as Promise<unknown[]>,
|
||||
getById: (tripId) => t.rpc('trips.getById', { tripId, _inv: invocationId }),
|
||||
getPlaces: (tripId) => t.rpc('trips.getPlaces', { tripId, _inv: invocationId }) as Promise<unknown[]>,
|
||||
getReservations: (tripId) => t.rpc('trips.getReservations', { tripId, _inv: invocationId }) as Promise<unknown[]>,
|
||||
},
|
||||
users: {
|
||||
getById: (uid) => t.rpc('users.getById', { id: uid }),
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { randomUUID } from 'node:crypto';
|
||||
import { fork, type ChildProcess } from 'node:child_process';
|
||||
import { resolveChildEntry, pluginCodeDir } from '../paths';
|
||||
import { resolveChildEntry, pluginCodeDir, pluginPermissionArgs } from '../paths';
|
||||
import type { Envelope, RpcError, RpcRequest } from '../protocol/envelope';
|
||||
import type { PluginRpcHost } from '../host/rpc-host';
|
||||
|
||||
@@ -48,6 +48,7 @@ interface Supervised {
|
||||
routes: PluginRouteInfo[];
|
||||
jobs: string[];
|
||||
pending: Map<string, Pending>; // host→child invokes awaiting a response
|
||||
invocations: Map<string, number | undefined>; // reqId -> acting user of that invoke (undefined = no user, e.g. a job)
|
||||
activation?: { resolve: () => void; reject: (e: Error) => void };
|
||||
}
|
||||
|
||||
@@ -96,6 +97,7 @@ export class PluginSupervisor {
|
||||
routes: [],
|
||||
jobs: [],
|
||||
pending: new Map(),
|
||||
invocations: new Map(),
|
||||
};
|
||||
this.running.set(id, sup);
|
||||
this.ensureSweep();
|
||||
@@ -126,8 +128,20 @@ export class PluginSupervisor {
|
||||
return this.running.get(id)?.routes ?? [];
|
||||
}
|
||||
|
||||
/** Send a host→child request (invoke a route or job) and await its response. */
|
||||
invoke(id: string, method: string, params: Record<string, unknown>, timeoutMs = 30_000): Promise<unknown> {
|
||||
/**
|
||||
* Send a host→child request (invoke a route or job) and await its response.
|
||||
* `actingUserId` binds the authenticated user of this invocation on the HOST
|
||||
* side: any trip read the child makes while handling it is membership-checked
|
||||
* against THIS user, never against an id the plugin passes. A job carries no
|
||||
* user (undefined) and therefore cannot read user-scoped data.
|
||||
*/
|
||||
invoke(
|
||||
id: string,
|
||||
method: string,
|
||||
params: Record<string, unknown>,
|
||||
opts: { timeoutMs?: number; actingUserId?: number } = {},
|
||||
): Promise<unknown> {
|
||||
const { timeoutMs = 30_000, actingUserId } = opts;
|
||||
const sup = this.running.get(id);
|
||||
if (!sup || sup.status !== 'active' || !sup.child) {
|
||||
return Promise.reject(new Error(`plugin ${id} is not active`));
|
||||
@@ -136,11 +150,15 @@ export class PluginSupervisor {
|
||||
const reqId = randomUUID();
|
||||
const timer = setTimeout(() => {
|
||||
sup.pending.delete(reqId);
|
||||
sup.invocations.delete(reqId);
|
||||
reject(new Error('plugin invoke timed out'));
|
||||
}, timeoutMs);
|
||||
timer.unref?.();
|
||||
sup.pending.set(reqId, { resolve, reject, timer });
|
||||
sup.child!.send({ k: 'req', id: reqId, method, params } satisfies Envelope);
|
||||
// The child echoes this reqId as `_inv` on its trip reads; the host resolves
|
||||
// the acting user from here, so the plugin cannot name an arbitrary user.
|
||||
sup.invocations.set(reqId, actingUserId);
|
||||
sup.child!.send({ k: 'req', id: reqId, method, params: { ...params, _inv: reqId } } satisfies Envelope);
|
||||
});
|
||||
}
|
||||
|
||||
@@ -156,10 +174,13 @@ export class PluginSupervisor {
|
||||
// ── internals ──────────────────────────────────────────────────────────────
|
||||
|
||||
private spawn(sup: Supervised): void {
|
||||
const { entry, execArgv, forkCwd } = resolveChildEntry();
|
||||
const { entry, execArgv, forkCwd, jsMode } = resolveChildEntry();
|
||||
// Prod (compiled) children get the OS permission model — a real kernel-level
|
||||
// fs/child_process/native jail on top of the env scrub and RPC boundary.
|
||||
const argv = jsMode ? [...execArgv, ...pluginPermissionArgs(sup.id)] : execArgv;
|
||||
const child = fork(entry, [sup.id, pluginCodeDir(sup.id)], {
|
||||
cwd: forkCwd ?? pluginCodeDir(sup.id),
|
||||
execArgv,
|
||||
execArgv: argv,
|
||||
stdio: ['ignore', 'pipe', 'pipe', 'ipc'],
|
||||
// Whitelist env — nothing inherited. No JWT_SECRET, no DB creds, no PATH-leaked secrets.
|
||||
env: {
|
||||
@@ -183,8 +204,13 @@ export class PluginSupervisor {
|
||||
if (!msg || typeof msg !== 'object') return;
|
||||
|
||||
if (msg.k === 'req') {
|
||||
// A ctx.* call from the plugin — dispatch through its capability host.
|
||||
const res = await sup.rpcHost.dispatch(msg as RpcRequest);
|
||||
// A ctx.* call from the plugin — dispatch through its capability host. The
|
||||
// acting user comes from the invocation the child is currently handling
|
||||
// (its `_inv` reqId → our invocation map), NOT from anything the plugin can
|
||||
// set in the call params.
|
||||
const inv = (msg as RpcRequest).params as { _inv?: unknown } | undefined;
|
||||
const actingUserId = typeof inv?._inv === 'string' ? sup.invocations.get(inv._inv) : undefined;
|
||||
const res = await sup.rpcHost.dispatch(msg as RpcRequest, actingUserId);
|
||||
sup.child?.send(res);
|
||||
return;
|
||||
}
|
||||
@@ -194,6 +220,7 @@ export class PluginSupervisor {
|
||||
const p = sup.pending.get(msg.id);
|
||||
if (!p) return;
|
||||
sup.pending.delete(msg.id);
|
||||
sup.invocations.delete(msg.id);
|
||||
clearTimeout(p.timer);
|
||||
if (msg.ok) p.resolve(msg.result);
|
||||
else p.reject(new Error((msg as RpcError).error.message));
|
||||
@@ -244,6 +271,7 @@ export class PluginSupervisor {
|
||||
p.reject(new Error(reason));
|
||||
}
|
||||
sup.pending.clear();
|
||||
sup.invocations.clear();
|
||||
}
|
||||
|
||||
private onExit(sup: Supervised, code: number | null, signal: string | null): void {
|
||||
|
||||
Reference in New Issue
Block a user