Skip to content

fix: sanitizer svg to avoid xss #16608

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 24, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 9 additions & 16 deletions web/app/components/base/markdown.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import SyntaxHighlighter from 'react-syntax-highlighter'
import { atelierHeathLight } from 'react-syntax-highlighter/dist/esm/styles/hljs'
import { Component, memo, useMemo, useRef, useState } from 'react'
import type { CodeComponent } from 'react-markdown/lib/ast-to-react'
import SVGRenderer from './svg-gallery'
import cn from '@/utils/classnames'
import CopyBtn from '@/app/components/base/copy-btn'
import SVGBtn from '@/app/components/base/svg'
Expand Down Expand Up @@ -118,13 +119,13 @@ const CodeBlock: CodeComponent = memo(({ inline, className, children, ...props }
</div>
)
}
// else if (language === 'svg' && isSVG) {
// return (
// <ErrorBoundary>
// <SVGRenderer content={content} />
// </ErrorBoundary>
// )
// }
else if (language === 'svg' && isSVG) {
return (
<ErrorBoundary>
<SVGRenderer content={content} />
</ErrorBoundary>
)
}
else {
return (
<SyntaxHighlighter
Expand Down Expand Up @@ -224,16 +225,8 @@ const Link = ({ node, ...props }: any) => {
}
}

function escapeSVGTags(htmlString: string): string {
return htmlString.replace(/(<svg[\s\S]*?>)([\s\S]*?)(<\/svg>)/gi, (match: string, openTag: string, innerContent: string, closeTag: string): string => {
return openTag.replace(/</g, '&lt;').replace(/>/g, '&gt;')
+ innerContent.replace(/</g, '&lt;').replace(/>/g, '&gt;')
+ closeTag.replace(/</g, '&lt;').replace(/>/g, '&gt;')
})
}

export function Markdown(props: { content: string; className?: string }) {
const latexContent = preprocessLaTeX(escapeSVGTags(props.content))
const latexContent = preprocessLaTeX(props.content)
return (
<div className={cn(props.className, 'markdown-body')}>
<ReactMarkdown
Expand Down
3 changes: 2 additions & 1 deletion web/app/components/base/svg-gallery/index.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { useEffect, useRef, useState } from 'react'
import { SVG } from '@svgdotjs/svg.js'
import DOMPurify from 'dompurify'
import ImagePreview from '@/app/components/base/image-uploader/image-preview'

export const SVGRenderer = ({ content }: { content: string }) => {
Expand Down Expand Up @@ -44,7 +45,7 @@ export const SVGRenderer = ({ content }: { content: string }) => {

svgRef.current.style.width = `${Math.min(originalWidth, 298)}px`

const rootElement = draw.svg(content)
const rootElement = draw.svg(DOMPurify.sanitize(content))

rootElement.click(() => {
setImagePreview(svgToDataURL(svgElement as Element))
Expand Down
1 change: 1 addition & 0 deletions web/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
"crypto-js": "^4.2.0",
"dayjs": "^1.11.7",
"decimal.js": "^10.4.3",
"dompurify": "^3.2.4",
"echarts": "^5.5.1",
"echarts-for-react": "^3.0.2",
"elkjs": "^0.9.3",
Expand Down