-
Notifications
You must be signed in to change notification settings - Fork 854
🔒 Security: Fix critical XSS vulnerability in Markdown renderer #914
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
base: master
Are you sure you want to change the base?
Changes from 1 commit
52142e8
8fcb3e9
fa739da
19e9821
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,6 @@ | ||
| import ReactMarkdown from 'react-markdown' | ||
| import rehypeRaw from 'rehype-raw' | ||
| import rehypeSanitize, { defaultSchema } from 'rehype-sanitize' | ||
| import rehypeHighlight from 'rehype-highlight' | ||
| import remarkGfm from 'remark-gfm' | ||
| import remarkBreaks from 'remark-breaks' | ||
|
|
@@ -108,6 +109,34 @@ const ThinkComponent = ({ node, children, ...props }) => { | |
| ) | ||
| } | ||
|
|
||
| // Configure sanitize schema to prevent XSS attacks | ||
| const sanitizeSchema = { | ||
| ...defaultSchema, | ||
| attributes: { | ||
| ...defaultSchema.attributes, | ||
| // Allow safe attributes | ||
| '*': ['className', 'id', 'dir'], | ||
| a: ['href', 'title', 'target', 'rel'], | ||
| img: ['src', 'alt', 'title', 'width', 'height'], | ||
| video: ['src', 'controls', 'width', 'height'], | ||
|
Comment on lines
+120
to
+121
|
||
| code: ['className'], | ||
| div: ['className', 'id', 'dir'], | ||
| span: ['className', 'id'], | ||
|
anuxs marked this conversation as resolved.
Outdated
|
||
| // Add custom element 'think' | ||
| think: [], | ||
| }, | ||
| tagNames: [ | ||
| ...(defaultSchema.tagNames || []), | ||
| 'think', // Add custom think tag | ||
| ].filter( | ||
| // Remove dangerous tags | ||
| (tag) => | ||
| !['script', 'iframe', 'object', 'embed', 'link', 'style', 'form', 'input', 'button'].includes( | ||
| tag, | ||
| ), | ||
| ), | ||
| } | ||
|
Comment on lines
+113
to
+138
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Comment on lines
+112
to
+138
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🧩 Analysis chain🏁 Script executed: cat -n src/components/MarkdownRender/markdown-without-katex.jsx | head -150Repository: ChatGPTBox-dev/chatGPTBox Length of output: 5355 🏁 Script executed: sed -n '140,200p' src/components/MarkdownRender/markdown-without-katex.jsxRepository: ChatGPTBox-dev/chatGPTBox Length of output: 1047 🏁 Script executed: sed -n '200,250p' src/components/MarkdownRender/markdown-without-katex.jsxRepository: ChatGPTBox-dev/chatGPTBox Length of output: 870 🏁 Script executed: cat package.json | grep -A 5 -B 5 "rehype-sanitize"Repository: ChatGPTBox-dev/chatGPTBox Length of output: 399 🌐 Web query:
💡 Result: [email protected] uses the defaultSchema exported by hast-util-sanitize (it does not redefine protocol rules itself). See the rehype-sanitize README and the hast-util-sanitize repo for the canonical defaultSchema (including the protocols map). [1][2] If you want the exact allowed protocols for video/src (and other URL-bearing properties), inspect the defaultSchema.protocols field in the hast-util-sanitize source. Here are those two references to check:
References: 🌐 Web query:
💡 Result: You can extend hast-util-sanitize's defaultSchema to allow const {defaultSchema} = require('hast-util-sanitize') // or: import {defaultSchema} from 'hast-util-sanitize' const schema = deepmerge(defaultSchema, { Notes:
Sources:
🌐 Web query:
💡 Result: Short answer
Implications / practical differences
References
Sources Fix video tag handling: explicitly add to The Additionally:
🤖 Prompt for AI Agents
Comment on lines
+112
to
+138
|
||
|
|
||
| export function MarkdownRender(props) { | ||
| return ( | ||
| <div dir="auto"> | ||
|
|
@@ -178,6 +207,7 @@ export function MarkdownRender(props) { | |
| remarkPlugins={[remarkGfm, remarkBreaks]} | ||
| rehypePlugins={[ | ||
| rehypeRaw, | ||
| [rehypeSanitize, sanitizeSchema], // Add sanitization after rehypeRaw to prevent XSS | ||
| [ | ||
| rehypeHighlight, | ||
| { | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The sanitization schema allows the 'target' attribute on anchor tags without restricting its values. While not directly an XSS vulnerability, this can be exploited for tabnabbing attacks where malicious links with
target="_blank"can access thewindow.openerobject.To mitigate this, you should either:
target="_blank"automatically getrel="noopener noreferrer"addedConsider updating the anchor tag configuration to enforce safe rel values or remove the target attribute allowance.