This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var orders = await context.Orders | |
| .AsNoTracking() | |
| .Include(o => o.Customer) | |
| .Where(o => o.OrderDate > DateTime.Now.AddDays(-30)) | |
| .ToListAsync(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Without tracking - better performance for read-only scenarios | |
| var customers = await context.Customers | |
| .AsNoTracking() | |
| .ToListAsync(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| [Test] | |
| public async Task ValidateAsync_WithEICARTestFile_ReturnsFail() | |
| { | |
| // EICAR test string - universally detected as malware | |
| const string eicarString = @"X5O!P%@AP[4\PZX54(P^)7CC)7}$EICAR-STANDARD-ANTIVIRUS-TEST-FILE!$H+H*"; | |
| var eicarBytes = Encoding.UTF8.GetBytes(eicarString); | |
| var mockFile = new Mock<IFormFile>(); | |
| mockFile.Setup(f => f.FileName).Returns("eicar.txt"); | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System.Runtime.InteropServices; | |
| public sealed class AmsiContext : IDisposable | |
| { | |
| private IntPtr _handle; | |
| private AmsiContext(IntPtr handle) | |
| { | |
| _handle = handle; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public sealed class MalwareScanValidationStep : IValidationStep | |
| { | |
| private readonly ILogger _logger; | |
| public MalwareScanValidationStep(ILogger logger) | |
| { | |
| _logger = logger; | |
| } | |
| public async Task<ValidationResult> ValidateAsync(IFormFile file, CancellationToken token) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public static ValidationPipeline CreateDefaultPipeline(ILogger logger) | |
| { | |
| const long maxFileSize = 128L * 1024 * 1024; // 128 MB | |
| var pipeline = new ValidationPipeline( | |
| [ | |
| new FileSizeValidationStep(maxFileSize, logger), // 1. Check size | |
| new ContentTypeValidationStep(SupportedTypes, logger), // 2. Check claimed type | |
| new FileSignatureValidationStep(SupportedTypes, logger), // 3. Verify actual type | |
| new MalwareScanValidationStep(logger) // 4. Scan for malware | |
| ]); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| fileStream.Position = 0; // Reset for subsequent operations |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public static class FileSignatureValidator | |
| { | |
| public static bool IsValid( | |
| IFileFormatInspector inspector, | |
| Stream fileStream, | |
| string fileName, | |
| SupportedContentTypes supportedContentTypes, | |
| ILogger logger) | |
| { | |
| try |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var allFormats = FileFormatLocator.GetFormats(this.GetType().Assembly, true); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public sealed class FileSignatureValidationStep : IValidationStep | |
| { | |
| private readonly SupportedContentTypes _supportedContentTypes; | |
| private readonly IFileFormatInspector _fileFormatInspector; | |
| private readonly ILogger _logger; | |
| public FileSignatureValidationStep(SupportedContentTypes supportedContentTypes, ILogger logger) | |
| { | |
| _supportedContentTypes = supportedContentTypes; |