Skip to content

Potential fix for code scanning alert no. 40: Improper code sanitization#49

Draft
Tanker187 wants to merge 58 commits intomainfrom
alert-autofix-40
Draft

Potential fix for code scanning alert no. 40: Improper code sanitization#49
Tanker187 wants to merge 58 commits intomainfrom
alert-autofix-40

Conversation

@Tanker187
Copy link
Owner

Potential fix for https://github.com/Tanker187/vite/security/code-scanning/40

In general, to fix this type of issue you should avoid inserting potentially untrusted strings directly into dynamically constructed JavaScript code, even if they have been run through JSON.stringify. When the generated JavaScript might be embedded in HTML, additional escaping for characters like <, >, /, and Unicode line separators is recommended so content such as </script> or \u2028 cannot break the surrounding context.

For this file, the best minimal fix is to introduce a small helper (e.g. escapeUnsafeChars) that post-processes the JSON.stringify output and replaces a shortlist of problematic characters with safe escape sequences. We then apply this helper to the uses that CodeQL flags as tainted: the JSON.stringify(importPath) used inside the dynamic import(...) call and the JSON.stringify(importKey) used inside the property access in the .then(m => m[...]). This leaves the surrounding logic and behaviour intact but ensures that even if importPath or importKey contain characters like < or </script>, the generated JavaScript is safe to embed.

Concretely:

  • Add a constant charMap and a function escapeUnsafeChars(str: string): string near the top of packages/vite/src/node/plugins/importMetaGlob.ts (after imports and before the interfaces).
  • Change line 554 to construct importStatement using `import(${escapeUnsafeChars(JSON.stringify(importPath))})`.
  • Change line 556 to use escapeUnsafeChars(JSON.stringify(importKey)) inside the computed member access: `.then(m => m[${escapeUnsafeChars(JSON.stringify(importKey))}])`.
    No new external dependencies are required; we can implement the helper using built-in JavaScript features.

Suggested fixes powered by Copilot Autofix. Review carefully before merging.

Tanker187 and others added 30 commits December 12, 2025 17:26
This workflow runs tests and publishes a Node.js package to GitHub Packages upon release creation.
This workflow installs Deno, runs linting, and executes tests on push and pull request events to the main branch.
Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com>
…n permissions

Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com>
…m environment values

Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com>
…ression

Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com>
Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com>
…in path expression

Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com>
…in path expression

Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com>
Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com>
…ression

Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com>
…ing or encoding

Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com>
…ripting

Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com>
Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com>
Tanker187 and others added 28 commits February 11, 2026 01:45
Potential fix for code scanning alert no. 6: Missing rate limiting
Potential fix for code scanning alert no. 1: Workflow does not contain permissions
Potential fix for code scanning alert no. 12: Shell command built from environment values
…ression

Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com>
Potential fix for code scanning alert no. 52: Inefficient regular expression
Create npm-publish-github-packages.yml
Create google-cloudrun-source.yml
Create google-cloudrun-docker.yml
Potential fix for code scanning alert no. 32: Bad HTML filtering regexp
Potential fix for code scanning alert no. 28: Uncontrolled data used in path expression
Potential fix for code scanning alert no. 30: Uncontrolled data used in path expression
Potential fix for code scanning alert no. 34: Incomplete string escaping or encoding
Potential fix for code scanning alert no. 4: Incorrect suffix check
Potential fix for code scanning alert no. 51: Inefficient regular expression
Potential fix for code scanning alert no. 5: Missing rate limiting
Potential fix for code scanning alert no. 16: Reflected cross-site scripting
Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com>
Repository owner locked and limited conversation to collaborators Feb 11, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant