💻
Building and hosting a WebApp
  • Getting started
  • Project setup
    • Requirements
    • Files organisation
    • Lerna
    • Linter
    • Prettier
    • GitHook
    • Testing
    • Conclusion
  • Backend
    • Files organisation
    • Environment config
    • Express API
    • Security
    • Database
    • GraphQL
    • User authentication
    • Conclusion
  • Frontend
    • Create React App
    • Files organisation
    • Styles
    • Apollo Hooks
    • Form management
    • User authentication
    • Writing tests
    • Types generation
    • Conclusion
  • DevOps
    • CI/CD
    • AWS
      • Managing secrets
      • Pricing
      • RDS
      • S3
      • Route53
      • CloudFront
      • Serverless
      • Security
      • CloudFormation
    • Conclusion
  • 🚧Stripe payment
  • 🚧File upload
Powered by GitBook
On this page
  • Helmet
  • API Rate Limit

Was this helpful?

  1. Backend

Security

PreviousExpress APINextDatabase

Last updated 5 years ago

Was this helpful?

This is too often overlooked or completely ignored. But just a few lines of code can make your app much harder to break.

  • helps you secure your Express apps by setting various HTTP headers.

  • Add an API rate limit to reduce the number of actions and protect from DDoS and brute force attacks.

  • Enforce HTTPS

Helmet

$ yarn add helmet

Start protecting your app with helmet

import helmet from "helmet";

export const createApp = () => {
  // ...
  app.use(helmet());
  app.use(helmet.noCache()); // disable browser caching
  app.use(
    helmet.hsts({
      includeSubDomains: true, // enforce https everywhere
      preload: true
    })
  ); 
 // ...
};

API Rate Limit

$ yarn add rate-limiter-flexible

We are going to create a new folder middlewaresand add RateLimiter.ts to it.

.
├── app.ts
└── middlewares
    ├── RateLimiter
    │   ├── index.spec.ts
    │   └── index.ts
    └── index.ts
import { RateLimiterMemory, IRateLimiterOptions } from 'rate-limiter-flexible';
import { Handler } from 'express';
import { AppRequest } from '../app';

class RateLimiterMiddleware extends RateLimiterMemory {
  constructor(opts: IRateLimiterOptions = {}) {
    super({
      points: 10,
      duration: 1,
      ...opts,
    });
  }
  middleware: Handler = async (request: AppRequest, response, next) => {
    try {
      await this.consume(request.user ? request.user.emailAddress : request.ip);
      next();
    } catch (error) {
      response.sendStatus(429); // Too Many Requests
    }
  };
}

export { RateLimiterMiddleware };
export * from './RateLimiter'

We can now use this middleware in app.ts.

app.ts
import { RateLimiterMiddleware } from './middlewares'

export const createApp = () => {
  const app = express();
  app.use(new RateLimiterMiddleware().middleware);
};

The number of requests is now limited to 10 per seconds. Go ahead an experiment with higher numbers to see if you will get rejected.

Or instead, we can also write a test for this:

~/packages/api
$ yarn add supertest @types/supertest -D
middlewares/RateLimiter/index.spec.ts
import * as express from "express";
import * as supertest from "supertest";
import { RateLimiterMiddleware } from "./index";

describe("middlewares/RateLimiter", () => {
  it("should limit the amount of requests", async () => {
    const app = express();
    app.use(new RateLimiterMiddleware({ points: 1 }).middleware);
    app.get("/test", (_, response) => response.sendStatus(200));
    const agent = supertest(app);

    await agent.get("/test").expect(200);
    await agent.get("/test").expect(429);
  });
});

The library helps us keep track of requests count.

branch available on GitHub.

Helmet
node-rate-limiter-flexible
security