From cfff6f9c356cd6ddd6b0c2e12a6f91bbbd604e74 Mon Sep 17 00:00:00 2001 From: Claude Date: Sat, 21 Nov 2026 23:45:13 -0600 Subject: [PATCH] feat(pm-stack): the console hands the Android app to the signed-in owner The signed APK had no way onto the phone that did not publish it somewhere else first. The console now serves the build that is on the host at /console/pm-console.apk, to the signed-in owner only, and the page header shows "Android app" when a build is there. Putting a new signed file in place is the whole release. - config: PM_CONSOLE_ANDROID_APK (default ~/.local/state/pm-console/android/pm-console.apk). - /console/api/me reports whether a build is present. Tests: 33 bun tests. Five planted faults in the route are all caught (no existence check, any method, wrong content type, the page told a build exists when none does, the setting ignored). Co-Authored-By: Claude Fable 5.2 Claude-Session: https://claude.ai/code/session_01YLT5K1QVuzZjfWGFhXSSNF --- pm-stack/console/README.md | 30 +++++++++++++++------------ pm-stack/console/android/README.md | 18 +++++++++++++--- pm-stack/console/public/app.js | 5 ++++- pm-stack/console/public/index.html | 3 +++ pm-stack/console/src/config.ts | 4 ++++ pm-stack/console/src/server.ts | 22 +++++++++++++++++++- pm-stack/console/test/console.test.ts | 30 ++++++++++++++++++++++++++- 7 files changed, 93 insertions(+), 19 deletions(-) diff --git a/pm-stack/console/README.md b/pm-stack/console/README.md index c996761f03..5d2f37afb4 100644 --- a/pm-stack/console/README.md +++ b/pm-stack/console/README.md @@ -85,17 +85,18 @@ the route if the console does not come up with its sign-in settings. ## Configuration -| Variable | Default | Meaning | -| --------------------------------------------------------- | ------------------------------------------------- | -------------------------------------------------- | -| `PM_CONSOLE_PORT`, `PM_CONSOLE_BIND` | `4872`, `127.0.0.1` | Listener; the address must be loopback | -| `PM_CONSOLE_OWNER_EMAILS` | none (nobody) | Comma-separated owner addresses | -| `PM_CONSOLE_ACCESS_TEAM_DOMAIN`, `PM_CONSOLE_ACCESS_AUD` | unset | The Access issuer; set together | -| `PM_CONSOLE_ISSUERS_FILE` | unset | JSON list of further issuers | -| `PM_CONSOLE_CERT_NAMES` | none | Certificate common names that are the owner | -| `PM_CONSOLE_DASHBOARD_URL` | `http://127.0.0.1:4870` | The dashboard behind the gate; loopback, or empty | -| `PM_CONSOLE_UNITS`, `PM_CONSOLE_PROTECTED_UNITS` | the stack's units | Units the console manages; units it will not stop | -| `PM_CONSOLE_ANDROID_PACKAGE`, `PM_CONSOLE_ANDROID_SHA256` | `today.developing.pmconsole`, none | The Android app's identity in the asset-links file | -| `PM_CONSOLE_REPO`, `PM_CONSOLE_QUEUE` | `developing-today/code`, `repo-health-code-pilot` | What it operates on | +| Variable | Default | Meaning | +| --------------------------------------------------------- | -------------------------------------------------- | -------------------------------------------------- | +| `PM_CONSOLE_PORT`, `PM_CONSOLE_BIND` | `4872`, `127.0.0.1` | Listener; the address must be loopback | +| `PM_CONSOLE_OWNER_EMAILS` | none (nobody) | Comma-separated owner addresses | +| `PM_CONSOLE_ACCESS_TEAM_DOMAIN`, `PM_CONSOLE_ACCESS_AUD` | unset | The Access issuer; set together | +| `PM_CONSOLE_ISSUERS_FILE` | unset | JSON list of further issuers | +| `PM_CONSOLE_CERT_NAMES` | none | Certificate common names that are the owner | +| `PM_CONSOLE_DASHBOARD_URL` | `http://127.0.0.1:4870` | The dashboard behind the gate; loopback, or empty | +| `PM_CONSOLE_UNITS`, `PM_CONSOLE_PROTECTED_UNITS` | the stack's units | Units the console manages; units it will not stop | +| `PM_CONSOLE_ANDROID_PACKAGE`, `PM_CONSOLE_ANDROID_SHA256` | `today.developing.pmconsole`, none | The Android app's identity in the asset-links file | +| `PM_CONSOLE_ANDROID_APK` | `~/.local/state/pm-console/android/pm-console.apk` | The signed build the console hands to the owner | +| `PM_CONSOLE_REPO`, `PM_CONSOLE_QUEUE` | `developing-today/code`, `repo-health-code-pilot` | What it operates on | A malformed port, a listen address or dashboard address that is not loopback, or a malformed issuer stops the process at boot and names the @@ -105,5 +106,8 @@ variable. `pm-stack/console/android/` is a Trusted Web Activity: the same site in Chrome without browser chrome, so sign-in is Chrome's own and nothing is -reimplemented. The signed APK is built by the `pm-console-android` -workflow and attached to the release of the same name. See its README. +reimplemented. The `pm-console-android` workflow builds it and the host +signs it. Once the signed file is on the host, the console's header +shows "Android app": a download for the signed-in owner at +`/console/pm-console.apk`, so the phone gets the app from the site it +opens. See its README. diff --git a/pm-stack/console/android/README.md b/pm-stack/console/android/README.md index dd35f6877b..590614b880 100644 --- a/pm-stack/console/android/README.md +++ b/pm-stack/console/android/README.md @@ -45,6 +45,18 @@ reinstall; nothing else depends on it. ## Install -Download `pm-console.apk` from the -[release](https://github.com/developing-today/code/releases/tag/pm-console-android) -on the phone and open it. Later builds install over it. +On the phone, open `https://pm.security.cab/console/`, sign in, and tap +"Android app" in the header. That downloads the signed build from the +host (`/console/pm-console.apk`, owner only); open it to install. Later +builds install over it. + +The console serves whatever signed build is at +`~/.local/state/pm-console/android/pm-console.apk` on the host, so +putting a new build in place is the whole release: + +```bash +install -m 644 /tmp/pm-apk/pm-console.apk ~/.local/state/pm-console/android/pm-console.apk +``` + +The same file can also be attached to the GitHub release +`pm-console-android` for a copy outside the host. diff --git a/pm-stack/console/public/app.js b/pm-stack/console/public/app.js index d6409fb276..953a9d0d4e 100644 --- a/pm-stack/console/public/app.js +++ b/pm-stack/console/public/app.js @@ -232,5 +232,8 @@ async function show(name) { } for (const [name, [label]] of Object.entries(VIEWS)) tabs.append(h("button", { "data-view": name, onclick: () => show(name) }, label)); -api("me").then((me) => (document.getElementById("who").textContent = me.email || me.certName || me.via)).catch(() => {}); +api("me").then((me) => { + document.getElementById("who").textContent = me.email || me.certName || me.via; + document.getElementById("apk").hidden = !me.androidApp; +}).catch(() => {}); show(location.hash.slice(1)); diff --git a/pm-stack/console/public/index.html b/pm-stack/console/public/index.html index ede8ee7d4b..fc072f8465 100644 --- a/pm-stack/console/public/index.html +++ b/pm-stack/console/public/index.html @@ -16,6 +16,9 @@
Console Dashboard +
diff --git a/pm-stack/console/src/config.ts b/pm-stack/console/src/config.ts index 182782781d..274150780f 100644 --- a/pm-stack/console/src/config.ts +++ b/pm-stack/console/src/config.ts @@ -49,6 +49,9 @@ export interface Config { /** Android app identity served at /.well-known/assetlinks.json. */ androidPackage: string; androidFingerprints: string[]; + /** The signed APK the console hands to the signed-in owner, if the + * file exists. */ + androidApk: string; } export class ConfigError extends Error {} @@ -207,5 +210,6 @@ export function readConfig( dashboardUrl, androidPackage: env.PM_CONSOLE_ANDROID_PACKAGE ?? "today.developing.pmconsole", androidFingerprints: fingerprints, + androidApk: env.PM_CONSOLE_ANDROID_APK ?? `${home}/.local/state/pm-console/android/pm-console.apk`, }; } diff --git a/pm-stack/console/src/server.ts b/pm-stack/console/src/server.ts index 5ae4403cda..a6fef3e7a3 100644 --- a/pm-stack/console/src/server.ts +++ b/pm-stack/console/src/server.ts @@ -21,6 +21,7 @@ const STATIC = new Map([ ["app.css", "text/css; charset=utf-8"], ["icon.svg", "image/svg+xml"], ]); +const APK_NAME = "pm-console.apk"; const SECURITY = { "Content-Security-Policy": "default-src 'self'; img-src 'self' data:; frame-ancestors 'none'; base-uri 'none'; form-action 'self'", @@ -204,6 +205,20 @@ export async function handle(req: Request, deps: Deps, peer: string | null = nul } const type = method === "GET" ? STATIC.get(rest) : undefined; if (type !== undefined) return file(rest, type); + // The Android app, for the signed-in owner: the phone gets it from the + // site it opens, and nothing has to be published anywhere else. + if (method === "GET" && rest === APK_NAME) { + const apk = Bun.file(deps.cfg.androidApk); + if (!(await apk.exists())) return json({ error: "no Android build is installed on this host" }, 404); + return new Response(apk, { + headers: { + "Content-Type": "application/vnd.android.package-archive", + "Content-Disposition": `attachment; filename="${APK_NAME}"`, + "Cache-Control": "no-store", + ...SECURITY, + }, + }); + } if (!rest.startsWith("api/")) return json({ error: "not found" }, 404); let seg: string[]; @@ -219,7 +234,12 @@ export async function handle(req: Request, deps: Deps, peer: string | null = nul if (seg.length === 1) { switch (seg[0]) { case "me": - return json({ ...who, repo: deps.cfg.repo, queue: deps.cfg.queue }); + return json({ + ...who, + repo: deps.cfg.repo, + queue: deps.cfg.queue, + androidApp: await Bun.file(deps.cfg.androidApk).exists(), + }); case "overview": return json(await ops.overview()); case "queue": diff --git a/pm-stack/console/test/console.test.ts b/pm-stack/console/test/console.test.ts index 1996b12229..82637ac9d0 100644 --- a/pm-stack/console/test/console.test.ts +++ b/pm-stack/console/test/console.test.ts @@ -13,7 +13,9 @@ // compared with literal URLs, so no spelling of a path can pass as one // of the public files unless the dashboard is asked for exactly that file. import { beforeEach, describe, expect, test } from "bun:test"; -import { readFileSync } from "node:fs"; +import { mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs"; +import { tmpdir } from "node:os"; +import { join } from "node:path"; import { AuthError, authorize, @@ -1164,6 +1166,32 @@ describe("HTTP surface", () => { expect([failed.status, await failed.json()]).toEqual([502, { error: "the dashboard did not answer" }]); }); + test("the Android app is handed to the signed-in owner only, when a build is on the host", async () => { + const dir = mkdtempSync(join(tmpdir(), "pm-console-test-")); + try { + const cfg = readConfig({ HOME: "/h", PM_CONSOLE_ANDROID_APK: join(dir, "pm-console.apk") }); + expect(readConfig({ HOME: "/h" }).androidApk).toBe("/h/.local/state/pm-console/android/pm-console.apk"); + const owner = { ...deps("owner"), cfg }; + // No build yet: the page is told so, and the link answers 404. + expect((await (await handle(get("/console/api/me"), owner)).json()).androidApp).toBe(false); + expect((await handle(get("/console/pm-console.apk"), owner)).status).toBe(404); + writeFileSync(join(dir, "pm-console.apk"), "PK\u0003\u0004 signed apk bytes"); + expect((await (await handle(get("/console/api/me"), owner)).json()).androidApp).toBe(true); + const res = await handle(get("/console/pm-console.apk"), owner); + expect(res.status).toBe(200); + expect(res.headers.get("content-type")).toBe("application/vnd.android.package-archive"); + expect(res.headers.get("content-disposition")).toBe('attachment; filename="pm-console.apk"'); + expect(await res.text()).toBe("PK\u0003\u0004 signed apk bytes"); + // Not for anyone else, and not by another method or name. + expect((await handle(get("/console/pm-console.apk"), { ...deps(401), cfg })).status).toBe(401); + expect((await handle(get("/console/pm-console.apk"), { ...deps(403), cfg })).status).toBe(403); + expect((await handle(post("/console/pm-console.apk", {}), owner)).status).toBe(404); + expect(owner.sent).toEqual([]); // never passed to the dashboard + } finally { + rmSync(dir, { recursive: true, force: true }); + } + }); + test("an action that cannot be recorded is not taken; one already taken is reported as it went", async () => { const r = recorder(); const order: string[] = [];