diff --git a/serve/server.py b/serve/server.py
index ad0e10a9..ba92ffba 100644
--- a/serve/server.py
+++ b/serve/server.py
@@ -449,6 +449,11 @@ class GpuBusy(RuntimeError):
self.retry_after = retry_after
+# The page's only CSP directive: its preview frames (srcdoc) may not load any address. Other directives are left out
+# on purpose: a frame made from srcdoc inherits the page's policy, and the opt-in "run scripts" preview needs its own inline script.
+PAGE_CSP = "frame-src 'none'"
+
+
def unload_wait(value, default: float) -> float:
"""#1997: the `wait` of POST /unload?wait=...: absent / 0 / false: 0 (answer "busy" at once, as before); 1 / true /
yes: `default` seconds; a number of seconds above 1: that long (at most an hour)."""
@@ -6646,6 +6651,10 @@ def make_handler(svc: Service):
body = (ROOT / "serve" / "web" / ("monitor.html" if path else "index.html")).read_bytes()
self.send_response(200)
self.send_header("Content-Type", "text/html; charset=utf-8")
+ if not path:
+ # the chat's svg/html preview is a sandboxed frame; this keeps it from navigating away to a web page
+ # (a link or inside a model's html), which no sandbox flag does (serve/web/preview.js)
+ self.send_header("Content-Security-Policy", PAGE_CSP)
self.send_header("Content-Length", str(len(body)))
self.end_headers()
self.wfile.write(body)
diff --git a/serve/test_web_preview.js b/serve/test_web_preview.js
new file mode 100644
index 00000000..7c1dad15
--- /dev/null
+++ b/serve/test_web_preview.js
@@ -0,0 +1,104 @@
+// The svg/html preview (serve/web/preview.js), run in Node. The real browser behaviour (what a sandboxed frame can and
+// cannot reach) is tested in tools/webtest/browser_test.mjs; here the documents and frames that are built are checked.
+// node serve/test_web_preview.js (serve/test_web_preview.py runs it too and skips when node is missing)
+"use strict";
+const assert = require("node:assert/strict");
+const path = require("node:path");
+const P = require(path.join(__dirname, "web", "preview.js"));
+
+const light = {dark: false, ink: "#221e1f"}, dark = {dark: true, ink: "#eef1f3"};
+const cspOf = (doc) => /Content-Security-Policy" content="([^"]*)"/.exec(doc)[1];
+const noImage = {DOMParser: undefined}; // no DOMParser: the inline svg path
+
+const cases = {
+ can_preview() {
+ assert.ok(P.canPreview("svg", "") && P.canPreview("SVG", " "));
+ assert.ok(P.canPreview("html", "