GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — feat(server): the server sends the designer under a content security … · u8array/ZPLab@2ca66fd

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • index.html
  • packages/server
    • README.md
    • src
      • designer.test.ts
      • designer.ts
  • public
    • theme.js
  • src/test
    • serverCsp.test.ts
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎index.html‎

Copy file name to clipboardExpand all lines: index.html
+1-16Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,7 @@
55
<link rel="icon" type="image/png" href="/favicon.png" />
66
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
77
<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>
249
</head>
2510
<body>
2611
<div id="root"></div>
Collapse file

‎packages/server/README.md‎

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 numberDiff line numberDiff line change
@@ -80,3 +80,5 @@ $env:ZPLAB_DESIGNER_DIR = "$PWD\dist"; $env:ZPLAB_TOKEN = "change-me"; pnpm --fi
8080
```
8181

8282
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

‎packages/server/src/designer.test.ts‎

Copy file name to clipboardExpand all lines: packages/server/src/designer.test.ts
+13Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,19 @@ describe("the designer around the API", () => {
7070
}
7171
});
7272

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+
7386
it("leaves the API paths to the API", async () => {
7487
expect(await (await app.request("/api/v1/health")).json()).toEqual({ ok: true, version: "test" });
7588
expect((await app.request("/api/v1/nope")).status).toBe(401);
Collapse file

‎packages/server/src/designer.ts‎

Copy file name to clipboardExpand all lines: packages/server/src/designer.ts
+17Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { Hono, type MiddlewareHandler } from "hono";
22
import { compress } from "hono/compress";
3+
import { secureHeaders } from "hono/secure-headers";
34
import { serveStatic } from "@hono/node-server/serve-static";
45
import { noSuchRoute, type createApp } from "./app.js";
56

@@ -9,6 +10,20 @@ const cacheControl = (value: string): MiddlewareHandler => async (c, next) => {
910
if (c.res.ok) c.header("Cache-Control", value);
1011
};
1112

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+
1227
/** The API with the designer build from `dir` added around it, under the cache headers of docker/nginx.conf. */
1328
export function withDesigner(api: ReturnType<typeof createApp>, dir: string): Hono {
1429
const build = serveStatic({ root: dir });
@@ -20,6 +35,8 @@ export function withDesigner(api: ReturnType<typeof createApp>, dir: string): Ho
2035
root.notFound(noSuchRoute);
2136
root.all("/api/*", noSuchRoute);
2237
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" }));
2340
// Vite content-hashes every file under assets, so an immutable cache is safe.
2441
root.get("/assets/*", cacheControl("public, max-age=31536000, immutable"), files, noSuchRoute);
2542
root.get("*", cacheControl("no-cache"), files, serveStatic({ root: dir, path: "index.html" }));
Collapse file

‎public/theme.js‎

Copy file name to clipboard
+12Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
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

‎src/test/serverCsp.test.ts‎

Copy file name to clipboard
+17Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
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+
});

0 commit comments

Comments
0 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.