Public: anyone can read this; the crew can change it.

Sentinel: shared:01m23vygyffx5hg2f5vm8r74sr:dc4579j8qd is past its budget: trevato/forge: "/apps/open sends a grant only to a host that reads one way > the proxy reads such a Host the same way: … #12718

Merged opened this

Check · Review · Land · Live done — Landed and live in production.

Merged

scout/forge:scout/12702-sentinel-shared-01m23vygyffx5hg2f5vm8r74 → main 10 commits fork 2 threads · 2 unresolved

  • Merged. scout/12702-sentinel-shared-01m23vygyffx5hg2f5vm8r74 was merged into main as 061f575.

Review

Sign in to review this change.
6 files changed +127 −32
src/crew/runner.ts +20 −5
@@ -632,14 +632,29 @@ async function pullHeadOf(
632632 return undefined;
633633 const pull = getPull(app.db, repo.id, issue.number);
634634 if (pull === undefined) return undefined;
635 if (!refresh) return pull.headOid;
635 // A fork's pull is measured by the fork's branch at both ends of a run, so
636 // a row a concurrent refresh has not yet written cannot pass for no push.
637 if (!refresh) return (await forkTipOf(app, pull)) ?? pull.headOid;
636638 // A fork's push reaches the pull's row by a fetch; take it now, not later.
637639 // A push's own follow-up may have taken the head already, after this row
638640 // was read: ask the row again rather than trust the copy in hand.
639 return (
640 (await refreshPullHead(app, repo, pull)) ??
641 getPull(app.db, repo.id, issue.number)?.headOid ??
642 pull.headOid
641 const fresh = await refreshPullHead(app, repo, pull);
642 if (fresh !== undefined) return fresh;
643 // A concurrent refresh writes the snapshot ref before the row, so "nothing
644 // moved" can meet a row still on the old head: the fork's branch says.
645 const live = await forkTipOf(app, pull);
646 return live ?? getPull(app.db, repo.id, issue.number)?.headOid ?? pull.headOid;
647 }
648
649 /** The tip of a fork pull's head branch, read from the fork; none for any other pull. */
650 async function forkTipOf(app: App, pull: Pull): Promise<string | undefined> {
651 if (!pull.crossRepo || pull.originHeadRepo !== null || pull.mergedAt !== null)
652 return undefined;
653 const head = getRepoById(app.db, pull.headRepoId);
654 if (head === undefined) return undefined;
655 return resolveOid(
656 repoDir(app.reposRoot, head.ownerName, head.name),
657 `refs/heads/${pull.headBranch}`,
643658 );
644659 }
645660
src/http/parseable_request.ts added +38
@@ -0,0 +1,38 @@
1 /**
2 * A request whose URL cannot be parsed. The runtime builds `request.url`
3 * from the Host header, so a Host like `evil#x` yields a URL every later
4 * `new URL(request.url)` throws on. Rebuilt on a neutral origin with the
5 * same path, method, headers (Host included, for dispatch) and body, it is
6 * answered as any unknown host is.
7 */
8 export function parseableRequest(request: Request): Request {
9 if (URL.canParse(request.url)) return request;
10 const match = /^[a-z][a-z0-9+.-]*:\/\/[^/]*(\/[^#]*)?/i.exec(request.url);
11 const path = match?.[1] ?? "/";
12 const init: RequestInit & { duplex?: "half" } = {
13 method: request.method,
14 headers: request.headers,
15 signal: request.signal,
16 };
17 if (request.body !== null) {
18 init.body = request.body;
19 init.duplex = "half";
20 }
21 return new Request(`http://forge.invalid${URL.canParse(`http://forge.invalid${path}`) ? path : "/"}`, init);
22 }
23
24 /**
25 * The listener's server, speaking for the request it handed over: a call
26 * that names the rebuilt request (`requestIP`, `upgrade`, `timeout`) reaches
27 * the runtime with the original, which is the only one it knows.
28 */
29 export function answeringFor<S extends object>(server: S, rebuilt: Request, original: Request): S {
30 return new Proxy(server, {
31 get(target, prop) {
32 const value = Reflect.get(target, prop, target);
33 if (typeof value !== "function") return value;
34 return (...args: unknown[]) =>
35 value.apply(target, args.map((arg) => (arg === rebuilt ? original : arg)));
36 },
37 });
38 }
src/http/server.ts +4 −25
@@ -5,6 +5,7 @@
55 * verdict. Auth is resolved once per request and handed to handlers as data.
66 */
77
8 import { answeringFor, parseableRequest } from "./parseable_request.ts";
89 import { expireCookie } from "../auth/cookies.ts";
910 import { checkText } from "./check_text.ts";
1011 import { noteOwnWrite } from "../flow/keep.ts";
@@ -2443,29 +2444,6 @@ export interface Servers {
24432444 stop(): void;
24442445 }
24452446
2446 /**
2447 * A request whose Host the runtime could not turn into a URL (`a#b.example`)
2448 * carries a `url` nothing downstream can parse. It names no host of ours, so
2449 * it is answered as the forge's own: the same request, its path kept, on a
2450 * Host the forge does not know either.
2451 */
2452 function readable(request: Request): Request {
2453 if (URL.canParse(request.url)) return request;
2454 const path = /^[a-z]+:\/\/[^/]*(\/.*)?$/i.exec(request.url)?.[1] ?? "/";
2455 const headers = new Headers(request.headers);
2456 headers.set("host", "forge.invalid");
2457 const hasBody = request.method !== "GET" && request.method !== "HEAD";
2458 try {
2459 return new Request(`http://forge.invalid${URL.canParse(`http://forge.invalid${path}`) ? path : "/"}`, {
2460 method: request.method,
2461 headers,
2462 ...(hasBody ? { body: request.body, duplex: "half" } : {}),
2463 } as RequestInit);
2464 } catch {
2465 return new Request("http://forge.invalid/", { headers });
2466 }
2467 }
2468
24692447 /**
24702448 * What a thrown handler is answered with: the guard's words (or a generic
24712449 * "refused") and a 403
@@ -3023,9 +3001,10 @@ export async function serve(app: App): Promise<Servers> {
30233001 // Open until it is answered, so a stall while it runs names it (#11478).
30243002 // Once the port is handed over, an answer on a connection opened before ends it (#11682).
30253003 fetch: (raw, server) => {
3026 const request = readable(raw);
3004 const request = parseableRequest(raw);
3005 const bound = request === raw ? server : answeringFor(server, request, raw);
30273006 return inFlight.during(request, performance.now(), async () => {
3028 const response = await handle(request, server);
3007 const response = await handle(request, bound);
30293008 return handedOver && response !== undefined ? withHeader(response, "connection", "close") : response;
30303009 });
30313010 },
test/ask-to-reach.test.ts +4 −1
@@ -202,8 +202,11 @@ beforeAll(async () => {
202202 afterAll(async () => {
203203 // Put back as "" where there was none, as `outbound-gate.test.ts` does: Bun's fetch keeps a
204204 // proxy variable that was deleted, and the next file's https would still go to this gate.
205 // FORGE_PARENT_LOCAL is not a proxy variable: "" would still say "in a cell"
206 // to `cellProxy`, and every later file in the shard would go by the gate.
205207 for (const [name, value] of Object.entries(env))
206 process.env[name] = value ?? "";
208 if (value === undefined && name === "FORGE_PARENT_LOCAL") delete process.env[name];
209 else process.env[name] = value ?? "";
207210 gate?.close();
208211 fake?.stop();
209212 for (const k of [maria, otto]) {
test/check-land-settle.test.ts +5 −1
@@ -88,7 +88,11 @@ test("two landings seconds apart: the first step's main check is superseded befo
8888 const two = await pullOf("two", "TWO.md");
8989 const first = await mergePull(app, alice, repo, one, "merge");
9090 if (!first.ok) throw new Error(JSON.stringify(first.error));
91 const second = await mergePull(app, alice, repo, two, "merge");
91 // The queue lands a step without asking for a green on the newest base
92 // (#3316). Pull two's own check may have gone green on the main before
93 // `one` landed; held to a fresh green, this landing would be refused
94 // `check_stale` by that race, not by what this test is about.
95 const second = await mergePull(app, alice, repo, two, "merge", undefined, { pastGate: true });
9296 if (!second.ok) throw new Error(JSON.stringify(second.error));
9397
9498 const tip = await finished((r) => r.commitOid === second.value.mergeOid);
test/parseable-request.test.ts added +56
@@ -0,0 +1,56 @@
1 import { expect, test } from "bun:test";
2 import { answeringFor, parseableRequest } from "../src/http/parseable_request.ts";
3
4 /** What the runtime hands over for a bad Host: a Request whose url will not parse. */
5 function crafted(url: string, init: RequestInit = {}): Request {
6 const real = new Request("http://forge.invalid/", init);
7 return {
8 url,
9 method: real.method,
10 headers: real.headers,
11 signal: real.signal,
12 body: real.body,
13 } as Request;
14 }
15
16 test("a parseable URL is returned unchanged", () => {
17 const request = new Request("http://example.test/a?b=1");
18 expect(parseableRequest(request)).toBe(request);
19 });
20
21 test("a bad Host keeps method, path, query, body and Host header", async () => {
22 const request = crafted("http://evil[stuff--notes.test/a/b?x=1&y=2", {
23 method: "POST",
24 headers: { host: "evil#stuff--notes.test", "x-k": "v" },
25 body: "payload",
26 });
27 const fixed = parseableRequest(request);
28 expect(fixed).not.toBe(request);
29 const url = new URL(fixed.url);
30 expect([fixed.method, url.pathname, url.search]).toEqual(["POST", "/a/b", "?x=1&y=2"]);
31 expect(fixed.headers.get("host")).toBe("evil#stuff--notes.test");
32 expect(fixed.headers.get("x-k")).toBe("v");
33 expect(await fixed.text()).toBe("payload");
34 });
35
36 test("a path with # or ? is cut where a URL would cut it", () => {
37 const fixed = parseableRequest(crafted("http://evil[x/p?q=1#frag"));
38 const url = new URL(fixed.url);
39 expect([url.pathname, url.search]).toEqual(["/p", "?q=1"]);
40 expect(new URL(parseableRequest(crafted("http://evil[x")).url).pathname).toBe("/");
41 });
42
43 test("the server speaks for the rebuilt request with the original", () => {
44 const original = new Request("http://a.test/");
45 const rebuilt = new Request("http://b.test/");
46 const seen: unknown[] = [];
47 const server = {
48 requestIP(request: Request) {
49 seen.push(request);
50 return request === original ? { address: "203.0.113.9" } : null;
51 },
52 };
53 const bound = answeringFor(server, rebuilt, original);
54 expect(bound.requestIP(rebuilt)?.address).toBe("203.0.113.9");
55 expect(seen[0]).toBe(original);
56 });

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs