Skip to content

🤯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.

🐛 fix: not use sanitizeHTMLContent to block the scripts & sandbox styles (#13649)

LiJian7300b53e9935
diff --git a/packages/utils/src/client/sanitize.ts b/packages/utils/src/client/sanitize.ts
index 3384be7ae07..23b0ca04e83 100644
--- a/packages/utils/src/client/sanitize.ts
+++ b/packages/utils/src/client/sanitize.ts
@@ -20,19 +20,6 @@ const FORBID_EVENT_HANDLERS = [
'onunload',
];
-/**
- * Sanitizes HTML content to prevent XSS attacks while preserving safe HTML elements
- * @param content - The HTML content to sanitize
- * @returns Sanitized HTML content safe for rendering
- */
-export const sanitizeHTMLContent = (content: string): string => {
- return DOMPurify.sanitize(content, {
- FORBID_ATTR: FORBID_EVENT_HANDLERS,
- FORBID_TAGS: ['embed', 'link', 'meta', 'object', 'script'],
- KEEP_CONTENT: true,
- });
-};
-
/**
* Sanitizes SVG content to prevent XSS attacks while preserving safe SVG elements and attributes
* @param content - The SVG content to sanitize
diff --git a/src/features/Portal/Artifacts/Body/Renderer/HTML.tsx b/src/features/Portal/Artifacts/Body/Renderer/HTML.tsx
index 9b1c7a6e3aa..3d09c2bbca1 100644
--- a/src/features/Portal/Artifacts/Body/Renderer/HTML.tsx
+++ b/src/features/Portal/Artifacts/Body/Renderer/HTML.tsx
@@ -1,18 +1,33 @@
-import { sanitizeHTMLContent } from '@lobechat/utils/client';
-import { memo, useMemo } from 'react';
+import { memo } from 'react';
interface HTMLRendererProps {
height?: string;
htmlContent: string;
width?: string;
}
-const HTMLRenderer = memo<HTMLRendererProps>(({ htmlContent, width = '100%', height = '100%' }) => {
- const sanitizedContent = useMemo(() => sanitizeHTMLContent(htmlContent), [htmlContent]);
+// Security boundary: the iframe runs in a unique opaque origin because the
+// sandbox attribute does NOT include `allow-same-origin`. This blocks
+// `window.parent.*` access (the GHSA-xq4x-622m-q8fq XSS-to-RCE path on
+// Electron), denies access to the app's cookies / storage, and prevents
+// top-level navigation, while still allowing scripts and styles to run so
+// that LLM-generated single-file HTML demos (Tailwind CDN, p5.js, three.js,
+// vanilla JS, etc.) actually work.
+//
+// IMPORTANT — do NOT add the following capabilities:
+// - `allow-same-origin`: combined with `allow-scripts` it lets the iframe
+// remove its own sandbox and reintroduces the original XSS-to-RCE.
+// - `allow-popups`: in the Electron desktop app `setWindowOpenHandler`
+// unconditionally forwards `window.open(url)` to `shell.openExternal`,
+// which means untrusted artifact scripts could launch arbitrary
+// protocol handlers / external URLs with zero user interaction.
+// - `allow-top-navigation` (any flavor): would let the artifact hijack
+// the host window.
+const HTMLRenderer = memo<HTMLRendererProps>(({ htmlContent, width = '100%', height = '100%' }) => {
return (
<iframe
- sandbox=""
- srcDoc={sanitizedContent}
+ sandbox="allow-scripts allow-forms allow-modals"
+ srcDoc={htmlContent}
style={{ border: 'none', height, width }}
title="html-renderer"
/>