Skip to content

FileTypeChecker is a easy to use library that allows you to identify and validate type of file.

License

Notifications You must be signed in to change notification settings

Adelzu/AyncFileTypeChecker

 
 

Repository files navigation

FileTypeCheckerFileTypeChecker - Don't let users to inject you an invalid file

Project Description

FileTypeChecker is a easy to use library that provides quality identification of a file type. This will help you to secure your applications and validate all files that are provided by external sources with few lines of code.

Project Build Status Nuget Package
FileTypeChecker Build status NuGet Badge
FileTypeChecker.Web Build status NuGet Badge

Why to use it?

Have you ever had a requirement for users to be able to upload files of a certain type? How do you validate that the file type is allowed? How do you protect your application from uploading a malicious file? It is standard practice to use the FileSystemInfo class provided by Microsoft and its Extension property for this kind of job, but is that enough? The answer is simple - No! This is why this small but effective library comes to help.

How it works?

FileTypeChecker use file's "magic numbers" to identify the type. According to Wikipedia this term ("magic numbers") was used for a specific set of 2-byte identifiers at the beginnings of files, but since any binary sequence can be regarded as a number, any feature of a file format which uniquely distinguishes it can be used for identification. This approach offers better guarantees that the format will be identified correctly, and can often determine more precise information about the file. See more about Magic Numbers

How to install?

You can install this library using NuGet into your project.

Install-Package File.TypeChecker

or by using dotnet CLI

dotnet add package File.TypeChecker

If you are working on a web project like MVC or WebApi use File.TypeChecker.Web. This web library will provide you with all validation attributes you needed for easy data validation. Again You can install it by using NuGet

You can install this library using NuGet into your project.

Install-Package File.TypeChecker.Web

or by using dotnet CLI

dotnet add package File.TypeChecker.Web

How to use?

using (var fileStream = File.OpenRead("myFileLocation"))
{
    var isRecognizableType = FileTypeValidator.IsTypeRecognizable(fileStream);

    if (!isRecognizableType)
    {
        // Do something ...
    }

    IFileType fileType = FileTypeValidator.GetFileType(fileStream);
    Console.WriteLine("Type Name: {0}", fileType.Name);
    Console.WriteLine("Type Extension: {0}", fileType.Extension);
    Console.WriteLine("Is Image?: {0}", fileStream.IsImage());
    Console.WriteLine("Is Bitmap?: {0}", fileStream.Is<Bitmap>());
}

Web Application

With web package you will recive access to our validation attributes that will gives you easy and powerfull way to allow or forbid file types. For example you can restrict your users to be able to upload only images or only archives just by setting an attribute into your method or class.

[HttpPost("filesUpload")]
public IActionResult UploadFiles([AllowImageOnly] IFormFile imageFile, [AllowArchiveOnly] IFormFile archiveFile)
{
    // Some cool stuf here ...
}
using FileTypeChecker.Web.Attributes;

public class InputModel
{
    [AllowImageOnly]
    public IFormFile FirstFile { get; set; }

    [AllowArchiveOnly]
    public IFormFile SecondFile { get; set; }

    [AllowedTypes(FileExtension.Bitmap)]
    public IFormFile ThirdFile { get; set; }

    [ForbidExecutableFile]
    public IFormFile FourthFile { get; set; }

    [ForbidTypes(FileExtension.Doc)]
    public IFormFile FifthFile { get; set; }
}

If you are interested in finding more samples please use our wiki page.

What types of file are supported?

FileTypeChecker is able to identify different types but also you are able to register your own types. For more information please visit our wiki page

Credits

Based on mjolka's answer to the Stack Overflow question Guessing a file type based on its content.

This repo is inspired from 0xbrock and the original code can be found in the "original" branch. I re-writed the all project with goal to make it Object oriented, with fluent API and easy to extend.

Support the project

  • If you like this library, ⭐️ the repository and show it to your friends!
  • If you find this library usefull and it helps you please consider to support the project, you can do by buying me a cup of coffee.

Buy Me A Coffee

About

FileTypeChecker is a easy to use library that allows you to identify and validate type of file.

Resources

License

Code of conduct

Stars

Watchers

Forks

Packages

No packages published

Languages

  • C# 100.0%