From 0aa64b2de39e26ad5368e630c36fb2f3ed08bf8c Mon Sep 17 00:00:00 2001
From: Niko1221 x with a data: URL: an
image runs no script, loads nothing and animates in every browser. The frame
takes the page's light or dark colours and is built again when the theme
changes. A page (html) can also be run WITH scripts, one block at a time, after
a warning that is shown every time: sandbox="allow-scripts" only (still an
opaque origin), the CSP allows the block's inline script and no address; closing
the preview ends it.
What the sandbox flags alone do not stop is a link or inside the
model's html loading a web page into the frame. The browser test shows it does
(tools/webtest/browser_test.mjs, with and without the header). So the page is
served with Content-Security-Policy: frame-src 'none' (nothing else: a srcdoc
frame inherits the page's policy, and the script preview needs its inline
script), and a in a previewed page is dropped.
tools/webtest holds the mock server (the page's own files, scripted answers,
no model) and the browser tests; serve/test_web_preview.js checks the documents,
flags and frames, serve/test_web_preview.py the header and that the page never
listens for a frame's postMessage.
Ported by hand from PR #1628 by thoives.
Co-authored-by: Thomas Ivesdal-Tronstad <325436323+thoives@users.noreply.github.com>
---
serve/server.py | 9 ++
serve/test_web_preview.js | 104 ++++++++++++++++++
serve/test_web_preview.py | 79 ++++++++++++++
serve/web/app.css | 13 +++
serve/web/app.js | 60 +++++++++--
serve/web/index.html | 1 +
serve/web/preview.js | 81 +++++++++++++++
tools/webtest/browser_test.mjs | 185 +++++++++++++++++++++++++++++++++
tools/webtest/lib.mjs | 77 ++++++++++++++
tools/webtest/mock.mjs | 129 +++++++++++++++++++++++
10 files changed, 732 insertions(+), 6 deletions(-)
create mode 100644 serve/test_web_preview.js
create mode 100644 serve/test_web_preview.py
create mode 100644 serve/web/preview.js
create mode 100644 tools/webtest/browser_test.mjs
create mode 100644 tools/webtest/lib.mjs
create mode 100644 tools/webtest/mock.mjs
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", "
not svg
")); + assert.ok(!P.canPreview("", "x
") && !P.canPreview("svgx", "