Skip to content

Commit e0c6fac

Browse files
committed
test(runtime): pin the REST and MCP doors' scoped-URL api forward placement
A same-origin type:'api' action called on a shared-origin multi-kernel host by its scoped URL (/environments/:env/...), through the real dispatch(), at the REST action route and at the MCP run_action bridge. Red on main: the forward reaches the raw app with no routing identity and the target answers 401. Co-authored-by: Claude <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_016WV4cykhsWAtSL6Sr4VWFG
1 parent 82838e8 commit e0c6fac

1 file changed

Lines changed: 67 additions & 2 deletions

File tree

‎packages/runtime/src/action-dispatch.test.ts‎

Lines changed: 67 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -483,12 +483,29 @@ describe('dispatchAction on a multi-kernel host runs on the environment\'s own k
483483
describe('a same-origin api action on a hosted environment kernel forwards as the REST door forwards it', () => {
484484
const ENV_T = 'env_t';
485485
const TENANT_HOST = 'acme.example.test';
486+
/** The host's shared origin: no environment is bound to it, so only a scoped URL places a request. */
487+
const SHARED_HOST = 'shared.example.test';
486488
const SESSION = 'os.session=member';
487489
const probe = { name: 'probe_api', label: 'Probe', type: 'api', target: '/api/v1/probe/echo', method: 'POST', ai: EXPOSED };
488490
const auth = { api: { getSession: async () => ({ user: { id: MEMBER_EC.userId } }) } };
489491

490492
function hostedEnvironment() {
491-
const env = makeRig([probe], { services: { auth } });
493+
// The environment's MCP runtime: one `run_action` call over the
494+
// dispatcher's real bridge, its answer (or the throw, read as the MCP
495+
// tool reads it) written back as JSON.
496+
const mcp = {
497+
handleHttpRequest: async (_req: Request, opts: { bridge: any }) => {
498+
let answer: unknown;
499+
try {
500+
answer = { ok: true, result: await opts.bridge.runAction(probe.name, { objectName: OBJECT }) };
501+
} catch (err) {
502+
const read = actionsDoorAnswerOf(err, OBJECT);
503+
answer = { ok: false, status: read.status, code: read.code };
504+
}
505+
return new Response(JSON.stringify(answer), { status: 200, headers: { 'content-type': 'application/json' } });
506+
},
507+
};
508+
const env = makeRig([probe], { services: { auth, mcp } });
492509
const place = (ctx: any) => {
493510
const headers = ctx.request?.headers ?? {};
494511
const named = headers.host === TENANT_HOST || headers['x-environment-id'] === ENV_T || ctx.urlEnvironmentId === ENV_T;
@@ -517,9 +534,57 @@ describe('a same-origin api action on a hosted environment kernel forwards as th
517534
const viaHost = (kernel: unknown, call: Partial<ActionDispatchRequest> = {}) => dispatchAction(kernel as any, {
518535
executionContext: MEMBER_EC as any, objectName: OBJECT, actionName: probe.name, credentialHeaders: { cookie: SESSION }, ...call,
519536
});
520-
return { env, host, forwards, viaRest, viaHost };
537+
/**
538+
* A request on the host's SHARED origin (no hostname binding), placed
539+
* only by its scoped URL `/environments/:env/…`, through the real
540+
* `dispatch()`: the REST action route, or the MCP endpoint's `run_action`.
541+
*/
542+
const viaScoped = async (route: string, headers: Record<string, string> = {}) => ((await new HttpDispatcher(host.kernel).dispatch(
543+
'POST', `/environments/${ENV_T}${route}`, {}, {},
544+
{ request: { method: 'POST', url: `/api/v1/environments/${ENV_T}${route}`, headers: { host: SHARED_HOST, cookie: SESSION, ...headers } } } as any,
545+
)) as any).response as { status: number; body: any };
546+
return { env, host, forwards, viaRest, viaHost, viaScoped };
521547
}
522548

549+
// [#32887] The REST and MCP doors run the api arm on the dispatch request
550+
// itself: on a shared origin a request placed only by its scoped URL
551+
// carried no routing identity into its forward (no bound `Host`, no
552+
// environment header), so the raw app placed the forward nowhere and the
553+
// target answered the member 401.
554+
it('a REST call placed only by its scoped URL forwards into the environment it was placed in', async () => {
555+
const h = hostedEnvironment();
556+
557+
const answer = await h.viaScoped(`/actions/${OBJECT}/${probe.name}`);
558+
559+
expect({ status: answer.status, data: answer.body?.data }).toEqual({ status: 200, data: { echoed: {} } });
560+
expect(h.forwards.map((f) => ({ url: f.url, placed: f.placed, environment: f.headers['x-environment-id'] })))
561+
.toEqual([{ url: `http://${SHARED_HOST}${probe.target}`, placed: true, environment: ENV_T }]);
562+
expect(h.env.rows.map((r) => r.outcome)).toEqual(['succeeded']);
563+
});
564+
565+
it('an MCP `run_action` placed only by its scoped URL forwards into the environment it was placed in', async () => {
566+
const h = hostedEnvironment();
567+
568+
const answer = await h.viaScoped('/mcp');
569+
570+
expect({ status: answer.status, body: answer.body }).toEqual({ status: 200, body: { ok: true, result: { echoed: {} } } });
571+
expect(h.forwards.map((f) => ({ url: f.url, placed: f.placed, environment: f.headers['x-environment-id'] })))
572+
.toEqual([{ url: `http://${SHARED_HOST}${probe.target}`, placed: true, environment: ENV_T }]);
573+
});
574+
575+
it('the scoped forward names the environment the request was placed in, never the caller\'s environment header', async () => {
576+
const h = hostedEnvironment();
577+
578+
// The rig's resolver places by the bound hostname, then the header,
579+
// then the scoped URL: a header naming another environment is not one
580+
// it places, so the URL places the call and the forward names ENV_T.
581+
const answer = await h.viaScoped(`/actions/${OBJECT}/${probe.name}`, { 'x-environment-id': 'env_elsewhere' });
582+
583+
expect(answer.status).toBe(200);
584+
expect(h.forwards.map((f) => ({ placed: f.placed, environment: f.headers['x-environment-id'] })))
585+
.toEqual([{ placed: true, environment: ENV_T }]);
586+
});
587+
523588
it('a call naming the environment on the host kernel forwards into that environment and answers what the REST door answers, with its invocation row', async () => {
524589
const h = hostedEnvironment();
525590

0 commit comments

Comments
 (0)