expressjs / expressjs/multer

fileFilter error, and using csurf

Open
#755 11 comments 0 reactions 0 assignees View on GitHub
Dominant language
JavaScript
Stars
12.1k
Forks
1.1k
Avg merge
8d 2h
Merged PRs (30d)
21

Description

My csrf protection middleware, using "csurf", is configured after multer, as it should (I guess).

If I have an error, for example, in the fileFilter function :
```app.use(multer({ storage: fileStorage, fileFilter: fileFilter }).single('image'));```

Then, it goes straight to the express error-handling middleware :

```
app.use((error, req, res, next) => {
res.status(500).render('500', {
pageTitle: 'Error!',
path: '/500',
isAuthenticated: req.session.isLoggedIn
});
});
```

But in the page I render, I use the csrf token defined in a previous middleware, so it's undefined ('invalid csrf token').

How can I then use multer with csurf, for those errors ?

Here's the fileFilter function :

```
const fileFilter = (req, file, cb) => {
if (
file.mimetype === 'image/png' ||
file.mimetype === 'image/jpg' ||
file.mimetype === 'image/jpeg'
) {
cb(null, true);
} else {
cb('INVALID FILE!!!!', false);
}
};
```

As I said, it is triggered before the request is handled by the csrf middleware, hence the issue.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.