feat(pages): Custom HTML block and hide-header switch in the page editor
The iframe guard allow-lists HtmlFrame.tsx for the named FRAME_SANDBOX only, and asserts it never grants same-origin, forms, top navigation or modals. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01EFUzfiCVCgCVJZq4NDEqZs
This commit is contained in:
1 parent
6a0a6613c1
commit
cfc817082d
12 files changed
+148
-7
No files matched your search
@@ -1489,6 +1489,14 @@ INSERT INTO message (code, content, area) SELECT 'PagesBlockFaq', JSON_OBJECT('e
|
||||
UPDATE message SET content = JSON_SET(COALESCE(content, JSON_OBJECT()), '$.zh_CN', '常见问题') WHERE LOWER(code) = LOWER('PagesBlockFaq') AND area = 'A';
|
||||
INSERT INTO message (code, content, area) SELECT 'PagesBlockForm', JSON_OBJECT('en_US', 'Sign-up form', 'zh_CN', '报名表'), 'A' FROM DUAL WHERE NOT EXISTS (SELECT 1 FROM message WHERE LOWER(code) = LOWER('PagesBlockForm') AND area = 'A');
|
||||
UPDATE message SET content = JSON_SET(COALESCE(content, JSON_OBJECT()), '$.zh_CN', '报名表') WHERE LOWER(code) = LOWER('PagesBlockForm') AND area = 'A';
|
||||
INSERT INTO message (code, content, area) SELECT 'PagesBlockHtml', JSON_OBJECT('en_US', 'Custom HTML', 'zh_CN', '自定义 HTML'), 'A' FROM DUAL WHERE NOT EXISTS (SELECT 1 FROM message WHERE LOWER(code) = LOWER('PagesBlockHtml') AND area = 'A');
|
||||
UPDATE message SET content = JSON_SET(COALESCE(content, JSON_OBJECT()), '$.zh_CN', '自定义 HTML') WHERE LOWER(code) = LOWER('PagesBlockHtml') AND area = 'A';
|
||||
INSERT INTO message (code, content, area) SELECT 'PagesHtml', JSON_OBJECT('en_US', 'HTML', 'zh_CN', 'HTML'), 'A' FROM DUAL WHERE NOT EXISTS (SELECT 1 FROM message WHERE LOWER(code) = LOWER('PagesHtml') AND area = 'A');
|
||||
UPDATE message SET content = JSON_SET(COALESCE(content, JSON_OBJECT()), '$.zh_CN', 'HTML') WHERE LOWER(code) = LOWER('PagesHtml') AND area = 'A';
|
||||
INSERT INTO message (code, content, area) SELECT 'PagesHtmlHint', JSON_OBJECT('en_US', 'Forms inside the pasted HTML don''t send. Add a Sign-up form block to collect leads.', 'zh_CN', '粘贴的 HTML 中的表单不会发送。请添加报名表单区块来收集线索。'), 'A' FROM DUAL WHERE NOT EXISTS (SELECT 1 FROM message WHERE LOWER(code) = LOWER('PagesHtmlHint') AND area = 'A');
|
||||
UPDATE message SET content = JSON_SET(COALESCE(content, JSON_OBJECT()), '$.zh_CN', '粘贴的 HTML 中的表单不会发送。请添加报名表单区块来收集线索。') WHERE LOWER(code) = LOWER('PagesHtmlHint') AND area = 'A';
|
||||
INSERT INTO message (code, content, area) SELECT 'PagesHideHeader', JSON_OBJECT('en_US', 'Hide page header', 'zh_CN', '隐藏页面顶栏'), 'A' FROM DUAL WHERE NOT EXISTS (SELECT 1 FROM message WHERE LOWER(code) = LOWER('PagesHideHeader') AND area = 'A');
|
||||
UPDATE message SET content = JSON_SET(COALESCE(content, JSON_OBJECT()), '$.zh_CN', '隐藏页面顶栏') WHERE LOWER(code) = LOWER('PagesHideHeader') AND area = 'A';
|
||||
INSERT INTO message (code, content, area) SELECT 'PagesMoveUp', JSON_OBJECT('en_US', 'Move up', 'zh_CN', '上移'), 'A' FROM DUAL WHERE NOT EXISTS (SELECT 1 FROM message WHERE LOWER(code) = LOWER('PagesMoveUp') AND area = 'A');
|
||||
UPDATE message SET content = JSON_SET(COALESCE(content, JSON_OBJECT()), '$.zh_CN', '上移') WHERE LOWER(code) = LOWER('PagesMoveUp') AND area = 'A';
|
||||
INSERT INTO message (code, content, area) SELECT 'PagesMoveDown', JSON_OBJECT('en_US', 'Move down', 'zh_CN', '下移'), 'A' FROM DUAL WHERE NOT EXISTS (SELECT 1 FROM message WHERE LOWER(code) = LOWER('PagesMoveDown') AND area = 'A');
|
||||
|
||||
@@ -0,0 +1,11 @@
|
||||
-- The labels added by add-custom-html-block, cut from the output of `npm run i18n:sql`. Run it only in an environment that already ran add-zh-cn-messages/sql/03-zh-cn-messages.sql; otherwise run that 03 file, which includes these labels.
|
||||
SET NAMES utf8mb4;
|
||||
USE `service-system`;
|
||||
INSERT INTO message (code, content, area) SELECT 'PagesBlockHtml', JSON_OBJECT('en_US', 'Custom HTML', 'zh_CN', '自定义 HTML'), 'A' FROM DUAL WHERE NOT EXISTS (SELECT 1 FROM message WHERE LOWER(code) = LOWER('PagesBlockHtml') AND area = 'A');
|
||||
UPDATE message SET content = JSON_SET(COALESCE(content, JSON_OBJECT()), '$.zh_CN', '自定义 HTML') WHERE LOWER(code) = LOWER('PagesBlockHtml') AND area = 'A';
|
||||
INSERT INTO message (code, content, area) SELECT 'PagesHtml', JSON_OBJECT('en_US', 'HTML', 'zh_CN', 'HTML'), 'A' FROM DUAL WHERE NOT EXISTS (SELECT 1 FROM message WHERE LOWER(code) = LOWER('PagesHtml') AND area = 'A');
|
||||
UPDATE message SET content = JSON_SET(COALESCE(content, JSON_OBJECT()), '$.zh_CN', 'HTML') WHERE LOWER(code) = LOWER('PagesHtml') AND area = 'A';
|
||||
INSERT INTO message (code, content, area) SELECT 'PagesHtmlHint', JSON_OBJECT('en_US', 'Forms inside the pasted HTML don''t send. Add a Sign-up form block to collect leads.', 'zh_CN', '粘贴的 HTML 中的表单不会发送。请添加报名表单区块来收集线索。'), 'A' FROM DUAL WHERE NOT EXISTS (SELECT 1 FROM message WHERE LOWER(code) = LOWER('PagesHtmlHint') AND area = 'A');
|
||||
UPDATE message SET content = JSON_SET(COALESCE(content, JSON_OBJECT()), '$.zh_CN', '粘贴的 HTML 中的表单不会发送。请添加报名表单区块来收集线索。') WHERE LOWER(code) = LOWER('PagesHtmlHint') AND area = 'A';
|
||||
INSERT INTO message (code, content, area) SELECT 'PagesHideHeader', JSON_OBJECT('en_US', 'Hide page header', 'zh_CN', '隐藏页面顶栏'), 'A' FROM DUAL WHERE NOT EXISTS (SELECT 1 FROM message WHERE LOWER(code) = LOWER('PagesHideHeader') AND area = 'A');
|
||||
UPDATE message SET content = JSON_SET(COALESCE(content, JSON_OBJECT()), '$.zh_CN', '隐藏页面顶栏') WHERE LOWER(code) = LOWER('PagesHideHeader') AND area = 'A';
|
||||
@@ -2,3 +2,5 @@
|
||||
-- because the previous release refuses the HTML block type when such a page is saved.
|
||||
USE `service-counter`;
|
||||
ALTER TABLE `cms_page` DROP COLUMN `hide_header`;
|
||||
USE `service-system`;
|
||||
DELETE FROM message WHERE code IN ('PagesBlockHtml', 'PagesHtml', 'PagesHtmlHint', 'PagesHideHeader') AND area = 'A';
|
||||
@@ -20,11 +20,11 @@ Design: `design.md` and `docs/superpowers/specs/2026-12-02-custom-html-block-des
|
||||
- [x] 2.2 `CmsBlocks`:
|
||||
- the sandboxed iframe with height messages from its own frame only;
|
||||
- `hideHeader` hides the header and the title.
|
||||
- [ ] 2.3 Editor:
|
||||
- [x] 2.3 Editor:
|
||||
- "Add Custom HTML", with a monospace text box, a counter and the forms note;
|
||||
- the "Hide page header" switch;
|
||||
- field errors at the HTML field.
|
||||
- [ ] 2.4 Labels in `defaultMessages.ts` with Chinese in `zh_CN.seed.json`, then `npm run i18n:sql`. `sql/02-html-block-labels.sql` holds only the new labels.
|
||||
- [x] 2.4 Labels in `defaultMessages.ts` with Chinese in `zh_CN.seed.json`, then `npm run i18n:sql`. `sql/02-html-block-labels.sql` holds only the new labels.
|
||||
- Verify (2.1 to 2.4): `cd solvway-admin-portal && npx vitest run; echo VITEST=$? && npx tsc --noEmit && npm run lint`
|
||||
|
||||
## 3. Release
|
||||
|
||||
@@ -19,6 +19,7 @@ const INLINE: Record<string, string[]> = {
|
||||
IMAGE_TEXT: ["imageId", "imagePosition", "heading", "html", "buttonLabel", "buttonUrl"],
|
||||
FAQ: ["heading", "items"],
|
||||
FORM: ["heading", "eventUuid"],
|
||||
HTML: ["html"],
|
||||
};
|
||||
const LISTED = /^items\[\d+\]\.(question|answer)$/;
|
||||
const SELECT = "flex h-9 w-full rounded-md border border-input bg-transparent px-3 py-1 text-sm shadow-sm disabled:cursor-not-allowed disabled:opacity-50";
|
||||
@@ -169,6 +170,15 @@ export function BlockFields(props: {
|
||||
</>
|
||||
)}
|
||||
|
||||
{block.type === "HTML" && (
|
||||
<Field id={fid("html")} label={t("PagesHtml")} error={errors.html}>
|
||||
<Textarea id={fid("html")} rows={12} spellCheck={false} className="font-mono text-xs" value={block.html ?? ""} disabled={saving}
|
||||
aria-invalid={!!errors.html} onChange={(e) => set({ html: e.target.value })} />
|
||||
<p className="tabular text-xs text-muted-foreground">{`${(block.html ?? "").length}/200000`}</p>
|
||||
<p className="text-xs text-muted-foreground">{t("PagesHtmlHint")}</p>
|
||||
</Field>
|
||||
)}
|
||||
|
||||
{block.type === "FORM" && (
|
||||
<>
|
||||
{text("heading", "PagesHeading")}
|
||||
|
||||
@@ -0,0 +1,67 @@
|
||||
import { render, screen, waitFor } from "@testing-library/react";
|
||||
import userEvent from "@testing-library/user-event";
|
||||
import { describe, it, expect, vi, beforeEach } from "vitest";
|
||||
import { ApiError } from "@/app/lib/clientApi";
|
||||
|
||||
// add-custom-html-block: the editor's Custom HTML block and "Hide page header" switch.
|
||||
const mocks = vi.hoisted(() => ({
|
||||
getPage: vi.fn(), savePage: vi.fn(), deletePage: vi.fn(), uploadPageImage: vi.fn(),
|
||||
listAdminEvents: vi.fn(), useAdminAuth: vi.fn(), useCompanyScope: vi.fn(),
|
||||
}));
|
||||
vi.mock("@/app/lib/services/adminPages", () => ({ getPage: mocks.getPage, savePage: mocks.savePage, deletePage: mocks.deletePage, uploadPageImage: mocks.uploadPageImage }));
|
||||
vi.mock("@/app/lib/services/adminEvents", () => ({ listAdminEvents: mocks.listAdminEvents }));
|
||||
vi.mock("@/app/lib/AdminAuthContext", () => ({ useAdminAuth: mocks.useAdminAuth }));
|
||||
vi.mock("@/app/lib/CompanyScope", () => ({ useCompanyScope: mocks.useCompanyScope }));
|
||||
vi.mock("next/navigation", async () => (await import("@/test/memoryRouter")).navigationMock);
|
||||
|
||||
import { memoryRouter } from "@/test/memoryRouter";
|
||||
import { PageEditor } from "./PageEditor";
|
||||
|
||||
const stored = {
|
||||
id: 5, companyId: 2, slug: "hotel", title: "Hotel", metaDescription: null, status: "PUBLISHED" as const, hideHeader: true,
|
||||
blocks: [{ type: "HTML" as const, html: "<h1>Designed</h1>" }],
|
||||
};
|
||||
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
memoryRouter.reset("/pages/5");
|
||||
mocks.useAdminAuth.mockReturnValue({ isSuper: false, companyCode: "INS" });
|
||||
mocks.useCompanyScope.mockReturnValue({ companyId: undefined, companies: [{ id: 2, code: "INS", name: "INS", status: "A" }], codeOf: (id?: number) => (id === 2 ? "INS" : "") });
|
||||
mocks.listAdminEvents.mockResolvedValue({ items: [], total: 0 });
|
||||
mocks.getPage.mockResolvedValue(stored);
|
||||
mocks.savePage.mockImplementation(async (id: number | undefined, body: object) => ({ ...stored, ...body, id: id ?? 7 }));
|
||||
});
|
||||
|
||||
describe("PageEditor: Custom HTML", () => {
|
||||
it("loads the HTML and the hide-header option, and saves both", async () => {
|
||||
const user = userEvent.setup();
|
||||
render(<PageEditor pageId={5} />);
|
||||
const box = await screen.findByLabelText("HTML");
|
||||
expect(box).toHaveValue("<h1>Designed</h1>");
|
||||
expect(box).toHaveClass("font-mono");
|
||||
expect(screen.getByText("17/200000")).toBeInTheDocument();
|
||||
expect(screen.getByText(/Forms inside the pasted HTML don't send/)).toBeInTheDocument();
|
||||
const toggle = screen.getByRole("switch", { name: "Hide page header" });
|
||||
expect(toggle).toBeChecked();
|
||||
|
||||
await user.click(toggle);
|
||||
await user.click(screen.getByRole("button", { name: "Add Custom HTML" }));
|
||||
await user.type(screen.getAllByLabelText("HTML")[1], "<p>x</p>");
|
||||
await user.click(screen.getByRole("button", { name: "Save" }));
|
||||
await waitFor(() => expect(mocks.savePage).toHaveBeenCalled());
|
||||
const body = mocks.savePage.mock.calls[0][1];
|
||||
expect(body.hideHeader).toBe(false);
|
||||
expect(body.blocks).toEqual([{ type: "HTML", html: "<h1>Designed</h1>" }, { type: "HTML", html: "<p>x</p>" }]);
|
||||
});
|
||||
|
||||
it("shows a refusal of the HTML at its field", async () => {
|
||||
mocks.savePage.mockRejectedValue(new ApiError("x", 422, { code: "PagesTooLong", details: { field: "blocks[0].html" } }));
|
||||
const user = userEvent.setup();
|
||||
render(<PageEditor pageId={5} />);
|
||||
const box = await screen.findByLabelText("HTML");
|
||||
await user.click(screen.getByRole("button", { name: "Save" }));
|
||||
expect(await screen.findByText("Too long.")).toBeInTheDocument();
|
||||
expect(box.closest("div")).toHaveTextContent("Too long.");
|
||||
expect(box).toHaveAttribute("aria-invalid", "true");
|
||||
});
|
||||
});
|
||||
@@ -8,6 +8,7 @@ import { Card, CardContent, CardHeader, CardTitle } from "@/app/components/ui/ca
|
||||
import { ConfirmDialog } from "@/app/components/ui/confirm-dialog";
|
||||
import { Input } from "@/app/components/ui/input";
|
||||
import { Label } from "@/app/components/ui/label";
|
||||
import { Switch } from "@/app/components/ui/switch";
|
||||
import { Textarea } from "@/app/components/ui/textarea";
|
||||
import { ErrorState } from "@/app/components/ui/states";
|
||||
import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "@/app/components/ui/select";
|
||||
@@ -76,7 +77,7 @@ export function PageEditor({ pageId }: { pageId?: number }) {
|
||||
getPage(pageId)
|
||||
.then((p) => {
|
||||
if (cancelled) return;
|
||||
setDraft({ id: p.id, companyId: p.companyId, slug: p.slug, slugTouched: true, title: p.title, metaDescription: p.metaDescription ?? "", status: p.status, blocks: [] });
|
||||
setDraft({ id: p.id, companyId: p.companyId, slug: p.slug, slugTouched: true, title: p.title, metaDescription: p.metaDescription ?? "", status: p.status, hideHeader: !!p.hideHeader, blocks: [] });
|
||||
setRows(toRows(p.blocks ?? []));
|
||||
setSaved({ slug: p.slug, status: p.status });
|
||||
})
|
||||
@@ -294,6 +295,10 @@ export function PageEditor({ pageId }: { pageId?: number }) {
|
||||
</label>
|
||||
))}
|
||||
<FieldError message={errors.status} />
|
||||
<div className="flex items-center gap-2 pt-2 text-sm">
|
||||
<Switch id="page-hide-header" checked={draft.hideHeader} disabled={saving} onCheckedChange={(on) => patch({ hideHeader: on })} />
|
||||
<Label htmlFor="page-hide-header">{t("PagesHideHeader")}</Label>
|
||||
</div>
|
||||
</CardContent>
|
||||
</Card>
|
||||
</div>
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
|
||||
export type BlockType = "HERO" | "RICH_TEXT" | "IMAGE_TEXT" | "FAQ" | "FORM" | "HTML";
|
||||
|
||||
export const BLOCK_TYPES: BlockType[] = ["HERO", "RICH_TEXT", "IMAGE_TEXT", "FAQ", "FORM"];
|
||||
export const BLOCK_TYPES: BlockType[] = ["HERO", "RICH_TEXT", "IMAGE_TEXT", "FAQ", "FORM", "HTML"];
|
||||
|
||||
export const BLOCK_LABELS: Record<BlockType, string> = {
|
||||
HERO: "PagesBlockHero",
|
||||
@@ -42,6 +42,7 @@ export interface CmsPage {
|
||||
title: string;
|
||||
metaDescription: string | null;
|
||||
status: PageStatus;
|
||||
hideHeader?: boolean;
|
||||
blocks: CmsBlock[];
|
||||
updatedAt?: string | null;
|
||||
}
|
||||
@@ -54,6 +55,8 @@ export interface CmsPageDraft {
|
||||
title: string;
|
||||
metaDescription: string;
|
||||
status: PageStatus;
|
||||
/** Hide the company header and the page title (add-custom-html-block). */
|
||||
hideHeader: boolean;
|
||||
blocks: CmsBlock[];
|
||||
}
|
||||
|
||||
@@ -62,7 +65,7 @@ export const imageSrc = (id: number) => `/api/public/page-images/${id}`;
|
||||
export const pageAddress = (companyCode: string, slug: string) => `/p/${companyCode}/${slug}`;
|
||||
|
||||
export function emptyPage(): CmsPageDraft {
|
||||
return { slug: "", slugTouched: false, title: "", metaDescription: "", status: "DRAFT", blocks: [] };
|
||||
return { slug: "", slugTouched: false, title: "", metaDescription: "", status: "DRAFT", hideHeader: false, blocks: [] };
|
||||
}
|
||||
|
||||
export function emptyBlock(type: BlockType): CmsBlock {
|
||||
@@ -73,6 +76,8 @@ export function emptyBlock(type: BlockType): CmsBlock {
|
||||
return { type, imageId: null, heading: "", html: "", imagePosition: "LEFT", buttonLabel: "", buttonUrl: "" };
|
||||
case "FORM":
|
||||
return { type, heading: "", eventUuid: null };
|
||||
case "HTML":
|
||||
return { type, html: "" };
|
||||
default:
|
||||
return { type };
|
||||
}
|
||||
@@ -97,6 +102,7 @@ export function pageBody(draft: CmsPageDraft) {
|
||||
title: draft.title.trim(),
|
||||
metaDescription: draft.metaDescription.trim() || null,
|
||||
status: draft.status,
|
||||
hideHeader: draft.hideHeader,
|
||||
blocks: draft.blocks.map((b) => {
|
||||
const out: Record<string, unknown> = { type: b.type };
|
||||
for (const [key, val] of Object.entries(b)) {
|
||||
|
||||
@@ -756,6 +756,10 @@ export const DEFAULT_MESSAGES: Record<string, string> = {
|
||||
PagesBlockImageText: "Image with text",
|
||||
PagesBlockFaq: "FAQ",
|
||||
PagesBlockForm: "Sign-up form",
|
||||
PagesBlockHtml: "Custom HTML",
|
||||
PagesHtml: "HTML",
|
||||
PagesHtmlHint: "Forms inside the pasted HTML don't send. Add a Sign-up form block to collect leads.",
|
||||
PagesHideHeader: "Hide page header",
|
||||
PagesMoveUp: "Move up",
|
||||
PagesMoveDown: "Move down",
|
||||
PagesRemoveBlock: "Remove block",
|
||||
|
||||
@@ -763,6 +763,10 @@
|
||||
"PagesChooseEvent": "选择活动",
|
||||
"PagesUnavailableEvent": "活动已不可用",
|
||||
"PagesFormEventHint": "报名会成为此活动的线索。一般咨询请选择长期的咨询活动。",
|
||||
"PagesBlockHtml": "自定义 HTML",
|
||||
"PagesHtml": "HTML",
|
||||
"PagesHtmlHint": "粘贴的 HTML 中的表单不会发送。请添加报名表单区块来收集线索。",
|
||||
"PagesHideHeader": "隐藏页面顶栏",
|
||||
"PagesFormClosed": "报名已截止。",
|
||||
"PagesFormPreview": "发布后的网页会在此显示报名表。",
|
||||
"PagesToolbar": "格式",
|
||||
|
||||
@@ -2,6 +2,7 @@ import { describe, it, expect } from "vitest";
|
||||
import fs from "node:fs";
|
||||
import path from "node:path";
|
||||
import ts from "typescript";
|
||||
import { FRAME_SANDBOX } from "@/app/lib/frameHtml";
|
||||
|
||||
// Spec (admin-portal-markup-safety), security finding F09: the admin portal never injects HTML into its own origin.
|
||||
// Data HTML goes into <iframe sandbox="" srcDoc>. Allow-listing a file here is a security
|
||||
@@ -26,6 +27,13 @@ const SINK_ALLOWED: Record<string, Sink[]> = {
|
||||
"components/pages/SafeHtml.tsx": ["dangerouslySetInnerHTML"],
|
||||
};
|
||||
|
||||
// add-custom-html-block: a Custom HTML block's frame loads its own address, whose response carries a `sandbox` CSP
|
||||
// (opaque origin), and may run scripts but never as this origin: FRAME_SANDBOX holds no allow-same-origin.
|
||||
// Only this file may give an iframe that one non-empty sandbox, and only as the named constant.
|
||||
const SANDBOX_ALLOWED: Record<string, string> = {
|
||||
"components/pages/HtmlFrame.tsx": "FRAME_SANDBOX",
|
||||
};
|
||||
|
||||
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;
|
||||
@@ -58,7 +66,9 @@ function iframeProblems(fileName: string, src: string): string[] {
|
||||
(p): p is ts.JsxAttribute => ts.isJsxAttribute(p) && p.name.getText(sf) === "sandbox",
|
||||
);
|
||||
const value = sandbox?.initializer;
|
||||
if (!value || !ts.isStringLiteral(value) || value.text !== "") {
|
||||
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 !== "")) {
|
||||
problems.push(`${fileName}:${line} iframe without sandbox=""`);
|
||||
}
|
||||
}
|
||||
@@ -106,6 +116,13 @@ describe("admin portal never injects HTML into its own origin", () => {
|
||||
}
|
||||
});
|
||||
|
||||
it("lets the Custom HTML frame run scripts, never as this origin, submit forms or navigate the page", () => {
|
||||
const grants = FRAME_SANDBOX.split(/\s+/);
|
||||
for (const g of ["allow-same-origin", "allow-forms", "allow-top-navigation", "allow-top-navigation-by-user-activation", "allow-modals"]) {
|
||||
expect(grants).not.toContain(g);
|
||||
}
|
||||
});
|
||||
|
||||
it("gives every iframe a literal empty sandbox", () => {
|
||||
const problems = files.flatMap((f) => iframeProblems(f.rel, f.src));
|
||||
expect(problems).toEqual([]);
|
||||
@@ -127,6 +144,13 @@ describe("iframe checker", () => {
|
||||
expect(check(`<iframe sandbox="" {...props} srcDoc={html} />`)).toHaveLength(1);
|
||||
});
|
||||
|
||||
it("accepts the named Custom HTML sandbox only in its allow-listed file", () => {
|
||||
const src = `export const X = () => <iframe title="t" sandbox={FRAME_SANDBOX} src={u} />;`;
|
||||
expect(iframeProblems("components/pages/HtmlFrame.tsx", src)).toEqual([]);
|
||||
expect(iframeProblems("fixture.tsx", src)).toHaveLength(1);
|
||||
expect(iframeProblems("components/pages/HtmlFrame.tsx", src.replace("FRAME_SANDBOX", "mode"))).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);
|
||||
|
||||
@@ -28,7 +28,7 @@ export default async function PagePreview({ params }: Props) {
|
||||
const banner = <p role="status" className="bg-warning/15 px-4 py-2 text-center text-sm font-medium">{t("PagesPreviewBanner")}</p>;
|
||||
return (
|
||||
<RequireModule pathname="/pages">
|
||||
<LandingPage company={page.company ?? { code: "", name: "" }} page={page} banner={banner} preview frameBase={`/pages/${id}/preview/frame`} />
|
||||
<LandingPage company={page.company ?? { code: "", name: "" }} page={page} banner={banner} preview frameBase={`/pages/${encodeURIComponent(id)}/preview/frame`} />
|
||||
</RequireModule>
|
||||
);
|
||||
}
|
||||
Reference in new issue
Block a user