Potential fix for code scanning alert no. 26: Client-side cross-site scripting#174
Merged
Potential fix for code scanning alert no. 26: Client-side cross-site scripting#174
Conversation
…scripting Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com>
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/rajbos/github-copilot-token-usage/security/code-scanning/26
In general, to fix DOM-based XSS you must ensure that any data coming from untrusted sources is either (1) safely encoded/escaped for the exact context where it is injected (HTML body, attribute, URL, etc.), or (2) inserted using DOM APIs (
textContent,setAttribute, etc.) rather than string concatenation andinnerHTML/insertAdjacentHTML.For this specific code, the simplest fix that preserves existing functionality is:
escapeHtmlfor text/HTML contexts.sf.count) is safely rendered as plain text by avoiding direct HTML string interpolation.insertAdjacentHTML/outerHTMLwith a large HTML string that contains interpolated untrusted data. Instead, build the DOM nodes programmatically:document.createElement.textContentand attributes viasetAttribute.appendChild/insertBefore/replaceWith.Concretely, within
src/webview/diagnostics/main.tsaround lines 915–962:sessionFilesHtml.<div class="session-folders-table">container and its child<h4>and<table>elements.sortedand for eachsfcreates a<tr>with<td>cells:titleset viasetAttribute('title', escapeHtml(sf.dir))or, more robustly, set the raw string and rely onescapeHtmlonly if needed;textContentset to the display string.<span class="editor-badge">and settextContenttoeditorName.textContenttoString(sf.count).href="#",class="reveal-link", anddata-pathviasetAttribute('data-path', sf.dir)orencodeURIComponent(sf.dir)if required by the extension..session-folders-tableviaexistingTable.replaceWith(container)or insert the new container after.report-contentwithinsertBeforeonparentNode.This keeps the rendered structure identical (same classes and attributes) but removes the XSS risk by no longer passing tainted data through
insertAdjacentHTML/outerHTML.Suggested fixes powered by Copilot Autofix. Review carefully before merging.