Skip to content

Shared task board where people and AI coding agents work as teammates. Source-available, self-hostable.

Go
2.2 KB
Raw Blame
"use client";

/**
 * Shared HTML preview iframe.
 *
 * Used by:
 *   - HtmlPreviewBody (the attachment viewer's html kind)
 *   - CodeBlockView for fenced ```html blocks (editable Tiptap NodeView),
 *     fed the same themed document as HtmlBlockPreview
 *   - HtmlBlockPreview for fenced ```html blocks (RichContent), which also
 *     listens to the size/error bridge in utils/html-block-document.ts
 *
 * Sandbox semantics:
 *   sandbox="allow-scripts" (NOT "allow-same-origin")
 *   → iframe runs in an opaque origin: scripts execute (chart JS works),
 *     but cookie / localStorage / parent access / top-nav / popups / forms
 *     remain blocked. This is the standard "preview untrusted HTML" model
 *     (HTML spec §iframe sandbox, MDN, Claude artifacts, v0.dev preview).
 *
 * The server-side `text/plain` + `nosniff` defense at
 * /api/attachments/{id}/content remains untouched — we only feed iframe.srcDoc
 * the text body we fetched, never point iframe.src at the proxy URL.
 */

import type { CSSProperties, Ref } from "react";
import { cn } from "@multica/ui/lib/utils";

interface CodeBlockIframeProps {
  /** Document source for srcDoc. Empty string renders a blank frame. */
  html: string;
  /** Iframe title for accessibility. */
  title: string;
  className?: string;
  /** Tailwind height token; defaults to h-[480px]. */
  heightClassName?: string;
  style?: CSSProperties;
  ref?: Ref<HTMLIFrameElement>;
  onLoad?: () => void;
}

export function CodeBlockIframe({
  html,
  title,
  className,
  heightClassName = "h-[480px]",
  style,
  ref,
  onLoad,
}: CodeBlockIframeProps) {
  return (
    <iframe
      ref={ref}
      // srcDoc keeps the body in the parent's process but isolated to an
      // opaque origin via sandbox. Critical that we never combine
      // `allow-scripts` with `allow-same-origin` — that pairing defeats the
      // sandbox per the HTML spec (notes on the sandbox attribute).
      srcDoc={html}
      sandbox="allow-scripts"
      title={title}
      className={cn(
        "w-full rounded-md border border-border bg-background",
        heightClassName,
        className,
      )}
      style={style}
      onLoad={onLoad}
    />
  );
}