DEV Community

Cover image for A Guide to Securing Node.js Applications

A Guide to Securing Node.js Applications

Shahid Shaikh on December 30, 2020

The one thing that developers tend to considers at the end of the development cycle is the “security” of the application. A secure application is n...
Collapse
 
wparad profile image
Warren Parad • Edited

A lot of this is very good advice. However I would add a couple of things:

  • DoS protection is best done using user identity rather than ip address. With cloud providers and potential DDoS attacks I would not rely as much on ip. Since most cloud providers can help with this, pull in one of those like WAF in AWS.
  • You don't need CSRF if you use SameSite=Strict and you really should be setting this to be true for all cookies. (This was missed when talking about cookies, and it is really easy to do.)
  • Passwords in 2021? really? Use WebAuthN and social logins and don't bother with trying to hash them. Even the bcrypt set up that was suggested here is now no longer the best practice, it's argon2, although at some point that will also be not the best solution either.
  • Data validation can and should actually be done before the controller handles the user input. The best way to do this is using an openapi specification verifier. We use api-scheme-validator it's awesome, and there are more complex ones available for different frameworks.
  • On the AuthZ side, you really need more than ACL lists, they don't provide comprehensive understanding of what your users are doing and who has access to what. It works for one service with simple permissions. But when you have lots of users which multiple services scaling up, you need more control and visibility than that. In those cases you need Authress to handle all the multitenant permissions cases.
Collapse
 
andreidascalu profile image
Andrei Dascalu • Edited

I would never consider it a good practice to force anyone to use social sites if they want to use a webapp.

As an optional alternative, sure (personally I wouldn't offer FB login at all and I would never use sites that need it, but that's just me) but not as a full replacement.

Oh, so you want to browse my blog / shop in my store but first you must hand over your data to Facebook / Google. Because security.

Collapse
 
shaikhshahid profile image
Shahid Shaikh

Totally agreed. A password-based auth might be old but still works great.

Thread Thread
 
manyrios56 profile image
Manuel Rios

for sure, even if is just one of the steps to log... I really like it yet

Collapse
 
wparad profile image
Warren Parad

That's because most companies are not security competent. Sure I would never hand my data over to Facebook, but Google has proven they don't know what do with it anyway. Realistically you must trust some repository for storing access credentials. Using federated login is much safer than using Lastpass, 1Password, etc... If you want a privacy first federated login, come talk to me in a couple of months, Authress will have that capability.

If you don't use a social login, the site you are using will most likely leak all your data. And sure for a blog you can argue nothing bad, but a site that I put my credit card in, I would never trust that site to handle my data correctly. It usually ends up like this: Hacked home cams used to livestream police raids in swatting attacks.

Thread Thread
 
andreidascalu profile image
Andrei Dascalu

For credentials, sure. But social login doesn't come with simply credentials storage but with an agreement to allow monitoring online behaviour to the extent that a user needs to put in place a lot of serious safeguards to protect their data and prevent traces, doubly so if they have social media presence.
Without social media profile, some generic data can be leaked, but it's much easier to prevent it even with basic modern browser enhancements.
Password storages, I trust (so far) which is why I prefer the password storages + generated password rather than social login.

Thread Thread
 
stephanep profile image
Stephane Paul

I would go password less for 2021, people are just dumb with passwords.

Thread Thread
 
andreidascalu profile image
Andrei Dascalu • Edited

Unless you mean biometrics, I don't see how that goes. Social login means the password is handled by Facebook. But it's still a password just somewhere else, incidentally with a bigger footprint, a bigger target both for hacking and phishing. I have nothing against providing social login as an alternative, just not a replacement for user password (with 2fa, maybe).
If there's a breach on their side, you can't do much except remove that provider.

Thread Thread
 
shaikhshahid profile image
Shahid Shaikh

Totally agreed.

Collapse
 
noclat profile image
Nicolas Torres

Just email magic links in that case :). Passwords are deprecated.

Thread Thread
 
andreidascalu profile image
Andrei Dascalu • Edited

Absolutely, password are deprecated. That's why nobody uses them anymore and the very few websites that use them are compromised 10 times a day.
Just gonna send an email link to an email address based solely on the existence of that email, which the user is going to access with a ... password. There, the bucket was kicked down the road a bit more.
Points:

  • passwords are acceptable. Not great or perfect, but acceptable. YOUR system needs to be secure, but you can't aim to protect people from themselves. There's no way to do that reliably that doesn't also deterr users from using your system. Sure, if you have a role system whereby a compromised account can be user to elevate access, then it stands to reason to aim higher - but that's not useful for end-user facing systems.
  • there are good systems of user/access management that you can reliably defer authentication and authorization to (Azure AD or even identity management systems like Okta). It's not necessarily because they are password-less but because authentication can be customized (eg: you can let the users decide what they want to login with: password, social, user management systems, magic links or simply default authentication to the use of given networks) and you can also restrict access based on chosen methods or disable certain methods in certain situations (eg: with Okta you can enable facebook login but say facebook has a breach -> then you can relegate facebook-enabled accounts to a different method).
Thread Thread
 
thedaveamour profile image
David Amour

"That's why nobody uses them anymore and the very few websites that use them are compromised 10 times a day" - What is your source for these facts, or are they just opinion?

