Skip to content

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

Merged
LendaVadym merged 1 commit intomainfrom
alert-autofix-9
Nov 1, 2025
Merged

Potential fix for code scanning alert no. 9: Uncontrolled data used in path expression#1
LendaVadym merged 1 commit intomainfrom
alert-autofix-9

Conversation

@LendaVadym
Copy link
Owner

Potential fix for https://github.com/LendaVadym/durga/security/code-scanning/9

To fix this path traversal vulnerability, we must ensure that fileName is a safe file name and cannot be used to escape the /app/files directory. There are two robust approaches:

  1. Single path component only: If the intent is to allow only file names (not paths), check for any path separator characters (/, \) or sequences like .., and reject the input if found.
  2. Contained within base directory: If fileName might be a relative path within /app/files but 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.GetFullPath on Path.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:

  • In ReadFile(), validate fileName before using it to construct the path.
  • Add the necessary file path validation logic in ReadFile.
  • No extra imports are required beyond the already imported System.IO.

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

…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>
@sonarqubecloud
Copy link

sonarqubecloud bot commented Nov 1, 2025

@LendaVadym LendaVadym self-assigned this Nov 1, 2025
@LendaVadym LendaVadym marked this pull request as ready for review November 1, 2025 11:53
Copy link
Owner Author

@LendaVadym LendaVadym left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

test review comment

@LendaVadym LendaVadym merged commit 7dddba7 into main Nov 1, 2025
5 checks passed
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