From 4c0d5122d691866db4d1d7611ffb3bd074ce1ab5 Mon Sep 17 00:00:00 2001 From: Bobo Date: Wed, 18 Sep 2024 12:27:00 +0200 Subject: [PATCH] Limit number of requests --- package.json | 1 + src/index.ts | 9 +++++++++ yarn.lock | 5 +++++ 3 files changed, 15 insertions(+) diff --git a/package.json b/package.json index 428fa1e..52f308e 100644 --- a/package.json +++ b/package.json @@ -76,6 +76,7 @@ "cors": "^2.8.5", "ethers": "^6.7.1", "express": "^4.18.2", + "express-rate-limit": "^7.4.0", "express-validator": "^7.0.1", "firebase-admin": "^11.10.1", "firebase-functions": "^4.4.1", diff --git a/src/index.ts b/src/index.ts index 87364f7..42ab575 100644 --- a/src/index.ts +++ b/src/index.ts @@ -3,6 +3,7 @@ import '@polkadot/api-augment'; // Fix end import express from 'express'; +import rateLimit from 'express-rate-limit'; import bodyParser from 'body-parser'; import * as functions from 'firebase-functions'; import cors from 'cors'; @@ -11,6 +12,14 @@ import { IControllerBase } from './controllers/IControllerBase'; import { ContainerTypes } from './containertypes'; const app = express(); + +// Limit the number or requests to 100 per minute. +const limiter = rateLimit({ + windowMs: 1 * 60 * 1000, + max: 100, +}); +app.use(limiter); + app.use(express.json({ limit: '5mb' })); app.use(bodyParser.urlencoded({ extended: true })); app.use(bodyParser.json()); diff --git a/yarn.lock b/yarn.lock index a0aaea8..1441691 100644 --- a/yarn.lock +++ b/yarn.lock @@ -4239,6 +4239,11 @@ expect@^29.0.0, expect@^29.6.3: jest-message-util "^29.6.3" jest-util "^29.6.3" +express-rate-limit@^7.4.0: + version "7.4.0" + resolved "https://registry.yarnpkg.com/express-rate-limit/-/express-rate-limit-7.4.0.tgz#5db412b8de83fa07ddb40f610c585ac8c1dab988" + integrity sha512-v1204w3cXu5gCDmAvgvzI6qjzZzoMWKnyVDk3ACgfswTQLYiGen+r8w0VnXnGMmzEN/g8fwIQ4JrFFd4ZP6ssg== + express-validator@^7.0.1: version "7.0.1" resolved "https://registry.yarnpkg.com/express-validator/-/express-validator-7.0.1.tgz#435fac6b5fa838763f78eca05d2206317b92106e"