Файли
openidea/src/platform/sandboxFrame.ts
T
Deng Liuтаdengliu 85cbc6bacd feat(mdx): add side-by-side MDX preview with opt-in sandboxed JSX execution (#6881)
Adds Editor Only / Editor and Preview / Preview Only layouts for .mdx files.

- Safe Static Mode on open: frontmatter, GFM, math, Mermaid, MDX comments and documentation tags (Steps, CardGroup, Note, Tabs, Accordion) render through the sanitized Markdown pipeline with scroll sync and no script execution.
- Run JSX (or Trust Project in Restricted Mode) lazy-loads @mdx-js/mdx and sucrase and evaluates JSX, workspace/tsconfig/node_modules imports, .mdx transclusions, stylesheets, assets and custom layouts inside an opaque-origin sandboxed iframe with its own CSP, no network and no IPC.
- The host-guest bridge is a per-document MessagePort; image, media, module and link resolution stay inside the window's project roots, checked lexically and after canonicalization (new fs_canonicalize command, which rejects parent traversal).
- Execution requires a current, affirmative trust result for the file's owning project, including attached workspace members, whose trust changes now emit ProjectTrustChanged.
- External links open only from a real click on an anchor in the frame and always go through a confirmation dialog showing the parsed destination.
- Settings > Languages & Frameworks > Markdown adds MDX component execution (Ask before running / Run automatically in trusted projects / Disabled), a user-only setting that project settings cannot enable.

Closes #6876

Co-authored-by: dengliu <[email protected]>
2026-12-02 08:39:22 +08:00

70 рядки
2.5 KiB
TypeScript

/**
* Shared sandboxed `<iframe sandbox="allow-scripts">` host URL, CSP, and theme
* token projection (`src-tauri/src/plugin_web.rs`).
*/
import { resolveTokenColor, toRgb } from "./cssColor";
export const MDX_FRAME_CSP =
"default-src 'none'; script-src 'unsafe-inline'; style-src 'unsafe-inline'; img-src data: blob:; font-src data:; media-src data: blob:";
export const SANDBOX_FRAME_TAG = "openidea-view";
export const MDX_FRAME_HOST_PATH = ".mdx-frame";
const SANDBOX_THEME_TOKENS = {
"--openidea-bg": "--bg-island",
"--openidea-bg-elevated": "--bg-elev",
"--openidea-bg-hover": "--bg-hover",
"--openidea-bg-selected": "--bg-tree-selected",
"--openidea-bg-input": "--bg-input",
"--openidea-fg": "--fg",
"--openidea-fg-strong": "--fg-strong",
"--openidea-fg-muted": "--fg-muted",
"--openidea-border": "--border",
"--openidea-accent": "--accent",
"--openidea-fg-on-accent": "--fg-on-accent",
"--openidea-link": "--link",
"--openidea-focus-ring": "--focus-ring",
"--openidea-danger": "--danger",
"--openidea-warning": "--warning",
"--openidea-success": "--success",
"--openidea-info": "--info",
"--openidea-change-added": "--gutter-added",
"--openidea-change-modified": "--gutter-modified",
"--openidea-change-deleted": "--gutter-deleted",
} as const;
const SANDBOX_FONT_TOKENS = {
"--openidea-font-ui": "--font-ui",
"--openidea-font-mono": "--font-mono",
"--openidea-font-size": "--fs-ui",
} as const;
export interface SandboxFrameTheme {
dark: boolean;
vars: Record<string, string>;
}
const safeCssValue = (v: string) => v.length <= 400 && !/[<>{};\\]/.test(v);
export function sandboxFrameUrl(path: string, windows = /windows/i.test(navigator.userAgent)): string {
const encoded = path.split("/").map(encodeURIComponent).join("/");
return windows ? `http://openidea-plugin.localhost/${encoded}` : `openidea-plugin://localhost/${encoded}`;
}
export function sandboxFrameTheme(root: HTMLElement = document.documentElement): SandboxFrameTheme {
const vars: Record<string, string> = {};
for (const [name, token] of Object.entries(SANDBOX_THEME_TOKENS)) {
const c = resolveTokenColor(token, root);
if (c) vars[name] = toRgb(c);
}
if (typeof getComputedStyle !== "undefined") {
const css = getComputedStyle(root);
for (const [name, token] of Object.entries(SANDBOX_FONT_TOKENS)) {
const v = css.getPropertyValue(token).trim();
if (v && safeCssValue(v)) vars[name] = v;
}
}
return { dark: root.dataset.theme !== "light", vars };
}