feat: Student Management System — Express + Mongoose with dual MongoDB connections#33
Conversation
- Express.js REST API with full CRUD for Students and Teachers - Dual Mongoose connections: student-mongo-db and teacher-mongo-db - Student model: name, email, grade, subjects, address, phone - Teacher model: name, email, department, qualification, experience, salary - Pagination, filtering, and search endpoints for both resources - CORS and JSON parsing middleware configured - Environment-based configuration via .env
🤖 Devin AI EngineerI'll be helping with this pull request! Here's what you should know: ✅ I will automatically:
Note: I can only respond to comments from users who have write access to this repository. ⚙️ Control Options:
|
| const students = await Student.find({ | ||
| $or: [ | ||
| { firstName: { $regex: q, $options: "i" } }, | ||
| { lastName: { $regex: q, $options: "i" } }, | ||
| { email: { $regex: q, $options: "i" } }, | ||
| ], | ||
| }); |
There was a problem hiding this comment.
🔴 Unsanitized user input passed to MongoDB $regex allows ReDoS and regex injection
The q query parameter from the user is passed directly as the pattern to MongoDB's $regex operator without any escaping of special regex characters. A malicious user can craft a search query containing regex metacharacters (e.g., (a+)+$, .*, (?:) that cause catastrophic backtracking (ReDoS) on the MongoDB server, or unintended broad matching. For example, GET /api/students/search?q=(a%2B)%2B%24 would send a ReDoS pattern. The same issue exists in teacher.controller.js:146-153.
| const students = await Student.find({ | |
| $or: [ | |
| { firstName: { $regex: q, $options: "i" } }, | |
| { lastName: { $regex: q, $options: "i" } }, | |
| { email: { $regex: q, $options: "i" } }, | |
| ], | |
| }); | |
| const escapedQ = q.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"); | |
| const students = await Student.find({ | |
| $or: [ | |
| { firstName: { $regex: escapedQ, $options: "i" } }, | |
| { lastName: { $regex: escapedQ, $options: "i" } }, | |
| { email: { $regex: escapedQ, $options: "i" } }, | |
| ], | |
| }); |
Was this helpful? React with 👍 or 👎 to provide feedback.
| const teachers = await Teacher.find({ | ||
| $or: [ | ||
| { firstName: { $regex: q, $options: "i" } }, | ||
| { lastName: { $regex: q, $options: "i" } }, | ||
| { email: { $regex: q, $options: "i" } }, | ||
| { department: { $regex: q, $options: "i" } }, | ||
| ], | ||
| }); |
There was a problem hiding this comment.
🔴 Unsanitized user input passed to MongoDB $regex in teacher search allows ReDoS and regex injection
Same regex injection vulnerability as in the student controller. The q query parameter is passed directly to $regex in teacher.controller.js:146-153 without escaping special regex characters, enabling ReDoS attacks and unintended pattern matching against teacher records.
| const teachers = await Teacher.find({ | |
| $or: [ | |
| { firstName: { $regex: q, $options: "i" } }, | |
| { lastName: { $regex: q, $options: "i" } }, | |
| { email: { $regex: q, $options: "i" } }, | |
| { department: { $regex: q, $options: "i" } }, | |
| ], | |
| }); | |
| const escapedQ = q.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"); | |
| const teachers = await Teacher.find({ | |
| $or: [ | |
| { firstName: { $regex: escapedQ, $options: "i" } }, | |
| { lastName: { $regex: escapedQ, $options: "i" } }, | |
| { email: { $regex: escapedQ, $options: "i" } }, | |
| { department: { $regex: escapedQ, $options: "i" } }, | |
| ], | |
| }); |
Was this helpful? React with 👍 or 👎 to provide feedback.
Summary
Adds a new
student-management-system/Express.js project with full CRUD REST APIs for Students and Teachers, each backed by a separate MongoDB database via Mongoose'screateConnection().student-mongo-db— stores Student documents (name, email, grade, subjects, address, etc.)teacher-mongo-db— stores Teacher documents (name, email, department, qualification, salary, etc.)Both resources expose Create, Read (single + list with pagination/filtering), Update, Delete, and Search endpoints under
/api/studentsand/api/teachers.Key files:
src/config/database.js— dualmongoose.createConnection()setupsrc/models/— Mongoose schemas registered on their respective connectionssrc/controllers/— CRUD + search logic with error handlingsrc/routes/— Express routers wired to controllerssrc/server.js— app entry point with CORS, JSON middleware, 404/error handlersReview & Testing Checklist for Human
package.jsonpins Express^5.2.1which has breaking changes from v4 (error handling middleware, query parsing, etc.). Verify the 4-arg error handler and route patterns work as expected at runtime.qparam is passed directly into$regexwithout escaping special characters — could allow ReDoS or unintended query behavior. Consider adding regex escaping (e.g.,q.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')).node -c), never started against actual MongoDB instances. Runnpm startwith two MongoDB databases available and hit all endpoints with curl to verify..gitignorein subdirectory: No.gitignorewas committed insidestudent-management-system/to excludenode_modules/. Confirm the parent repo's.gitignorecovers this, or add one.package-lock.jsonexcluded: The original project.gitignoreexcludedpackage-lock.json. For reproducible installs, consider committing it.Recommended test plan:
cd student-management-system && npm install.env.exampleto.envand adjust URIs if needednpm start— verify server starts and both DB connections log "Connected"Notes
createConnection()API is stable but worth a quick sanity check if you haven't used v9 before.parseIntis applied inconsistently (limitis parsed,pagein.skip()calculation is not).Link to Devin session: https://partner-workshops.devinenterprise.com/sessions/a4a2e8223c55457eb67c4fabcbe1ef5b