kernel / core / mind-worker / src / sandbox / egress.tsmain at 949894f
// Every request a container makes is the mind's own `itx.fetch`, as a call, never a request to
// the context's `fetch`: that one routes on headers, and a container controls every header it sends.
// Both resolve `itx.fetch` on the sandbox's context, so a rule there for `itx.fetch` sees
// every request, handshakes included: that rule is how a mind limits one sandbox (contract.ts says
// how to write it). Its target passes a socket on only through `env.ITX.fetch`, never over RPC.
// One request is answered here and not sent: a sandbox's own session with the mind's API
// (./own-session.ts), and only for a sandbox that no such rule limits.
import { WorkerEntrypoint } from "cloudflare:workers";
import {
contextAt,
DurableObjectNameCodec,
encodeFetchExpression,
ITX_EXPRESSION_FETCH_HEADER,
serveSession,
stampCallerHeaders,
} from "core-itx";
import { SANDBOX_SESSION_REFUSED_HEADER } from "iterate/mind-api";
import { mindWorkerConfigOf } from "../config.ts";
import type { Env } from "../env.ts";
import type { SandboxContainer } from "./container.ts";
import {
asHeaderValue,
asksForOwnSession,
egressFilterOf,
ownSessionOf,
ownSessionRoot,
sessionWatch,
} from "./own-session.ts";
/** Its props name the sandbox's context, which the container cannot reach. */
export class SandboxEgress extends WorkerEntrypoint<Env, { iterateContextName: string }> {
override async fetch(request: Request): Promise<Response> {
const { path } = DurableObjectNameCodec.parse(this.ctx.props.iterateContextName);
const { controlPlaneHostname } = mindWorkerConfigOf(this.env);
if (!asksForOwnSession(request, controlPlaneHostname)) return this.#send(request, path);
const filter = await this.#filter(path);
if (!filter) return this.#session(path);
// The rule's own target may add a bearer, so the request still goes. With none, the boundary's
// 401 says why the sandbox got no session of its own. So does a request that cannot go at all
// (a masked `itx.fetch`): a dropped connection would say nothing.
console.info({ event: "sandbox.session-refused", path, filter });
const answer = await this.#send(request, path).catch((error: unknown) => {
console.warn({ event: "sandbox.session-refused-send-failed", path, error: String(error) });
return null;
});
if (answer && answer.status !== 401) return answer;
const headers = new Headers(answer?.headers);
headers.set(SANDBOX_SESSION_REFUSED_HEADER, asHeaderValue(filter));
return new Response(answer ? answer.body : "This sandbox gets no session with its mind\n", {
status: 401,
headers,
});
}
async #send(request: Request, path: string): Promise<Response> {
if (request.headers.get("upgrade")?.toLowerCase() === "websocket")
return this.#upgrade(request, path);
const answer = await contextAt(path).invoke(["itx", ["fetch", request]], [], {
principal: null,
path,
});
// the context's `fetch` answers the `Response` it was given
return withKnownLength(answer as Response, request);
}
/** Why this sandbox gets no session of its own, or null (./own-session.ts `egressFilterOf`). */
async #filter(path: string): Promise<string | null> {
const rows = await contextAt(path).invoke(["itx", "builtins", "rewriteRules", ["list"]], [], {
principal: null,
path,
});
return egressFilterOf(rows);
}
/** The mind's API as the code of the sandbox's own context, for as long as the socket lives. No
* credential ends it: a rule that lands later does (`sessionWatch`). */
async #session(path: string): Promise<Response> {
// `ctx.exports` holds what the Worker's main module exports, and is typed from that module.
// The module exports this class, so this file cannot name its type: the cast names the one
// class that is used here (./container.ts, exported by ../worker.ts), which is safe because
// the sandbox's facet reaches the same namespace by the same name.
const { SandboxContainer } = this.ctx.exports as unknown as {
SandboxContainer: DurableObjectNamespace<SandboxContainer>;
};
const container = SandboxContainer.getByName(this.ctx.props.iterateContextName);
// Never waited for: a stamp that fails or does not answer delays no session and skips no check.
const used = () =>
container.used().catch((error: unknown) => {
console.warn({ event: "sandbox.session-not-stamped", path, error: String(error) });
});
// At the fixed point: the fact is the platform's record, which no row of the sandbox masks.
const record = (fact: "opened" | "ended", payload: { reason?: string }) => {
console.info({ event: `sandbox.session-${fact}`, path, ...payload });
const event = { type: `events.iterate.com/sandbox/session-${fact}`, payload };
this.ctx.waitUntil(
contextAt(path)
.invoke(["itx", "builtins", ["append", event]], [], { principal: null, path })
.catch((error: unknown) =>
console.warn({ event: "sandbox.session-not-recorded", path, error: String(error) }),
),
);
};
this.ctx.waitUntil(used());
record("opened", {});
return serveSession(this.ctx, {
...ownSessionOf(path),
root: ownSessionRoot,
endsAt: null,
endReason: "",
onFrame: sessionWatch({
now: () => Date.now(),
used,
filter: () => this.#filter(path),
ended: (reason) => record("ended", { reason }),
}),
});
}
/** workerd's RPC cannot carry a Response that holds a socket, so a handshake takes the context's
* `fetch`. That one believes `x-itx-*` stamps, so the container's are dropped and ours set. */
async #upgrade(request: Request, path: string): Promise<Response> {
const headers = new Headers(request.headers);
for (const name of [...headers.keys()])
if (/^x-(itx|iterate)-/i.test(name)) headers.delete(name);
stampCallerHeaders(headers, { principal: null, path });
headers.set(ITX_EXPRESSION_FETCH_HEADER, encodeFetchExpression(["itx"]));
const answer = await contextAt(path).fetch(new Request(request, { headers }));
if (answer.status !== 101 || !answer.webSocket) return answer;
// The interception adds its own hop-by-hop `Upgrade` and `Connection`; two of each is refused by
// a strict client such as Node's WebSocket.
const answered = new Headers(answer.headers);
answered.delete("upgrade");
answered.delete("connection");
return new Response(null, { status: 101, webSocket: answer.webSocket, headers: answered });
}
}
const BUFFER_UP_TO_BYTES = 32 * 1024 * 1024;
/** The interception answers a response of unknown length close-delimited, which `apt` reads as an
* empty file, so the length is learned, up to BUFFER_UP_TO_BYTES; an event stream must not wait.
* `fetch` decodes gzip and Brotli but keeps the compressed `Content-Length`, so a decoded body has no
* known length. */
async function withKnownLength(response: Response, request: Request): Promise<Response> {
const { status, headers, body } = response;
const bodiless =
!body || request.method === "HEAD" || status === 101 || status === 204 || status === 304;
if (bodiless) return response;
const decoded = /^(gzip|br)$/i.test(headers.get("content-encoding") ?? "");
const answer = (bytes: BodyInit) => {
const plain = new Response(bytes, response);
if (decoded) {
plain.headers.delete("content-encoding");
plain.headers.delete("content-length");
}
return plain;
};
const declared = Number(headers.get("content-length"));
if (
!decoded &&
headers.has("content-length") &&
Number.isSafeInteger(declared) &&
declared >= 0
) {
const { readable, writable } = new FixedLengthStream(declared);
void body.pipeTo(writable).catch(() => undefined);
return new Response(readable, response);
}
if (headers.get("content-type")?.startsWith("text/event-stream")) return response;
const reader = body.getReader();
const held: Uint8Array[] = [];
let size = 0;
for (;;) {
const { done, value } = await reader.read();
if (done) {
const whole = new Uint8Array(size);
let at = 0;
for (const chunk of held) {
whole.set(chunk, at);
at += chunk.byteLength;
}
const known = answer(whole);
known.headers.set("content-length", String(size));
return known;
}
held.push(value);
size += value.byteLength;
if (size > BUFFER_UP_TO_BYTES) {
const rest = new ReadableStream<Uint8Array>({
start: (controller) => {
for (const chunk of held) controller.enqueue(chunk);
},
pull: async (controller) => {
const next = await reader.read();
if (next.done) controller.close();
else controller.enqueue(next.value);
},
cancel: () => reader.cancel(),
});
return answer(rest);
}
}
}