Advertisement
Advanced ASP.NET Core, File Uploads & Streaming Large Files

How would you implement virus/malware scanning for uploaded files before they're made available for download by other users?

Short answer

A common pattern uploads the file to a quarantine/staging location first, triggers an asynchronous scan (via an integrated antivirus engine like ClamAV, or a cloud provider's malware scanning service), and only moves the file to its final, publicly-accessible location once the…

Save ASP.NET Core mock interview
Advertisement
AModel answer
A common pattern uploads the file to a quarantine/staging location first, triggers an asynchronous scan (via an integrated antivirus engine like ClamAV, or a cloud provider's malware scanning service), and only moves the file to its final, publicly-accessible location once the scan confirms it's clean -- rejecting or quarantining files that fail the scan, ensuring no malicious content is ever served to other users even briefly.
[HttpPost("upload")]
public async Task<IActionResult> Upload(IFormFile file) {
    var quarantinePath = await _storageService.SaveToQuarantineAsync(file);
    await _backgroundTaskQueue.QueueBackgroundWorkItem(async ct => {
        var scanResult = await _virusScanner.ScanAsync(quarantinePath, ct);
        if (scanResult.IsClean) await _storageService.PromoteToPublicAsync(quarantinePath);
        else await _storageService.DeleteAsync(quarantinePath);
    });
    return Accepted(new { status = "processing" });
}
Real-world example
A file-sharing platform quarantines every upload and runs it through ClamAV asynchronously before promoting it to publicly-downloadable storage, ensuring a malicious file uploaded by one user is never served to another user even in the brief window before scanning completes.
Likely follow-ups
  • How do you communicate scan status/results back to the uploading user?
  • What's the performance and UX trade-off of synchronous versus asynchronous scanning?
Related topics