🤯 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.
TypeScript agentagent-collaborationagent-harnessloop-engineeringmcppersonal-assistantproactive-agentskillssuper-agenttask-management
🔒 fix: sanitize HTML artifact content and sandbox iframe to prevent XSS-to-RCE (#13529)
Arvin Xu25e851b3592f
diff --git a/packages/utils/src/client/sanitize.ts b/packages/utils/src/client/sanitize.tsindex cb37abae0ed..3384be7ae07 100644--- a/packages/utils/src/client/sanitize.ts+++ b/packages/utils/src/client/sanitize.ts@@ -1,5 +1,38 @@import DOMPurify from 'dompurify';+const FORBID_EVENT_HANDLERS = [+ 'onblur',+ 'onchange',+ 'onclick',+ 'onerror',+ 'onfocus',+ 'onkeydown',+ 'onkeypress',+ 'onkeyup',+ 'onload',+ 'onmousedown',+ 'onmouseout',+ 'onmouseover',+ 'onmouseup',+ 'onreset',+ 'onselect',+ 'onsubmit',+ '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@@ -7,25 +40,7 @@ import DOMPurify from 'dompurify';*/export const sanitizeSVGContent = (content: string): string => {return DOMPurify.sanitize(content, {- FORBID_ATTR: [- 'onblur',- 'onchange',- 'onclick',- 'onerror',- 'onfocus',- 'onkeydown',- 'onkeypress',- 'onkeyup',- 'onload',- 'onmousedown',- 'onmouseout',- 'onmouseover',- 'onmouseup',- 'onreset',- 'onselect',- 'onsubmit',- 'onunload',- ],+ FORBID_ATTR: FORBID_EVENT_HANDLERS,FORBID_TAGS: ['embed', 'link', 'object', 'script', 'style'],KEEP_CONTENT: false,USE_PROFILES: { svg: true, svgFilters: true },diff --git a/src/features/Portal/Artifacts/Body/Renderer/HTML.tsx b/src/features/Portal/Artifacts/Body/Renderer/HTML.tsxindex 8b4455ec25e..9b1c7a6e3aa 100644--- a/src/features/Portal/Artifacts/Body/Renderer/HTML.tsx+++ b/src/features/Portal/Artifacts/Body/Renderer/HTML.tsx@@ -1,4 +1,5 @@-import { memo, useEffect, useRef } from 'react';+import { sanitizeHTMLContent } from '@lobechat/utils/client';+import { memo, useMemo } from 'react';interface HTMLRendererProps {height?: string;@@ -6,20 +7,16 @@ interface HTMLRendererProps {width?: string;}const HTMLRenderer = memo<HTMLRendererProps>(({ htmlContent, width = '100%', height = '100%' }) => {- const iframeRef = useRef<HTMLIFrameElement>(null);+ const sanitizedContent = useMemo(() => sanitizeHTMLContent(htmlContent), [htmlContent]);- useEffect(() => {- if (!iframeRef.current) return;-- const doc = iframeRef.current.contentDocument;- if (!doc) return;-- doc.open();- doc.write(htmlContent);- doc.close();- }, [htmlContent]);-- return <iframe ref={iframeRef} style={{ border: 'none', height, width }} title="html-renderer" />;+ return (+ <iframe+ sandbox=""+ srcDoc={sanitizedContent}+ style={{ border: 'none', height, width }}+ title="html-renderer"+ />+ );});export default HTMLRenderer;