test(security): allow-list the editor canvas frame by its constant blank document

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01EFUzfiCVCgCVJZq4NDEqZs
This commit is contained in:
ngqileandClaude Opus 5.5 committed 2026-12-03 14:46:05 +08:00
1 parent 5f4f3bc1ee
commit b768b66f6f
2 files changed
+25 -1

No files matched your search

@@ -40,6 +40,8 @@ The editor mounts `<iframe>` with no `src` (about:blank, same origin), then rend
**Rejected: a canvas address that exchanges the draft by `postMessage`,** as the design doc sketched. It needs a sync protocol both ways, origin checks and a second copy of the draft, which makes undo and in-place editing harder.
**Security.** The canvas frame has no `sandbox`, unlike every other frame in the portal. It must be same-origin for the portal, and a sandbox without `allow-scripts` would also stop the scripts of the Custom HTML frames nested in it. It holds no data HTML: its only document is a constant blank page (`srcDoc`, no `src`), and everything in it is React-rendered as on the preview page. The portal's markup sweep test allow-lists exactly that file, constant and shape.
**Fallback.** Task 1 is a spike with pass criteria. If it fails (Tiptap inside the frame, focus, drag events, or production CSS), use a canvas route `/pages/canvas` that reads the draft and posts edits back. Only the canvas wiring changes.
### 2. Desktop is 1280 px, scaled
@@ -34,6 +34,13 @@ const SANDBOX_ALLOWED: Record<string, string> = {
"components/pages/HtmlFrame.tsx": "FRAME_SANDBOX",
};
// add-page-visual-editor design Decision 1: the editor canvas is a same-origin frame the editor renders its own React
// tree into by portal, so it cannot be sandboxed (that would also strip scripts from the Custom HTML frames nested in
// it). It holds no data HTML: its only document is this file's constant blank page, with no src.
const UNSANDBOXED_ALLOWED: Record<string, { srcDoc: string; html: string }> = {
"components/pages/CanvasFrame.tsx": { srcDoc: "BLANK", html: `const BLANK = "<!doctype html><html><head></head><body></body></html>";` },
};
function sourceFiles(dir: string, out: string[] = []): string[] {
for (const entry of fs.readdirSync(dir, { withFileTypes: true })) {
if (entry.name === "node_modules" || entry.name === ".next") continue;
@@ -68,7 +75,12 @@ function iframeProblems(fileName: string, src: string): string[] {
const value = sandbox?.initializer;
const allowed = SANDBOX_ALLOWED[fileName];
const named = !!allowed && !!value && ts.isJsxExpression(value) && value.expression?.getText(sf) === allowed;
if (!named && (!value || !ts.isStringLiteral(value) || value.text !== "")) {
const attr = (n: string) => props.find((p): p is ts.JsxAttribute => ts.isJsxAttribute(p) && p.name.getText(sf) === n);
const canvas = UNSANDBOXED_ALLOWED[fileName];
const srcDoc = attr("srcDoc")?.initializer;
const blank = !!canvas && !sandbox && !attr("src") && src.includes(canvas.html)
&& !!srcDoc && ts.isJsxExpression(srcDoc) && srcDoc.expression?.getText(sf) === canvas.srcDoc;
if (!named && !blank && (!value || !ts.isStringLiteral(value) || value.text !== "")) {
problems.push(`${fileName}:${line} iframe without sandbox=""`);
}
}
@@ -151,6 +163,16 @@ describe("iframe checker", () => {
expect(iframeProblems("components/pages/HtmlFrame.tsx", src.replace("FRAME_SANDBOX", "mode"))).toHaveLength(1);
});
it("accepts the editor canvas unsandboxed only in its file, with its constant blank document and no src", () => {
const blank = `const BLANK = "<!doctype html><html><head></head><body></body></html>";\n`;
const src = `${blank}export const X = () => <iframe title="t" srcDoc={BLANK} />;`;
expect(iframeProblems("components/pages/CanvasFrame.tsx", src)).toEqual([]);
expect(iframeProblems("fixture.tsx", src)).toHaveLength(1);
expect(iframeProblems("components/pages/CanvasFrame.tsx", src.replace("srcDoc={BLANK}", "srcDoc={html}"))).toHaveLength(1);
expect(iframeProblems("components/pages/CanvasFrame.tsx", src.replace("srcDoc={BLANK}", "srcDoc={BLANK} src={u}"))).toHaveLength(1);
expect(iframeProblems("components/pages/CanvasFrame.tsx", src.replace("<body></body>", "<body><script>x()</script></body>"))).toHaveLength(1);
});
it("rejects a non-empty or computed sandbox", () => {
expect(check(`<iframe sandbox="allow-scripts" srcDoc={html} />`)).toHaveLength(1);
expect(check(`<iframe sandbox={mode} srcDoc={html} />`)).toHaveLength(1);