Files
co-lite/solvway-admin-portal/app/lib/frameHtml.ts
T
ngqileandClaude Opus 5.5 36dbb4c949 fix(pages): keep the frame's sandbox policy past the config headers
next.config's baseline Content-Security-Policy replaced the frame route's
own header, so a frame address opened directly ran without the sandbox.
A later headers rule for the two frame paths restores it. Also drop the
gap above a header-less page.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01EFUzfiCVCgCVJZq4NDEqZs
2026-12-02 18:18:51 +08:00

68 lines
3.5 KiB
TypeScript

// add-custom-html-block: a Custom HTML block runs sealed. It is served as its own document whose
// Content-Security-Policy `sandbox` gives it an opaque origin, even when its address is opened directly, and it is
// shown in an <iframe sandbox> with the same grants. No same-origin, forms, top navigation or modals.
export const FRAME_SANDBOX = "allow-scripts allow-popups allow-popups-to-escape-sandbox";
/** The frame document's own policy. next.config sends it too: config headers replace a route's header of the same name. */
export const FRAME_CSP = `sandbox ${FRAME_SANDBOX}; frame-ancestors 'self'`;
export const FRAME_MESSAGE = "colite-frame-height";
export const START_HEIGHT = 600;
const MIN_HEIGHT = 100;
const MAX_HEIGHT = 20000;
// Posts the document height to the parent page on load and whenever it changes.
const HEIGHT_SCRIPT =
`<script>(function(){function s(){parent.postMessage({type:"${FRAME_MESSAGE}",height:Math.ceil(document.documentElement.scrollHeight)},"*")}` +
`addEventListener("load",s);if(window.ResizeObserver)new ResizeObserver(s).observe(document.documentElement);s()})();</script>`;
/** The pasted HTML with the height script before its last </body>, or appended when it is a fragment. */
export function frameDocument(html: string): string {
const at = html.toLowerCase().lastIndexOf("</body>");
return at < 0 ? html + HEIGHT_SCRIPT : html.slice(0, at) + HEIGHT_SCRIPT + html.slice(at);
}
export function frameResponse(html: string, { preview }: { preview: boolean }): Response {
return new Response(frameDocument(html), {
headers: {
"Content-Type": "text/html; charset=utf-8",
"Content-Security-Policy": FRAME_CSP,
"X-Content-Type-Options": "nosniff",
"Referrer-Policy": "no-referrer",
"Cache-Control": preview ? "no-store" : "public, max-age=60",
},
});
}
/** The same empty answer for every frame miss. */
export const frameNotFound = () => new Response(null, { status: 404, headers: { "Cache-Control": "no-store" } });
/** The HTML of block `index` when it is an HTML block, else null. */
export function htmlBlock(blocks: unknown, index: string): string | null {
if (!/^\d{1,2}$/.test(index) || !Array.isArray(blocks)) return null;
const block = blocks[Number(index)] as { type?: unknown; html?: unknown } | undefined;
return block?.type === "HTML" && typeof block.html === "string" ? block.html : null;
}
export interface FrameHeight {
height: number;
/** The last growth steps, to spot content that grows with the frame. */
steps: number[];
}
/**
* The frame's next height after a message from it. Junk leaves the state as it is. A section sized by the viewport
* (100vh) grows with the frame, so each report is the frame plus the same remainder: after two equal steps the
* frame stops growing.
* ponytail: equal-step heuristic; a page that grows by the same step three times for another reason stops early.
*/
export function nextFrameHeight(state: FrameHeight, data: unknown): FrameHeight {
const d = data as { type?: unknown; height?: unknown } | null;
if (!d || typeof d !== "object" || d.type !== FRAME_MESSAGE || typeof d.height !== "number" || !Number.isFinite(d.height)) return state;
const height = Math.min(MAX_HEIGHT, Math.max(MIN_HEIGHT, Math.ceil(d.height)));
if (height <= state.height) return { height, steps: [] };
const step = height - state.height;
const [a, b] = state.steps.slice(-2);
if (a !== undefined && b !== undefined && Math.abs(a - step) <= 2 && Math.abs(b - step) <= 2) return state;
return { height, steps: [...state.steps, step].slice(-2) };
}