Fix: Add authentication and ownership checks to health-data [id] API routes - #107
Open
RohithVangalla1 wants to merge 3 commits into
Open
Conversation
The GET, PATCH, and DELETE endpoints for individual health data records had no authentication or authorization checks. Any unauthenticated request with a valid record ID could read, modify, or delete any user's health data — a critical security vulnerability for a health application. This change: - Adds session authentication check (returns 401 if not logged in) - Adds ownership verification using authorId (returns 404 if the record doesn't belong to the current user, preventing IDOR attacks) - Uses findFirst with authorId filter instead of findUniqueOrThrow to avoid leaking existence of other users' records via error messages The parent route (/api/health-data) already had these checks — this brings the [id] sub-route to the same security standard.
There was a problem hiding this comment.
Code Review
This pull request introduces authentication and ownership checks to the GET, PATCH, and DELETE endpoints for health data by ID. The review feedback highlights a potential mass assignment vulnerability in the PATCH endpoint where the request body is passed directly to the update query, and suggests optimizing the DELETE endpoint by combining the ownership check and deletion into a single deleteMany database operation.
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The GET, PATCH, and DELETE endpoints for individual health data records had no authentication or authorization checks. Any unauthenticated request with a valid record ID could read, modify, or delete any user's health data — a critical security vulnerability for a health application.
This change:
The parent route (/api/health-data) already had these checks — this brings the [id] sub-route to the same security standard.
name: Add authentication and ownership checks to health-data [id] routes
about: Adds missing authentication and authorization checks to the
/api/health-data/[id]GET, PATCH, and DELETE endpoints.
What's changed?
The individual health data record endpoints had no authentication or authorization
checks. Any unauthenticated request with a valid record ID could:
This is a critical security vulnerability (IDOR - Insecure Direct Object Reference)
for a health application handling sensitive patient data.
The parent route (
/api/health-data) already has properauth()checks — thissub-route was missing them.
Fix
auth()session check → returns 401 if not logged inauthorIdfilter → returns 404 if the recorddoesn't belong to the current user
findFirstwithauthorIdinstead offindUniqueOrThrowto avoid leakingexistence of other users' records
Type of change
✨ Screenshots (optional)