Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rajeshkumaravel/3053f058eb2db6130c0d18541d1da0b3 to your computer and use it in GitHub Desktop.
Save rajeshkumaravel/3053f058eb2db6130c0d18541d1da0b3 to your computer and use it in GitHub Desktop.
How to make your NodeJS application or API secure ?

How to prevent these attacks from happening in your own project?

Preventing DOS Attacks

  1. First thing to consider when dealing with DOS attacks prevention is to limit the actual payload that user can submit to your app / api / service. You can limit the body payload using body-parser. If you are using ExpressJS as your backend framework, then you are golden. ExpressJS comes with built-in body-parser that you can use.
const express = require('express');
const app = express();
app.use(express.json({ limit: '10kb' })); // Body limit is 10
  1. Another useful express feature is express-rate-limit dependency. This dependency lets you set rate limit for users. So basically, you can set maximum amount of requests for each user, after user uses all of his requests, you can lock him out for certain amount of time. Here is how you can configure express-rate-limit:
npm install express-rate-limit - Installing it using NPM
const limit = rateLimit({
    max: 100,// max requests
    windowMs: 60 * 60 * 1000, // 1 Hour
    message: 'Too many requests' // message to send
});
app.use('/routeName', limit); // Setting limiter on specific route

Preventing XSS Attacks

  1. First of all, you can sanitize user data, on input. This is very easy to implement, and we can use another useful dependency called xss-clean. This dependency will prevent users from inserting HTML & Scripts on input.
npm install xss-clean - Installing it using NPM
// Data Sanitization against XSS
app.use(xss());
  1. Give your project special HTTP headers using helmet dependency. Helmet is a collection of middleware functions. By default, not all of the middleware functions are included, but you can enable rest of them manually. You can check this link to see other middleware functions. Here is how you can configure helmet - It should be declared on the top of the code:
npm install helmet - Installing it using NPM
app.use(helmet());
  1. If you are using JSON Web Tokens (JWT) instead of express-session for example, you should consider storing JWT’s into the cookies. As well, make sure these cookies for JWT storing are HTTP Only!

Preventing Brute Force Attacks

  1. One of the most efficient way to help you to deal with brute force attacks, is to set limit to login attempts, or anything related to authentication that requires users to insert their passwords, special codes or PINs.
  2. Next up, and again, if you are using ExpressJS, you could implement express-rate-limit dependency. Which we did indeed implemented in DOS attacks prevention. But this dependency works both for Brute Force attacks and DOS attacks.
  3. To slower down and make life a little bit harder for attackers when guessing sensitive data (passwords, PINs), you could implement bcrypt dependency. Bcrypt will encrypt sensitive data such as passwords and it will make them harder to guess. Bcrypt is a little bit complex dependency, but if you want to learn more (and you should) checkout this link.
  4. Another thing that will make brute force attacks less likely is implementing 2-Step verification process, or two-factor authentication. It does take couple of lines of codes to implement this, so here is the link that will help you out with it!

Preventing SQL/NoSQL Injection Attacks

  1. Either if you are working with SQL or NoSQL database, you should sanitize your data. If you are working with SQL database, you should consider using node-mysql dependency. Learn more about node-mysql here. If you are working with NoSQL database (MongoDB) and ExpressJS, consider using express-mongo-sanitize dependency.
npm install express-mongo-sanitize - Installing it using NPM
app.use(mongoSanitize());
  1. If working with MongoDB, use Object Data Modeling tool (ODM) Mongoose. Mongoose lets you define schemas and schema types for each one of your documents, making it secure from the beginning. Mongoose is pretty complex on it’s own, but if you want to dive deep into it, i’ll suggest you to check this link or get a crash course on it and learn it on the go!

Your main app file could look like this after you implemented these dependencies:

// Importing Dependencies
const express = require('express');
const rateLimit = require('express-rate-limit');
const helmet = require('helmet');
const mongoSanitize = require('express-mongo-sanitize');
const xss = require('xss-clean');
const app = express();
// Helmet
app.use(helmet());
// Rate Limiting
const limit = rateLimit({
    max: 100,// max requests
    windowMs: 60 * 60 * 1000, // 1 Hour of 'ban' / lockout 
    message: 'Too many requests' // message to send
});
app.use('/routeName', limit); // Setting limiter on specific route
// Body Parser
app.use(express.json({ limit: '10kb' })); // Body limit is 10
// Data Sanitization against NoSQL Injection Attacks
app.use(mongoSanitize());
// Data Sanitization against XSS attacks
app.use(xss()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment