Skip to content

Potential fix for code scanning alert no. 26: Uncontrolled data used in path expression#57

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

Potential fix for code scanning alert no. 26: Uncontrolled data used in path expression#57
Tanker187 wants to merge 58 commits intomainfrom
alert-autofix-26

Conversation

@Tanker187
Copy link
Owner

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

In general, untrusted URL data must not be used directly in filesystem paths. For paths that may contain directory components, the usual fix is:

  1. Resolve the user-supplied part relative to a trusted root using path.resolve.
  2. Optionally pass this through fs.realpathSync to resolve symlinks.
  3. Verify the final normalized path is still within the root, using a robust check like startsWith on normalized absolute paths.
  4. Reject or ignore the request if the check fails.

In this specific code, most of this is already implemented, but the intent can be made clearer and slightly safer. The main risk is in the else branch for non-FS_PREFIX URLs, where filePath is constructed from decodeURIComponent(url) and then normalized. We should:

  • Explicitly treat root as an absolute base, and resolve the URL path against it using path.resolve(root, decodedUrlPathWithoutQueryAndHash). The existing path.join(root, decodeURIComponent(url)) followed by path.resolve works, but using root directly as the first argument to resolve is clearer and avoids relying on any implicit path state.
  • Ensure we always normalize and check the path after realpathSync.native resolution, which is already done.
  • Keep the invariant that filePath is a normalized absolute path before calling isParentDirectory, matching that helper’s contract.
  • Slightly tighten the root check by ensuring root is normalized once and reused, preventing discrepancies between how root and filePath are normalized.

Given the constraints (only touching shown snippets, no new dependencies), the minimally invasive but clear fix is to adjust the filePath computation block in packages/vite/src/node/server/middlewares/indexHtml.ts to:

  • Build filePath via path.resolve(root, decodeURIComponent(url)) instead of path.resolve(path.join(root, ...)).
  • Keep the existing realpathSync.native call and isParentDirectory check, but ensure the comment matches the behavior.

These changes keep existing functionality (mapping /something.html under the dev/preview root) while reinforcing that paths cannot escape the root.


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
…in path expression

Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com>
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