Giter Site home page Giter Site logo

Add "Security" section about docs.nestjs.com HOT 7 CLOSED

nestjs avatar nestjs commented on May 4, 2024 9
Add "Security" section

from docs.nestjs.com.

Comments (7)

jamshally avatar jamshally commented on May 4, 2024 16

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:

  • NestJS Auth Chapter

    • Pro: NestJS best practice - straight from kamilmysliwiec
    • Pro: Illustrates some of the code for handling JWT auth
    • Con: Examples/docs do not seem in depth enough to provide a solid foundation for production use
    • Con: Comments like "... in a best-case scenario the jwt package and token configuration (secret key and expiration time) should be registered as custom providers"... melted my already overloaded brain. An code example of this would be super.
    • Con: It is unclear exactly how a "local" passport auth strategy (username and password) would be implemented... or if it could be done with the existing code. In the current examples, it is not obvious where username and password checking could/should be inserted. The simplification of the code for example purposes (by using a faked user) has in some ways made it more confusing to understand.
    • Con: Not clear how this would integrate with social login, CRSF etc
  • NesJS Basic Auth and Sessions Blogpost (artonio)

    • Pro: Step by step commentary for implementing auth
    • Con: Implementation too basic for a production system... and yet somehow still seems complicated
    • Con: Even with this basic implementation, finds (and reports) a bug in the Nest Passport library, which is as-yet unfixed
    • Con: No CRSF protection
    • Con: No use of helmet or such for additional security
  • NestJS Starter Project (CanKattwinkel)

    • Pro: Includes CSRF protection
    • Pro: Seems to use NestJS best practices (like using Nest middleware)
    • Con: A lot of custom complexity, including custom sessions, which makes the auth specific code hard to follow, and distinguish which is necessary auth, and what is extraneous
    • Con: The CSRF protection is very hard to follow
    • Con: Not leveraging the Nest/Passport libraries
    • Con: No license, so can be used for example only
    • Con: Does not support mobile auth
  • Nest-Angular open Source Project (bojidaryovchev)

    • Pro: Comprehensive example including social authentication, and helmet
    • Con: Seems to not use standard NestJS techniques (uses express and express middleware directly instead of NestJS style, uses custom JOI validation via middleware)
    • Con: Uses "off brand" passport strategies for reasons that are not clear
    • Con: No CRSF protection

Some Specific Points of Confusion

  • How to reconcile Passport Strategy session needs with NestJS
  • If CRSF is a threat to NestJS and how to mitigate
  • How to handle auth token refresh needs
  • If/how auth for mobile devices would be accommodated. Especially with the long-lived auth requirement of apps

Wishlist

  • How to implement local authentication using JWT Bearer Tokens
    • How to accommodate the token timeout and refresh scenarios
    • How to position this to accommodate both web and mobile clients
  • How to integrate auth via social login
  • How to implement CRSF protection
  • How to implement additional hardening (such as via helmet)
  • Nice to have: 2FA example

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!

from docs.nestjs.com.

jamshally avatar jamshally commented on May 4, 2024 8

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

from docs.nestjs.com.

johnbiundo avatar johnbiundo commented on May 4, 2024 5

@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).

from docs.nestjs.com.

kamilmysliwiec avatar kamilmysliwiec commented on May 4, 2024 2

MVC apps are very rare nowadays @bhaidar

from docs.nestjs.com.

bhaidar avatar bhaidar commented on May 4, 2024 1

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.

from docs.nestjs.com.

abouroubi avatar abouroubi commented on May 4, 2024

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.

from docs.nestjs.com.

bhaidar avatar bhaidar commented on May 4, 2024

Any follow up on how to use CSRF in Nestjs?

from docs.nestjs.com.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.