dorfteich/packages/plugin-sdk/src/host.test.ts
Claude Fable 5 0875e2a087
All checks were successful
CI / Build container images (push) Has been skipped
CI / Lint, typecheck, test (push) Successful in 2m57s
CI / Import/export fidelity gate (push) Successful in 46s
CD / Build and push images (push) Successful in 3m16s
CD / Deploy to Test (push) Successful in 8s
CI / Auth e2e pack (push) Successful in 4m8s
CD / Smoke tests against Test (push) Successful in 1m9s
CD / Promote to Int (push) Successful in 10s
Add the sandbox host runtime for plugin iframes (#73)
Implements the security core of the plugin system: code-plugin surfaces
run in opaque-origin iframes (sandbox="allow-scripts", never
allow-same-origin) with a capability-filtered RPC bridge.

- api: serve a per-plugin sandbox frame document at
  /plugins/:id/:version/frame with a CSP that pins every load to the
  plugin's own asset path (built from APP_BASE_URL, not the request Host,
  so a Host-rewriting proxy cannot break it) and forbids network access
  (connect-src 'none'). Plugin assets get Access-Control-Allow-Origin: *
  so the null-origin frame can load its own module bundle.
- web: sandbox-host creates the frame, wires the SDK host bridge over a
  source-filtered postMessage transport, drives render under a 5 s
  deadline (hung/failed plugin -> placeholder, never a frozen page), and
  tears down on unmount. PluginFrame/PluginPreviewPage surface it; the
  built-in ui.resize handler clamps plugin-requested heights.
- plugin-sdk: host bridge reports gate violations via onViolation and
  registers a gated handler for every v1 method, so an undeclared
  capability is rejected with capability_not_permitted (not
  unknown_method).
- tests: SDK gate unit test; web sandbox unit tests (opaque origin,
  source filtering, timeout); and the e2e security pack with a permanent
  malicious fixture plugin proving no escape (DOM/cookies/storage/fetch/
  undeclared capability all blocked) plus well-behaved and hung cases.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01EwZ4jR4KFAPvpjWevfUGX1
2026-07-11 09:16:30 +02:00

77 lines
2.7 KiB
TypeScript

import { describe, expect, it, vi } from 'vitest';
import { createHostBridge, type CapabilityViolation } from './host';
import { createRpcEndpoint, type RpcMessage, type RpcTransport } from './rpc';
/** Two directly-wired in-memory transports (host side, plugin side). */
function transportPair(): [RpcTransport, RpcTransport] {
const toA: Array<(message: RpcMessage) => void> = [];
const toB: Array<(message: RpcMessage) => void> = [];
const a: RpcTransport = {
post: (message) => queueMicrotask(() => toB.forEach((listener) => listener(message))),
listen: (onMessage) => {
toA.push(onMessage);
return () => toA.splice(toA.indexOf(onMessage), 1);
},
};
const b: RpcTransport = {
post: (message) => queueMicrotask(() => toA.forEach((listener) => listener(message))),
listen: (onMessage) => {
toB.push(onMessage);
return () => toB.splice(toB.indexOf(onMessage), 1);
},
};
return [a, b];
}
describe('createHostBridge gating', () => {
it('answers declared capabilities, rejects undeclared ones, and reports violations', async () => {
const [hostSide, pluginSide] = transportPair();
const violations: CapabilityViolation[] = [];
const getOutline = vi.fn().mockResolvedValue([{ id: 'h1', level: 1, text: 'Hello' }]);
const bridge = createHostBridge({
manifest: { permissions: ['readCurrentPage'] },
capabilities: { getOutline, listPages: vi.fn() },
transport: hostSide,
onViolation: (violation) => violations.push(violation),
});
const plugin = createRpcEndpoint({ ...pluginSide, timeoutMs: 500 });
// Declared capability → answered by the host implementation.
await expect(plugin.request('getOutline')).resolves.toEqual([
{ id: 'h1', level: 1, text: 'Hello' },
]);
// Undeclared capability → rejected before the implementation, and logged.
await expect(plugin.request('listPages')).rejects.toMatchObject({
code: 'capability_not_permitted',
});
expect(violations).toEqual([
{ method: 'listPages', capability: 'readPond', code: 'capability_not_permitted' },
]);
// A method outside the v1 surface → unknown_method (no handler installed).
await expect(plugin.request('formatHardDrive')).rejects.toMatchObject({
code: 'unknown_method',
});
bridge.dispose();
plugin.dispose();
});
it('refuses to invoke non-lifecycle methods on the plugin', async () => {
const [hostSide] = transportPair();
const bridge = createHostBridge({
manifest: { permissions: [] },
capabilities: {},
transport: hostSide,
});
await expect(
bridge.invoke('getOutline' as never, undefined, { timeoutMs: 100 }),
).rejects.toMatchObject({ code: 'unknown_method' });
bridge.dispose();
});
});