|
18 | 18 | * |
19 | 19 | * Fail closed: an envelope with no user is injected nothing, and a grant |
20 | 20 | * resolution that throws admits nothing. |
| 21 | + * |
| 22 | + * [#37944] The last group pins the in-process entry |
| 23 | + * (`createInProcessAuthoringRegistry`) against this door: the same five tool |
| 24 | + * definitions, the same refusal shape for a person without `manage_metadata`, |
| 25 | + * the same writer, and saves stamped `copilot` where this door stamps `mcp`. |
21 | 26 | */ |
22 | 27 |
|
23 | 28 | import { describe, it, expect, afterEach, vi } from 'vitest'; |
@@ -53,6 +58,7 @@ import { |
53 | 58 | type OpenAuthoringProtocol, |
54 | 59 | type OpenAuthoringReadGateSources, |
55 | 60 | } from './mcp-authoring.js'; |
| 61 | +import { createInProcessAuthoringRegistry } from '../in-process-authoring.js'; |
56 | 62 | import { resolveExecutionContext } from '../security/resolve-execution-context.js'; |
57 | 63 | import { bindCatalogFromTables } from '../security/security-catalog.testkit.js'; |
58 | 64 |
|
@@ -1404,3 +1410,162 @@ describe('done when: an OAuth author saves an app-crm flow draft with no routabl |
1404 | 1410 | expect((await call(authoring, 'list_drafts', {})).body.drafts).toEqual([]); |
1405 | 1411 | }); |
1406 | 1412 | }); |
| 1413 | + |
| 1414 | +// --------------------------------------------------------------------------- |
| 1415 | +// [#37944] The in-process entry: the same five tools, as the signed-in person |
| 1416 | +// --------------------------------------------------------------------------- |
| 1417 | + |
| 1418 | +/** |
| 1419 | + * One host kernel serving the door's services, as `dispatchAction`'s pins |
| 1420 | + * build one: the recording protocol, the human's grant engine, the security |
| 1421 | + * service, and `tenancy` / `kernel-resolver` when given. `scopes` records the |
| 1422 | + * environment each scoped lookup was asked under. |
| 1423 | + */ |
| 1424 | +function inProcessHost(opts: { |
| 1425 | + systemPermissions?: string[]; protocol?: OpenAuthoringProtocol; tenancy?: { posture: string }; |
| 1426 | + qlThrows?: boolean; resolveKernel?: (ctx: any) => unknown; |
| 1427 | +} = {}) { |
| 1428 | + const { ql, reads } = userQl(opts.systemPermissions ?? ADMIN); |
| 1429 | + const fake = fakeProtocol(); |
| 1430 | + const protocol = opts.protocol ?? fake.protocol; |
| 1431 | + const security = { resolvePermissionSetNames: async (ctx: any) => (Array.isArray(ctx?.permissions) ? ctx.permissions : []) }; |
| 1432 | + const warnings: unknown[][] = []; |
| 1433 | + const services: Record<string, unknown> = { |
| 1434 | + protocol, |
| 1435 | + security, |
| 1436 | + ...(opts.tenancy ? { tenancy: opts.tenancy } : {}), |
| 1437 | + ...(opts.resolveKernel ? { 'kernel-resolver': { resolveKernel: opts.resolveKernel } } : {}), |
| 1438 | + logger: { warn: (...args: unknown[]) => { warnings.push(args); }, debug: () => {}, info: () => {}, error: () => {} }, |
| 1439 | + }; |
| 1440 | + const resolve = (name: string): any => { |
| 1441 | + if (name === 'objectql') { |
| 1442 | + if (opts.qlThrows) throw new Error('engine lookup failed'); |
| 1443 | + return ql; |
| 1444 | + } |
| 1445 | + return services[name] ?? null; |
| 1446 | + }; |
| 1447 | + const scopes: string[] = []; |
| 1448 | + const kernel: any = { |
| 1449 | + getService: resolve, |
| 1450 | + getServiceAsync: async (name: string, scopeId?: string) => { |
| 1451 | + if (scopeId !== undefined) scopes.push(scopeId); |
| 1452 | + return resolve(name); |
| 1453 | + }, |
| 1454 | + context: { getService: resolve }, |
| 1455 | + }; |
| 1456 | + return { kernel, grantReads: reads, protocolCalls: fake.calls, scopes, warnings }; |
| 1457 | +} |
| 1458 | + |
| 1459 | +/** A signed-in person's session envelope, as a host's session door resolves it: no OAuth grant. */ |
| 1460 | +const SESSION_EC = { userId: 'u1', isSystem: false, roles: [], permissions: [], systemPermissions: [] } as any; |
| 1461 | + |
| 1462 | +async function inProcessCall(authoring: { execute: (call: any) => Promise<any> }, toolName: string, input: unknown) { |
| 1463 | + const result = await authoring.execute({ toolCallId: `t-${toolName}`, toolName, input }); |
| 1464 | + return { isError: result.isError === true, body: JSON.parse(result.output.value) }; |
| 1465 | +} |
| 1466 | + |
| 1467 | +describe('[#37944] createInProcessAuthoringRegistry serves the MCP door\'s five tools in process, as the signed-in person', () => { |
| 1468 | + it('is the runtime package\'s public entry', async () => { |
| 1469 | + const runtime = await import('../index.js'); |
| 1470 | + expect(runtime.createInProcessAuthoringRegistry).toBe(createInProcessAuthoringRegistry); |
| 1471 | + }); |
| 1472 | + |
| 1473 | + it('a person holding manage_metadata is listed the same five tool definitions the MCP door lists, with no OAuth grant', async () => { |
| 1474 | + const host = inProcessHost(); |
| 1475 | + const authoring = await createInProcessAuthoringRegistry(host.kernel, { executionContext: SESSION_EC }); |
| 1476 | + expect(authoring.admitted).toBe(true); |
| 1477 | + // The admission read the person's own grants: the session envelope carries no capability. |
| 1478 | + expect(host.grantReads.length).toBeGreaterThan(0); |
| 1479 | + |
| 1480 | + const { authoring: mcpDoor } = await authorThroughDomain(); |
| 1481 | + expect(authoring.getAll()).toEqual(mcpDoor.registry.getAll()); |
| 1482 | + expect(authoring.getAll().map((tool) => tool.name)).toEqual([...OPEN_AUTHORING_READ_TOOL_NAMES, ...OPEN_AUTHORING_WRITE_TOOL_NAMES]); |
| 1483 | + // No publish tool rides in. |
| 1484 | + expect(authoring.getAll().map((tool) => tool.name).some((name) => /publish/.test(name))).toBe(false); |
| 1485 | + |
| 1486 | + const listed = await inProcessCall(authoring, 'list_metadata_items', { type: 'flow' }); |
| 1487 | + expect(listed.isError).toBe(false); |
| 1488 | + expect(listed.body.items.map((item: any) => item.name)).toEqual(['lead_intake', 'close_won']); |
| 1489 | + }); |
| 1490 | + |
| 1491 | + it('a person without manage_metadata is listed no tool and a call is refused 403, the MCP door\'s refusal shape, before any read', async () => { |
| 1492 | + const host = inProcessHost({ systemPermissions: NOT_AN_AUTHOR }); |
| 1493 | + const authoring = await createInProcessAuthoringRegistry(host.kernel, { executionContext: SESSION_EC }); |
| 1494 | + expect(authoring.admitted).toBe(false); |
| 1495 | + expect(authoring.getAll()).toEqual([]); |
| 1496 | + |
| 1497 | + const mcpEc = await oauthEnvelope(['metadata:write'], NOT_AN_AUTHOR); |
| 1498 | + const mcpRefusal = await call((await serve(mcpEc, { systemPermissions: NOT_AN_AUTHOR })).authoring!, 'save_metadata_draft', {}); |
| 1499 | + for (const tool of [...OPEN_AUTHORING_READ_TOOL_NAMES, ...OPEN_AUTHORING_WRITE_TOOL_NAMES]) { |
| 1500 | + const refused = await inProcessCall(authoring, tool, { type: 'flow', name: 'lead_intake', item: FLOW_DRAFT, package: 'com.acme.crm' }); |
| 1501 | + expect(refused.isError, tool).toBe(true); |
| 1502 | + expect(refused.body.error, tool).toMatchObject({ code: mcpRefusal.body.error.code, status: mcpRefusal.body.error.status }); |
| 1503 | + expect(refused.body.error, tool).toMatchObject({ code: 'PERMISSION_DENIED', status: 403 }); |
| 1504 | + // No OAuth half to name: the sentence names only the capability. |
| 1505 | + expect(refused.body.error.message, tool).not.toContain('metadata:write'); |
| 1506 | + } |
| 1507 | + expect(host.protocolCalls).toEqual([]); |
| 1508 | + }); |
| 1509 | + |
| 1510 | + it('a draft save stamps surface copilot, not mcp, with no client, as the person, and answers the publish verdict', async () => { |
| 1511 | + const host = inProcessHost(); |
| 1512 | + const authoring = await createInProcessAuthoringRegistry(host.kernel, { executionContext: SESSION_EC }); |
| 1513 | + const saved = await inProcessCall(authoring, 'save_metadata_draft', { type: 'flow', name: 'lead_intake', item: FLOW_DRAFT, package: 'com.acme.crm', ifMatch: 'v7' }); |
| 1514 | + |
| 1515 | + expect(saved.isError).toBe(false); |
| 1516 | + const save = host.protocolCalls.find((c: any) => c[0] === 'saveMetaItem') as any[]; |
| 1517 | + expect(save[1]).toMatchObject({ |
| 1518 | + type: 'flow', name: 'lead_intake', item: FLOW_DRAFT, packageId: 'com.acme.crm', |
| 1519 | + mode: 'draft', surface: 'copilot', writeFace: 'meta-dispatch', parentVersion: 'v7', actor: 'u1', |
| 1520 | + }); |
| 1521 | + expect(save[1]).not.toHaveProperty('client'); |
| 1522 | + expect(saved.body).toMatchObject({ |
| 1523 | + saved: { type: 'flow', name: 'lead_intake', package: 'com.acme.crm', state: 'draft', version: 'v8' }, |
| 1524 | + verdict: { publishable: false, refusals: [UNROUTABLE_ISSUE], advisories: [] }, |
| 1525 | + review: { changes: '/studio/com.acme.crm/data?changes=review', item: '/studio/com.acme.crm/metadata/flow/lead_intake?review=1' }, |
| 1526 | + }); |
| 1527 | + |
| 1528 | + // The MCP door's save, unchanged: `mcp` and the token's client. |
| 1529 | + const { authoring: mcpDoor, served } = await authorThroughDomain(); |
| 1530 | + await call(mcpDoor, 'save_metadata_draft', { type: 'flow', name: 'lead_intake', item: FLOW_DRAFT, package: 'com.acme.crm' }); |
| 1531 | + expect((served.protocolCalls.find((c: any) => c[0] === 'saveMetaItem') as any[])[1]).toMatchObject({ surface: 'mcp', client: { id: 'c1' } }); |
| 1532 | + }); |
| 1533 | + |
| 1534 | + it('the writer is the MCP door\'s: under a walled tenancy posture the save is refused 403 before anything is written', async () => { |
| 1535 | + const host = inProcessHost({ tenancy: { posture: 'isolated' } }); |
| 1536 | + const authoring = await createInProcessAuthoringRegistry(host.kernel, { executionContext: { ...SESSION_EC, posture: 'TENANT_ADMIN' } }); |
| 1537 | + const refused = await inProcessCall(authoring, 'save_metadata_draft', { type: 'flow', name: 'lead_intake', item: FLOW_DRAFT, package: 'com.acme.crm' }); |
| 1538 | + expect(refused.body.error).toMatchObject({ code: 'PERMISSION_DENIED', status: 403 }); |
| 1539 | + expect(host.protocolCalls.filter((c: any) => c[0] === 'saveMetaItem')).toEqual([]); |
| 1540 | + }); |
| 1541 | + |
| 1542 | + it('fails closed: no person, a system envelope, or a grant read that throws admits nothing', async () => { |
| 1543 | + for (const executionContext of [{ isSystem: false } as any, { ...SESSION_EC, isSystem: true }]) { |
| 1544 | + const host = inProcessHost(); |
| 1545 | + const authoring = await createInProcessAuthoringRegistry(host.kernel, { executionContext }); |
| 1546 | + expect(authoring.admitted).toBe(false); |
| 1547 | + expect(authoring.getAll()).toEqual([]); |
| 1548 | + expect((await inProcessCall(authoring, 'list_drafts', {})).body.error).toMatchObject({ code: 'PERMISSION_DENIED', status: 403 }); |
| 1549 | + // No grant is read for it. |
| 1550 | + expect(host.grantReads).toEqual([]); |
| 1551 | + expect(host.protocolCalls).toEqual([]); |
| 1552 | + } |
| 1553 | + const throwing = inProcessHost({ qlThrows: true }); |
| 1554 | + const authoring = await createInProcessAuthoringRegistry(throwing.kernel, { executionContext: SESSION_EC }); |
| 1555 | + expect(authoring.admitted).toBe(false); |
| 1556 | + expect(authoring.getAll()).toEqual([]); |
| 1557 | + }); |
| 1558 | + |
| 1559 | + it('is placed as dispatchAction places a host call: the named environment scopes the lookups, and a resolver that refuses rejects', async () => { |
| 1560 | + const host = inProcessHost(); |
| 1561 | + const authoring = await createInProcessAuthoringRegistry(host.kernel, { executionContext: SESSION_EC, environmentId: 'env_named' }); |
| 1562 | + await inProcessCall(authoring, 'list_drafts', {}); |
| 1563 | + expect(host.scopes.length).toBeGreaterThan(0); |
| 1564 | + expect(new Set(host.scopes)).toEqual(new Set(['env_named'])); |
| 1565 | + |
| 1566 | + const refusing = inProcessHost({ resolveKernel: async () => { throw Object.assign(new Error('not a member of env_named'), { status: 403 }); } }); |
| 1567 | + await expect(createInProcessAuthoringRegistry(refusing.kernel, { executionContext: SESSION_EC, environmentId: 'env_named' })) |
| 1568 | + .rejects.toThrow('not a member of env_named'); |
| 1569 | + expect(refusing.grantReads).toEqual([]); |
| 1570 | + }); |
| 1571 | +}); |
0 commit comments