Potential fix for code scanning alert no. 20: DOM text reinterpreted as HTML#1123
Merged
Potential fix for code scanning alert no. 20: DOM text reinterpreted as HTML#1123
Conversation
…as HTML Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com>
Contributor
There was a problem hiding this comment.
Pull request overview
This pull request addresses a security vulnerability (DOM-based XSS) where user-controlled text from the DOM is injected into innerHTML without proper sanitization. The fix introduces an escapeHtml function to encode HTML special characters before inserting the matched error text into the DOM.
Changes:
- Added
escapeHtmlhelper function to sanitize user-influenced content - Applied HTML escaping to regex match results before concatenating into
innerHTML
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Potential fix for https://github.com/zhblue/hustoj/security/code-scanning/20
In general terms, the problem is that text read from the DOM (
#errtxt) flows intoinnerHTMLwithout escaping, so any HTML meta-characters in that text will be interpreted as HTML. To fix this, we must ensure that the dynamic parts derived fromerrmsgare HTML-escaped before they are concatenated into the HTML string assigned toinnerHTML.The best fix without changing the existing functionality is to keep using
innerHTML(so the<br>and<hr>formatting is preserved), but escape the tainted part (ret) before concatenation. Theexpstrings are static, developer-controlled messages that already contain some intended HTML, so they should remain unescaped. We can implement a small helper functionescapeHtmlinsidereinfo(or just above it in the same file snippet) that replaces special characters (&,<,>,",', and possibly/) with their HTML entity equivalents, then call this helper when appendingretintoexpmsg.Concretely:
trunk/web/include/reinfo.js, define anescapeHtmlfunction within thereinfofunction (or immediately before it, if you prefer a shared helper) using only standard JavaScript (no new imports needed).expmsgis built, change the lineexpmsg+=ret+":"+exp+"<br><hr />";to instead useescapeHtml(ret)so that any special characters fromerrmsgare safely encoded.Suggested fixes powered by Copilot Autofix. Review carefully before merging.