feat(plugins): secure installer — manifest, discovery, safe extract/fetch/scan (M4)

Plugins placed on the /plugins volume are now discovered, validated and registered
as inactive, ready to activate.

- manifest.ts: strict trek-plugin.json validation (id/version/type, known
  permissions only, egress required with http:outbound, native modules rejected).
- discovery.ts: scans the volume on startup + on demand (POST /api/admin/plugins/
  rescan), upserts rows INACTIVE, refreshes settings-field descriptors, and never
  downgrades or wipes an already-installed plugin's status / grants / config.
  Invalid or native-carrying plugins are skipped and logged.
- Activation now grants the DECLARED permissions (the consent gate) and persists
  them before spawning.
- install/ utilities for the registry installer (M5), each independently tested:
  - safe-fetch: host allowlist (GitHub only) + private-IP refusal + manual
    redirect following + size cap + sha256 (constant-time compare).
  - safe-extract: zip/tar-slip-safe extraction with its own minimal tar.gz + zip
    readers; rejects traversal, absolute paths, symlinks, oversized/too-many
    entries, and unsupported formats.
  - native-scan: refuses .node / binding.gyp / prebuilds, never follows symlinks.
This commit is contained in:
mauriceboe
2026-07-03 17:26:46 +02:00
parent 037eb8a209
commit be07f29acd
13 changed files with 840 additions and 4 deletions
@@ -5,6 +5,7 @@ import { decrypt_api_key } from '../../services/apiKeyCrypto';
import { PluginSupervisor, type PluginRouteInfo } from './supervisor/plugin-supervisor';
import { createRealRpcHost, closePluginDataDb } from './host/create-rpc-host';
import { isKnownPermission } from './protocol/envelope';
import { discoverPlugins } from './install/discovery';
/**
* Owns the isolated-plugin runtime lifecycle inside NestJS (#plugins, M2).
@@ -41,6 +42,13 @@ export class PluginRuntimeService implements OnModuleInit, OnModuleDestroy {
onModuleInit(): void {
if (!pluginsEnabled()) return;
// Discover plugins placed on the volume (registers new ones inactive), then
// boot the ones an admin had already activated.
try {
discoverPlugins(db);
} catch {
/* discovery must never block boot */
}
const active = db.prepare("SELECT id FROM plugins WHERE status = 'active'").all() as Array<{ id: string }>;
for (const { id } of active) {
this.activate(id).catch(() => {
@@ -49,6 +57,11 @@ export class PluginRuntimeService implements OnModuleInit, OnModuleDestroy {
}
}
/** Re-scan the plugins volume on demand (admin action). */
rescan(): { discovered: string[]; skipped: string[] } {
return discoverPlugins(db);
}
async onModuleDestroy(): Promise<void> {
await this.supervisor.shutdownAll();
}
@@ -60,9 +73,12 @@ export class PluginRuntimeService implements OnModuleInit, OnModuleDestroy {
| undefined;
if (!row) throw new Error(`plugin ${id} not found`);
const granted = new Set(parseArray(row.granted_permissions).filter(isKnownPermission));
// Activating IS the consent gate: grant the plugin's DECLARED permissions
// (the admin reviewed them on the consent screen), persist, then spawn.
const declared = parseArray(row.permissions).filter(isKnownPermission);
db.prepare('UPDATE plugins SET granted_permissions = ? WHERE id = ?').run(JSON.stringify(declared), id);
const config = decryptConfig(parseObject(row.config));
await this.supervisor.activate(id, granted, config);
await this.supervisor.activate(id, new Set(declared), config);
}
async deactivate(id: string): Promise<void> {