-
Notifications
You must be signed in to change notification settings - Fork 2.6k
fix: eliminate XSS vulnerability in CodeBlock component #5157
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
mrubens
merged 3 commits into
RooCodeInc:main
from
KJ7LNW:fix-codeblock-xss-vulnerability
Jun 27, 2025
Merged
fix: eliminate XSS vulnerability in CodeBlock component #5157
mrubens
merged 3 commits into
RooCodeInc:main
from
KJ7LNW:fix-codeblock-xss-vulnerability
Jun 27, 2025
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Replace dangerouslySetInnerHTML with safer codeToHast approach to render syntax-highlighted code from Shiki. This eliminates the cross-site scripting vulnerability while maintaining identical rendering output and performance. Security considerations: - Eliminates potential for HTML injection attacks - Maintains all syntax highlighting capabilities - Preserves exact visual output Performance considerations: - Direct React element creation is more efficient than HTML parsing - No browser HTML parsing overhead - Memoization pattern preserved for optimal rendering This issue was discovered as part of security review #3785. Fixes: #5156 Signed-off-by: Eric Wheeler <[email protected]>
Updated the CodeBlock test mock to support the new codeToHast method that was implemented to fix the XSS vulnerability by replacing dangerouslySetInnerHTML with direct React element rendering. Signed-off-by: Eric Wheeler <[email protected]>
- Replace HTML string fallback with React elements to prevent potential XSS - Add error handling for toJsxRuntime conversion with proper fallback - Add explanatory comment about why hast-util-to-jsx-runtime was chosen - Improve test mock to handle transformers and be more comprehensive
daniel-lxs
approved these changes
Jun 27, 2025
Member
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.
Thank you @KJ7LNW!
We also have other instances of dangerouslySetInnerHTML that we can remove using a similar approach to this PR.
LGTM
mrubens
approved these changes
Jun 27, 2025
hannesrudolph
pushed a commit
that referenced
this pull request
Jul 3, 2025
Co-authored-by: Eric Wheeler <[email protected]> Co-authored-by: Daniel Riccio <[email protected]>
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Labels
lgtm
This PR has been approved by a maintainer
PR - Needs Review
size:M
This PR changes 30-99 lines, ignoring generated files.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR addresses a cross-site scripting (XSS) vulnerability in the CodeBlock component identified by CodeQL.
Context
The CodeBlock component was using
dangerouslySetInnerHTMLto render syntax-highlighted code from Shiki, which posed a security risk.Implementation
The fix replaces
dangerouslySetInnerHTMLwith a safer approach usingcodeToHastand direct React element rendering:hast-util-to-jsx-runtimedependencycodeToHastinstead ofcodeToHtmlMemoizedCodeContentcomponent to render React elements directlystringtoReact.ReactNodeSecurity Considerations
Performance Considerations
How to Test
This issue was discovered as part of security review #3785.
Fixes #5156
Important
Fixes XSS vulnerability in
CodeBlockby replacingdangerouslySetInnerHTMLwithhast-util-to-jsx-runtimefor safer rendering.dangerouslySetInnerHTMLwithhast-util-to-jsx-runtimeinCodeBlock.tsxto prevent XSS.highlightedCodestate type fromstringtoReact.ReactNode.hast-util-to-jsx-runtimetopackage.json.codeToHastfor syntax highlighting instead ofcodeToHtml.MemoizedCodeContentnow renders React elements directly.This description was created by
for e1fc1aa. You can customize this summary. It will automatically update as commits are pushed.