mufidu / booku

0 stars 1 forks source link

Sweep: Handle non validated request in books endpoint #148

Closed mufidu closed 5 months ago

mufidu commented 5 months ago

Details

In the POST ("/") and PUT ("/:id") routes, there's no validation of req.body. If any of the required fields (title, author, year, category, cover) are missing or invalid, this could lead to unexpected behavior or errors when trying to save or update the book.

Fix it.

Checklist - [X] Modify `routes/book.routes.js` ✓ https://github.com/mufidu/booku/commit/673f9da34b571d392cc6036e7d20559e04a37827 [Edit](https://github.com/mufidu/booku/edit/sweep/handle_non_validated_request_in_books_en/routes/book.routes.js) - [X] Running GitHub Actions for `routes/book.routes.js` ✓ [Edit](https://github.com/mufidu/booku/edit/sweep/handle_non_validated_request_in_books_en/routes/book.routes.js)
sweep-ai[bot] commented 5 months ago

🚀 Here's the PR! #149

See Sweep's progress at the progress dashboard!
💎 Sweep Pro: I'm using GPT-4. You have unlimited GPT-4 tickets. (tracking ID: be4147a247)

[!TIP] I can email you next time I complete a pull request if you set up your email here!


Actions (click)


Step 1: 🔎 Searching

I found the following snippets in your repository. I will now analyze these snippets and come up with a plan.

Some code snippets I think are relevant in decreasing order of relevance (click to expand). If some file is missing from here, you can mention the path in the ticket description. https://github.com/mufidu/booku/blob/eb4e56883d6d98d3d4a2027cfc09087ba915ae0d/routes/book.routes.js#L1-L107 https://github.com/mufidu/booku/blob/eb4e56883d6d98d3d4a2027cfc09087ba915ae0d/utils/validateInput.js#L1-L30

Step 2: ⌨️ Coding

--- 
+++ 
@@ -26,6 +26,9 @@
 // Create a new book
 router.post("/", async (req, res) => {
     let { title, author, year, category, cover } = req.body;
+    if (!title || !author || !year || !category || !cover || !validateInput('year', year)) {
+        return res.status(400).json({ message: 'Missing or invalid required field' });
+    }
     const book = new Book({ title, author, year, category, cover });
     try {
         await book.save();
@@ -49,6 +52,9 @@
 router.put('/:id', async (req, res) => {
     const { id } = req.params;
     const { title, author, year, category, cover } = req.body;
+    if (!title || !author || !year || !category || !cover || !validateInput('year', year)) {
+        return res.status(400).json({ message: 'Missing or invalid required field' });
+    }

     try {
         const book = await Book.findByIdAndUpdate(id,

Ran GitHub Actions for 673f9da34b571d392cc6036e7d20559e04a37827:


Step 3: 🔁 Code Review

I have finished reviewing the code for completeness. I did not find errors for sweep/handle_non_validated_request_in_books_en.


🎉 Latest improvements to Sweep:
  • New dashboard launched for real-time tracking of Sweep issues, covering all stages from search to coding.
  • Integration of OpenAI's latest Assistant API for more efficient and reliable code planning and editing, improving speed by 3x.
  • Use the GitHub issues extension for creating Sweep issues directly from your editor.

💡 To recreate the pull request edit the issue title or description. Something wrong? Let us know.

This is an automated message generated by Sweep AI.