Skip to content

[Bug Fix] Enforce author ownership check for updating and deleting blogs - #22

Open
JyantiM wants to merge 2 commits into
aaditya-dubey09:mainfrom
JyantiM:fix/21-blog-ownership-check
Open

[Bug Fix] Enforce author ownership check for updating and deleting blogs#22
JyantiM wants to merge 2 commits into
aaditya-dubey09:mainfrom
JyantiM:fix/21-blog-ownership-check

Conversation

@JyantiM

@JyantiM JyantiM commented Aug 2, 2026

Copy link
Copy Markdown

Description

This is a Bug Fix.

The goal of this change is to secure the blog update and delete endpoints by ensuring that only the original author of a blog can modify or delete it. Previously, these routes only checked if the user was authenticated and if the blog existed. This meant any logged in user could potentially manipulate another user's blog.

I updated the logic to fetch the blog first and verify that the requesting user's ID matches the blog author's ID. If they do not match, the API now correctly returns a 403 Unauthorized error. This brings the security of these endpoints in line with the existing comment deletion logic.

Related Issues

Closes #21

PR Type & Checklist

  • I have read and followed the project's CONTRIBUTING.md guidelines.
  • My PR title follows the convention: [TYPE] Description (e.g., [Feat] Add Dark Mode Toggle).
  • I have ensured this PR is tightly scoped (one feature/fix per PR).

Code & Testing

  • My changes are clean, well-commented, and logically organized.
  • New components or functions include necessary JSDoc/docstrings.
  • I have tested my changes locally.
  • If this is a UI change, I have tested responsiveness on common screen sizes (mobile/desktop).

Security & Architecture (Crucial)

  • This PR does not introduce any hardcoded secrets (API keys, tokens, passwords). All secrets must be loaded from environment variables.
  • If introducing a new API endpoint, it includes basic security (validation, authentication, authorization, or rate limiting).
  • My changes integrate correctly into the existing MERN/Next.js architecture (e.g., no second, standalone Express servers).

📸 Screenshots / Demos (If applicable)

Not applicable for this backend security fix.

Summary by CodeRabbit

  • Bug Fixes
    • Added clearer responses when a requested blog post does not exist.
    • Prevented unauthorized users from updating or deleting blog posts.
    • Ensured blog changes are applied only after ownership is verified.

@vercel

vercel Bot commented Aug 2, 2026

Copy link
Copy Markdown

@jyantimalan is attempting to deploy a commit to the Aaditya's projects Team on Vercel.

A member of the Team first needs to authorize it.

@coderabbitai

coderabbitai Bot commented Aug 2, 2026

Copy link
Copy Markdown

Review Change Stack

Warning

Review limit reached

@JyantiM, you've reached your PR review limit, so we couldn't start this review.

Next review available in: 52 minutes

Enable usage-based reviews in Billing to review now. Otherwise, wait until the next included review is available.
You're only billed for reviews past your plan's rate limits ($0.25/file).

How can I continue?

After more reviews become available, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

To avoid repeated limits, reduce automatic review volume by pausing incremental auto-reviews earlier, using label-based review opt-in, excluding WIP or generated PR titles, or requesting reviews manually when the PR is ready. If your team needs uninterrupted high-volume reviews, an organization admin can enable usage-based reviews.

How do review limits work?

CodeRabbit enforces per-developer PR review limits for each organization. Most developers receive the normal plan review availability.

For paid Pro and Pro+ PR reviews, CodeRabbit uses adaptive limits for sustained high-volume activity. When a developer's recent PR review activity reaches the 95th percentile or higher among CodeRabbit users, additional reviews become available more gradually as earlier reviews age out of the rolling window.

Please refer docs for additional details.

Review details
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: 9cc62c6d-991d-4204-9413-2051c1a748cb

📥 Commits

Reviewing files that changed from the base of the PR and between 5a0f672 and 4c0b8c1.

📒 Files selected for processing (1)
  • server/controllers/blogs.controller.js
📝 Walkthrough

Walkthrough

updateBlog and deleteBlog now fetch the target blog before mutation. Both handlers return 404 when the blog does not exist and 403 when the requesting user is not the author.

Changes

Blog ownership validation

Layer / File(s) Summary
Validate ownership before blog mutations
server/controllers/blogs.controller.js
updateBlog and deleteBlog validate blog existence and author ownership before performing mutations. Unauthorized requests return 403.

Estimated code review effort: 2 (Simple) | ~10 minutes

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly describes the ownership checks added to blog update and delete operations.
Linked Issues check ✅ Passed The changes address issue #21 by checking blog ownership and returning 403 for unauthorized update or delete requests.
Out of Scope Changes check ✅ Passed The changes are limited to blog retrieval, ownership validation, and guarded update or delete operations.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
✨ Finishing Touches 💡 1
🛠️ Fix failing CI checks 💡
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

🧹 Nitpick comments (1)
server/controllers/blogs.controller.js (1)

38-67: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win

Add regression tests for both mutation endpoints.

Cover these cases for updateBlog and deleteBlog:

  • Blog author receives 200.
  • Missing blog receives 404.
  • Authenticated non-owner receives 403.
  • Missing-author records receive a controlled response.
  • Concurrent ownership changes do not permit stale mutations.

Assert the persisted blog state, not only the response status.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@server/controllers/blogs.controller.js` around lines 38 - 67, Add regression
tests for the updateBlog and deleteBlog controller endpoints covering successful
author mutations (200), missing blogs (404), non-owner access (403), records
without an author returning a controlled response, and concurrent ownership
changes preventing stale mutations. Verify the database state after each
mutation rather than asserting response status alone.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@server/controllers/blogs.controller.js`:
- Around line 41-43: The authorization checks in updateBlog
(server/controllers/blogs.controller.js:41-43) and deleteBlog
(server/controllers/blogs.controller.js:62-64) must safely handle blogs without
an author; add a null-safe guard before calling toString while preserving the
unauthorized response. Migrate existing blog documents to populate author, and
update the blog schema in server/models/blogs.model.js to mark author as
required.
- Around line 45-49: Update updateBlog and deleteBlog to perform ownership
checks atomically by replacing _id-only mutations with owner-filtered
findOneAndUpdate and findOneAndDelete queries using the blog ID and
authenticated owner. In both methods, return a 404 when the mutation returns
null, while preserving the existing success responses for a returned blog or
successful deletion.

---

Nitpick comments:
In `@server/controllers/blogs.controller.js`:
- Around line 38-67: Add regression tests for the updateBlog and deleteBlog
controller endpoints covering successful author mutations (200), missing blogs
(404), non-owner access (403), records without an author returning a controlled
response, and concurrent ownership changes preventing stale mutations. Verify
the database state after each mutation rather than asserting response status
alone.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: 9de0d6e8-a09f-4686-baa6-9abec32a381c

📥 Commits

Reviewing files that changed from the base of the PR and between e934574 and 5a0f672.

📒 Files selected for processing (1)
  • server/controllers/blogs.controller.js

Comment thread server/controllers/blogs.controller.js Outdated
Comment thread server/controllers/blogs.controller.js Outdated
@JyantiM

JyantiM commented Aug 2, 2026

Copy link
Copy Markdown
Author

Hi! I've fully resolved the issue and incorporated the CodeRabbit AI feedback. However, the Vercel check is currently blocked could a team member please authorize the Vercel deployment so the checks can finish running? Thanks!

@vercel

vercel Bot commented Aug 2, 2026

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
devodyssey Ready Ready Preview Aug 2, 2026 6:26pm

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Missing author-ownership check in updateBlog and deleteBlog allows any authenticated user to modify/delete other users' blogs

2 participants