Thread Thread
 
andreidascalu profile image
Andrei Dascalu

sarcasm

Thread Thread
 
thedaveamour profile image
David Amour

Ok. Funny then but not helpful.

Thread Thread
 
andreidascalu profile image
Andrei Dascalu • Edited

That's what the second part of the post was for. Unless you're looking for validation.

Collapse
 
gtalinn profile image
Aruj

I did a fair bit of reading on CSRF. While we should always try and use SameSite attribute for cookies, it is not a fool-proof method for protecting against CSRF. CSRF token should still be used. The SameSite=Strict will only work for browsers that implement this feature. If a user uses a browser which does not support it, they can be vulnerable. Additionally there are ways to bypass this feature even for browsers supporting SameSite attribute.

Quoting from CSRF sameSite:

It is important to note that this attribute should be implemented as an additional layer defense in depth concept. This attribute protects the user through the browsers supporting it, and it contains as well 2 ways to bypass it as mentioned in the following section. This attribute should not replace having a CSRF Token. Instead, it should co-exist with that token in order to protect the user in a more robust way.

Collapse
 
wparad profile image
Warren Parad

If you are using REST APIs to secure content and not sending it back as part of a SSR. Then those bypasses don't apply. Since it isn't secure to send content back as part of page navigation anyway. CSRF can entirely be replaced by SameSite.

Collapse
 
ecyrbe profile image
ecyrbe

I would argue that many things you list are controversial.
First, not everybody is building secured services on cloud, and many developpers are building in enterprise private infrastructures.

For many of us:

  • exit social auth, but use openid connect on your validated provider (in enterprise there's a chance you have one if your are in medium or Big company). But the result is the same, decoupling the authentification stack from your code is better. You finally only need to validate a JWT.
  • exit authress, i think you are biased, because it's your product. Try to detach yourself when giving advices. This service, to be honest sounds like an horrible Idea. I prefer building my own IAM on top of an opensource one than using a service like that.
Collapse
 
slidenerd profile image
slidenerd

i appreciate your suggestions but here is my reverse suggestion for webauthn caniuse.com/?search=webauthn check it out, very incompatible on many browsers

Collapse
 
tbhaxor profile image
Gurkirat Singh • Edited

Nice work brother. I am making this post more visible.

Also for those who are looking for mongodb security? I have posted a completed write up here: dev.to/tbhaxor/one-step-to-prevent...

Also for express js users, I have created a middleware: mongo-secure.tbhaxor.com/

Collapse
 
mjgs profile image
Mark Smith • Edited

Great list, and very timely for me. I was asked recently on a call what backend security techniques I am familiar with. On the call I remembered about 1/2 of these, so it’s good to see a full list.

I’ve used all of these apart from perhaps the regex denial of service detection.

Some other libs worth considering:

Collapse
 
talk2sohail profile image
Sohail

i have a question here, will highly appreciate the answer by anyone. what is the point of express-brute if not been able to use it in production? Thanks.

Collapse
 
shaikhshahid profile image
Shahid Shaikh

it's for the demonstration purpose. You can use the rate-limiter technique of a similar kind in the production server.

Collapse
 
talk2sohail profile image
Sohail

Or maybe having it built. Thanks.

Collapse
 
analyticsinstitute profile image
Anna

This is a great piece! Thank you. Security still seems to be underemphasized in dev curricula. For many devs, when it comes to learning about security, we don't know where to start. Do you have any pointers/suggested resources on how to get started on cybersecurity?

Collapse
 
thedenisnikulin profile image
Denis • Edited

Start with hacking first (ethical one for sure), learn how to hack a web server, how to scan it for vulnerabilities, look what typical vulnerabilities exist and how to exploit them, it'll give you a lot of knowledge and motivation to create more secure services and websites :). I can send some resources if you're really interested!

Collapse
 
nishajames86 profile image
Nisha James

Securing application in the best way👍

Collapse
 
ajax27 profile image
Shaun Collins

Great post, fantastic comments. Lots of views!!!

Collapse
 
shubh874 profile image
shubh874

Good job brother, appreciate all the efforts!

Collapse
 
syamjayaraj profile image
Syamlal CM

Great article. I will definitely consider these in my next project.

Collapse
 
simonholdorf profile image
Simon Holdorf

Cool Post, thank you very much!

Collapse
 
shaikhshahid profile image
Shahid Shaikh

Your welcome :)

Collapse
 
msnimbal profile image
msnimbal

This is really good. I really appreciate your effort to put these things together.

Collapse
 
ch3ckmat3 profile image
Sohail Iqbal

Great collection of security practices! Thank you for taking the time to write this. 👍

Collapse
 
shaikhshahid profile image
Shahid Shaikh

Your welcome. It took me almost a month to compile it :)

Collapse
 
ganeshdeore profile image
ganeshdeore

Great collection

Collapse
 
harshrathod50 profile image
Harsh Rathod

Very useful info here. I learnt new things. 😇

Collapse
 
congvu307 profile image
Duong Cong Vu

Total great!!! Thank for your writing!

Collapse
 
hpplus5 profile image
Sam Warner

Please don't limit the length of your password to so low a value! If the complaint is that users choose bad passwords, at least give them a chance to prove you wrong instead of ensuring they can't.