nestjs / docs.nestjs.com

The official documentation https://docs.nestjs.com 📕
MIT License
1.2k stars 1.74k forks source link

Add "Security" section #109

Closed kamilmysliwiec closed 3 years ago

kamilmysliwiec commented 6 years ago

I'm submitting a...


[ ] Regression 
[ ] Bug report
[ ] Feature request
[x] Documentation issue or request (new chapter/page)
[ ] Support request => Please do not submit support request here, instead post your question on Stack Overflow.

Current behavior

Expected behavior

As a user, I would like to see Security chapter.

Minimal reproduction of the problem with instructions

What is the motivation / use case for changing the behavior?

Environment


For Tooling issues:
- Node version: XX  
- Platform:  

Others:

jamshally commented 6 years ago

A comprehensive security section would be extremely helpful. There is a lot of complexity and confusion around the best way to accomplish tight security using NestJS - which is essential if it is to be used for production applications. There are very few examples available to follow. The examples that do exist all seem to either be too simple, have problems, or be somewhat contradictory.

Existing Examples:

Some Specific Points of Confusion

Wishlist

Any guidance for the above would be much appreciated. I think that NestJS is a fantastic project, and would love to start using it in production with confidence that I have got the security right.

Thanks for all the time and effort on NestJS!

jamshally commented 6 years ago

One more plug for the above guidance... if NestJS could had a solid production-ready auth strategy, it could be a game-changer in the NodeJS field. Having been reading around in preparation to implement my own auth, I have seen so much confusion and out of date and incorrect guidance, it is clear that this is a NodeJS-wide challenge, and not just a NestJS challenge.

This article gives a pretty good peek-behind-the-curtain of the state of affairs: https://hackernoon.com/your-node-js-authentication-tutorial-is-wrong-f1a3bf831a46

abouroubi commented 5 years ago

Hello @ahrnee I'm working on a sample nestjs app, that will be used mainly by a mobile application. It will use short lived JWT access tokens and sliding refresh tokens, Facebook and Google connect.

I'll try to push it on GitHub by the end of the week. Comments, issues and PR's are welcome.

bhaidar commented 5 years ago

Any follow up on how to use CSRF in Nestjs?

johnbiundo commented 5 years ago

@bhaidar I think most of the considerations are not specific to Nest. I use the csurf module, and cookie parser. I also use express sessions for session management, and found that there's a side-effect in relying only on sessions for storing the csurf secret (probably particular to my use case), hence relying on cookies instead for that function. So my main.ts looks like

  app.use(cookieParser());
  app.use(csurf({ cookie: true }));

I mentioned the details above because they are generic Express/Sessions/CSurf issues, not Nest-specific, and illustrate the point that if your use case is complex (e.g., also using sessions), you may have to do some additional research into those packages.

Another aspect of the CSRF equation is your client side. In my case, for example, I have a SPA and use Angular, which handles the client side transparently. If you have an MVC style app, you'll need to do more work in how you render pages so that you pass the CSRF token to the forms so they're passed back on POSTs (just as one example).

bhaidar commented 5 years ago

Thanks @johnbiundo I figured out that CSRF should be used with MVC apps rather than SPAs. I also use Angular with Nest.

I believe the docs need more work regarding MVC unless building MVC apps with Nest or Node is not popular.

kamilmysliwiec commented 5 years ago

MVC apps are very rare nowadays @bhaidar