Potential fix for code scanning alert no. 40: Improper code sanitization#49
Draft
Potential fix for code scanning alert no. 40: Improper code sanitization#49
Conversation
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
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 subscribe to this conversation on GitHub.
Already have an account?
Sign in.
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/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\u2028cannot break the surrounding context.For this file, the best minimal fix is to introduce a small helper (e.g.
escapeUnsafeChars) that post-processes theJSON.stringifyoutput and replaces a shortlist of problematic characters with safe escape sequences. We then apply this helper to the uses that CodeQL flags as tainted: theJSON.stringify(importPath)used inside the dynamicimport(...)call and theJSON.stringify(importKey)used inside the property access in the.then(m => m[...]). This leaves the surrounding logic and behaviour intact but ensures that even ifimportPathorimportKeycontain characters like<or</script>, the generated JavaScript is safe to embed.Concretely:
charMapand a functionescapeUnsafeChars(str: string): stringnear the top ofpackages/vite/src/node/plugins/importMetaGlob.ts(after imports and before the interfaces).importStatementusing`import(${escapeUnsafeChars(JSON.stringify(importPath))})`.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.