outbound.ts
This file
Path
apps/sandbox/src/outbound.ts
96 lines · 3.1 KiB · TypeScript
Last changed by
Merge build/comments-ops into mainoscabrielbefore adoptione4c407e
History of this fileTake it
The walk stopped after 200 commits: lines older than 678c8b8 point at it.
Merge build/comments-ops into mainoscabrielbefore adoptione4c407e
lines 1–2why, one step back1// The Checker container's only ways out: the outbound handler is the2// credential boundary, and the container never sees a gateway's props.Merge build/614-env-service: deciders set write-only values for @sensitive items, the public sees the env shape, and checks see only placeholdersoscabrielbefore adoption678c8b8
lines 3–22why, one step back3import { WorkerEntrypoint } from "cloudflare:workers";4import { Option } from "effect";56import { allows, CLONE_HOST } from "./checker/network.ts";78export interface CloneProps {9 readonly remote: string;10 readonly token: string;11}1213export interface EgressProps {14 readonly allow: readonly string[];15}1617export const refused = (text: string) =>18 new Response(text, {19 headers: { "content-type": "text/plain; charset=utf-8" },20 status: 403,21 });22Merge build/comments-ops into mainoscabrielbefore adoptione4c407e
line 23why, one step back23/** Redirects go back to the container, so a followed one passes a gateway too. */Merge build/614-env-service: deciders set write-only values for @sensitive items, the public sees the env shape, and checks see only placeholdersoscabrielbefore adoption678c8b8
line 24why, one step back24export const forward = (request: Request): Promise<Response> =>Drop the private build plan's citations from the code, and fail verify if one comes backoscabrielbefore adoptiona29cef6
line 25why, one step back25 // @effect-diagnostics-next-line globalFetch:off -- an outbound entrypoint is the Worker's own network edge: workerd hands it a Request and takes back a Response, and its only way to the internet is the runtime's fetch; sandbox code may not use effect/http.Merge build/614-env-service: deciders set write-only values for @sensitive items, the public sees the env shape, and checks see only placeholdersoscabrielbefore adoption678c8b8
lines 26–40why, one step back26 fetch(request, { redirect: "manual" });2728export const resent = (url: string, request: Request, headers: Headers) => {29 // Node sends a streamed body only with `duplex: "half"`; the Workers types30 // lack the key, and workerd ignores it.31 const init = {32 body: request.body,33 duplex: "half",34 headers,35 method: request.method,36 };3738 return new Request(url, init);39};40Merge build/comments-ops into mainoscabrielbefore adoptione4c407e
line 41why, one step back41/** Only `git-upload-pack` reads of the job's repo pass; a push or any other query does not. */Merge build/614-env-service: deciders set write-only values for @sensitive items, the public sees the env shape, and checks see only placeholdersoscabrielbefore adoption678c8b8
lines 42–70why, one step back42const cloneRequestFor = (43 request: Request,44 props: CloneProps45): Option.Option<Request> => {46 const url = new URL(request.url);47 const remote = new URL(props.remote);48 const repo = remote.pathname.replace(/\/$/u, "");4950 const reads =51 (request.method === "GET" &&52 url.pathname === `${repo}/info/refs` &&53 url.search === "?service=git-upload-pack") ||54 (request.method === "POST" &&55 url.pathname === `${repo}/git-upload-pack` &&56 url.search === "");5758 if (url.hostname !== CLONE_HOST || !reads) {59 return Option.none();60 }6162 const headers = new Headers(request.headers);6364 headers.set("authorization", `Bearer ${props.token}`);6566 return Option.some(67 resent(`${remote.origin}${url.pathname}${url.search}`, request, headers)68 );69};70Merge build/comments-ops into mainoscabrielbefore adoptione4c407e
line 71why, one step back71/** The container clones with no credential; the token is added here, on the Worker's side. */Merge build/614-env-service: deciders set write-only values for @sensitive items, the public sees the env shape, and checks see only placeholdersoscabrielbefore adoption678c8b8
lines 72–96why, one step back72export class CloneGateway extends WorkerEntrypoint<Env, CloneProps> {73 override fetch(request: Request): Promise<Response> {74 return Option.match(cloneRequestFor(request, this.ctx.props), {75 onNone: () =>76 Promise.resolve(77 refused("the clone route serves reads of this job's repo only\n")78 ),79 onSome: forward,80 });81 }82}8384export class EgressGateway extends WorkerEntrypoint<Env, EgressProps> {85 override fetch(request: Request): Promise<Response> {86 const { hostname } = new URL(request.url);8788 return allows(this.ctx.props.allow, hostname)89 ? forward(request)90 : Promise.resolve(91 refused(92 `egress refused: ${hostname} is not on this check's allow-list\n`93 )94 );95 }96}