fix(home): escape HTML entities in CodeLine before injecting highlight spans#466
fix(home): escape HTML entities in CodeLine before injecting highlight spans#466nyxsky404 wants to merge 1 commit into
Conversation
…t spans highlightCode() injected <span> tags via dangerouslySetInnerHTML without first escaping HTML special characters in the source text. Any code line containing '<', '>', or '&' (generics, JSX, comparison operators) would be parsed as HTML by the browser, corrupting the rendered output. Added escapeHtml() that replaces &, <, > with their entities before the regex replacements run. Single and double quotes are intentionally left unescaped because the string-literal highlight patterns depend on them. Fixes piyushdotcomm#462
Qodo reviews are paused for this user.Troubleshooting steps vary by plan Learn more → On a Teams plan? Using GitHub Enterprise Server, GitLab Self-Managed, or Bitbucket Data Center? |
|
Warning Review limit reached
More reviews will be available in 21 minutes and 15 seconds. Learn how PR review limits work. Your organization has run out of usage credits. Purchase more in the billing tab. ⌛ How to resolve this issue?After more reviews become available, a review can be triggered using the We recommend that you space out your commits to avoid hitting the rate limit. 🚦 How do rate limits work?CodeRabbit enforces hourly rate limits for each developer per organization. Our paid plans include higher PR review limits than trial, open-source, and free plans. In all cases, reviews become available again over time. During sustained high-volume PR review activity, CodeRabbit may temporarily slow when the next review becomes available. Please see our Fair Usage Limits Policy for further information. ℹ️ Review info⚙️ Run configurationConfiguration used: Path: .coderabbit.yaml Review profile: CHILL Plan: Pro Plus Run ID: 📒 Files selected for processing (1)
✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
👋 Thanks for opening a PR, @nyxsky404!Your PR has entered the 🚦 PR Review Pipeline.
What happens next
A pipeline status comment will appear below and update automatically as your PR progresses. While you wait
This comment is posted only once. |
|
@piyushdotcomm Could you please review it when you get a chance? |
Summary
escapeHtml()helper insideCodeLinethat converts&→&,<→<, and>→>before the keyword/string replacement regexes runhighlightCode()now callsescapeHtml(code)first, then performs the span-injection replacements on the safe string/'[^']*'/gand/"[^"]*"/g) depend on them being literal characters in the inputType of change
Related issue
Closes #462
Validation
npm run lintnpm testnpm run buildAdditional manual verification:
Array<string>), comparison operators (a < b), and JSX (<div>hello</div>) tocodeSnippetinhero-code.tsxlocally — all now render as readable escaped text instead of corrupted/invisible DOM nodes&in source code (e.g.&&) now correctly renders as&in the browser, not a broken entityPre-existing
useAI.test.tsfailure is unrelated to this change (reproducible on unmodifiedmain).Screenshots or recordings
Before —
Array<string>renders asArraywith<string>disappearing into the DOM:After — same line renders correctly as readable text with keyword highlighting intact.
Checklist
Root cause:
highlightCode()performed string replacements that inject<span>tags, then passed the result directly todangerouslySetInnerHTML. Because HTML special characters in the source text were never escaped, any<,>, or&in the input would be interpreted as raw HTML by the browser — silently dropping content or breaking layout. The fix escapes those three characters first, so the injected spans are still valid HTML while the code content renders as literal text. The component's existing TODO comment (// Note: This is a very simplistic implementation and should be replaced with a proper library) remains valid for a future deeper refactor; this PR addresses the correctness bug without a dependency change.