Potential fix for code scanning alert no. 9: Uncontrolled data used in path expression#1
Merged
LendaVadym merged 1 commit intomainfrom Nov 1, 2025
Merged
Conversation
…n path expression Potential fix for code scanning alert no. 9: Uncontrolled data used in path expression Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com>
|
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/LendaVadym/durga/security/code-scanning/9
To fix this path traversal vulnerability, we must ensure that
fileNameis a safe file name and cannot be used to escape the/app/filesdirectory. There are two robust approaches:/,\) or sequences like.., and reject the input if found.fileNamemight be a relative path within/app/filesbut must never escape that base directory, resolve the full path and ensure it starts with the trusted base directory.The safest option here is to use the second approach, as it handles both single file names and safe subdirectories. We'll use
Path.GetFullPathonPath.Combine(basePath, fileName)and compare against the base directory's full path (with a trailing separator for robust prefix checking). If the resolved file path starts with the base directory, we accept it; otherwise, we reject the request with a 400 Bad Request.Additionally, we should handle errors gracefully if the path is rejected.
The required changes are as follows:
ReadFile(), validatefileNamebefore using it to construct the path.ReadFile.System.IO.Suggested fixes powered by Copilot Autofix. Review carefully before merging.