-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement method whitelisting middleware to secure API endpoints
Update methodWhitelist.ts update methodWhitelist status code refactor methodWhitelist middleware logic
- Loading branch information
1 parent
adac9f9
commit 1f0d033
Showing
2 changed files
with
15 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 next(); | ||
} | ||
return res.status(403).json({ error: 'Forbidden' }); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters