Potential fix for code scanning alert no. 25: Client-side cross-site scripting#173
Merged
Potential fix for code scanning alert no. 25: Client-side cross-site scripting#173
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/25
In general, to fix DOM XSS issues like this, avoid building large chunks of HTML with string concatenation that mix untrusted data and then injecting them via
innerHTML,outerHTML, orinsertAdjacentHTML. Instead, either (a) use a robust templating or sanitization library to sanitize full HTML strings before injection, or (b) build the DOM structure programmatically withdocument.createElement, setting text content and attributes via the DOM API so that data is not interpreted as markup.For this specific code, the best fix without changing the visible behavior is to stop constructing
sessionFilesHtmlas an HTML string and stop usingouterHTML/insertAdjacentHTML. Instead, we will:.session-folders-tablecontainer element using DOM methods.<h4>,<table>,<thead>,<tbody>,<tr>,<td>,<span>, and<a>elements usingdocument.createElement.sf.dir,display,editorName,sf.count), assign them by:textContentfor textual nodes (folder display, editor badge, and count), so HTML is not parsed.titleviasetAttribute('title', ...)usingescapeHtmlonly if we want to preserve that helper; alternatively, since attributes are not HTML-parsed, we can assign them directly and rely on the browser to treat them as plain text (but we will preserveescapeHtmlfor minimal behavior change where already used).encodeURIComponent(sf.dir)as the value of thedata-pathattribute viaelement.setAttribute('data-path', ...)instead of interpolating into an HTML string.existingTable.outerHTML = sessionFilesHtml) with clearing and repopulating the existing element, or by removing and re-inserting a newly constructed element.reportContent.insertAdjacentHTML('afterend', sessionFilesHtml)) withinsertAdjacentElement('afterend', tableContainerElement).These changes all occur in
src/webview/diagnostics/main.tsaround lines 915–962. No extra methods beyond DOM operations are strictly required; we will reuse the existingescapeHtmlfunction that the snippet references (since it’s already used for the titles and cell text). No new imports are needed.Suggested fixes powered by Copilot Autofix. Review carefully before merging.