ByteGuard.FileValidator
1.1.1
Prefix Reserved
See the version list below for details.
dotnet add package ByteGuard.FileValidator --version 1.1.1
NuGet\Install-Package ByteGuard.FileValidator -Version 1.1.1
<PackageReference Include="ByteGuard.FileValidator" Version="1.1.1" />
<PackageVersion Include="ByteGuard.FileValidator" Version="1.1.1" />
<PackageReference Include="ByteGuard.FileValidator" />
paket add ByteGuard.FileValidator --version 1.1.1
#r "nuget: ByteGuard.FileValidator, 1.1.1"
#:package ByteGuard.FileValidator@1.1.1
#addin nuget:?package=ByteGuard.FileValidator&version=1.1.1
#tool nuget:?package=ByteGuard.FileValidator&version=1.1.1
ByteGuard File Validator 
ByteGuard.FileValidator is a lightweight security-focused library for validating user-supplied files in .NET applications.
It helps you enforce consistent file upload rules by checking:
- Allowed file extensions
- File size limits
- File signatures (magic numbers) to detect spoofed types
- Specification conformance for Office Open XML / Open Document Formats (
.docx,.xlsx,.pptx,.odt) - Malware scan result using a varity of scanners (requires the addition of a specific ByteGuard.FileValidator scanner package)
⚠️ Important: This package is one layer in a defense-in-depth strategy.
It does not replace endpoint protection, sandboxing, input validation, or other security controls.
Features
- ✅ Validate files by extension
- ✅ Validate files by size
- ✅ Validate files by signature (magic-numbers)
- ✅ Validate files by specification conformance for archive-based formats (Open XML and Open Document Formats)
- ✅ Ensure no malware through a variety of antimalware scanners
- ✅ Validate using file path,
Stream, orbyte[] - ✅ Configure which file types to support
- ✅ Configure whether to throw exceptions or simply return a boolean
- ✅ Fluent configuration API for easy setup
Getting Started
Installation
This package is published and installed via NuGet.
Reference the package in your project:
dotnet add package ByteGuard.FileValidator
Antimalware scanners
In order to use the antimalware scanning capabilities, ensure you have a ByteGuard.FileValidator antimalware package referenced as well. Youo can find the relevant scanner package on NuGet under the namespace ByteGuard.FileValidator.Scanners.
Usage
Basic validation
// Without antimalware scanner
var configuration = new FileValidatorConfiguration
{
SupportedFileTypes = [FileExtensions.Pdf, FileExtensions.Jpg, FileExtensions.Png],
FileSizeLimit = ByteSize.MegaBytes(25),
ThrowExceptionOnInvalidFile = false
};
var fileValidator = new FileValidator(configuration);
var isValid = fileValidator.IsValidFile("example.pdf", fileStream);
// With antimalware
var antimalwareScanner = AntimalwareScannerImplementation();
var fileValidator = new FileValidator(configuration, antimalwareScanner);
var isValid = fileValidator.IsValidFile("example.pdf", fileStream);
Using the fluent builder
var configuration = new FileValidatorConfigurationBuilder()
.AllowFileTypes(FileExtensions.Pdf, FileExtensions.Jpg, FileExtensions.Png)
.SetFileSizeLimit(ByteSize.MegaBytes(25))
.SetThrowExceptionOnInvalidFile(false)
.Build();
var fileValidator = new FileValidator(configuration);
var isValid = fileValidator.IsValidFile("example.pdf", fileStream);
Validating specific aspects
The FileValidator class provides methods to validate specific aspects of a file.
⚠️ It’s recommended to use
IsValidFilefor comprehensive validation.
IsValidFileperforms, in order:
- Extension validation
- File size validation
- Signature (magic-number) validation
- Optional Open XML / Open Document Format specification conformance validation (for supported types)
- Optional antimalware scanning with a compatible scanning package
bool isExtensionValid = fileValidator.IsValidFileType(fileName);
bool isFileSizeValid = fileValidator.HasValidSize(fileStream);
bool isSignatureValid = fileValidator.HasValidSignature(fileName, fileStream);
bool isOpenXmlValid = fileValidator.IsValidOpenXmlDocument(fileName, fileStream);
bool isOpenDocumentFormatValid = fileValidator.IsValidOpenDocumentFormat(fileName, fileStream);
bool isMalwareClean = fileValidator.IsMalwareClean(fileName, fileStream);
Example
[HttpPost("upload")]
public async Task<IActionResult> Upload(IFormFile file)
{
using var stream = file.OpenReadStream();
var antimalwareScanner = AntimalwareScannerImplementation();
var configuration = new FileValidatorConfiguration
{
SupportedFileTypes = [FileExtensions.Pdf, FileExtensions.Docx],
FileSizeLimit = ByteSize.MegaBytes(10),
ThrowExceptionOnInvalidFile = false
};
var validator = new FileValidator(configuration, antimalwareScanner);
if (!validator.IsValidFile(file.FileName, stream))
{
return BadRequest("Invalid or unsupported file.");
}
// Proceed with processing/saving...
return Ok();
}
Supported File Extensions
The following file extensions are supported by the FileValidator:
.jpeg,.jpg.pdf.png.bmp.doc.docx.odt.rtf.xls.xlsx.pptx.m4a.mov.avi.mp3.mp4.wav
Validation coverage per type
IsValidFile always validates:
- File extension (against
SupportedFileTypes) - File size (against
FileSizeLimit) - File signature (magic number)
- Malware scan result (if an antimalware scanner has been configured)
For some formats, additional checks are performed:
Office Open XML / Open Document Format (
.docx,.xlsx,.pptx,.odt):- Extension
- File size
- Signature
- Specification conformance
- Malware scan result
Other binary formats (e.g. images, audio, video such as
.jpg,.png,.mp3,.mp4):- Extension
- File size
- Signature
- Malware scan result
Configuration Options
The FileValidatorConfiguration supports:
| Setting | Required | Default | Description |
|---|---|---|---|
SupportedFileTypes |
Yes | N/A | A list of allowed file extensions (e.g., .pdf, .jpg).<br>Use the predefined constants in FileExtensions for supported types. |
FileSizeLimit |
Yes | N/A | Maximum permitted size of files.<br>Use the static ByteSize class provided with this package, to simplify your limit. |
ThrowExceptionOnInvalidFile |
No | true |
Whether to throw an exception on invalid files or return false. |
Exceptions
When ThrowExceptionOnInvalidFile is set to true, validation functions will throw one of the appropriate exceptions defined below. However, when ThrowExceptionOnInvalidFile is set to false, all validation functions will either return true or false.
| Exception type | Scenario |
|---|---|
EmptyFileException |
Thrown when the file content is null or empty, indicating a file without any content. |
UnsupportedFileException |
Thrown when the file extension is not in the list of supported types. |
InvalidFileSizeException |
Thrown when the file size exceeds the configured file size limit. |
InvalidSignatureException |
Thrown when the file's signature does not match the expected signature for its type. |
InvalidOpenXmlFormatException |
Thrown when the internal structure of an Open XML file is invalid (.docx, .xlsx, .pptx, etc.). |
InvalidOpenDocumentFormatException |
Thrown when the specification conformance of an Open Document Format file is invalid (.odt, etc.). |
MalwareDetectedException |
Thrown when the configured antimalware scanner detected malware in the file from a scan result. |
When to use this package
- ✅ Whenever you need consistent file validation rules across projects
- ✅ When handling user uploads in APIs or web applications
- ✅ When you want defense-in-depth against spoofed or malicious files
License
ByteGuard FileValidator is Copyright © ByteGuard Contributors - Provided under the MIT license.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 is compatible. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 is compatible. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
| .NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
| .NET Standard | netstandard2.0 is compatible. netstandard2.1 was computed. |
| .NET Framework | net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
| MonoAndroid | monoandroid was computed. |
| MonoMac | monomac was computed. |
| MonoTouch | monotouch was computed. |
| Tizen | tizen40 was computed. tizen60 was computed. |
| Xamarin.iOS | xamarinios was computed. |
| Xamarin.Mac | xamarinmac was computed. |
| Xamarin.TVOS | xamarintvos was computed. |
| Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETStandard 2.0
- DocumentFormat.OpenXml (>= 3.3.0)
-
net10.0
- DocumentFormat.OpenXml (>= 3.3.0)
-
net8.0
- DocumentFormat.OpenXml (>= 3.3.0)
-
net9.0
- DocumentFormat.OpenXml (>= 3.3.0)
NuGet packages (3)
Showing the top 3 NuGet packages that depend on ByteGuard.FileValidator:
| Package | Downloads |
|---|---|
|
ByteGuard.FileValidator.Scanner.Amsi
ByteGuard File Validator Microsoft Antimalware Scan Interface (AMSI) antimalware scanner. |
|
|
ByteGuard.FileValidator.Extensions.DependencyInjection
ByteGuard File Validator support for Microsoft.Extensions.DependencyInjection. |
|
|
ByteGuard.FileValidator.AspNetCore
ByteGuard File Validator support for ASP.NET Core. |
GitHub repositories
This package is not used by any popular GitHub repositories.
| Version | Downloads | Last Updated |
|---|---|---|
| 1.2.0-dev-01044 | 146 | 12/21/2025 |
| 1.2.0-dev-01041 | 144 | 12/21/2025 |
| 1.1.1 | 239 | 12/19/2025 |
| 1.1.1-dev-01033 | 235 | 12/19/2025 |
| 1.1.0 | 640 | 12/1/2025 |
| 1.0.1-dev-01029 | 519 | 12/1/2025 |
| 1.0.1-dev-01027 | 108 | 11/29/2025 |
| 1.0.1-dev-01025 | 175 | 11/27/2025 |
| 1.0.0 | 400 | 11/21/2025 |