Skip to content
Open
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
15 changes: 12 additions & 3 deletions FileShares/src/FileSharesWeb/Controllers/FilesController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,17 @@ public ActionResult List()
[HttpDelete]
public JsonResult Delete(string fileToDelete)
{
string actualFileName = HttpUtility.UrlDecode(fileToDelete);
SystemFile.Delete(actualFileName);
return Json($"Successfully deleted {actualFileName}");
string fileName = HttpUtility.UrlDecode(fileToDelete);
string shareRoot = Path.GetFullPath(fileShareConfiguration.Location);
string filePath = Path.GetFullPath(Path.Combine(shareRoot, fileName));

if (!filePath.StartsWith(shareRoot + '\\', StringComparison.OrdinalIgnoreCase) &&
!filePath.StartsWith(shareRoot + '/', StringComparison.OrdinalIgnoreCase))
{
throw new UnauthorizedAccessException("Deleting files outside the share root is not permitted.");
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This currently returns a 500 and dumps the stack trace.

How about this?

    return StatusCode(403, "Deleting files outside the share root is not permitted.");

It does require changing the method to return IActionResult, but I don't think that causes any issues

}

SystemFile.Delete(filePath);
return Json($"Successfully deleted {fileName}");
}
}