/** * The sandboxed plugin frame server (#plugins, M3): serves a plugin's client/ * assets with a locked-down per-frame CSP and a strict path guard, only when the * plugin is active and the runtime is enabled. */ import { describe, it, expect, beforeAll, afterAll, vi } from 'vitest'; import fs from 'node:fs'; import os from 'node:os'; import path from 'node:path'; const { pluginsEnabledMock } = vi.hoisted(() => ({ pluginsEnabledMock: vi.fn(() => true) })); vi.mock('../../../src/nest/plugins/kill-switch', () => ({ pluginsEnabled: pluginsEnabledMock })); import { PluginFrameController } from '../../../src/nest/plugins/plugin-frame.controller'; import type { PluginRuntimeService } from '../../../src/nest/plugins/plugin-runtime.service'; let codeRoot: string; let outsideRoot: string; // Windows only lets an unprivileged process create a directory junction. const dirLink = process.platform === 'win32' ? 'junction' : 'dir'; let symlinksAvailable = true; beforeAll(() => { codeRoot = fs.mkdtempSync(path.join(os.tmpdir(), 'trekplug-frame-')); outsideRoot = fs.mkdtempSync(path.join(os.tmpdir(), 'trekplug-outside-')); process.env.TREK_PLUGINS_DIR = codeRoot; const dir = path.join(codeRoot, 'widget', 'client'); fs.mkdirSync(dir, { recursive: true }); fs.writeFileSync(path.join(dir, 'index.html'), 'hi'); // Something worth stealing, plus a link out of client/ that points at it. fs.mkdirSync(path.join(outsideRoot, 'secrets'), { recursive: true }); fs.writeFileSync(path.join(outsideRoot, 'secrets', 'leak.js'), 'TOP SECRET'); // A dev-linked plugin: the whole tree is a link, which must still be served. fs.mkdirSync(path.join(outsideRoot, 'linked', 'client'), { recursive: true }); fs.writeFileSync(path.join(outsideRoot, 'linked', 'client', 'index.html'), 'dev'); try { fs.symlinkSync(path.join(outsideRoot, 'secrets'), path.join(dir, 'esc'), dirLink); fs.symlinkSync(path.join(outsideRoot, 'linked'), path.join(codeRoot, 'linked'), dirLink); } catch { symlinksAvailable = false; } }); afterAll(() => { delete process.env.TREK_PLUGINS_DIR; fs.rmSync(codeRoot, { recursive: true, force: true }); fs.rmSync(outsideRoot, { recursive: true, force: true }); }); function fakeRes() { const res = { statusCode: 0, headers: {} as Record, sent: undefined as unknown, filePath: undefined as string | undefined, fileRoot: undefined as string | undefined, status(c: number) { res.statusCode = c; return res; }, setHeader(k: string, v: string) { res.headers[k] = v; }, send(b: unknown) { res.sent = b; return res; }, sendFile(p: string, opts?: { root?: string }) { res.filePath = opts?.root ? path.join(opts.root, p) : p; res.fileRoot = opts?.root; res.statusCode ||= 200; }, }; return res; } const req = (p: string, host?: string) => ({ params: { path: p }, get: (h: string) => (h.toLowerCase() === 'host' ? host : undefined) }) as never; function runtime(active = true, hosts: string[] = [], operatorHosts: string[] = []): PluginRuntimeService { return { isActive: vi.fn(() => active), outboundHostsOf: vi.fn(() => hosts), operatorEgressHosts: vi.fn(() => operatorHosts), } as unknown as PluginRuntimeService; } describe('PluginFrameController', () => { it('serves index.html with the opaque-frame CSP + sandbox', () => { const res = fakeRes(); new PluginFrameController(runtime(true, ['api.weather.com'])).serve('widget', req(''), res as never); expect(res.filePath).toContain(path.join('widget', 'client', 'index.html')); const csp = res.headers['Content-Security-Policy']; expect(csp).toContain("default-src 'none'"); expect(csp).toContain('sandbox allow-scripts allow-forms'); expect(csp).not.toContain('allow-popups'); expect(csp).not.toContain('allow-same-origin'); expect(csp).toContain('connect-src \'self\' https://api.weather.com'); expect(res.headers['X-Content-Type-Options']).toBe('nosniff'); }); // Under the Nest ExpressAdapter, res.sendFile(absolutePath) resolves against the // rewritten req.url and 404s spuriously — files must go out root-relative. it('serves files with an explicit root, not an absolute path', () => { const res = fakeRes(); new PluginFrameController(runtime(true)).serve('widget', req(''), res as never); expect(res.fileRoot).toBe(path.join(codeRoot, 'widget', 'client')); }); // The frame document runs at an opaque origin, so its own