> For the complete documentation index, see [llms.txt](https://tutorial.specian.co.uk/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://tutorial.specian.co.uk/backend/security.md).

# Security

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

* [Helmet](https://helmetjs.github.io) 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

The library [node-rate-limiter-flexible](https://github.com/animir/node-rate-limiter-flexible#readme) helps us keep track of requests count.

```
$ yarn add rate-limiter-flexible
```

We are going to create a new folder `middlewares`and add `RateLimiter.ts` to it.

```
.
├── app.ts
└── middlewares
    ├── RateLimiter
    │   ├── index.spec.ts
    │   └── index.ts
    └── index.ts
```

{% tabs %}
{% tab title="middlewares/RateLimiter/index.ts" %}

```typescript
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 };
```

{% endtab %}

{% tab title="middlewares/index.ts" %}

```typescript
export * from './RateLimiter'
```

{% endtab %}
{% endtabs %}

We can now use this middleware in `app.ts`.

{% code title="app.ts" %}

```typescript
import { RateLimiterMiddleware } from './middlewares'

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

{% endcode %}

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:

{% code title="\~/packages/api" %}

```
$ yarn add supertest @types/supertest -D
```

{% endcode %}

{% code title="middlewares/RateLimiter/index.spec.ts" %}

```typescript
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);
  });
});
```

{% endcode %}

{% hint style="info" %}
[`security`](https://github.com/florianherrengt/book-code/tree/security) branch available on GitHub.
{% endhint %}
