Public: anyone can read this; the crew can change it.

Migrated from github.com/Trevato/SO-ARM100

workbenchthis machine

The identity line on /story and /admin/data #594

Merged opened this

Check · Review · Land done · Live — Landed; nothing deploys from this branch, so this is the end of its path.

Merged

smith/arm:smith/593-the-identity-line-on-story-and-admin-dat → main 1 commit fork 2 threads · 2 unresolved

  • Merged. smith/593-the-identity-line-on-story-and-admin-dat was merged into main as 568b0ea.

Review

Sign in to review this change.
4 files changed +56 −8
Software/so101/src/data-page.ts +6 −3
@@ -9,10 +9,12 @@ import { createHash } from "node:crypto";
99 import { readFileSync } from "node:fs";
1010 import type { IncomingMessage, ServerResponse } from "node:http";
1111 import type { Lobby } from "./lobby.ts";
12 import { renderSignedIn, SIGNED_IN_STYLE, type SignedIn } from "./signed-in.ts";
1213
1314 export const DATA_SCRIPT = readFileSync(new URL("./ui/data-view.js", import.meta.url), "utf8");
1415
1516 export const DATA_STYLE = `
17 ${SIGNED_IN_STYLE}
1618 :root { color-scheme: dark light; --bg: #0b0d10; --panel: #11161d; --fg: #e8eaed; --dim: #9aa4ae; --line: #2a313a; --accent: #58a6ff; --good: #3fb950; --bad: #f85149;
1719 --c0: #58a6ff; --c1: #3fb950; --c2: #d2a8ff; --c3: #f0883e; --c4: #f85149; --c5: #e3b341; --c6: #8b949e; --c7: #39c5cf; }
1820 @media (prefers-color-scheme: light) {
@@ -126,11 +128,11 @@ dialog .close { float: right; }
126128 `;
127129
128130 const sha = (text: string) => createHash("sha256").update(text).digest("base64");
129 export const dataCsp = (): string => `default-src 'none'; style-src 'sha256-${sha(DATA_STYLE)}'; script-src 'sha256-${sha(DATA_SCRIPT)}'; connect-src 'self'; img-src 'none'; form-action 'none'; frame-ancestors 'none'; base-uri 'none'`;
131 export const dataCsp = (): string => `default-src 'none'; style-src 'sha256-${sha(DATA_STYLE)}'; script-src 'sha256-${sha(DATA_SCRIPT)}'; connect-src 'self'; img-src 'none'; form-action 'self'; frame-ancestors 'none'; base-uri 'none'`;
130132
131133 const plot = (id: string) => `<div class="plot" id="${id}"></div>`;
132134
133 export function renderDataPage(): string {
135 export function renderDataPage(who: SignedIn | null = null): string {
134136 return `<!doctype html>
135137 <html lang="en">
136138 <head>
@@ -144,6 +146,7 @@ export function renderDataPage(): string {
144146 <main>
145147 <header>
146148 <div class="title"><h1>Data</h1><nav><a href="/">The arm</a><a href="/admin">Admin</a></nav></div>
149 ${renderSignedIn(who, "/admin/data")}
147150 <div class="controls">
148151 <div class="seg" role="group" aria-label="Time range">
149152 <button type="button" data-range="hour" aria-pressed="false">Hour</button><button type="button" data-range="day" aria-pressed="true">Day</button><button type="button" data-range="week" aria-pressed="false">Week</button><button type="button" data-range="all" aria-pressed="false">All</button>
@@ -198,5 +201,5 @@ export function serveDataPage(lobby: Lobby, req: IncomingMessage, res: ServerRes
198201 return;
199202 }
200203 res.writeHead(200, { "content-type": "text/html; charset=utf-8", "cache-control": "no-store", "content-security-policy": dataCsp() });
201 res.end(req.method === "HEAD" ? undefined : renderDataPage());
204 res.end(req.method === "HEAD" ? undefined : renderDataPage(lobby.signedIn(req)));
202205 }
Software/so101/src/relay.ts +1 −1
@@ -831,7 +831,7 @@ export async function startRelay(options: RelayOptions): Promise<RunningRelay> {
831831 if (cameraAdmin) return await administer(req, res, cameraAdmin[1] === undefined ? null : Number(cameraAdmin[1]));
832832 if (lobby && url.pathname === "/admin/lift") return await handleLift(lobby, req, res, readBody);
833833 if (lobby && (url.pathname === "/admin" || url.pathname === "/admin/health")) return await handleAdmin(lobby, req, res, url, readBody, askPrecision, askServos, videoCards, options.piSync ? () => options.piSync!.versions() : undefined, tuneP);
834 if (serveStory(req, res, url.pathname, { cameras: () => (restingNow() ? [] : cameras.list()), numbers: storyNumbers })) return;
834 if (serveStory(req, res, url.pathname, { cameras: () => (restingNow() ? [] : cameras.list()), numbers: storyNumbers, signedIn: lobby && !lobby.local ? (r) => lobby.signedIn(r) : undefined })) return;
835835 if (url.pathname === "/admin/versions") return versionsRead(req, res);
836836 if (lobby && url.pathname === "/admin/data") return serveDataPage(lobby, req, res);
837837 if (url.pathname === "/data") return serveData(facts ?? undefined, lobby ?? undefined, req, res, url);
Software/so101/src/signout.test.ts +33
@@ -10,6 +10,8 @@ import { Lobby } from "./lobby.ts";
1010 import { startRelay, type RunningRelay } from "./relay.ts";
1111 import { startServer, type RunningServer } from "./server.ts";
1212 import { FORGE_HOME, forgeSignin, renderSignedIn, signedIn } from "./signed-in.ts";
13 import { dataCsp, renderDataPage } from "./data-page.ts";
14 import { SIGNED_IN_STYLE } from "./signed-in.ts";

Second import from signed-in.ts. Merge it into the line above.

1315 import { signedInParts } from "./ui/lobby-panel.js";
1416
1517 const KEY = "k".repeat(64);
@@ -125,6 +127,37 @@ describe("POST /signout (#580)", () => {
125127 });
126128 });
127129
130 describe("the identity line on /story and /admin/data (#593)", () => {
131 const form = '<form method="post" action="/signout"><input type="hidden" name="forge" value="1"><button class="link">Sign out</button></form>';
132
133 test("/story: a guest is offered sign-in, a signed-in reader sees their name and Sign out, and the CSP lets the form post home and nothing more", async () => {
134 const base = await boot();
135 const guest = await fetch(`${base}/story`);
136 expect(await guest.text()).toContain('Watching as a guest · <a href="/__forge/signin?to=%2Fstory">Sign in</a>');
137 const named = await fetch(`${base}/story`, { headers: owner });
138 const html = await named.text();
139 expect(html).toContain(`Signed in as <b>trevato</b> (forge)</span>${form}`);
140 expect(html).toContain(".signed-in {");
141 const csp = named.headers.get("content-security-policy")!;
142 expect(csp).toContain("form-action 'self'");
143 expect(csp).not.toContain("form-action 'none'");
144 expect(named.headers.get("cache-control")).toContain("private");
145 const cookie = await join_(base, "altmia");
146 expect(await (await fetch(`${base}/story`, { headers: { cookie } })).text()).toContain('Signed in as <b>altmia</b> (this app)</span><form method="post" action="/signout"><button');
147 });
148
149 test("/admin/data: the owner sees the line and the form, a guest line renders as the sign-in, and the CSP allows form-action 'self' only", async () => {
150 const base = await boot();
151 const res = await fetch(`${base}/admin/data`, { headers: owner });
152 const html = await res.text();
153 expect(html).toContain(`Signed in as <b>trevato</b> (forge)</span>${form}`);
154 expect(res.headers.get("content-security-policy")).toContain("form-action 'self';");
155 expect(dataCsp()).not.toContain("form-action 'none'");
156 expect(renderDataPage(null)).toContain('Watching as a guest · <a href="/__forge/signin?to=%2Fadmin%2Fdata">Sign in</a>');
157 expect(html).toContain(SIGNED_IN_STYLE);
158 });
159 });
160
128161 describe("the identity line", () => {
129162 test("names the person escaped, says where they signed in, and a guest gets sign-in back to where they were", () => {
130163 expect(renderSignedIn({ name: "<b>x</b>", via: "app" })).toContain("Signed in as <b>&lt;b&gt;x&lt;/b&gt;</b> (this app)");
Software/so101/src/story.ts +16 −4
@@ -12,6 +12,7 @@ import { readFileSync } from "node:fs";
1212 import type { IncomingMessage, ServerResponse } from "node:http";
1313 import { aggregate } from "./data-series.ts";
1414 import type { Facts } from "./facts.ts";
15 import { renderSignedIn, SIGNED_IN_STYLE, type SignedIn } from "./signed-in.ts";
1516 import { esc, renderMarkdown } from "./story-markdown.ts";
1617
1718 export const STORY_FILE = new URL("../../../docs/story.md", import.meta.url);
@@ -41,6 +42,8 @@ export interface StoryView {
4142 origin: string | null;
4243 camera: StoryCamera | null;
4344 numbers: StoryNumbers | null;
45 /** Who is reading, for the line under the nav; undefined where the app has no sign-in (no line). */
46 signedIn?: SignedIn | null;
4447 }
4548
4649 export const STORY_STYLE = `
@@ -51,7 +54,7 @@ body { margin: 0; background: var(--bg); color: var(--fg); font: 18px/1.65 ui-sa
5154 .top, main, footer { max-width: 40rem; margin: 0 auto; padding-left: max(1.1rem, env(safe-area-inset-left)); padding-right: max(1.1rem, env(safe-area-inset-right)); }
5255 .top { display: flex; justify-content: space-between; align-items: center; gap: 1rem; padding-top: max(.8rem, env(safe-area-inset-top)); font-size: .9rem; }
5356 .top a { min-height: 44px; display: inline-flex; align-items: center; }
54 a { color: var(--link); text-underline-offset: .15em; }
57 ${SIGNED_IN_STYLE}a { color: var(--link); text-underline-offset: .15em; }

SIGNED_IN_STYLE is glued to the start of the a rule. Put the interpolation on its own line so the style stays readable.

5558 a:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; border-radius: 2px; }
5659 .go { display: inline-flex; align-items: center; min-height: 44px; padding: 0 1rem; border-radius: 999px; background: var(--accent); color: #fff; font-weight: 650; text-decoration: none; }
5760 h1 { font-size: clamp(2rem, 7vw, 2.9rem); line-height: 1.1; letter-spacing: -.02em; margin: 1.2rem 0 1rem; text-wrap: balance; }
@@ -87,7 +90,7 @@ footer a { min-height: 44px; display: inline-flex; align-items: center; }
8790 `;
8891
8992 const sha = (text: string) => createHash("sha256").update(text).digest("base64");
90 export const storyCsp = (): string => `default-src 'none'; style-src 'sha256-${sha(STORY_STYLE)}'; img-src 'self'; form-action 'none'; frame-ancestors 'none'; base-uri 'none'`;
93 export const storyCsp = (): string => `default-src 'none'; style-src 'sha256-${sha(STORY_STYLE)}'; img-src 'self'; form-action 'self'; frame-ancestors 'none'; base-uri 'none'`;
9194
9295 /** The camera the story shows: the one looking down (named Above or Overhead), else the first listed. */
9396 export function pickCamera(list: readonly StoryCamera[]): StoryCamera | null {
@@ -127,6 +130,11 @@ function numbersSlot(numbers: StoryNumbers | null): string {
127130 ].join("")}</dl></section>`;
128131 }
129132
133 const signedInLine = (s: SignedIn | null): string => {
134 const line = renderSignedIn(s, "/story");
135 return line && `<div class="top">${line}</div>`;
136 };
137
130138 export function renderStory(view: StoryView): string {
131139 const body = renderMarkdown(view.markdown, { camera: () => cameraSlot(view.camera), numbers: () => numbersSlot(view.numbers) });
132140 const at = (path: string) => esc((view.origin ?? "") + path);
@@ -155,6 +163,7 @@ export function renderStory(view: StoryView): string {
155163 </head>
156164 <body>
157165 <nav class="top" aria-label="Site"><a href="/">← The arm, live</a><a class="go" href="/">Drive it</a></nav>
166 ${view.signedIn === undefined ? "" : signedInLine(view.signedIn)}
158167 <main>
159168 <article>
160169 ${body}
@@ -171,6 +180,8 @@ export interface StorySource {
171180 numbers: () => StoryNumbers | null;
172181 /** The story's Markdown; docs/story.md by default. */
173182 markdown?: () => string;
183 /** Who is asking; the page is then private to them. */
184 signedIn?: (req: IncomingMessage) => SignedIn | null;
174185 }
175186
176187 let kept: string | null = null;
@@ -214,8 +225,9 @@ export function serveStory(req: IncomingMessage, res: ServerResponse, path: stri
214225 } catch (err) {
215226 console.error(`story numbers: ${(err as Error).message}`);
216227 }
217 const html = renderStory({ markdown, origin: originOf(req), camera: pickCamera(source.cameras()), numbers });
218 res.writeHead(200, { "content-type": "text/html; charset=utf-8", "cache-control": "public, max-age=60", "content-security-policy": storyCsp() });
228 const who = source.signedIn ? source.signedIn(req) : undefined;
229 const html = renderStory({ markdown, origin: originOf(req), camera: pickCamera(source.cameras()), numbers, signedIn: who });
230 res.writeHead(200, { "content-type": "text/html; charset=utf-8", "cache-control": who === undefined ? "public, max-age=60" : "private, max-age=60", "content-security-policy": storyCsp() });
219231 res.end(head ? undefined : html);
220232 return true;
221233 }

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs