Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions pm-stack/aggregator/src/console-gate.ts
Original file line number Diff line number Diff line change
@@ -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<Response> {
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<string, unknown>;
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("</nav>", '<a href="/console/">Console</a></nav>');
} else {
return response;
}
return new Response(body, { status: response.status, statusText: response.statusText, headers: response.headers });
}
4 changes: 3 additions & 1 deletion pm-stack/aggregator/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -4480,7 +4481,8 @@ async function fetchHandler(req: Request): Promise<Response> {
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
Expand Down
16 changes: 16 additions & 0 deletions pm-stack/aggregator/test/a11y-1215.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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") +
Expand Down Expand Up @@ -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('<nav class="navbar">Dashboard</nav>', { 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('<a href="/console/">Console</a>');
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 = {
Expand Down
17 changes: 17 additions & 0 deletions pm-stack/aggregator/test/curation-1497.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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, [
Expand Down
16 changes: 16 additions & 0 deletions pm-stack/aggregator/test/nav.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down Expand Up @@ -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('<nav class="navbar">Dashboard</nav>', { headers: { "content-type": "text/html" } }),
new Request("http://aggregator/", { headers: { host } }),
"pm.security.cab",
);
expect(await (await response("pm.security.cab")).text()).toContain('<a href="/console/">Console</a>');
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");
Expand Down