-
-
Notifications
You must be signed in to change notification settings - Fork 683
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Formidable detects MIME-type according to the file extension and not by the real content #749
Comments
That is good point, and the readme could definitely have a small guide on how to check this. I don't think we will ever bake in content-based MIME detection since there are thousands of file types (and more created each year) and each one needs special detection. For example to know if it is a jpg is very different algorithm than zip, pdf etc. |
Sure, it's difficult to support all possible types. But a deep check for the very common types (e.g. office, PDF, media) could be a great feature with ability to extend it by custom plugins. |
How does multer do it ? |
Common types is very subjective. And we are trying to go to the opposite direction, make the lib smaller not bigger. For example #718 (comment) Personally I think we should embrace the npm philosophy of having a lot of modules that do 1 thing and do it well. Then everyone can decide to import or not, if he needs alongside formidable. |
Sure, ideally would be to make |
Great approach, zero dependencies and 85 KB package are much better then alternative solutions with 2-5 MB of dependencies. |
It looks like the file type is detected by checking the magic number of the buffer. It worth using the import FileType from "file-type";
(async () => {
console.log(await FileType.fromFile("Unicorn.png"));
console.log(await FileType.fromBuffer(fileBuffer));
console.log(await FileType.fromStream(fileStream));
//Output: {ext: "png", mime: "image/png'"}
})(); |
Except file-type adds not a single dependency but twelve. Some of which are redundant at this point:
And formidable v3 already has plugin support so if you really want file type detection magic, just make a plugin for that. |
OK, since such functionality is not must have and it takes to implement about 10 minutes, this issue can be closed. |
Leaning more and more towards a separate package like There's never ending discussion between small and big packages. And I'm still bouncing between the two. Okay, small, almost no dependencies package.. but end users anyway will include a lot of deps to do what their needs are.. so.. that's why i don't see big problem of including things like some of the two qs.. or other basic things like correct basic mime handling, here in the core package. Convenience and usability, over impracticality. I'm for staying thin and this to remain in userland with some guides, but it also might be a good thing to include some basic detection for most common types, and not something huge and very generic like file-type. |
I think there is no right or wrong answer here.
@tunnckoCore, should I reopen this issue? |
Agree.
I don't know. I'm not very active last half year or more. Maybe we can open it if we decide to implement some basic detection. |
If nobody wants to work on this we might as well close the issue |
I'll work on it in some time. Made some research:
Can make |
https://github.com/vader-sama/typective similar to magic-bytes but works with streams Also remember that a valid number does not prove anything at the end, I remember it was possible to make valid jpeg files that were also valid php files, which was one way to hack a server that looked like it did everything correct. |
Oh yea.. 😆 I remember that too.
yea, seems good. |
Another one: https://github.com/lukeed/mrmime |
I'm happy user of mmmagic. It was chosen ~6 years ago for execution inside aws lambda because of speed. But for formidable I would suggest to make it plugin-able. I mean not a specific plugin, but just a parameter like: const form = formidable({
getMimeType(file: Buffer): string {
// custom logic
}
}) Or plugin which allows custom logic. |
Support plan
Enterprise): Community
Context
Formidable detects MIME-type according to the file extension and not by the real content. Which means that user can fake the file MIME by changing file's extension and as a result to upload to the server not allowed file types.
BTW,
multer
detects file's MIME not by the extension only.What are you trying to achieve or the steps to reproduce?
tst.jpg
tst.pdf
uploader.parse(req, async (err, fields, files) => {…}
tst.pdf
What was the result you got?
mimetype = application/pdf
What result did you expect?
mimetype = image/jpeg
Since this file is actually JPG file but with a from extension.
The text was updated successfully, but these errors were encountered: