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
Check · Review · Land · Live done — Landed and live in production.
mason/forge:mason/13738-capture-the-response-headers-of-a-bare-4 → main 2 commits fork
3 files
changed +49
−11
src/spawn/reach.ts +37 −10
| @@ -50,6 +50,11 @@ export interface Proof { | ||
| 50 | 50 | readonly status: number | null; |
| 51 | 51 | /** One sentence: what was asked and what is wrong, or "answered". */ |
| 52 | 52 | 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>>; | |
| 53 | 58 | } |
| 54 | 59 | |
| 55 | 60 | export interface ChildReach { |
| @@ -59,7 +64,9 @@ export interface ChildReach { | ||
| 59 | 64 | readonly ok: boolean; |
| 60 | 65 | /** When it first failed in the run of failures it is in, ISO; null while it is reachable. */ |
| 61 | 66 | 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 | */ | |
| 63 | 70 | readonly alarmed: boolean; |
| 64 | 71 | readonly proofs: readonly Proof[]; |
| 65 | 72 | } |
| @@ -67,7 +74,10 @@ export interface ChildReach { | ||
| 67 | 74 | type FetchLike = (url: string, init?: RequestInit) => Promise<Response>; |
| 68 | 75 | |
| 69 | 76 | 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 | */ | |
| 71 | 81 | readonly off?: boolean; |
| 72 | 82 | readonly fetch?: FetchLike; |
| 73 | 83 | readonly settleMs?: number; |
| @@ -105,13 +115,28 @@ export const appUrlOf = (app: App, child: Pick<Child, "name">, slug: string): st | ||
| 105 | 115 | */ |
| 106 | 116 | export async function slugsOf(app: App, child: Child): Promise<string[]> { |
| 107 | 117 | 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. | |
| 111 | 122 | for (const slug of await childPublicSlugs(childDirOf(app, child.bodyId))) if (isSlug(slug)) slugs.add(slug); |
| 112 | 123 | return [...slugs].sort(); |
| 113 | 124 | } |
| 114 | 125 | |
| 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 | ||
| 115 | 140 | async function ask(fetcher: FetchLike, url: string): Promise<{ status: number; headers: Headers; body: string } | string> { |
| 116 | 141 | try { |
| 117 | 142 | 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">): | ||
| 134 | 159 | claimed = false; |
| 135 | 160 | } |
| 136 | 161 | 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); | |
| 138 | 163 | } |
| 139 | 164 | |
| 140 | 165 | /** 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 | ||
| 142 | 167 | const got = await ask(fetcher, url); |
| 143 | 168 | if (typeof got === "string") return { label: slug, url, ok: false, status: null, says: `no answer (${got})` }; |
| 144 | 169 | 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); | |
| 148 | 173 | } |
| 149 | 174 | |
| 150 | 175 | /** |
| @@ -290,7 +315,9 @@ export async function sweepReach(app: App, asleep: (name: string) => boolean = ( | ||
| 290 | 315 | } |
| 291 | 316 | } |
| 292 | 317 | |
| 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 | */ | |
| 294 | 321 | export function startReachWatch( |
| 295 | 322 | app: App, |
| 296 | 323 | 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 | ||
| 75 | 75 | expect(made.reach.says).toContain("Running, not reachable: notes answered 200, not by the app"); |
| 76 | 76 | expect(made.reach.proofs).toEqual([ |
| 77 | 77 | { 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": "" } }, | |
| 79 | 79 | ]); |
| 80 | 80 | // The never-answering app is the alarm, once its time to come up is over. |
| 81 | 81 | 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", () => { | ||
| 54 | 54 | }); |
| 55 | 55 | }); |
| 56 | 56 | |
| 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 | ||
| 57 | 68 | describe("a forge watches its children's addresses", () => { |
| 58 | 69 | let dir = ""; |
| 59 | 70 | let parent: App; |
new above
offline