Skip to content

Commit

Permalink
Implement method whitelisting middleware to secure API endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
S0naliThakur committed Aug 30, 2024
1 parent 7933f76 commit 9c7649c
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/middlewares/methodWhitelist.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { Request, Response, NextFunction } from 'express';
import { methods } from '../api';

const allowedMethods = Object.keys(methods);

export const methodWhitelist = (req: Request, res: Response, next: NextFunction) => {
const method = req.body?.method;
if (method && !allowedMethods.includes(method)) {
return res.status(404).json({ error: 'Method not found' });
}
next();
};
3 changes: 3 additions & 0 deletions src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import { setupEvmLogProviderConnectionStream } from './websocket/log_server'
import { setupArchiverDiscovery } from '@shardus/archiver-discovery'
import { setDefaultResultOrder } from 'dns'
import { nestedCountersInstance } from './utils/nestedCounters'
import { methodWhitelist } from './middlewares/methodWhitelist'
setDefaultResultOrder('ipv4first')

// const path = require('path');
Expand Down Expand Up @@ -195,6 +196,8 @@ app.use('/log', authorize, logRoute)
app.use('/authenticate', authenticate)
app.use('/', healthCheckRouter)
app.use(injectIP)
// Method Whitelisting Middleware
app.use(methodWhitelist)
// reject subscription methods from http
app.use(rejectSubscription)
app.use(server.middleware())
Expand Down

0 comments on commit 9c7649c

Please sign in to comment.