Skip to content

Commit 559b441

Browse files
committed
add ratelimit on logs fetch
1 parent ae1382f commit 559b441

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

packages/express/package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
"cors": "^2.8.5",
2929
"dotenv": "^16.4.7",
3030
"express": "^4.21.2",
31+
"express-rate-limit": "^7.5.0",
3132
"ffmpeg-static": "^5.2.0",
3233
"hashids": "^2.3.0",
3334
"morgan": "^1.10.0",
@@ -45,6 +46,7 @@
4546
"@types/cors": "^2.8.17",
4647
"@types/dotenv": "^8.2.3",
4748
"@types/express": "4.17.21",
49+
"@types/express-rate-limit": "^6.0.2",
4850
"@types/ffmpeg-static": "^2.0.0",
4951
"@types/hashids": "^1.0.30",
5052
"@types/jest": "^29.5.14",

packages/express/src/routes/logs.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,27 @@
11
import express, { Request, Response } from 'express';
22
import { promises as fs } from 'fs';
33
import path from 'path';
4+
import rateLimit from 'express-rate-limit';
45

56
import logger from '../logger.js';
67
import process from 'process';
78

89
const router = express.Router();
910

11+
// Rate limiting middleware for logs routes
12+
const logsRateLimit = rateLimit({
13+
windowMs: 15 * 60 * 1000, // 15 minutes
14+
max: 20, // Limit each IP to 20 requests per windowMs
15+
message: {
16+
error: 'Too many log requests from this IP, please try again later.'
17+
},
18+
standardHeaders: true, // Return rate limit info in the `RateLimit-*` headers
19+
legacyHeaders: false, // Disable the `X-RateLimit-*` headers
20+
});
21+
22+
// Apply rate limiting to all routes in this router
23+
router.use(logsRateLimit);
24+
1025
// Get list of available log files
1126
router.get('/', async (_req: Request, res: Response) => {
1227
// [ ] add an auth mechanism. Below fcn is based on

0 commit comments

Comments
 (0)