Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions Controllers/VulnerableController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,18 @@ public IActionResult PingServer(string hostname)
[HttpGet("download")]
public IActionResult DownloadFile(string filename)
{
// Vulnerable: No path validation
var filePath = $"/var/www/files/{filename}";
// Validate and normalize the requested file path to prevent path traversal
var baseDirectory = "/var/www/files";
var baseDirectoryFullPath = System.IO.Path.GetFullPath(baseDirectory);
var combinedPath = System.IO.Path.Combine(baseDirectoryFullPath, filename ?? string.Empty);
var filePath = System.IO.Path.GetFullPath(combinedPath);

// Ensure the resolved path is still within the intended base directory
if (!filePath.StartsWith(baseDirectoryFullPath + System.IO.Path.DirectorySeparatorChar))
{
return BadRequest("Invalid file path.");
}

if (System.IO.File.Exists(filePath))
{
var fileBytes = System.IO.File.ReadAllBytes(filePath);
Expand Down
Loading