Skip to content

Commit

Permalink
Potential fix for code scanning alert no. 63: Missing rate limiting
Browse files Browse the repository at this point in the history
Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com>
  • Loading branch information
sthsuyash and github-advanced-security[bot] authored Jan 25, 2025
1 parent 5fca159 commit c4eae41
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
3 changes: 2 additions & 1 deletion api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@
"mongoose": "^8.9.4",
"morgan": "^1.10.0",
"multer": "^1.4.5-lts.1",
"nodemailer": "^6.9.16"
"nodemailer": "^6.9.16",
"express-rate-limit": "^7.5.0"
},
"devDependencies": {
"nodemon": "^3.1.9",
Expand Down
10 changes: 9 additions & 1 deletion api/routes/post.route.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import express from "express";
import rateLimit from "express-rate-limit";
import {
getAllPosts,
createPost,
Expand All @@ -23,11 +24,18 @@ const storage = multer.diskStorage({
},
});
const upload = multer({ storage });

// Configure rate limiter: maximum of 100 requests per 15 minutes
const limiter = rateLimit({
windowMs: 15 * 60 * 1000, // 15 minutes
max: 100, // limit each IP to 100 requests per windowMs
});

const router = express.Router();

// Admin routes
router.get("/admin", verifyToken, isAdmin, getAllPosts);
router.post("/admin", verifyToken, isAdmin, upload.single("image"), createPost);
router.post("/admin", limiter, verifyToken, isAdmin, upload.single("image"), createPost);
router.get("/admin/:postId", verifyToken, isAdmin, getPostById);
router.put("/admin/:postId", verifyToken, isAdmin, updatePostById);
router.delete("/admin/:postId", verifyToken, isAdmin, deletePostById);
Expand Down

0 comments on commit c4eae41

Please sign in to comment.