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

Capture the response headers of a bare 400 from the app proxy during a restart #13739

Merged opened this

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

Merged

mason/forge:mason/13738-capture-the-response-headers-of-a-bare-4 → main 2 commits fork

  • Merged. mason/13738-capture-the-response-headers-of-a-bare-4 was merged into main as 74eaf92.

Review

Sign in to review this change.
3 files changed +49 −11
src/spawn/reach.ts +37 −10
@@ -50,6 +50,11 @@ export interface Proof {
5050 readonly status: number | null;
5151 /** One sentence: what was asked and what is wrong, or "answered". */
5252 readonly says: string;
53 /**
54 * Which headers an unwelcome answer (not ok, or a 4xx) carried, of those
55 * that tell the app, the proxy and an edge apart (#13738); absent otherwise.
56 */
57 readonly seen?: Readonly<Record<string, string>>;
5358 }
5459
5560 export interface ChildReach {
@@ -59,7 +64,9 @@ export interface ChildReach {
5964 readonly ok: boolean;
6065 /** When it first failed in the run of failures it is in, ISO; null while it is reachable. */
6166 readonly since: string | null;
62 /** Whether the owner has been told it is unreachable; false while a new child is still settling. */
67 /**
68 * Whether the owner has been told it is unreachable; false while a new child is still settling.
69 */
6370 readonly alarmed: boolean;
6471 readonly proofs: readonly Proof[];
6572 }
@@ -67,7 +74,10 @@ export interface ChildReach {
6774 type FetchLike = (url: string, init?: RequestInit) => Promise<Response>;
6875
6976 interface Options {
70 /** True where there is no public network to prove through (a test suite); an injected `fetch` still works. */
77 /**
78 * True where there is no public network to prove through (a test suite); an injected `fetch`
79 * still works.
80 */
7181 readonly off?: boolean;
7282 readonly fetch?: FetchLike;
7383 readonly settleMs?: number;
@@ -105,13 +115,28 @@ export const appUrlOf = (app: App, child: Pick<Child, "name">, slug: string): st
105115 */
106116 export async function slugsOf(app: App, child: Child): Promise<string[]> {
107117 const slugs = new Set(child.apps.map((ref) => slugFor(ref.split("/")[1] ?? ref)));
108 // The child's database is the child's to write: it is asked off this thread with a deadline (`child_db.ts`),
109 // and a slug that is not the forge's slug rule would steer this fetch to another host (`evil.example#`,
110 // `u@host`), so it is dropped. Not yet a database to read: the ledger's repositories are what is known.
118 // The child's database is the child's to write: it is asked off this thread with a deadline
119 // (`child_db.ts`), and a slug that is not the forge's slug rule would steer this fetch to another
120 // host (`evil.example#`, `u@host`), so it is dropped. Not yet a database to read: the ledger's
121 // repositories are what is known.
111122 for (const slug of await childPublicSlugs(childDirOf(app, child.bodyId))) if (isSlug(slug)) slugs.add(slug);
112123 return [...slugs].sort();
113124 }
114125
126 /**
127 * The headers that tell who answered: the app (`x-forge-app`), the proxy (`server`) or an edge
128 * (`via`).
129 */
130 const WHO_HEADERS = ["server", APP_HEADER, "via"] as const;
131
132 /** A proof, with the answer's identifying headers kept when the answer was unwelcome. */
133 function withSeen(proof: Proof, headers: Headers): Proof {
134 if (proof.ok && (proof.status ?? 0) < 400) return proof;
135 const seen: Record<string, string> = {};
136 for (const name of WHO_HEADERS) seen[name] = (headers.get(name) ?? "").slice(0, 200);
137 return { ...proof, seen };
138 }
139
115140 async function ask(fetcher: FetchLike, url: string): Promise<{ status: number; headers: Headers; body: string } | string> {
116141 try {
117142 const res = await fetcher(url, { redirect: "manual", signal: AbortSignal.timeout(PROBE_MS), headers: { accept: "*/*" } });
@@ -134,7 +159,7 @@ export async function proveHome(fetcher: FetchLike, child: Pick<Child, "url">):
134159 claimed = false;
135160 }
136161 if (got.status === 200 && claimed) return { label: "home", url, ok: true, status: 200, says: "answered 200" };
137 return { label: "home", url, ok: false, status: got.status, says: `answered ${got.status}, not by the forge` };
162 return withSeen({ label: "home", url, ok: false, status: got.status, says: `answered ${got.status}, not by the forge` }, got.headers);
138163 }
139164
140165 /** An app answers as itself: not a 5xx, and carrying the proxy's `x-forge-app` for this slug. */
@@ -142,9 +167,9 @@ export async function proveApp(fetcher: FetchLike, url: string, slug: string): P
142167 const got = await ask(fetcher, url);
143168 if (typeof got === "string") return { label: slug, url, ok: false, status: null, says: `no answer (${got})` };
144169 if (got.headers.get(APP_HEADER) !== slug)
145 return { label: slug, url, ok: false, status: got.status, says: `answered ${got.status}, not by the app` };
146 if (got.status >= 500) return { label: slug, url, ok: false, status: got.status, says: `the app answered ${got.status}` };
147 return { label: slug, url, ok: true, status: got.status, says: `answered ${got.status}` };
170 return withSeen({ label: slug, url, ok: false, status: got.status, says: `answered ${got.status}, not by the app` }, got.headers);
171 if (got.status >= 500) return withSeen({ label: slug, url, ok: false, status: got.status, says: `the app answered ${got.status}` }, got.headers);
172 return withSeen({ label: slug, url, ok: true, status: got.status, says: `answered ${got.status}` }, got.headers);
148173 }
149174
150175 /**
@@ -290,7 +315,9 @@ export async function sweepReach(app: App, asleep: (name: string) => boolean = (
290315 }
291316 }
292317
293 /** Re-prove every child on a schedule, the first pass soon after boot. Returns the timer to clear. */
318 /**
319 * Re-prove every child on a schedule, the first pass soon after boot. Returns the timer to clear.
320 */
294321 export function startReachWatch(
295322 app: App,
296323 everyMs = REACH_EVERY_MS,
test/spawn-reach-door.test.ts +1 −1
@@ -75,7 +75,7 @@ test("the spawn result lists each address with its result; a standby page is a f
7575 expect(made.reach.says).toContain("Running, not reachable: notes answered 200, not by the app");
7676 expect(made.reach.proofs).toEqual([
7777 { label: "home", url: "https://kid--forge.test/api/v1/forge", ok: true, status: 200, says: "answered 200" },
78 { label: "notes", url: "https://notes--kid--forge.test/", ok: false, status: 200, says: "answered 200, not by the app" },
78 { label: "notes", url: "https://notes--kid--forge.test/", ok: false, status: 200, says: "answered 200, not by the app", seen: { server: "", via: "", "x-forge-app": "" } },
7979 ]);
8080 // The never-answering app is the alarm, once its time to come up is over.
8181 for (let i = 0; i < 100 && reachAlarms(app).length === 0; i++) await Bun.sleep(20);
test/spawn-reach.test.ts +11
@@ -54,6 +54,17 @@ describe("a probe asserts the answer, not the status", () => {
5454 });
5555 });
5656
57 describe("an unwelcome answer keeps the headers that say who sent it (#13738)", () => {
58 test("a bare 400 records server, x-forge-app and via; a welcome answer records none", async () => {
59 const bare = await proveApp(async () => new Response("", { status: 400, headers: { server: "edge", via: "1.1 front" } }), "https://a--k--f.test/", "a");
60 expect(bare).toMatchObject({ ok: false, status: 400, seen: { server: "edge", "x-forge-app": "", via: "1.1 front" } });
61 const ours = await proveApp(async () => new Response("", { status: 400, headers: { "x-forge-app": "a" } }), "https://a--k--f.test/", "a");
62 expect(ours).toMatchObject({ ok: true, seen: { "x-forge-app": "a" } });
63 const fine = await proveApp(async () => new Response("hi", { headers: { "x-forge-app": "a" } }), "https://a--k--f.test/", "a");
64 expect(fine.seen).toBeUndefined();
65 });
66 });
67
5768 describe("a forge watches its children's addresses", () => {
5869 let dir = "";
5970 let parent: App;

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs