Potential fix for code scanning alert no. 45: Client-side cross-site scripting#330
Merged
Potential fix for code scanning alert no. 45: Client-side cross-site scripting#330
Conversation
…scripting Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com>
Owner
Author
|
@copilot , build fails with this error: |
Contributor
…issing properties Co-authored-by: rajbos <6085745+rajbos@users.noreply.github.com>
Fix TypeScript errors in sanitizeStats XSS security fix
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/45
In general, to fix this kind of problem you must ensure that any data coming from
postMessage(or other untrusted sources) is either (a) validated and normalized into safe types (numbers, booleans, enums) before being used, or (b) sanitized/encoded before being written to the DOM, particularly when usinginnerHTML. Where feasible, replacinginnerHTMLconstruction withtextContentand DOM APIs is even safer.For this specific code, the minimal, non‑functional‑changing fix is to:
message.datainto a trustedUsageAnalysisStatsobject before passing it torenderLayout, andsanitizeStatsthat ensures all interpolated fields in the template are safe primitive types (numbers/booleans) and cannot contain attacker-controlled HTML.Because we must not assume anything outside the shown snippets, the fix will be self-contained in
src/webview/usage/main.ts:sanitizeStats(raw: any): UsageAnalysisStats | nulljust aboverenderLayout(line ~282). This function will:rawis an object and that all required properties (today,last30Days,backendConfigured, etc.) exist with the expected shape.Number(...)and default to0if the result is not a finite number.!!raw.prop.nullif the object is grossly malformed, so we can refuse to render.'updateStats'case in the message handler to callsanitizeStats(message.data)and only pass the sanitized result intorenderLayout. If sanitization fails, we simply do not re-render (or could add a minimal fallback).This approach keeps the existing
innerHTMLtemplate intact (so layout and behavior remain unchanged) but removes the ability for arbitrary strings to flow into the template: everything is normalized to non-executable primitives before use. That directly breaks the taint flow path identified by CodeQL.Suggested fixes powered by Copilot Autofix. Review carefully before merging.