Conversation
rajat1saxena
commented
Apr 26, 2025
- As seen in
|
The latest updates on your projects. Learn more about Vercel for Git ↗︎
|
| processedSvg(svgCode, svgStyle) || | ||
| '<div class="text-red-500">Invalid SVG</div>', |
Check warning
Code scanning / CodeQL
DOM text reinterpreted as HTML Medium
Show autofix suggestion
Hide autofix suggestion
Copilot Autofix
AI 12 months ago
To fix the issue, we need to ensure that the svgCode passed to dangerouslySetInnerHTML is sanitized to prevent XSS. This can be achieved by using a library like DOMPurify to sanitize the SVG content before rendering it. DOMPurify is a well-known library for sanitizing HTML and SVG content, ensuring that only safe elements and attributes are allowed.
Steps to fix:
- Install the
dompurifylibrary if it is not already installed. - Import
DOMPurifyinto the file. - Use
DOMPurify.sanitizeto sanitize the output ofprocessedSvgbefore passing it todangerouslySetInnerHTML.
| @@ -1,2 +1,3 @@ | ||
| import React, { useState, useRef, useEffect } from "react"; | ||
| import DOMPurify from "dompurify"; | ||
| import { SvgStyle } from "../settings"; | ||
| @@ -104,5 +105,6 @@ | ||
| dangerouslySetInnerHTML={{ | ||
| __html: | ||
| __html: DOMPurify.sanitize( | ||
| processedSvg(svgCode, svgStyle) || | ||
| '<div class="text-red-500">Invalid SVG</div>', | ||
| '<div class="text-red-500">Invalid SVG</div>' | ||
| ), | ||
| }} |
| @@ -63,3 +63,4 @@ | ||
| "clsx": "^2.1.1", | ||
| "react-redux": "^8.1.2" | ||
| "react-redux": "^8.1.2", | ||
| "dompurify": "^3.2.5" | ||
| }, |
| Package | Version | Security advisories |
| dompurify (npm) | 3.2.5 | None |
| __html: processedSvg( | ||
| '<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-star-icon lucide-star"><path d="M11.525 2.295a.53.53 0 0 1 .95 0l2.31 4.679a2.123 2.123 0 0 0 1.595 1.16l5.166.756a.53.53 0 0 1 .294.904l-3.736 3.638a2.123 2.123 0 0 0-.611 1.878l.882 5.14a.53.53 0 0 1-.771.56l-4.618-2.428a2.122 2.122 0 0 0-1.973 0L6.396 21.01a.53.53 0 0 1-.77-.56l.881-5.139a2.122 2.122 0 0 0-.611-1.879L2.16 9.795a.53.53 0 0 1 .294-.906l5.165-.755a2.122 2.122 0 0 0 1.597-1.16z"/></svg>', | ||
| internalSvgStyle, | ||
| ), |
Check warning
Code scanning / CodeQL
DOM text reinterpreted as HTML Medium
Show autofix suggestion
Hide autofix suggestion
Copilot Autofix
AI 12 months ago
To fix the issue, we need to ensure that any data passed to dangerouslySetInnerHTML is sanitized to prevent XSS attacks. This can be achieved by using a library like DOMPurify to sanitize the SVG content before injecting it into the DOM. DOMPurify is a well-known library for sanitizing HTML and SVG content, and it is widely used to mitigate XSS vulnerabilities.
Steps to fix:
- Install the
dompurifylibrary as a dependency. - Import
DOMPurifyin thehelpers.tsfile where theprocessedSvgfunction is defined. - Use
DOMPurify.sanitizeto sanitize the SVG content before returning it from theprocessedSvgfunction. - Ensure that the sanitized SVG content is used in the
dangerouslySetInnerHTMLproperty.
| @@ -18,2 +18,4 @@ | ||
|
|
||
| import DOMPurify from "dompurify"; | ||
|
|
||
| export const processedSvg = ( | ||
| @@ -27,3 +29,3 @@ | ||
| // For other SVGs, replace the fill attribute if it exists | ||
| return svgCode | ||
| const updatedSvgCode = svgCode | ||
| .replace(/currentColor/g, svgStyle.svgColor) | ||
| @@ -34,2 +36,5 @@ | ||
| .replace(/<svg/, '<svg width="100%" height="100%"'); | ||
|
|
||
| // Sanitize the SVG content to prevent XSS | ||
| return DOMPurify.sanitize(updatedSvgCode, { USE_PROFILES: { svg: true } }); | ||
| }; |
| @@ -63,3 +63,4 @@ | ||
| "clsx": "^2.1.1", | ||
| "react-redux": "^8.1.2" | ||
| "react-redux": "^8.1.2", | ||
| "dompurify": "^3.2.5" | ||
| }, |
| Package | Version | Security advisories |
| dompurify (npm) | 3.2.5 | None |
| </p> | ||
| <div | ||
| className="w-[100px] h-[60px] flex items-center justify-center" | ||
| dangerouslySetInnerHTML={{ __html: svgText }} |
Check warning
Code scanning / CodeQL
DOM text reinterpreted as HTML Medium
Show autofix suggestion
Hide autofix suggestion
Copilot Autofix
AI 12 months ago
To fix the issue, we need to sanitize the svgText input before using it in dangerouslySetInnerHTML. A well-known library like dompurify can be used to sanitize the input, ensuring that only safe HTML is allowed. This approach preserves the functionality of rendering SVG content while mitigating the XSS risk.
Steps to implement the fix:
- Install the
dompurifylibrary if it is not already installed. - Import
dompurifyinto the file. - Use
DOMPurify.sanitizeto sanitize thesvgTextvalue before passing it todangerouslySetInnerHTML.
| @@ -1,2 +1,3 @@ | ||
| import React from "react"; | ||
| import DOMPurify from "dompurify"; | ||
| import { | ||
| @@ -79,3 +80,3 @@ | ||
| className="w-[100px] h-[60px] flex items-center justify-center" | ||
| dangerouslySetInnerHTML={{ __html: svgText }} | ||
| dangerouslySetInnerHTML={{ __html: DOMPurify.sanitize(svgText) }} | ||
| /> |
| @@ -63,3 +63,4 @@ | ||
| "clsx": "^2.1.1", | ||
| "react-redux": "^8.1.2" | ||
| "react-redux": "^8.1.2", | ||
| "dompurify": "^3.2.5" | ||
| }, |
| Package | Version | Security advisories |
| dompurify (npm) | 3.2.5 | None |
| const contentRef = useRef<HTMLDivElement>(null); | ||
|
|
||
| // Convert children to array | ||
| const childrenArray = React.Children.toArray(children); |
Check notice
Code scanning / CodeQL
Unused variable, import, function or class Note
Show autofix suggestion
Hide autofix suggestion
Copilot Autofix
AI 12 months ago
To fix the issue, the unused variable childrenArray should be removed from the code. This involves deleting its declaration on line 29. Since the variable is not used anywhere else in the code, no additional changes are required. This will improve code clarity and eliminate the unnecessary computation of React.Children.toArray(children).
| @@ -27,4 +27,2 @@ | ||
|
|
||
| // Convert children to array | ||
| const childrenArray = React.Children.toArray(children); | ||
|
|