| 1 | /** |
| 2 | * Guardrails, as the runner applies them to a sandbox: how long a run may |
| 3 | * take, and the outbound handlers its requests may pass. |
| 4 | * |
| 5 | * - The time cap is enforced twice: by the harness, and by the sandbox's |
| 6 | * own alarm here, which stops the whole sandbox a little after. |
| 7 | * - Every sandbox reaches the internet directly. A workflow job that may |
| 8 | * be given brokered secrets sends its HTTP and HTTPS through `egress` |
| 9 | * below, which fills them in and sends everything on, to any host. |
| 10 | * - A sandbox's report that it stopped itself for mining comes to `abuse`. |
| 11 | */ |
| 12 | import type { OutboundHandlerContext } from "@cloudflare/containers"; |
| 13 | |
| 14 | import { type RepoPath, type RunKind, type ServiceBinding, guardrailsClient } from "@g1t/contracts"; |
| 15 | |
| 16 | import { ABUSE_HOST, type RunGuard, sandboxNamespace } from "./egress"; |
| 17 | import { type BrokeredParams, brokered } from "./broker-egress.ts"; |
| 18 | import { forward } from "./forward.ts"; |
| 19 | |
| 20 | export { |
| 21 | ABUSE_EXIT_CODE, |
| 22 | ABUSE_HOST, |
| 23 | ABUSE_MESSAGE, |
| 24 | SANDBOX_BINDINGS, |
| 25 | dockerFor, |
| 26 | harnessEnv, |
| 27 | sandboxNamespace, |
| 28 | throughWorker, |
| 29 | timeCapMessage, |
| 30 | withPlanLimits, |
| 31 | } from "./egress"; |
| 32 | export type { PlanLimits, RunGuard } from "./egress"; |
| 33 | |
| 34 | /** What the outbound handler is given: what brokered secrets need (broker-egress.ts). */ |
| 35 | export type EgressParams = BrokeredParams; |
| 36 | |
| 37 | /** The stop by the sandbox's alarm comes this long after the harness's own. */ |
| 38 | export const ALARM_GRACE_SECONDS = 3 * 60; |
| 39 | |
| 40 | /** |
| 41 | * The guardrails of a run in `repo`. Throws when they cannot be read: a |
| 42 | * sandbox is not started without them. |
| 43 | */ |
| 44 | export async function guardFor(work: ServiceBinding, repo: RepoPath, kind: RunKind): Promise<RunGuard> { |
| 45 | const found = await guardrailsClient(work).runGuardrails(repo); |
| 46 | if (!found.ok) throw new Error(`g1t could not read this project's guardrails: ${found.error.message}`); |
| 47 | const policy = found.value; |
| 48 | return { policy, minutes: policy.minutes[kind] ?? 60 }; |
| 49 | } |
| 50 | |
| 51 | /** |
| 52 | * The guardrails of a workflow job, deploy build or update: the time cap it |
| 53 | * was given, which is all a build's guardrails are. |
| 54 | */ |
| 55 | export function buildGuard(minutes: number): RunGuard { |
| 56 | return { policy: { minutes: {} }, minutes }; |
| 57 | } |
| 58 | |
| 59 | /** |
| 60 | * The outbound handler of a sandbox whose HTTP and HTTPS pass the Worker |
| 61 | * (a workflow job that may be given brokered secrets): every request it |
| 62 | * makes, to any host. Its brokered secrets are filled in first, if it has |
| 63 | * any, and the request is sent on with `forward`'s retries. |
| 64 | */ |
| 65 | export async function egress( |
| 66 | request: Request, |
| 67 | env: { SANDBOX: DurableObjectNamespace; ACTIONS?: ServiceBinding }, |
| 68 | ctx: OutboundHandlerContext<EgressParams>, |
| 69 | ): Promise<Response> { |
| 70 | const host = new URL(request.url).host; |
| 71 | // A sandbox reporting that it stopped itself for mining. |
| 72 | if (host === ABUSE_HOST) return abuse(request, env, ctx); |
| 73 | return (await brokered(request, env, ctx)) ?? forward(request); |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * A sandbox's report that it stopped itself for mining (crates/runner |
| 78 | * abuse.rs), handed to its Durable Object. The handler for `ABUSE_HOST` |
| 79 | * on every sandbox, and reached through `egress` for one whose requests |
| 80 | * pass the Worker. |
| 81 | */ |
| 82 | export async function abuse( |
| 83 | request: Request, |
| 84 | env: { SANDBOX: DurableObjectNamespace }, |
| 85 | ctx: OutboundHandlerContext<unknown>, |
| 86 | ): Promise<Response> { |
| 87 | let verdict: unknown = null; |
| 88 | try { |
| 89 | verdict = ((await request.json()) as { verdict?: unknown }).verdict ?? null; |
| 90 | } catch { |
| 91 | // A report without its metrics still stops the run. |
| 92 | } |
| 93 | try { |
| 94 | const namespace = sandboxNamespace(env, ctx.className); |
| 95 | const sandbox = namespace.get(namespace.idFromString(ctx.containerId)) as unknown as { |
| 96 | flagAbuse(verdict: unknown): Promise<void>; |
| 97 | }; |
| 98 | await sandbox.flagAbuse(verdict); |
| 99 | } catch (error) { |
| 100 | console.log("abuse report not handled", String(error)); |
| 101 | } |
| 102 | return new Response("noted\n"); |
| 103 | } |
| 104 | |
| 105 | /** Adds a step to a run, with its token. Never fails the caller. */ |
| 106 | export async function reportRun( |
| 107 | work: ServiceBinding, |
| 108 | tracked: { runId: string; token: string }, |
| 109 | report: { steps?: string[]; halt?: "budget" | "time" | "abuse"; error?: string }, |
| 110 | ): Promise<void> { |
| 111 | await work |
| 112 | .fetch("https://service/rpc/report_run", { |
| 113 | method: "POST", |
| 114 | headers: { "content-type": "application/json" }, |
| 115 | body: JSON.stringify({ runId: tracked.runId, token: tracked.token, ...report }), |
| 116 | }) |
| 117 | .catch((error: unknown) => console.log("run report failed", tracked.runId, String(error))); |
| 118 | } |