Comments (12)
Just for a bit more context, I end up writing something like this as a work-around:
/// <summary>
/// Just for exposing FileTypeValidator.FileTypes
/// </summary>
private abstract class TypeValidator : FileTypeValidator
{
public static new IReadOnlyCollection<IFileType> FileTypes
=> FileTypeValidator.FileTypes;
}
private bool IsSignatureBitsValid(IFormFile file, string extension)
{
if (!TypeValidator.FileTypes.Any(t => t.Extension == extension))
{
return true; //or false
}
using var stream = file.OpenReadStream();
if (!FileTypeValidator.IsTypeRecognizable(stream))
{
return false;
}
var fileType = FileTypeValidator.GetFileType(stream);
return fileType.Extension == extension;
}
from filetypechecker.
@RetainPhil You are doing basically the same thing that the IsTypeRecognizable method does with the only difference that the IsTypeRecognizable method is comparing the bytes while your code compares two strings. This library is supporting a large variety of types. If the type you are looking for is not supported you can implement it easily.
The expression TypeValidator.FileTypes.Any(t => t.Extension == extension)
will return the same result as FileTypeValidator.IsTypeRecognizable(stream)
every time.
from filetypechecker.
EDIT: Yeh I see what you're saying. Well I guess I was thinking that maybe someone might want to handle the cases where a file claims to be of a type which has a validator, but the validation fails, and a file which doesn't even have a validator to begin with. (and might or might not be valid).
In the end I suppose you'd have to verify that the extension is something that the system supports (and has a validator) before checking the signature bits, either way there's a hard-coded list of 'supported types' you'd have to manage, whether that's the types supported out of the box by the library already or not.
from filetypechecker.
quick question guys , does filesteam.IsImage , incluse Webp images ?
from filetypechecker.
At the moment no, but I am planning to include it in the next release. If you want your code to recognize it you can implement a custom type
from filetypechecker.
@AJMitev thanks, and great work btw 😉
from filetypechecker.
@AJMitev sorry to bother you again , can you say if this code is safe enought to test for webp ?
public class WebPFileType : FileType
{
private static readonly string name = "WebPacl Custom Type 1.0";
private static readonly string extension = "webp";
private static readonly byte[] magicBytes = new byte[] { 0x52, 0x49 };
public WebPFileType() : base(name, extension, magicBytes) { }
}
from filetypechecker.
It's looking good :)
from filetypechecker.
@AJMitev sorry to bother you again , can you say if this code is safe enought to test for webp ?
public class WebPFileType : FileType { private static readonly string name = "WebPacl Custom Type 1.0"; private static readonly string extension = "webp"; private static readonly byte[] magicBytes = new byte[] { 0x52, 0x49 }; public WebPFileType() : base(name, extension, magicBytes) { } }
@AJMitev can you help me out please ? i created my custom type , and when a test it with a wepb img ( i double checked the header for magic bytes ) but the code 'fileStream.Is<WebPFileType>()'
always return false , any ideas why?
from filetypechecker.
Try this:
using (var fileStream = File.OpenRead("myFileLocation"))
{
var isRecognizableType = FileTypeValidator.IsTypeRecognizable(fileStream);
}
If isRecognizableType is false then you are not registered your type correctly. And you should read this.
from filetypechecker.
@AJMitev i did registered in the startup class
and isRecognizableType return true
from filetypechecker.
Well, this is easy to debug mate, just retrieve the type and see what you will receive.
var fileType = FileTypeValidator.GetFileType(fileStream);
Now when I saw the bytes you are defined in your type, I bet it will return AVI instead of WebP, this is because the beginning of both types are the same and for now FindMaxScore returns the first type that scores the highest. In that case AVI.
For now I can recommend you to set more specific byte definition. In near future I will improve the behavior.
from filetypechecker.
Related Issues (20)
- Renamed executable file does not detect as executable HOT 1
- Word Documents recognized as Zip files HOT 2
- System.InvalidOperationException: Can't search in collection with no items! HOT 2
- Async usage error on .NET 6 HOT 1
- Cannot recognize excel file HOT 2
- .xlsx is recognized as .docx HOT 2
- Support for HEIC file type HOT 2
- Nuget package built with Debug mode HOT 1
- Zip file recognized as docx HOT 3
- OWASP dependency check fail for pkg:nuget/[email protected] HOT 1
- File Version of latest repository is version 1.0.0.0. HOT 1
- If you need more magic bytes HOT 1
- stream of dll reported as exe HOT 2
- Can you add the Byte parameter? HOT 5
- More intelligent recognition of file types HOT 1
- Ms Office files is more then docx HOT 2
- FileType magic sequence with offset cannot be recognised HOT 4
- byte[] or stream versions of apis HOT 2
- Error on validating HTML files HOT 4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from filetypechecker.