Giter VIP home page Giter VIP logo

filetypechecker's Introduction

FileTypeCheckerFileTypeChecker - Protect your application from malicious files

Build status NuGet Badge License: MIT CodeFactor Discord

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.
  • If you love this library and want to help me continue maintaining it and also have a direct contact with the developers consider becoming a member.

Buy Me A Coffee

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 by validating all files that are provided by external sources with few lines of code.

Table of Contents

1. Why to use it?

2. How it works?

3. How to install?

4. How to use?

5. Web Applications

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

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>());
}

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

What types of file are supported?

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

Web Applications

If you are developing Web application you can check this. That package is designed for ASP.NET applications and contains very useful predefined validation attributes that can be used for IFormFile.

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.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.