Skip to main content

Unverifiable files

Need

Ensuring file integrity and verifiability

Context

  • Usage of C# 7.0 for modern and efficient programming in the C# language
  • Usage of Microsoft.AspNetCore.Mvc for building web applications with ASP.NET Core
  • Usage of Microsoft.AspNetCore.Hosting for hosting ASP.NET Core applications
  • Usage of Microsoft.AspNetCore.Http for handling HTTP requests and responses in ASP.NET Core applications
  • Usage of System.IO for Input and Output operations in .NET development

Description

Non compliant code

public class FileController : Controller
{
private IHostingEnvironment _env;

public FileController(IHostingEnvironment env)
{
_env = env;
}

[HttpPost]
public IActionResult UploadFile(IFormFile file)
{
var filePath = Path.Combine(_env.WebRootPath, "uploads", file.FileName);

using (var fileStream = new FileStream(filePath, FileMode.Create))
{
file.CopyTo(fileStream);
}

return Ok();
}
}

In the provided code snippet, we have a simple ASP.NET Core controller named FileController. This controller has a single action UploadFile which accepts a file from a client and saves it to the server's file system.

When a file is uploaded, it is saved to the server without any checks on its content or file extension. This is a security vulnerability because a malicious user could upload a file with a misleading extension. For example, a user could upload a file with a .txt extension that actually contains executable code. This could potentially lead to a variety of security issues, such as code execution attacks, if the file is later accessed and interpreted based on its extension.

There is no mechanism in place to verify that the content of the file matches its extension. This could lead to unexpected behavior if the server or another user tries to interact with the file based on its extension.

This is a clear example of the "Unverifiable files" vulnerability, as the system is currently accepting and storing files without verifying their integrity or ensuring their content matches their declared file type.

Steps

  • Validate the file extension before saving it to the repository.
  • Use a whitelist of allowed file extensions to ensure that only verifiable files are accepted.
  • Consider using a file signature verification to ensure the integrity of the file content.
  • Implement server-side validation to prevent any potential file upload vulnerabilities.

Compliant code

public class FileController : Controller
{
private IHostingEnvironment _env;
private readonly List<string> _allowedExtensions = new List<string> { ".jpg", ".png", ".txt", ".pdf" }; // Add more extensions as per your requirements

public FileController(IHostingEnvironment env)
{
_env = env;
}

[HttpPost]
public IActionResult UploadFile(IFormFile file)
{
var extension = Path.GetExtension(file.FileName).ToLowerInvariant();

if (string.IsNullOrEmpty(extension) || !_allowedExtensions.Contains(extension))
{
return BadRequest("Invalid file extension");
}

var filePath = Path.Combine(_env.WebRootPath, "uploads", file.FileName);

using (var fileStream = new FileStream(filePath, FileMode.Create))
{
file.CopyTo(fileStream);
}

return Ok();
}
}

The updated code now includes a whitelist of allowed file extensions. This is done by creating a list of allowed extensions _allowedExtensions and checking if the uploaded file's extension is in this list before saving it to the repository.

In the UploadFile method, we first get the extension of the uploaded file and convert it to lower case to ensure case-insensitive comparison. If the extension is either null or not in the allowed extensions list, we return a bad request response indicating an invalid file extension.

This way, we ensure that only files with verifiable extensions are accepted and saved to the repository, thus mitigating the risk of unverifiable files being stored.

Please note that this is a basic level of validation and does not guarantee the file's content is safe or matches the file extension. For more robust validation, consider implementing server-side validation and file signature verification to ensure the integrity of the file content.

References