Recognize filetypes from Streams or IFormfiles using mime types or file extensions and validate them against the magic bytes according to the filetypes.
The existing FileTypes can be expanded in various ways.
- Install nuget package into your project:
Install-Package MagicBytesValidator -Version 1.0.16dotnet add package MagicBytesValidator --version 1.0.16- Reference in your csproj:
<PackageReference Include="MagicBytesValidator" Version="1.0.16" />- Create new instances of the validators:
var validator = new MagicBytesValidator.Services.Validator();
var formFileTypeProvider = new MagicBytesValidator.Services.Http.FormFileTypeProvider();- Find a filetype by extension or mimetype:
var pngFileType = validator.Mapping.FindByExtension("png");
var pdfFileType = validator.Mapping.FindByMimeType("application/pdf");
// in case of a given IFormFile:
var fileType = formFileTypeProvider.FindFileTypeForFormFile(file);- Check a file with its stream and filetype:
var isValid = await validator.IsValidAsync(memoryStream, fileType);- Get mapping:
// use the validator:
var mapping = validator.Mapping;
// or create an instance of the mapping:
var mapping = new MagicBytesValidator.Services.Mapping(); - Register a single Filetype:
mapping.Register(
new FileType(
"traperto/trp", // mime type
new[] { "trp" }, // file extensions
new[] { // magic byte sequences
new byte[] { 0x74, 0x72, 0x61, 0x70, 0x65, 0x72, 0x74, 0x6f }
}
)
)- Register a list of filetypes:
mapping.Register(listOfFileTypes);You can also create variants of FileType and register them by passing the Assembly of the new FileTypes, e.g.
mapping.Register(typeof(CustomFileType).Assembly);. This will register all FileTypes of the given Assembly that are also
not abstract and have an empty constructor!
public class CustomFileType : FileType
{
public CustomFileType() : base(
"traperto/trp", // mime type
new[] { "trp" }, // file extensions
new[] { // magic byte sequences
new byte[] { 0x74, 0x72, 0x61, 0x70, 0x65, 0x72, 0x74, 0x6f }
}
)
{
}
}
var assembly = typeof(CustomFileType).Assembly;
_mapping.Register(assembly);| Mimetype | Extension | Magicbytes (decimal) |
|---|---|---|
| audio/x-pn-realaudio-plugin | rpm | 237 171 238 219 |
| application/octet-stream | bin file com class ini |
|
| video/3gpp | 3gp | 102 116 121 112 51 103 |
| image/x-icon | ico | 0 0 1 0 |
| image/gif | gif |
|
| image/tiff | tif tiff |
|
| image/jpeg | jpg jpeg jpe |
|
| image/png | png | 137 80 78 71 13 10 26 10 |
| video/ogg | ogg ogv |
79 103 103 83 |
| audio/basic | snd au |
|
| application/dsptype | tsp | 77 90 |
| text/plain | txt |
|
| application/zip | zip | 80 75 3 4 |
| application | docx xlsx |
80 75 7 8 |
| application/vnd.oasis.opendocument.presentation | odp | 80 75 7 8 |
| application/vnd.oasis.opendocument.spreadsheet | ods | 80 75 7 8 |
| application/vnd.oasis.opendocument.text | odt | 80 75 7 8 |
| audio/mpeg | mp3 | 73 68 51 |
| image/bmp | bmp | 66 77 |
| audio/x-midi | midi mid |
77 84 104 100 |
| application/msword | doc dot |
208 207 17 224 161 177 26 255 |
| application/msexcel | xlx xla |
208 207 17 224 161 177 26 255 |
| application/mspowerpoint | ppt ppz pps pt |
208 207 17 224 161 177 26 225 |
| application/gzip | gz | 31 139 |
| video/webm | webm | 26 69 223 163 |
| application/rtf | rtf | 123 92 114 116 102 49 |
| text/tab-separated-values | tsv | 71 |
| video/mpeg | mpg mpeg mpe |
|
| video/mp4 | mp4 |
|
| image/x-portable-bitmap | pbm | 80 49 10 |
| image/x-portable-graymap | pgm | 80 50 10 |
| image/x-portable-pixmap | ppm | 80 51 10 |
| application/pdf | 25 50 44 46 |
MIT License
▓▓ ▓▓▓▓▓▓▓▓▓▓
▓▓ ▓▓
▓▓▓▓▓▓▓▓▓ ▓▓
▓▓ ▓▓ traperto GmbH
▓▓ ▓▓▓▓▓▓▓▓▓▓
▓▓
▓▓▓▓▓▓▓▓▓ ▓▓