Files
strata/serve/web/preview.js
Niko1221andThomas Ivesdal-Tronstad 0aa64b2de3 Web chat: preview a generated svg or html block in a sandboxed frame (#1628)
A complete ```svg or ```html block in an answer gets a Preview button. The
block's own code is drawn in an iframe (srcdoc) that is cut off from the page:
sandbox="" (an opaque origin: no access to the page's DOM, storage, cookies or
server, no scripts, forms, popups, downloads or navigation of the page), a CSP
in the frame's document (default-src 'none': no network, fonts, frames or media;
styles inline, pictures as data: only; no <base>, no form target), no referrer
and no permissions. A well-formed svg is shown as an <img> 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 <meta refresh> 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 <meta http-equiv=refresh> 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>
2026-10-26 19:57:56 +01:00

82 lines
5.7 KiB
JavaScript

// serve/web/preview.js - the preview of an ```svg or ```html block in an answer (PR #1628 by thoives).
//
// A block is shown in an <iframe srcdoc> that is cut off from the page:
// - sandbox="" : an opaque origin, so no access to the page's DOM, storage, cookies or the server; no scripts, no forms,
// no popups, no navigation of the page, no downloads;
// - a CSP in the frame's own document: default-src 'none' (no network, no fonts, no frames, no media), styles inline and
// pictures as data: only, no <base>, no form target;
// - the page's own header CSP frame-src 'none' (serve/server.py) stops the frame from navigating away to a web page
// (a link, or <meta refresh>), which no sandbox flag can.
// An svg is shown as an <img> with a data: URL where it parses as XML: an image runs no scripts and loads nothing, and it
// animates in every browser. The user can run ONE html block with scripts, after a warning, never remembered: the frame then
// has sandbox="allow-scripts" (still an opaque origin, still no network), and the CSP allows its inline script.
//
// This file builds strings and frames only; app.js decides when. Nothing in it touches the page's own DOM.
"use strict";
(function (root) {
// no allow-same-origin: that would give the frame the page's origin and with it the page itself
const SANDBOX = {static: "", scripts: "allow-scripts"};
const csp = (scripts) => "default-src 'none'; " + (scripts ? "script-src 'unsafe-inline'; " : "") +
"style-src 'unsafe-inline'; img-src data:; base-uri 'none'; form-action 'none'";
const metaCsp = (scripts) => `<meta http-equiv="Content-Security-Policy" content="${csp(scripts)}">`;
const SVG_NS = "http://www.w3.org/2000/svg";
// theme = {dark: bool, ink: "#rrggbb"}: the frame carries the page's colours, so it is transparent on the page's surface
const safeInk = (ink) => (/^[#\w(),.\s%-]{1,40}$/.test(ink || "") ? ink : "CanvasText");
const themeCss = (theme) => `html{color-scheme:${theme && theme.dark ? "dark" : "light"};color:${safeInk(theme && theme.ink)}}`;
const frameHead = (css, scripts, theme) => `<!doctype html><html><head>${metaCsp(scripts)}<style>${themeCss(theme)}html,body{background:transparent;margin:0;height:100%}${css}</style></head><body>`;
// a plain svg as an <img> with a data: URL, or null when it is not well-formed XML (then the inline svg is used)
function svgImage(s, theme, env = root) {
try {
const doc = new env.DOMParser().parseFromString(/^<svg\b[^>]*\sxmlns\s*=/i.test(s) ? s : s.replace(/^<svg\b/i, `<svg xmlns="${SVG_NS}"`), "image/svg+xml");
const r = doc.documentElement;
if (doc.querySelector("parsererror") || r.namespaceURI !== SVG_NS || r.localName !== "svg") return null;
const st = doc.createElementNS(SVG_NS, "style"); // an image has no page around it: give currentColor the page's ink
st.textContent = `svg{color:${safeInk(theme && theme.ink)}}`;
r.insertBefore(st, r.firstChild);
const fill = !r.hasAttribute("width") && !r.hasAttribute("height");
return `<img alt="" ${fill ? 'style="width:100%;height:100%;object-fit:contain" ' : ""}src="data:image/svg+xml;charset=utf-8,${encodeURIComponent(new env.XMLSerializer().serializeToString(doc))}">`;
} catch (e) { return null; }
}
// the document for a block, or null when the block is not a previewable svg or html (lang is the fence's language)
function previewDoc(lang, code, scripts, theme, env) {
let s = String(code).trim();
if (/^svg\b/i.test(lang) && /^<svg[\s>]/i.test(s)) {
const img = scripts ? null : svgImage(s, theme, env);
return frameHead("body{display:flex;align-items:center;justify-content:center}" + (img ? "img" : "svg") + "{max-width:100%;max-height:100%}", scripts, theme) +
(img || s) + "</body></html>";
}
if (/^html?$/i.test(lang) && /^<[/a-z!]/i.test(s)) {
s = s.replace(/<meta\b[^>]*\bhttp-equiv\s*=\s*["']?\s*refresh\b[^>]*>/gi, ""); // a page that redirects itself would leave the sandbox's document
// a complete document: our CSP and theme come first, then the page without its doctype (its own html and head tags merge
// into ours, and its own CSS still wins); nothing of the page can run before the CSP is in force
if (/^<!doctype\s+html/i.test(s)) return `<!doctype html><html><head>${metaCsp(scripts)}<style>${themeCss(theme)}</style></head>` + s.replace(/^<!doctype[^>]*>/i, "");
return frameHead("", scripts, theme) + s + "</body></html>"; // a fragment: wrap in a minimal document
}
return null;
}
const kindOf = (lang) => (/^svg\b/i.test(lang) ? "svg" : "page");
// cheap: can this block be previewed at all (the same test as previewDoc, without building anything)
const canPreview = (lang, code) => {
const s = String(code).trim();
return (/^svg\b/i.test(lang) && /^<svg[\s>]/i.test(s)) || (/^html?$/i.test(lang) && /^<[/a-z!]/i.test(s));
};
// a new frame holding the document (the sandbox attribute only counts when a frame is created, so a frame is never reused)
function makeFrame(doc, {scripts = false, title = "", lang = ""} = {}, env = root) {
const f = env.document.createElement("iframe");
f.setAttribute("sandbox", scripts ? SANDBOX.scripts : SANDBOX.static);
f.setAttribute("referrerpolicy", "no-referrer");
f.setAttribute("allow", "");
f.title = title; f.dataset.lang = lang;
if (doc) { f.dataset.opened = "1"; if (scripts) f.dataset.scripts = "1"; f.srcdoc = doc; }
return f;
}
const api = {SANDBOX, csp, metaCsp, previewDoc, svgImage, kindOf, canPreview, makeFrame, themeCss};
root.StrataPreview = api;
if (typeof module !== "undefined" && module.exports) module.exports = api;
})(typeof globalThis !== "undefined" ? globalThis : this);