Potential fix for code scanning alert no. 32: Client-side cross-site scripting#329
Merged
Potential fix for code scanning alert no. 32: Client-side cross-site scripting#329
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/32
In general, to fix DOM-based XSS when writing untrusted data into a webpage, either (1) avoid
innerHTMLby constructing DOM nodes withtextContent/setAttribute, or (2) ensure that every piece of untrusted data that flows into HTML or attributes is validated and contextually escaped. Here we are constrained to the existing pattern of generating an HTML string and settinginnerHTML, so the best fix is to ensure that all tainted values used inside that HTML are safely transformed first.The concrete tainted path CodeQL flags runs through
formatFileSize(sf.size), wheresf.sizeultimately comes fromevent.data. The safest minimal change is to hardenformatFileSizeso that it (a) defensively coerces its input to a finite non‑negative number and (b) falls back to a safe placeholder if coercion fails. This prevents any non-numeric or unexpected value from being reflected verbatim into the DOM, even if theSessionFileDetailsobject is malformed or attacker-controlled. No behavioral change occurs for valid numeric inputs, and the rest of the rendering logic remains unchanged.To implement this in
src/webview/diagnostics/main.ts:formatFileSize(bytes: number): stringto:byteswithNumber(bytes)intoconst numericBytes.Number.isFinite(numericBytes)andnumericBytes >= 0; if these fail, return a neutral string like"N/A".numericBytesfor all subsequent comparisons and arithmetic.formatFileSize(sf.size)) continue to work.This makes it clear to CodeQL (and to human reviewers) that tainted inputs are sanitized before being included in the HTML, eliminating the flagged vulnerability path.
Suggested fixes powered by Copilot Autofix. Review carefully before merging.