🪢 Open source agent evals & observability: Trace, evaluate, and improve LLM applications with one open platform.
TypeScript analyticsautogenevaluationlangchainlarge-language-modelsllama-indexllmllm-evaluationllm-observabilityllmopsmonitoringobservabilityopen-sourceopenaiplaygroundprompt-engineeringprompt-managementself-hostedycombinator
feat(MarkdownViewer): add URL sanitization using DOMPurify for safe link rendering (#7232)
marliessophieeccfec36e86a
diff --git a/web/src/components/ui/MarkdownViewer.tsx b/web/src/components/ui/MarkdownViewer.tsxindex 3731221f5b..56efd9eae0 100644--- a/web/src/components/ui/MarkdownViewer.tsx+++ b/web/src/components/ui/MarkdownViewer.tsx@@ -34,6 +34,7 @@ import { type MediaReturnType } from "@/src/features/media/validation";import { JSONView } from "@/src/components/ui/CodeJsonViewer";import { MarkdownJsonViewHeader } from "@/src/components/ui/MarkdownJsonView";import { copyTextToClipboard } from "@/src/utils/clipboard";+import DOMPurify from "dompurify";type ReactMarkdownNode = ReactMarkdownExtraProps["node"];type ReactMarkdownNodeChildren = Exclude<@@ -50,6 +51,33 @@ const MemoizedReactMarkdown: FC<Options> = memo(prevProps.className === nextProps.className,);+const getSafeUrl = (href: string | undefined | null): string | null => {+ if (!href || typeof href !== "string") return null;++ // DOMPurify's default sanitization is quite permissive but safe+ // It blocks javascript:, data: with scripts, vbscript:, etc.+ // But allows http:, https:, ftp:, mailto:, tel:, and many others+ try {+ const sanitized = DOMPurify.sanitize(href, {+ // ALLOWED_TAGS: An array of HTML tags that are explicitly permitted in the output.+ // Setting this to an empty array means that no HTML tags are allowed.+ // Any HTML tag found within the 'href' string would be stripped out.+ ALLOWED_TAGS: [],++ // ALLOWED_ATTR: An array of HTML attributes that are explicitly permitted on allowed tags.+ // Setting this to an empty array means that no HTML attributes are allowed.+ // Similar to ALLOWED_TAGS, this ensures that if any attributes are somehow+ // embedded within the URL string (e.g., malformed or attempting injection),+ // they will be removed by DOMPurify. We only expect a pure URL string.+ ALLOWED_ATTR: [],+ });++ return sanitized || null;+ } catch (error) {+ return null;+ }+};+const isTextElement = (child: ReactNode): child is ReactElement =>isValidElement(child) &&typeof child.type !== "string" &&@@ -110,12 +138,24 @@ function MarkdownRenderer({);},a({ children, href }) {- if (href)+ const safeHref = getSafeUrl(href);+ if (safeHref) {return (- <Link href={href} className="underline" target="_blank">+ <Link+ href={safeHref}+ className="underline"+ target="_blank"+ rel="noopener noreferrer"+ >{children}</Link>);+ }+ return (+ <span className="text-muted-foreground underline">+ {children}+ </span>+ );},ul({ children }) {if (isChecklist(children))