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
Check · Review · Land done · Live — Landed; nothing deploys from this branch, so this is the end of its path.
smith/arm:smith/593-the-identity-line-on-story-and-admin-dat → main 1 commit fork 2 threads · 2 unresolved
4 files
changed +56
−8
Software/so101/src/data-page.ts +6 −3
| @@ -9,10 +9,12 @@ import { createHash } from "node:crypto"; | ||
| 9 | 9 | import { readFileSync } from "node:fs"; |
| 10 | 10 | import type { IncomingMessage, ServerResponse } from "node:http"; |
| 11 | 11 | import type { Lobby } from "./lobby.ts"; |
| 12 | import { renderSignedIn, SIGNED_IN_STYLE, type SignedIn } from "./signed-in.ts"; | |
| 12 | 13 | |
| 13 | 14 | export const DATA_SCRIPT = readFileSync(new URL("./ui/data-view.js", import.meta.url), "utf8"); |
| 14 | 15 | |
| 15 | 16 | export const DATA_STYLE = ` |
| 17 | ${SIGNED_IN_STYLE} | |
| 16 | 18 | :root { color-scheme: dark light; --bg: #0b0d10; --panel: #11161d; --fg: #e8eaed; --dim: #9aa4ae; --line: #2a313a; --accent: #58a6ff; --good: #3fb950; --bad: #f85149; |
| 17 | 19 | --c0: #58a6ff; --c1: #3fb950; --c2: #d2a8ff; --c3: #f0883e; --c4: #f85149; --c5: #e3b341; --c6: #8b949e; --c7: #39c5cf; } |
| 18 | 20 | @media (prefers-color-scheme: light) { |
| @@ -126,11 +128,11 @@ dialog .close { float: right; } | ||
| 126 | 128 | `; |
| 127 | 129 | |
| 128 | 130 | 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'`; | |
| 130 | 132 | |
| 131 | 133 | const plot = (id: string) => `<div class="plot" id="${id}"></div>`; |
| 132 | 134 | |
| 133 | export function renderDataPage(): string { | |
| 135 | export function renderDataPage(who: SignedIn | null = null): string { | |
| 134 | 136 | return `<!doctype html> |
| 135 | 137 | <html lang="en"> |
| 136 | 138 | <head> |
| @@ -144,6 +146,7 @@ export function renderDataPage(): string { | ||
| 144 | 146 | <main> |
| 145 | 147 | <header> |
| 146 | 148 | <div class="title"><h1>Data</h1><nav><a href="/">The arm</a><a href="/admin">Admin</a></nav></div> |
| 149 | ${renderSignedIn(who, "/admin/data")} | |
| 147 | 150 | <div class="controls"> |
| 148 | 151 | <div class="seg" role="group" aria-label="Time range"> |
| 149 | 152 | <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 | ||
| 198 | 201 | return; |
| 199 | 202 | } |
| 200 | 203 | 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))); | |
| 202 | 205 | } |
Software/so101/src/relay.ts +1 −1
| @@ -831,7 +831,7 @@ export async function startRelay(options: RelayOptions): Promise<RunningRelay> { | ||
| 831 | 831 | if (cameraAdmin) return await administer(req, res, cameraAdmin[1] === undefined ? null : Number(cameraAdmin[1])); |
| 832 | 832 | if (lobby && url.pathname === "/admin/lift") return await handleLift(lobby, req, res, readBody); |
| 833 | 833 | 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; | |
| 835 | 835 | if (url.pathname === "/admin/versions") return versionsRead(req, res); |
| 836 | 836 | if (lobby && url.pathname === "/admin/data") return serveDataPage(lobby, req, res); |
| 837 | 837 | 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"; | ||
| 10 | 10 | import { startRelay, type RunningRelay } from "./relay.ts"; |
| 11 | 11 | import { startServer, type RunningServer } from "./server.ts"; |
| 12 | 12 | 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"; | |
| 13 | 15 | import { signedInParts } from "./ui/lobby-panel.js"; |
| 14 | 16 | |
| 15 | 17 | const KEY = "k".repeat(64); |
| @@ -125,6 +127,37 @@ describe("POST /signout (#580)", () => { | ||
| 125 | 127 | }); |
| 126 | 128 | }); |
| 127 | 129 | |
| 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 | ||
| 128 | 161 | describe("the identity line", () => { |
| 129 | 162 | test("names the person escaped, says where they signed in, and a guest gets sign-in back to where they were", () => { |
| 130 | 163 | expect(renderSignedIn({ name: "<b>x</b>", via: "app" })).toContain("Signed in as <b><b>x</b></b> (this app)"); |
Software/so101/src/story.ts +16 −4
| @@ -12,6 +12,7 @@ import { readFileSync } from "node:fs"; | ||
| 12 | 12 | import type { IncomingMessage, ServerResponse } from "node:http"; |
| 13 | 13 | import { aggregate } from "./data-series.ts"; |
| 14 | 14 | import type { Facts } from "./facts.ts"; |
| 15 | import { renderSignedIn, SIGNED_IN_STYLE, type SignedIn } from "./signed-in.ts"; | |
| 15 | 16 | import { esc, renderMarkdown } from "./story-markdown.ts"; |
| 16 | 17 | |
| 17 | 18 | export const STORY_FILE = new URL("../../../docs/story.md", import.meta.url); |
| @@ -41,6 +42,8 @@ export interface StoryView { | ||
| 41 | 42 | origin: string | null; |
| 42 | 43 | camera: StoryCamera | null; |
| 43 | 44 | 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; | |
| 44 | 47 | } |
| 45 | 48 | |
| 46 | 49 | export const STORY_STYLE = ` |
| @@ -51,7 +54,7 @@ body { margin: 0; background: var(--bg); color: var(--fg); font: 18px/1.65 ui-sa | ||
| 51 | 54 | .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)); } |
| 52 | 55 | .top { display: flex; justify-content: space-between; align-items: center; gap: 1rem; padding-top: max(.8rem, env(safe-area-inset-top)); font-size: .9rem; } |
| 53 | 56 | .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. |
||
| 55 | 58 | a:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; border-radius: 2px; } |
| 56 | 59 | .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; } |
| 57 | 60 | 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; } | ||
| 87 | 90 | `; |
| 88 | 91 | |
| 89 | 92 | 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'`; | |
| 91 | 94 | |
| 92 | 95 | /** The camera the story shows: the one looking down (named Above or Overhead), else the first listed. */ |
| 93 | 96 | export function pickCamera(list: readonly StoryCamera[]): StoryCamera | null { |
| @@ -127,6 +130,11 @@ function numbersSlot(numbers: StoryNumbers | null): string { | ||
| 127 | 130 | ].join("")}</dl></section>`; |
| 128 | 131 | } |
| 129 | 132 | |
| 133 | const signedInLine = (s: SignedIn | null): string => { | |
| 134 | const line = renderSignedIn(s, "/story"); | |
| 135 | return line && `<div class="top">${line}</div>`; | |
| 136 | }; | |
| 137 | ||
| 130 | 138 | export function renderStory(view: StoryView): string { |
| 131 | 139 | const body = renderMarkdown(view.markdown, { camera: () => cameraSlot(view.camera), numbers: () => numbersSlot(view.numbers) }); |
| 132 | 140 | const at = (path: string) => esc((view.origin ?? "") + path); |
| @@ -155,6 +163,7 @@ export function renderStory(view: StoryView): string { | ||
| 155 | 163 | </head> |
| 156 | 164 | <body> |
| 157 | 165 | <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)} | |
| 158 | 167 | <main> |
| 159 | 168 | <article> |
| 160 | 169 | ${body} |
| @@ -171,6 +180,8 @@ export interface StorySource { | ||
| 171 | 180 | numbers: () => StoryNumbers | null; |
| 172 | 181 | /** The story's Markdown; docs/story.md by default. */ |
| 173 | 182 | markdown?: () => string; |
| 183 | /** Who is asking; the page is then private to them. */ | |
| 184 | signedIn?: (req: IncomingMessage) => SignedIn | null; | |
| 174 | 185 | } |
| 175 | 186 | |
| 176 | 187 | let kept: string | null = null; |
| @@ -214,8 +225,9 @@ export function serveStory(req: IncomingMessage, res: ServerResponse, path: stri | ||
| 214 | 225 | } catch (err) { |
| 215 | 226 | console.error(`story numbers: ${(err as Error).message}`); |
| 216 | 227 | } |
| 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() }); | |
| 219 | 231 | res.end(head ? undefined : html); |
| 220 | 232 | return true; |
| 221 | 233 | } |
new above
offline
Second import from signed-in.ts. Merge it into the line above.