-
Notifications
You must be signed in to change notification settings - Fork 8.2k
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
Error: CSRF token missing #312
Comments
Ok I think I understand what is going on here. This is sort of a complicated issue. I really believe that body-parser needs to handle multi-part data. Here are the 3 cases
I believe this to be the issue that I am experiencing. And I think the solution is for body-parse to handle file inputs. There's an open bug for this here: expressjs/body-parser#88. Does anyone have a work around for handling both file and field inputs in the same form? I'm fine with using the middleware for the files but I would still like to continue using body-parser for the rest of my form field items but it's doesn't seem to be possible. |
@macmichael01 I can tell you right now that body-parser is nto going to handle file uploads. It used to do that, but for if I remember correctly for security reasons it was removed. So, instead they suggested users to use packages like I haven't tried this, but did you attempt to add multer middleware to your form submission route? Wouldn't that give you access to both uploaded file and form inputs? If that doesn't work, then I am not sure how to help you. The only time I had to deal with a file upload, was when I needed to do just a single file upload, no other input fields. I am going to close this issue since this is not directly related to the hackathon starter, nor is there anything I can do to make body-parser handle file uploads. |
Good luck, and let me know if you find a better solution because I will consider adding it as a standalone example to this project, similar to http://hackathonstarter.herokuapp.com/api/scraping. |
I got into the same issue using Here's the code changes I've done to send only a simple file upload: |
body-parser isn't breaking anything. multipart is not supported by body-parser. The reason for the 403 is that lusca.csrf() is looking for the _csrf in the req.body. The body gets set to {} since multipart data cannot be read from the raw request. I really like body-parser, I just wished that multipart was supported. Adding multupart support to body-parser is a huge task which is why nobody has added such support yet. So perhaps lusca should be replaced with something that works with standard and multipart forms. I believe this would also mean that express-validator (assuming that it uses body-parser) would also need to be replaced with something else. formidable perhaps? |
👍 to supporting/accommodating file uploads in tandem with csrf security, if possible. Probably not the end of the world for a hackathon starter, but would be nice to have. Will disable like @gianpaj for now. |
I have added a simple file upload example. The only way to get it to work with Lusca's CSRF was to explicitly exclude |
Hello, First, I would like to thank you for this amazing starter kit! Cheers. |
I would also be interested in the security implications. If I undestand it correctly, I could use file upload without having the token. So probably I should not mix file-upload and some important information within one form but split it up, correct? |
@nik-ffm In a token-base authentication, every request to the server is accompanied by a token which the server uses to verify the authenticity of the request. The flow works like this:
From: Cookies vs Tokens: The Definitive Guide CSRF is an attack that forces an end user to execute unwanted actions on a web application in which they're currently authenticated. CSRF attacks specifically target state-changing requests, not theft of data, since the attacker has no way to see the response to the forged request. So, based on my understanding (correct me if I am wrong), if you disable the CSRF check in your application, you will be exposing your application to a CSRF attack, thus you can be tricked into executing state changing requests like transferring funds, changing their email address, etc. For your question, I am not able to provide you with a answer as I am myself wondering what are the impacts of this trick on security. |
I have done this another way: that's the original code: app.use((req, res, next) => {
if (req.path === '/api/upload') {
next();
} else {
lusca.csrf()(req, res, next);
}
}); i changed it a bit: |
works for me, thanks ! |
Can you post your code for that? |
First off thanks for this starter kit. Nicely done! I have a form which needs to handle a file upload. However the moment I add this form attr enctype="multipart/form-data" I receive a CSRF error. I tried multiparty, and busboy without success which I believe the problem happens before it even gets to busboy or multiparty. Articles suggest to place file parsing before the app.use(lusca(...)) but still doesn't work for me.
I also tried checking out a fresh copy of this repo and adding the enctype="multipart/form-data" to the login form just to make sure there was nothing that I did to cause the CSRF error and it immediately goes to a 403 Error: CSRF token missing when the form is submitted.
Hoping this is a simple mistake on my end.
The text was updated successfully, but these errors were encountered: