Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 | 4x 4x 8x 8x 5x 4x 8x 5x 8x 6x 4x 4x | import React from "react";
/**
* The preview document's policy. `default-src 'none'` forbids every fetch —
* trackers, fonts, stylesheets, frames — so even markup that slipped past the
* server's sanitiser cannot reach the network. Only the preview's own inline
* stylesheet and inline `data:` images are allowed.
*/
export const SNAPSHOT_PREVIEW_CSP = [
"default-src 'none'",
"style-src 'unsafe-inline'",
"img-src data:",
"form-action 'none'",
"base-uri 'none'",
"frame-src 'none'",
].join("; ");
/** A neutral reading style: the site's own stylesheets were never captured. */
const PREVIEW_CSS = `
:root { color-scheme: light; }
body { font: 14px/1.6 system-ui, sans-serif; color: #1f2937; background: #fff;
margin: 0; padding: 20px 24px; overflow-wrap: anywhere; }
h1, h2, h3, h4 { line-height: 1.25; margin: 1.2em 0 0.5em; }
table { border-collapse: collapse; margin: 1em 0; }
th, td { border: 1px solid #e5e7eb; padding: 4px 8px; text-align: left; }
pre { background: #f3f4f6; padding: 10px; overflow-x: auto; }
code { font-family: ui-monospace, monospace; font-size: 0.92em; }
img { max-width: 100%; }
img:not([src]) { display: inline-block; border: 1px dashed #d1d5db;
padding: 2px 6px; color: #6b7280; font-size: 12px; }
a { color: #1d4ed8; }
a:not([href]) { color: inherit; text-decoration: underline dotted; }
`;
/**
* Strip everything that can move the frame somewhere else.
*
* The CSP stops fetches, but not navigation: a followed link (or a meta
* refresh) loads a new document into the frame, and that document does not
* inherit the preview's policy. svc-core already removes these; this is the
* second, independent layer for navigation specifically. `DOMParser` builds
* an inert document — nothing in it runs or loads — so parsing here is safe.
*/
export function neutraliseNavigation(html: string): string {
const doc = new DOMParser().parseFromString(html, "text/html");
doc.querySelectorAll("a[href], area[href]").forEach((el) => {
if (!(el.getAttribute("href") ?? "").startsWith("#")) {
el.removeAttribute("href");
}
});
doc
.querySelectorAll("meta, base, form, iframe, frame, object, embed")
.forEach((el) => el.remove());
return doc.body.innerHTML;
}
/**
* The document the frame renders. The CSP `<meta>` is the very first thing
* in `<head>` — ahead of any content — so it governs everything after it;
* a policy the content might declare later can only narrow it further.
*/
export function snapshotPreviewDocument(sanitisedHtml: string): string {
return (
"<!doctype html><html><head>" +
`<meta http-equiv="Content-Security-Policy" content="${SNAPSHOT_PREVIEW_CSP}">` +
'<meta name="referrer" content="no-referrer">' +
'<meta charset="utf-8">' +
`<style>${PREVIEW_CSS}</style>` +
`</head><body>${neutraliseNavigation(sanitisedHtml)}</body></html>`
);
}
/**
* Renders a page snapshot's HTML — as captured from somebody else's site —
* without trusting it. Four layers, each sufficient on its own for the
* threat it covers:
*
* 1. svc-core's allow-list sanitiser has already removed scripts, handlers,
* frames, forms, styles and every external URL (html_preview.py).
* 2. `sandbox=""` — every restriction on: no scripts, no same-origin (an
* opaque origin, so no cookies, storage or access to this app), no forms,
* popups, modals, downloads or navigation of the app.
* 3. The CSP above: no network access from inside the frame at all.
* 4. `neutraliseNavigation`: no link or refresh that could move the frame to
* a live page, where (3) would no longer apply.
*
* Verified in Chromium with the server layer deliberately bypassed: raw
* hostile markup ran no script and reached the network only through a
* clicked link — which is what (4) removes.
*
* `srcDoc`, never `src`: there is no URL on this origin that serves the
* snapshot as a document, and there must never be one.
*/
const SnapshotPreviewFrame: React.FC<{ html: string; title: string }> = ({
html,
title,
}) => (
<iframe
title={title}
sandbox=""
srcDoc={snapshotPreviewDocument(html)}
referrerPolicy="no-referrer"
loading="lazy"
className="block h-[55vh] w-full rounded-lg border border-gray-200 bg-white dark:border-gray-700"
/>
);
export default SnapshotPreviewFrame;
|