GeezerVison is showing github.com — feat(server): the server sends the designer under a content security … · u8array/ZPLab@2ca66fd
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: index.html +1-16Lines changed: 1 addition & 16 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 5 | 5 | <link rel="icon" type="image/png" href="/favicon.png" /> | 6 | 6 | <meta name="viewport" content="width=device-width, initial-scale=1.0" /> | 7 | 7 | <title>ZPLab</title> | 8 | | - <script> | 9 | | - // Set data-theme before first paint so the user's persisted preference | 10 | | - // applies before React mounts. Without this, a light-theme user sees | 11 | | - // a brief dark flash while the bridge effect in AppShell catches up. | 12 | | - (function () { | 13 | | - try { | 14 | | - var raw = localStorage.getItem('zpl-designer-session'); | 15 | | - var parsed = raw ? JSON.parse(raw) : null; | 16 | | - var stored = parsed && parsed.state && parsed.state.theme; | 17 | | - var theme = stored === 'light' || stored === 'dark' | 18 | | - ? stored | 19 | | - : window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light'; | 20 | | - document.documentElement.setAttribute('data-theme', theme); | 21 | | - } catch (e) { /* fall through to dark default in CSS */ } | 22 | | - })(); | 23 | | - </script> | | 8 | + <script src="/theme.js"></script> | 24 | 9 | </head> | 25 | 10 | <body> | 26 | 11 | <div id="root"></div> | |
Collapse file Copy file name to clipboardExpand all lines: packages/server/README.md +2Lines changed: 2 additions & 0 deletions - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
@@ -80,3 +80,5 @@ $env:ZPLAB_DESIGNER_DIR = "$PWD\dist"; $env:ZPLAB_TOKEN = "change-me"; pnpm --fi | 80 | 80 | ``` | 81 | 81 |
| 82 | 82 | Behind a reverse proxy, do not buffer the answer of `POST /printers/{id}/query`. The route sets `X-Accel-Buffering: no`, which nginx follows. Other proxies need their own setting. | | 83 | + | | 84 | +The server sends the designer with a content security policy. The policy carries `frame-ancestors 'none'`, and the server sets `X-Frame-Options: DENY`. Set `Strict-Transport-Security` on the proxy that ends TLS. |
Collapse file Copy file name to clipboardExpand all lines: packages/server/src/designer.test.ts +13Lines changed: 13 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -70,6 +70,19 @@ describe("the designer around the API", () => { | 70 | 70 | } | 71 | 71 | }); | 72 | 72 |
| | 73 | + it("sends the same content security policy with the page and its assets and none with an API answer", async () => { | | 74 | + const policy = (await app.request("/")).headers.get("content-security-policy"); | | 75 | + expect(policy).toBeTruthy(); | | 76 | + expect((await app.request("/assets/index-abc123.js")).headers.get("content-security-policy")).toBe(policy); | | 77 | + expect((await app.request("/api/v1/health")).headers.get("content-security-policy")).toBeNull(); | | 78 | + }); | | 79 | + | | 80 | + it("forbids framing and leaves HSTS to the proxy in front", async () => { | | 81 | + const res = await app.request("/"); | | 82 | + expect(res.headers.get("x-frame-options")).toBe("DENY"); | | 83 | + expect(res.headers.get("strict-transport-security")).toBeNull(); | | 84 | + }); | | 85 | + | 73 | 86 | it("leaves the API paths to the API", async () => { | 74 | 87 | expect(await (await app.request("/api/v1/health")).json()).toEqual({ ok: true, version: "test" }); | 75 | 88 | expect((await app.request("/api/v1/nope")).status).toBe(401); | |
Collapse file Copy file name to clipboardExpand all lines: packages/server/src/designer.ts +17Lines changed: 17 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 1 | 1 | import { Hono, type MiddlewareHandler } from "hono"; | 2 | 2 | import { compress } from "hono/compress"; | | 3 | +import { secureHeaders } from "hono/secure-headers"; | 3 | 4 | import { serveStatic } from "@hono/node-server/serve-static"; | 4 | 5 | import { noSuchRoute, type createApp } from "./app.js"; | 5 | 6 |
| @@ -9,6 +10,20 @@ const cacheControl = (value: string): MiddlewareHandler => async (c, next) => { | 9 | 10 | if (c.res.ok) c.header("Cache-Control", value); | 10 | 11 | }; | 11 | 12 |
| | 13 | +/** The desktop policy of src-tauri/tauri.conf.json, with connect-src open for Labelary, Browser Print and a typed printer address. */ | | 14 | +const CSP = { | | 15 | + defaultSrc: ["'self'"], | | 16 | + scriptSrc: ["'self'"], | | 17 | + styleSrc: ["'self'", "'unsafe-inline'"], | | 18 | + imgSrc: ["'self'", "data:", "blob:"], | | 19 | + fontSrc: ["'self'", "data:"], | | 20 | + connectSrc: ["'self'", "http:", "https:"], | | 21 | + objectSrc: ["'none'"], | | 22 | + baseUri: ["'self'"], | | 23 | + frameAncestors: ["'none'"], | | 24 | + formAction: ["'self'"], | | 25 | +}; | | 26 | + | 12 | 27 | /** The API with the designer build from `dir` added around it, under the cache headers of docker/nginx.conf. */ | 13 | 28 | export function withDesigner(api: ReturnType<typeof createApp>, dir: string): Hono { | 14 | 29 | const build = serveStatic({ root: dir }); | @@ -20,6 +35,8 @@ export function withDesigner(api: ReturnType<typeof createApp>, dir: string): Ho | 20 | 35 | root.notFound(noSuchRoute); | 21 | 36 | root.all("/api/*", noSuchRoute); | 22 | 37 | root.use(compress()); | | 38 | + // The server ends no TLS, so HSTS is up to whoever does, and the default would bind every subdomain of its host. | | 39 | + root.use(secureHeaders({ contentSecurityPolicy: CSP, strictTransportSecurity: false, xFrameOptions: "DENY" })); | 23 | 40 | // Vite content-hashes every file under assets, so an immutable cache is safe. | 24 | 41 | root.get("/assets/*", cacheControl("public, max-age=31536000, immutable"), files, noSuchRoute); | 25 | 42 | root.get("*", cacheControl("no-cache"), files, serveStatic({ root: dir, path: "index.html" })); | |
Collapse file Copy file name to clipboard +12Lines changed: 12 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +// Set data-theme before first paint so the persisted preference applies before React mounts and no dark flash shows. | | 2 | +(function () { | | 3 | + try { | | 4 | + var raw = localStorage.getItem('zpl-designer-session'); | | 5 | + var parsed = raw ? JSON.parse(raw) : null; | | 6 | + var stored = parsed && parsed.state && parsed.state.theme; | | 7 | + var theme = stored === 'light' || stored === 'dark' | | 8 | + ? stored | | 9 | + : window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light'; | | 10 | + document.documentElement.setAttribute('data-theme', theme); | | 11 | + } catch (e) { /* fall through to dark default in CSS */ } | | 12 | +})(); |
Collapse file Copy file name to clipboard +17Lines changed: 17 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +import { describe, expect, it } from "vitest"; | | 2 | +import { readFileSync } from "node:fs"; | | 3 | +import { createApp } from "../../packages/server/src/app"; | | 4 | +import { withDesigner } from "../../packages/server/src/designer"; | | 5 | + | | 6 | +describe("the content security policy of the server", () => { | | 7 | + it("leaves the page without an inline script that script-src 'self' would block", () => { | | 8 | + expect(readFileSync("index.html", "utf8")).not.toMatch(/<script(?![^>]*\ssrc=)/); | | 9 | + }); | | 10 | + | | 11 | + it("matches the desktop policy except for an open connect-src", async () => { | | 12 | + const conf = JSON.parse(readFileSync("src-tauri/tauri.conf.json", "utf8")) as { app: { security: { csp: string } } }; | | 13 | + const desktop = conf.app.security.csp.replace(/connect-src [^;]*/, "connect-src 'self' http: https:"); | | 14 | + const page = await withDesigner(createApp({ token: "t", version: "test", printers: new Map() }), ".").request("/"); | | 15 | + expect(page.headers.get("content-security-policy")).toBe(desktop); | | 16 | + }); | | 17 | +}); |
|