Public: anyone can read this; the crew can change it.
Defect on the v1.0.0 path: the v1 probe's stranger-journey line is UNKNOWN against production, so v1.0.0 cannot be proven live #15541
Check · Review · Land · Live done — Landed and live in production.
scout/forge:scout/15540-defect-on-the-v1-0-0-path-the-v1-probe-s → main 0 commits fork 1 thread · 1 unresolved
3 files
changed +255
−25
src/release/host_check.ts +82 −21
| @@ -5,6 +5,13 @@ | ||
| 5 | 5 | * app its repository (`FORGE_APP_REPO`), its API (`FORGE_API_URL`) and a read-only token |
| 6 | 6 | * (`FORGE_TOKEN`); this asks `GET …/repos/<repo>/commits/<commit>/check` with them, and keeps |
| 7 | 7 | * only the state and the run id of the answer. |
| 8 | * | |
| 9 | * A host older than #15538 does not name the repository (#15540): then the app's own URL | |
| 10 | * (`FORGE_APP_URL`) is looked up in the host's `GET …/apps`, which an owner's token reads, and the | |
| 11 | * repository of the app served there is the one asked. That route is on every host since #406 | |
| 12 | * (`GET /api/v1/apps` in `http/api.ts`); production's token reads it as its owner, while a | |
| 13 | * preview's, bound to its one repository (`http/run_gate.ts`), is refused it and reads the list as | |
| 14 | * anyone does, without the token. | |
| 8 | 15 | */ |
| 9 | 16 | |
| 10 | 17 | import type { CommitCheckState } from "../checks/commit_check.ts"; |
| @@ -23,17 +30,21 @@ export const HOST_WAIT_MS = 2_000; | ||
| 23 | 30 | const STATES: readonly string[] = ["ok", "failed", "pending", "superseded", "none"]; |
| 24 | 31 | const REPO = /^[A-Za-z0-9._-]{1,80}\/[A-Za-z0-9._-]{1,80}$/; |
| 25 | 32 | |
| 26 | /** Where to ask, or undefined when this forge is not an app of a host that told it enough. */ | |
| 27 | export function hostCheckUrl(env: Env, commit: string): { url: string; token: string } | undefined { | |
| 28 | const repo = (env["FORGE_APP_REPO"] ?? "").trim(); | |
| 33 | const repoOk = (repo: string): boolean => REPO.test(repo) && !repo.split("/").some((part) => /^\.+$/.test(part)); | |
| 34 | ||
| 35 | /** The host's API and the app's token, or undefined when the host told neither. */ | |
| 36 | function hostApi(env: Env): { api: string; token: string } | undefined { | |
| 29 | 37 | const api = (env["FORGE_API_URL"] ?? "").trim().replace(/\/+$/, ""); |
| 30 | 38 | const token = (env["FORGE_TOKEN"] ?? "").trim(); |
| 31 | if (!REPO.test(repo) || repo.split("/").some((part) => /^\.+$/.test(part)) || !/^https?:\/\/[^/\s]+/.test(api) || token === "") return undefined; | |
| 32 | if (!/^[0-9a-f]{40}$/.test(commit)) return undefined; | |
| 33 | const [owner = "", name = ""] = repo.split("/"); | |
| 34 | return { url: `${api}/repos/${encodeURIComponent(owner)}/${encodeURIComponent(name)}/commits/${commit}/check`, token }; | |
| 39 | return /^https?:\/\/[^/\s]+/.test(api) && token !== "" ? { api, token } : undefined; | |
| 35 | 40 | } |
| 36 | 41 | |
| 42 | /** The check API of `repo`'s `commit` on the host at `api`. */ | |
| 43 | const checkUrl = (api: string, repo: string, commit: string): string => { | |
| 44 | const [owner = "", name = ""] = repo.split("/"); | |
| 45 | return `${api}/repos/${encodeURIComponent(owner)}/${encodeURIComponent(name)}/commits/${commit}/check`; | |
| 46 | }; | |
| 47 | ||
| 37 | 48 | /** The host's state and run id from its JSON answer, or undefined when it is not one. */ |
| 38 | 49 | export function verdictOf(body: unknown): HostVerdict | undefined { |
| 39 | 50 | if (body === null || typeof body !== "object") return undefined; |
| @@ -43,6 +54,64 @@ export function verdictOf(body: unknown): HostVerdict | undefined { | ||
| 43 | 54 | return { state: state as CommitCheckState, runId: id }; |
| 44 | 55 | } |
| 45 | 56 | |
| 57 | const originOf = (url: unknown): string | undefined => { | |
| 58 | if (typeof url !== "string") return undefined; | |
| 59 | try { | |
| 60 | return new URL(url).origin; | |
| 61 | } catch { | |
| 62 | return undefined; | |
| 63 | } | |
| 64 | }; | |
| 65 | ||
| 66 | /** | |
| 67 | * The `owner/name` of the app served at `appUrl`, from the host's `GET …/apps` answer: the app | |
| 68 | * whose production URL, or one of whose environments' URLs, has that origin; undefined otherwise. | |
| 69 | */ | |
| 70 | export function repoOfApps(body: unknown, appUrl: string): string | undefined { | |
| 71 | const want = originOf(appUrl); | |
| 72 | const items = (body as { items?: unknown } | null)?.items; | |
| 73 | if (want === undefined || !Array.isArray(items)) return undefined; | |
| 74 | for (const item of items as unknown[]) { | |
| 75 | if (item === null || typeof item !== "object") continue; | |
| 76 | const { url, environments, repo } = item as { url?: unknown; environments?: unknown; repo?: unknown }; | |
| 77 | const urls = [url, ...(Array.isArray(environments) ? environments.map((e) => (e as { url?: unknown } | null)?.url) : [])]; | |
| 78 | if (!urls.some((u) => originOf(u) === want)) continue; | |
| 79 | const { owner, name } = (repo ?? {}) as { owner?: unknown; name?: unknown }; | |
| 80 | const full = typeof owner === "string" && typeof name === "string" ? `${owner}/${name}` : ""; | |
| 81 | return repoOk(full) ? full : undefined; | |
| 82 | } | |
| 83 | return undefined; | |
| 84 | } | |
| 85 | ||
| 86 | type Fetcher = (url: string, init: RequestInit) => Promise<Response>; | |
| 87 | ||
| 88 | /** GETs `url` with the token and answers its JSON, or undefined on anything else. */ | |
| 89 | async function readJson(fetcher: Fetcher, url: string, token: string | undefined): Promise<unknown> { | |
| 90 | try { | |
| 91 | const response = await fetcher(url, { | |
| 92 | headers: token === undefined ? { accept: "application/json" } : { authorization: `Bearer ${token}`, accept: "application/json" }, | |
| 93 | signal: AbortSignal.timeout(HOST_WAIT_MS), | |
| 94 | redirect: "error", | |
| 95 | }); | |
| 96 | return response.ok ? await response.json() : undefined; | |
| 97 | } catch { | |
| 98 | return undefined; | |
| 99 | } | |
| 100 | } | |
| 101 | ||
| 102 | /** | |
| 103 | * The repository the host named, else the one its apps list serves at `FORGE_APP_URL`: read with | |
| 104 | * the token, and once without it when the token is refused the list (a preview's). | |
| 105 | */ | |
| 106 | async function appRepo(env: Env, host: { api: string; token: string }, fetcher: Fetcher): Promise<string | undefined> { | |
| 107 | const named = (env["FORGE_APP_REPO"] ?? "").trim(); | |
| 108 | if (named !== "") return named; | |
| 109 | const appUrl = (env["FORGE_APP_URL"] ?? "").trim(); | |
| 110 | if (appUrl === "") return undefined; | |
| 111 | const list = `${host.api}/apps`; | |
| 112 | return repoOfApps(await readJson(fetcher, list, host.token), appUrl) ?? repoOfApps(await readJson(fetcher, list, undefined), appUrl); | |
| 113 | } | |
| 114 | ||
| 46 | 115 | /** |
| 47 | 116 | * The host's verdict on `commit`, or undefined when there is no host to ask, it does not answer |
| 48 | 117 | * in time, or answers anything but the check's JSON. |
| @@ -50,19 +119,11 @@ export function verdictOf(body: unknown): HostVerdict | undefined { | ||
| 50 | 119 | export async function hostCheck( |
| 51 | 120 | env: Env, |
| 52 | 121 | commit: string, |
| 53 | fetcher: (url: string, init: RequestInit) => Promise<Response> = fetch, | |
| 122 | fetcher: Fetcher = fetch, | |
| 54 | 123 | ): Promise<HostVerdict | undefined> { |
| 55 | const asked = hostCheckUrl(env, commit); | |
| 56 | if (asked === undefined) return undefined; | |
| 57 | try { | |
| 58 | const response = await fetcher(asked.url, { | |
| 59 | headers: { authorization: `Bearer ${asked.token}`, accept: "application/json" }, | |
| 60 | signal: AbortSignal.timeout(HOST_WAIT_MS), | |
| 61 | redirect: "error", | |
| 62 | }); | |
| 63 | if (!response.ok) return undefined; | |
| 64 | return verdictOf(await response.json()); | |
| 65 | } catch { | |
| 66 | return undefined; | |
| 67 | } | |
| 124 | const host = hostApi(env); | |
| 125 | if (host === undefined || !/^[0-9a-f]{40}$/.test(commit)) return undefined; | |
| 126 | const repo = await appRepo(env, host, fetcher); | |
| 127 | if (repo === undefined || !repoOk(repo)) return undefined; | |
| 128 | return verdictOf(await readJson(fetcher, checkUrl(host.api, repo, commit), host.token)); | |
| 68 | 129 | } |
test/host-check-apps.test.ts added +132
| @@ -0,0 +1,132 @@ | ||
| 1 | /** | |
| 2 | * A host older than #15538 names no FORGE_APP_REPO (#15540): the hosted forge finds its repository | |
| 3 | * in the host's `GET /api/v1/apps` by its own URL. Here the host is this forge, served for real, | |
| 4 | * and the app is told exactly what `appEnv` tells it, so the shape of the list and who may read it | |
| 5 | * are pinned to the route itself, not to a hand-written answer. | |
| 6 | */ | |
| 7 | import { afterAll, beforeAll, expect, test } from "bun:test"; | |
| 8 | import { mkdtemp, rm } from "node:fs/promises"; | |
| 9 | import { tmpdir } from "node:os"; | |
| 10 | import { join } from "node:path"; | |
| 11 | import { createApp as createForge, type App } from "../src/app.ts"; | |
| 12 | import { commitToBranch, createRepo } from "../src/actions/repos.ts"; | |
| 13 | import { createApp, getAppBySlug } from "../src/core/apps.ts"; | |
| 14 | import { createUser } from "../src/core/users.ts"; | |
| 15 | import { appEnv } from "../src/apps/process.ts"; | |
| 16 | import { AppsRuntime } from "../src/apps/runtime.ts"; | |
| 17 | import { mintAppToken } from "../src/apps/token.ts"; | |
| 18 | import { serve, type Servers } from "../src/http/server.ts"; | |
| 19 | import { hostCheck } from "../src/release/host_check.ts"; | |
| 20 | import { quietLogs } from "./helpers/globals.ts"; | |
| 21 | ||
| 22 | quietLogs(); | |
| 23 | ||
| 24 | let dataDir: string; | |
| 25 | let app: App; | |
| 26 | let servers: Servers; | |
| 27 | let runtime: AppsRuntime; | |
| 28 | let ownerId = ""; | |
| 29 | const made: Record<string, { commit: string }> = {}; | |
| 30 | const cells = process.env["FORGE_CELLS"]; | |
| 31 | ||
| 32 | beforeAll(async () => { | |
| 33 | process.env["FORGE_CELLS"] = "off"; | |
| 34 | dataDir = await mkdtemp(join(tmpdir(), "forge-host-check-apps-")); | |
| 35 | app = createForge({ dataDir, port: 0 }); | |
| 36 | servers = await serve(app); | |
| 37 | runtime = new AppsRuntime(app, { domain: "apps.test", scheme: "https", forgeUrl: servers.url }); | |
| 38 | const user = await createUser(app.db, { | |
| 39 | origin: "seed", | |
| 40 | trusted: true, | |
| 41 | username: "trevato", | |
| 42 | email: "[email protected]", | |
| 43 | password: "a-good-passphrase", | |
| 44 | adminIfFirst: true, | |
| 45 | }); | |
| 46 | if (!user.ok) throw new Error(String(user.error)); | |
| 47 | ownerId = user.value.id; | |
| 48 | for (const [name, visibility, slug] of [["forge", "private", "lab"], ["notes", "public", "notes"]] as const) { | |
| 49 | const repo = await createRepo(app, user.value, { name, visibility }); | |
| 50 | if (!repo.ok) throw new Error(String(repo.error)); | |
| 51 | const commit = await commitToBranch(app, user.value, repo.value, { | |
| 52 | branch: repo.value.defaultBranch, | |
| 53 | message: "init", | |
| 54 | files: [{ path: "README.md", content: "hi\n" }], | |
| 55 | }); | |
| 56 | if (!commit.ok) throw new Error(String(commit.error)); | |
| 57 | const record = createApp(app.db, { repoId: repo.value.id, slug, access: "public" }); | |
| 58 | if (!record.ok) throw new Error(record.error); | |
| 59 | made[slug] = { commit: commit.value.commitOid }; | |
| 60 | } | |
| 61 | }); | |
| 62 | ||
| 63 | afterAll(async () => { | |
| 64 | await servers.stop(); | |
| 65 | app.close(); | |
| 66 | if (cells === undefined) delete process.env["FORGE_CELLS"]; | |
| 67 | else process.env["FORGE_CELLS"] = cells; | |
| 68 | await rm(dataDir, { recursive: true, force: true }); | |
| 69 | }); | |
| 70 | ||
| 71 | /** What the host tells the app `slug` in `env`, minus FORGE_APP_REPO, as a 0.7.629 host does. */ | |
| 72 | function oldHostEnv(slug: string, env: "production" | "pull-7"): Record<string, string> { | |
| 73 | const record = getAppBySlug(app.db, slug); | |
| 74 | if (record === undefined) throw new Error(slug); | |
| 75 | const url = runtime.urlFor(record, env); | |
| 76 | const told = appEnv({ | |
| 77 | port: 1, | |
| 78 | dataDir, | |
| 79 | env, | |
| 80 | url, | |
| 81 | commitOid: made[slug]!.commit, | |
| 82 | forgeUrl: servers.url, | |
| 83 | identityKey: "k", | |
| 84 | forgeToken: mintAppToken(app.db, ownerId, slug, env).token, | |
| 85 | }); | |
| 86 | expect(told["FORGE_APP_REPO"]).toBeUndefined(); | |
| 87 | return told; | |
| 88 | } | |
| 89 | ||
| 90 | /** `fetch`, noting each URL asked and whether it carried the token. */ | |
| 91 | function recording(): { asked: string[]; fetcher: (url: string, init: RequestInit) => Promise<Response> } { | |
| 92 | const asked: string[] = []; | |
| 93 | return { | |
| 94 | asked, | |
| 95 | fetcher: (url, init) => { | |
| 96 | asked.push(`${new URL(url).pathname} ${new Headers(init.headers).has("authorization") ? "token" : "anon"}`); | |
| 97 | return fetch(url, init); | |
| 98 | }, | |
| 99 | }; | |
| 100 | } | |
| 101 | ||
| 102 | test("production's token reads the host's apps list, which names its private repository at its own URL", async () => { | |
| 103 | const env = oldHostEnv("lab", "production"); | |
| 104 | const { asked, fetcher } = recording(); | |
| 105 | const verdict = await hostCheck(env, made["lab"]!.commit, fetcher); | |
| 106 | expect(verdict?.state).toBe("none"); | |
| 107 | expect(asked).toEqual(["/api/v1/apps token", `/api/v1/repos/trevato/forge/commits/${made["lab"]!.commit}/check token`]); | |
| 108 | }); | |
| 109 | ||
| 110 | test("a preview's token is refused the list; read without it, the list still names each public app's repository and URL", async () => { | |
| 111 | const env = oldHostEnv("notes", "pull-7"); | |
| 112 | const refused = await fetch(`${env["FORGE_API_URL"]}/apps`, { headers: { authorization: `Bearer ${env["FORGE_TOKEN"]}` } }); | |
| 113 | expect(refused.status).toBe(404); | |
| 114 | const anon = await fetch(`${env["FORGE_API_URL"]}/apps`); | |
| 115 | expect(anon.status).toBe(200); | |
| 116 | const items = ((await anon.json()) as { items: { slug: string; url: unknown; repo: unknown; environments: unknown }[] }).items; | |
| 117 | const notes = items.find((i) => i.slug === "notes"); | |
| 118 | expect(notes?.repo).toEqual({ owner: "trevato", name: "notes" }); | |
| 119 | expect(notes?.url).toBe(oldHostEnv("notes", "production")["FORGE_APP_URL"]); | |
| 120 | expect(Array.isArray(notes?.environments)).toBe(true); | |
| 121 | // So the preview, refused with its token, asks the list again without it. | |
| 122 | const { asked, fetcher } = recording(); | |
| 123 | await hostCheck(env, made["notes"]!.commit, fetcher); | |
| 124 | expect(asked.slice(0, 2)).toEqual(["/api/v1/apps token", "/api/v1/apps anon"]); | |
| 125 | }); | |
| 126 | ||
| 127 | test("an app the list does not serve at that URL asks no check at all", async () => { | |
| 128 | const env = { ...oldHostEnv("lab", "production"), FORGE_APP_URL: "https://nobody.apps.test" }; | |
| 129 | const { asked, fetcher } = recording(); | |
| 130 | expect(await hostCheck(env, made["lab"]!.commit, fetcher)).toBeUndefined(); | |
| 131 | expect(asked.every((a) => a.startsWith("/api/v1/apps "))).toBe(true); | |
| 132 | }); |
test/running-check-hosted.test.ts +41 −4
| @@ -9,7 +9,7 @@ import { tmpdir } from "node:os"; | ||
| 9 | 9 | import { join } from "node:path"; |
| 10 | 10 | import { createApp, type App } from "../src/app.ts"; |
| 11 | 11 | import { appEnv } from "../src/apps/process.ts"; |
| 12 | import { hostCheck, hostCheckUrl, verdictOf } from "../src/release/host_check.ts"; | |
| 12 | import { hostCheck, repoOfApps, verdictOf } from "../src/release/host_check.ts"; | |
| 13 | 13 | import { HOST_FRESH_MS, runningCheck } from "../src/release/running_check.ts"; |
| 14 | 14 | import { shippedAnswer } from "../src/release/answer.ts"; |
| 15 | 15 | import { quietLogs } from "./helpers/globals.ts"; |
| @@ -107,9 +107,46 @@ test("the host is asked at its check API with the token, and only state and run | ||
| 107 | 107 | test("anything but the check's JSON, or a host that is not named, is no verdict", async () => { |
| 108 | 108 | expect(await hostCheck(HOST, OID("f"), async () => new Response("no", { status: 404 }))).toBeUndefined(); |
| 109 | 109 | expect(await hostCheck(HOST, OID("f"), async () => { throw new Error("refused"); })).toBeUndefined(); |
| 110 | expect(hostCheckUrl({ ...HOST, FORGE_APP_REPO: "../x" }, OID("f"))).toBeUndefined(); | |
| 111 | expect(hostCheckUrl({ ...HOST, FORGE_TOKEN: "" }, OID("f"))).toBeUndefined(); | |
| 112 | expect(hostCheckUrl(HOST, "nope")).toBeUndefined(); | |
| 110 | let asked = 0; | |
| 111 | const never = async () => { | |
| 112 | asked++; | |
| 113 | return Response.json({ state: "ok", runId: "x" }); | |
| 114 | }; | |
| 115 | expect(await hostCheck({ ...HOST, FORGE_APP_REPO: "../x" }, OID("f"), never)).toBeUndefined(); | |
| 116 | expect(await hostCheck({ ...HOST, FORGE_TOKEN: "" }, OID("f"), never)).toBeUndefined(); | |
| 117 | expect(await hostCheck(HOST, "nope", never)).toBeUndefined(); | |
| 118 | expect(asked).toBe(0); | |
| 113 | 119 | expect(verdictOf({ state: "great", runId: "x" })).toBeUndefined(); |
| 114 | 120 | expect(verdictOf({ state: "ok", runId: "<script>" })).toEqual({ state: "ok", runId: null }); |
| 115 | 121 | }); |
| 122 | ||
| 123 | // A host older than #15538 names no FORGE_APP_REPO (#15540): the app finds its repository in the | |
| 124 | // host's apps list by its own URL, so production's changelog still states green, red or nothing. | |
| 125 | test("a host that names no repository is asked its apps list, and the app's own URL picks the repository", async () => { | |
| 126 | const OLD = { FORGE_API_URL: "https://forge.test/api/v1", FORGE_TOKEN: "ro-token", FORGE_APP_URL: "https://lab--forge-trevato-apps.test" }; | |
| 127 | const apps = { | |
| 128 | items: [ | |
| 129 | { url: "https://other--x-apps.test", repo: { owner: "someone", name: "else" }, environments: [] }, | |
| 130 | { url: "https://lab--forge-trevato-apps.test", repo: { owner: "trevato", name: "forge" }, environments: [] }, | |
| 131 | ], | |
| 132 | }; | |
| 133 | // The host's answer to each URL: its apps list, then the check in `state` (null: no check). | |
| 134 | const host = (state: string | null) => { | |
| 135 | const asked: string[] = []; | |
| 136 | const answer = (url: string): Response => | |
| 137 | url.endsWith("/apps") ? Response.json(apps) : state === null ? new Response("gone", { status: 404 }) : Response.json({ state, runId: "r1", summary: "log" }); | |
| 138 | return { asked, fetcher: async (url: string) => (asked.push(url), answer(url)) }; | |
| 139 | }; | |
| 140 | const green = host("ok"); | |
| 141 | expect(await hostCheck(OLD, OID("a"), green.fetcher)).toEqual({ state: "ok", runId: "r1" }); | |
| 142 | expect(green.asked).toEqual(["https://forge.test/api/v1/apps", `https://forge.test/api/v1/repos/trevato/forge/commits/${OID("a")}/check`]); | |
| 143 | expect(await hostCheck(OLD, OID("a"), host("failed").fetcher)).toEqual({ state: "failed", runId: "r1" }); | |
| 144 | expect(await hostCheck(OLD, OID("a"), host(null).fetcher)).toBeUndefined(); | |
| 145 | // Not in the list, or the list unreadable: no verdict, and the check API is never guessed at. | |
| 146 | const lost = host("ok"); | |
| 147 | expect(await hostCheck({ ...OLD, FORGE_APP_URL: "https://nope.test" }, OID("a"), lost.fetcher)).toBeUndefined(); | |
| 148 | expect(lost.asked).toEqual(["https://forge.test/api/v1/apps", "https://forge.test/api/v1/apps"]); | |
| 149 | expect(repoOfApps({ items: [{ url: "https://a.test", repo: { owner: "..", name: "x" } }] }, "https://a.test")).toBeUndefined(); | |
| 150 | expect(repoOfApps({ items: [{ url: null, environments: [{ url: "https://p--a.test" }], repo: { owner: "o", name: "r" } }] }, "https://p--a.test/")).toBe("o/r"); | |
| 151 | expect(repoOfApps("nope", "https://a.test")).toBeUndefined(); | |
| 152 | }); |
new above
offline
This
${host.api}/appsroute returns 404 on the current forge API. Prove it exists on the production host, or use a route that does.