Skip to content

Potential fix for code scanning alert no. 32: Client-side cross-site scripting#329

Merged
rajbos merged 1 commit intomainfrom
alert-autofix-32
Mar 1, 2026
Merged

Potential fix for code scanning alert no. 32: Client-side cross-site scripting#329
rajbos merged 1 commit intomainfrom
alert-autofix-32

Conversation

@rajbos
Copy link
Owner

@rajbos rajbos commented Mar 1, 2026

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 innerHTML by constructing DOM nodes with textContent/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 setting innerHTML, 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), where sf.size ultimately comes from event.data. The safest minimal change is to harden formatFileSize so 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 the SessionFileDetails object 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:

  • Modify formatFileSize(bytes: number): string to:
    • Coerce bytes with Number(bytes) into const numericBytes.
    • Check Number.isFinite(numericBytes) and numericBytes >= 0; if these fail, return a neutral string like "N/A".
    • Use numericBytes for all subsequent comparisons and arithmetic.
  • This change lives entirely within the existing function at lines 158–165; no imports or new helpers are required, and all current call sites (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.

…scripting

Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com>
@rajbos rajbos marked this pull request as ready for review March 1, 2026 18:11
@rajbos rajbos enabled auto-merge March 1, 2026 18:11
@rajbos rajbos merged commit e0c0cf9 into main Mar 1, 2026
12 checks passed
@rajbos rajbos deleted the alert-autofix-32 branch March 1, 2026 18:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant