-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
How to validate by extension? #114
Comments
It would be nice to have a filter by extension. multer currently does not have an option property for this. But you should be able to customize the 'options.onFileUploadStart' function. Pass in something like:: onFileUploadStart: function(file, req, res) {
return (file.ext.toLowerCase() === 'pdf')
} Did that help? |
This is now possible in If you want to abort the upload: multer({
fileFilter: function (req, file, cb) {
if (path.extension(file.originalname) !== '.pdf') {
return cb(new Error('Only pdfs are allowed'))
}
cb(null, true)
}
}) If you want to skip any files that is not pdf: multer({
fileFilter: function (req, file, cb) {
if (path.extension(file.originalname) !== '.pdf') {
return cb(null, false)
}
cb(null, true)
}
}) |
EDIT: Nevermind, I did some googling, and see that you are considering a magic numbers implementation. Is this robust enough to prevent abuse of file upload? Or could someone theoretically upload a malicious file that just has the extension .pdf? I have read a lot about the magic bytes method for validating file-types, but would prefer this if equivalent. Thanks! |
A file (and therefore it's file extension) can be renamed. It's better to check files by mimetype and magic numbers as well. Not sure how to test magic numbers with Multer, but did ask for a field named "magicNumber" #155. const path = require('path');
multer({
fileFilter: function (req, file, cb) {
var filetypes = /jpeg|jpg/;
var mimetype = filetypes.test(file.mimetype);
var extname = filetypes.test(path.extname(file.originalname).toLowerCase());
if (mimetype && extname) {
return cb(null, true);
}
cb("Error: File upload only supports the following filetypes - " + filetypes);
}
}); |
This worked for me:
|
To be sure about files types, we need to use magic number. import fileType from 'file-type';
multer({
fileFilter (req, file, cb) {
// How can I get the buffer to read and test ?
const infoFile = fileType(file.buffer);
}
}); |
@EmixMaxime
If you are using memory store, then |
Where do you see |
@jonataswalker I'm not using |
Sorry but this is kind of a newbie question and a bit off topic but how do I handle the error that is thrown? i.e. how do i return this error to the client/user from the server side? Current this results in an internal server error kind of response. P.S. : I came across this : https://github.com/koajs/koa/wiki/Error-Handling P.S. 2 : This works, but is it safe? I don't want to return any other error (SQL etc) that would disclose too much information.
|
@pratham2003 The general principle is that you have a central error handler, which captures all the errors of your application and translates them into the HTTP response. I believe all servers should support this one way or another. I know for sure it works in Express and Restify this way. Then, in your error handler, you can decide what errors and what details should be displayed. It's very convenient to rely on environment here, because you can show all errors with stack traces in development/test and only show error messages in production. Also, you can filter by error type, just make sure to have different classes for different errors. Also, in the future I would recommend you to ask such questions on StackOverflow, rather than writing an off-topic questions on GitHub. |
I am using this:
|
This should be (notice || replaced with &&):
or else it will always fail validation if not a png. |
Multer doesn't have File validation using Magic number. What you could do instead is use NPM packages like read-chunk and file-type to detect the file type after it's uploaded. Since we are using readchunk to read only the beginning of the file as a Buffer, it will not utilize the memory much. The following are the rules in this approach.
First, get the stored file Route.
and store the file name to this variable when you process the file using Multer like
So, We now have the saved file route. Let's read a portion of that as Buffer using a custom function and readChunk.
Now, check value against a list of mime-types that you want to support and keep or delete or move the file.
|
Just for learning purposes.. where that path attribute is coming from ? |
The Hope this helps! |
can any one tell me to pass failed messagge in cb() |
How could I use multer to validate the extension so it can only allow to upload PDF files?
The text was updated successfully, but these errors were encountered: