Skip to content

Commit 0d91583

Browse files
committed
fix: security issue, IDOR vulnerability
1 parent d080468 commit 0d91583

File tree

3 files changed

+24
-4
lines changed

3 files changed

+24
-4
lines changed

server/src/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ app.use(express.json({ limit: "200mb" }));
1212
app.use(express.urlencoded({ extended: true, limit: "200mb" }));
1313
app.use(cors());
1414
app.use(express.urlencoded({ extended: false }));
15-
app.use(errorHandler);
1615
app.use("/api/user", require("./routes/userRoutes"));
1716
app.use("/api/bookmark", require("./routes/bookmarkRoutes"));
17+
app.use(errorHandler);
1818

1919
app.listen(5000, () => console.log("Server is running on PORT 5000"));
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { Request, Response, NextFunction } from "express";
2+
import Bookmark from "../models/bookmarkModel";
3+
import { IBookmark } from "../interfaces/BookmarkInterface";
4+
import { ObjectId } from "mongodb";
5+
6+
export const checkBookmark = async (
7+
req: Request,
8+
res: Response,
9+
next: NextFunction
10+
) => {
11+
try {
12+
const bookmark: IBookmark = await Bookmark.findById({ _id: req.params.id });
13+
if (bookmark.user.toString() !== res.locals.user._id)
14+
return res.status(404).send("Bookmark not found!");
15+
next();
16+
} catch (error) {
17+
next(error);
18+
}
19+
};

server/src/routes/bookmarkRoutes.ts

+4-3
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,14 @@ import {
88
singleBookmark,
99
} from "../controllers/bookmarkController";
1010
import { Protected } from "../middleware/authMiddleware";
11+
import { checkBookmark } from "../middleware/bookmarkMiddleware";
1112

1213
router.get("/", Protected, getBookmark);
1314
router.post("/add", Protected, addBookmark);
1415
router
1516
.route("/:id")
16-
.get(singleBookmark)
17-
.put(Protected, updateBookmark)
18-
.delete(Protected, deleteBookmark);
17+
.get(Protected, checkBookmark, singleBookmark)
18+
.put(Protected, checkBookmark, updateBookmark)
19+
.delete(Protected, checkBookmark, deleteBookmark);
1920

2021
module.exports = router;

0 commit comments

Comments
 (0)