Potential fix for code scanning alert no. 39: Improper code sanitization#48
Merged
Potential fix for code scanning alert no. 39: Improper code sanitization#48
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/39
In general, to fix this kind of issue you should not rely on
JSON.stringifyalone when embedding potentially user-controlled strings into generated JavaScript source code. Instead, apply an additional escaping step that replaces characters problematic in HTML/script contexts (<,>,/, certain Unicode line separators, etc.) with safe escape sequences so that, even if the code is inlined into a<script>tag, no string value can prematurely terminate the script or inject new code.For this specific file, the best fix is to introduce a small helper function (local to this module) that escapes unsafe characters in a string already processed by
JSON.stringify. Then, update all uses ofJSON.stringifythat feed into constructed code strings (importPath,importKey,filePath) to pass their result through this helper. Concretely:constmapJS_UNSAFE_CHARSand a functionescapeUnsafeCharsForJs(str: string): stringnear the top ofimportMetaGlob.ts(after the imports) that replaces characters like<,>,/, backslash, control characters, and the Unicode line/paragraph separators\u2028and\u2029.JSON.stringify(importPath)withescapeUnsafeCharsForJs(JSON.stringify(importPath))in the construction ofstaticImports(line 546) andimportStatement(line 554).JSON.stringify(importKey)withescapeUnsafeCharsForJs(JSON.stringify(importKey))in the.then(m => m[...])expression (line 556).JSON.stringify(filePath)withescapeUnsafeCharsForJs(JSON.stringify(filePath))in all places wherefilePathis embedded intoobjectProps(lines 523, 551, 560).No new external dependencies are needed; the helper can be implemented with built-in JavaScript string operations. Existing functionality is preserved because valid paths/keys will produce equivalent JavaScript strings, just with additional escapes for characters that would otherwise be interpreted specially by HTML/script parsers.
Suggested fixes powered by Copilot Autofix. Review carefully before merging.