diff --git a/pm-stack/aggregator/src/console-gate.ts b/pm-stack/aggregator/src/console-gate.ts new file mode 100644 index 0000000000..d0ccbd8b1b --- /dev/null +++ b/pm-stack/aggregator/src/console-gate.ts @@ -0,0 +1,27 @@ +/** Add the console shortcut only to dashboard responses served through its public gate. */ +export function throughConsoleGate(request: Request, publicHost = "pm.security.cab"): boolean { + const requestHost = (request.headers.get("host") ?? "").trim().toLowerCase().replace(/:\d+$/, ""); + const expectedHost = publicHost.trim().toLowerCase().replace(/:\d+$/, ""); + return requestHost !== "" && requestHost === expectedHost; +} + +export async function addConsoleShortcut(response: Response, request: Request, publicHost?: string): Promise { + if (!throughConsoleGate(request, publicHost)) return response; + const type = response.headers.get("content-type") ?? ""; + const path = new URL(request.url).pathname; + let body: string; + if (path === "/manifest.webmanifest" && type.includes("application/manifest+json")) { + const manifest = JSON.parse(await response.text()) as Record; + const shortcuts = Array.isArray(manifest.shortcuts) ? manifest.shortcuts : []; + if (!shortcuts.some((item) => item && typeof item === "object" && (item as { url?: unknown }).url === "/console/")) { + manifest.shortcuts = [...shortcuts, { name: "Console", short_name: "Console", url: "/console/" }]; + } + body = JSON.stringify(manifest, null, 2); + } else if (type.includes("text/html")) { + body = await response.text(); + body = body.replace("", 'Console'); + } else { + return response; + } + return new Response(body, { status: response.status, statusText: response.statusText, headers: response.headers }); +} diff --git a/pm-stack/aggregator/src/server.ts b/pm-stack/aggregator/src/server.ts index 57a13edb81..2e2ee62d5a 100644 --- a/pm-stack/aggregator/src/server.ts +++ b/pm-stack/aggregator/src/server.ts @@ -223,6 +223,7 @@ import { } from "./notifications.ts"; import { readJsonBody } from "./request-body.ts"; import { parseRegistryQuery, queryRegistry } from "./registry-view.ts"; +import { addConsoleShortcut } from "./console-gate.ts"; import { searchWork } from "./search-view.ts"; import type { Source } from "./source.ts"; import { fixtureSource } from "./sources/fixtures.ts"; @@ -4480,7 +4481,8 @@ async function fetchHandler(req: Request): Promise { recordRender(route, res.status < 400); } } - return res; + const publicHost = process.env.PM_AGG_CONSOLE_HOST?.trim() || "pm.security.cab"; + return addConsoleShortcut(res, req, publicHost); } // One server per bind address (PM_AGG_HOSTS). Deployment on the diff --git a/pm-stack/aggregator/test/a11y-1215.test.ts b/pm-stack/aggregator/test/a11y-1215.test.ts index 55b05bbca3..b447a2ec70 100644 --- a/pm-stack/aggregator/test/a11y-1215.test.ts +++ b/pm-stack/aggregator/test/a11y-1215.test.ts @@ -8,6 +8,7 @@ import { join } from "node:path"; import { PAGES } from "../src/pages.ts"; import { navLinks, pageDropdown, renderAa } from "../src/render.ts"; import { FIXTURE_AA } from "../src/sources/fixtures.ts"; +import { addConsoleShortcut } from "../src/console-gate.ts"; const renderSource = readFileSync(join(import.meta.dir, "..", "src", "ui.css"), "utf8") + @@ -108,6 +109,21 @@ describe("one navigation model (#1215, curated by #1497)", () => { }); }); +describe("console link accessibility (#12416)", () => { + test("the gated link has an accessible name, and direct hosts omit it", async () => { + const response = (host: string) => + addConsoleShortcut( + new Response('', { headers: { "content-type": "text/html" } }), + new Request("http://aggregator/", { headers: { host } }), + "pm.security.cab", + ); + const gated = await (await response("pm.security.cab")).text(); + expect(gated).toContain('Console'); + const direct = await (await response("100.64.0.1")).text(); + expect(direct).not.toContain("/console/"); + }); +}); + describe("the emitted explorer script stays valid (#1215, Codex P1 on PR #3308)", () => { test("the /aa inline script parses — attribute quotes survive both string layers", () => { const prov = { diff --git a/pm-stack/aggregator/test/curation-1497.test.ts b/pm-stack/aggregator/test/curation-1497.test.ts index 27e822dc1d..2fd1daf8d4 100644 --- a/pm-stack/aggregator/test/curation-1497.test.ts +++ b/pm-stack/aggregator/test/curation-1497.test.ts @@ -8,6 +8,7 @@ import { PAGES, SUBROUTE_PARENTS, manifestRouteFor, pageByRoute, primaryGroupedP import { buildProgramData } from "../src/program.ts"; import { navLinks, pageDropdown, renderPages, renderProgram } from "../src/render.ts"; import { STANDING_IDENTITIES } from "../src/registry.ts"; +import { addConsoleShortcut } from "../src/console-gate.ts"; const PROV: Provenance = { board: "x", @@ -106,6 +107,22 @@ describe("the #1497 decision table is applied to the manifest", () => { }); }); +describe("console-gated install shortcut (#12416)", () => { + test("the web manifest gains /console/ only on the public Host", async () => { + const manifest = { name: "PM Aggregator", start_url: "/attention" }; + const response = (host: string) => + addConsoleShortcut( + new Response(JSON.stringify(manifest), { headers: { "content-type": "application/manifest+json" } }), + new Request("http://aggregator/manifest.webmanifest", { headers: { host } }), + "pm.security.cab", + ); + const gated = JSON.parse(await (await response("pm.security.cab")).text()); + expect(gated.shortcuts).toContainEqual({ name: "Console", short_name: "Console", url: "/console/" }); + const direct = JSON.parse(await (await response("localhost")).text()); + expect(direct.shortcuts).toBeUndefined(); + }); +}); + describe("the /pages live index (#1497)", () => { test("renders last-rendered + ok from the registry, and says so for unrendered pages", () => { const html = renderPages(PROV, [ diff --git a/pm-stack/aggregator/test/nav.test.ts b/pm-stack/aggregator/test/nav.test.ts index 8829c25096..9b35d0ecef 100644 --- a/pm-stack/aggregator/test/nav.test.ts +++ b/pm-stack/aggregator/test/nav.test.ts @@ -15,6 +15,7 @@ import { pageByRoute, } from "../src/pages.ts"; import { navLinks } from "../src/render.ts"; +import { addConsoleShortcut } from "../src/console-gate.ts"; const SRC = join(import.meta.dir, "..", "src"); const serverSource = readFileSync(join(SRC, "server.ts"), "utf8"); @@ -87,6 +88,21 @@ describe("the nav bar renders the groups (#1135)", () => { }); }); +describe("console-gated navigation (#12416)", () => { + test("adds the Console link only for the configured public Host", async () => { + const response = (host: string) => + addConsoleShortcut( + new Response('', { headers: { "content-type": "text/html" } }), + new Request("http://aggregator/", { headers: { host } }), + "pm.security.cab", + ); + expect(await (await response("pm.security.cab")).text()).toContain('Console'); + for (const host of ["100.64.0.1", "127.0.0.1:4870", "localhost"]) { + expect(await (await response(host)).text()).not.toContain("/console/"); + } + }); +}); + describe("the bar wraps instead of scrolling sideways (#1135, 390px)", () => { test("the nav rule wraps and has no horizontal scroll", () => { const rule = cssRule("nav");