-
Notifications
You must be signed in to change notification settings - Fork 23
fix: enforce security, validation, runtime stability, and dashboard correctness #8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
ArhanAnsari
wants to merge
24
commits into
JavaScript-Mastery-Pro:main
Choose a base branch
from
ArhanAnsari:fix/bughunt-arhan
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
24 commits
Select commit
Hold shift + click to select a range
58148e0
fix: Refactor MongoDB connection logic and enhance seed script diagno…
ArhanAnsari f61c3ba
fix: Update background gradient class for sign-in and sign-up pages
ArhanAnsari a6ae510
fix: Enhance PUT and DELETE endpoints for announcements with improved…
ArhanAnsari 936e24c
fix: Improve error handling and authorization checks in PUT and DELET…
ArhanAnsari 511f19a
fix: Refactor and enhance error handling in GET and POST endpoints fo…
ArhanAnsari eefe3a9
fix: Enhance error handling and validation in PUT and DELETE endpoint…
ArhanAnsari 316f41f
fix: Refactor GET and PUT endpoints in profile API with improved erro…
ArhanAnsari 7ed61ef
fix: Improve formatting and error handling in GET and POST endpoints …
ArhanAnsari c9b3530
fix: Enhance error handling and authorization checks in PUT and DELET…
ArhanAnsari 78b16e0
fix: Simplify error response in GET assignments endpoint
ArhanAnsari 10e65c4
fix: Validate studentId format in Attendance schema and query
ArhanAnsari b16a97e
fix: Validate studentId format in Grade schema and handle invalid cas…
ArhanAnsari d7afc42
fix: Improve error handling for dashboard data loading and adjust tot…
ArhanAnsari 2eece6f
fix: Format GRADE_POINT for improved readability
ArhanAnsari 8d95d84
fix: Format GRADE_POINT object for improved readability
ArhanAnsari bf49f6f
fix: apply PR review thread feedback across APIs and dashboard
Copilot 9b0e536
chore: finalize validation feedback
Copilot 3c45899
fix: keep Next.js route signature while avoiding unused warning
Copilot 4cc36a8
chore: close remaining code review nits
Copilot d2fb705
fix: resolve remaining validation nits in profile and attendance routes
Copilot 16f8467
chore: document strict objectid validation and keep request signature…
Copilot dbcc99b
fix: remove redundant upsert fields and avoid unnecessary grade lookup
Copilot e1063a7
fix: guard non-object payloads and enforce effective maxMarks validation
Copilot 1e55bb3
Merge pull request #1 from ArhanAnsari/copilot/fix-security-validatio…
ArhanAnsari File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,79 +1,115 @@ | ||
| import { auth } from '@clerk/nextjs/server' | ||
| import { NextRequest, NextResponse } from 'next/server' | ||
| import mongoose from 'mongoose' | ||
| import { connectDB } from '@/lib/mongodb' | ||
| import { Announcement } from '@/models/Announcement' | ||
| import { auth } from "@clerk/nextjs/server"; | ||
| import { NextRequest, NextResponse } from "next/server"; | ||
| import mongoose from "mongoose"; | ||
| import { connectDB } from "@/lib/mongodb"; | ||
| import { Announcement } from "@/models/Announcement"; | ||
|
|
||
| const ALLOWED_FIELDS = ['title', 'content', 'body', 'audience', 'category', 'pinned', 'expiresAt'] | ||
| const ALLOWED_FIELDS = [ | ||
| "title", | ||
| "content", | ||
| "body", | ||
| "audience", | ||
| "category", | ||
| "pinned", | ||
| "expiresAt", | ||
| ]; | ||
|
|
||
| export async function PUT(req: NextRequest, ctx: { params: Promise<{ id: string }> }) { | ||
| const { userId } = await auth() | ||
| if (!userId) return NextResponse.json({ error: 'Unauthorized' }, { status: 401 }) | ||
| export async function PUT( | ||
| req: NextRequest, | ||
| ctx: { params: Promise<{ id: string }> }, | ||
| ) { | ||
| const { userId } = await auth(); | ||
| if (!userId) | ||
| return NextResponse.json({ error: "Unauthorized" }, { status: 401 }); | ||
|
|
||
| try { | ||
| const { id } = await ctx.params | ||
| const { id } = await ctx.params; | ||
|
|
||
| // Validate ObjectId | ||
| if (!mongoose.Types.ObjectId.isValid(id)) { | ||
| return NextResponse.json({ error: 'Invalid id' }, { status: 400 }) | ||
| return NextResponse.json({ error: "Invalid id" }, { status: 400 }); | ||
| } | ||
|
|
||
| await connectDB() | ||
| let body | ||
| await connectDB(); | ||
|
|
||
| let body; | ||
| try { | ||
| body = await req.json() | ||
| body = await req.json(); | ||
| } catch { | ||
| return NextResponse.json({ error: 'Invalid JSON request body' }, { status: 400 }) | ||
| return NextResponse.json( | ||
| { error: "Invalid JSON request body" }, | ||
| { status: 400 }, | ||
| ); | ||
| } | ||
| if (body === null || typeof body !== "object" || Array.isArray(body)) { | ||
| // Valid JSON can still be a primitive/null/array, but this route requires an object payload. | ||
| return NextResponse.json( | ||
| { error: "Invalid JSON request body" }, | ||
| { status: 400 }, | ||
| ); | ||
| } | ||
|
|
||
| // Sanitize: only allow whitelisted fields | ||
| const sanitizedBody: Record<string, unknown> = {} | ||
| const sanitizedBody: Record<string, unknown> = {}; | ||
| for (const key of ALLOWED_FIELDS) { | ||
| if (key in body) { | ||
| sanitizedBody[key] = body[key] | ||
| sanitizedBody[key] = body[key]; | ||
| } | ||
| } | ||
|
|
||
| const announcement = await Announcement.findOneAndUpdate( | ||
| { _id: id }, | ||
| { _id: id, teacherId: userId }, | ||
| { $set: sanitizedBody }, | ||
| { new: true, runValidators: true, context: 'query' } | ||
| ) | ||
| if (!announcement) return NextResponse.json({ error: 'Not found' }, { status: 404 }) | ||
| return NextResponse.json(announcement) | ||
| { new: true, runValidators: true, context: "query" }, | ||
| ); | ||
| if (!announcement) | ||
| return NextResponse.json({ error: "Not found" }, { status: 404 }); | ||
| return NextResponse.json(announcement); | ||
| } catch (error) { | ||
| if (error instanceof Error) { | ||
| console.error('PUT /api/announcements/[id] error:', error.message) | ||
| console.error("PUT /api/announcements/[id] error:", error.message); | ||
| } | ||
| return NextResponse.json({ error: 'Internal server error' }, { status: 500 }) | ||
| return NextResponse.json( | ||
| { error: "Internal server error" }, | ||
| { status: 500 }, | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| export async function DELETE(_req: NextRequest, ctx: { params: Promise<{ id: string }> }) { | ||
| const { userId } = await auth() | ||
| if (!userId) return NextResponse.json({ error: 'Unauthorized' }, { status: 401 }) | ||
| export async function DELETE( | ||
| _req: NextRequest, | ||
| ctx: { params: Promise<{ id: string }> }, | ||
| ) { | ||
| const { userId } = await auth(); | ||
| if (!userId) | ||
| return NextResponse.json({ error: "Unauthorized" }, { status: 401 }); | ||
|
|
||
| try { | ||
| const { id } = await ctx.params | ||
| const { id } = await ctx.params; | ||
|
|
||
| // Validate ObjectId | ||
| if (!mongoose.Types.ObjectId.isValid(id)) { | ||
| return NextResponse.json({ error: 'Invalid id' }, { status: 400 }) | ||
| return NextResponse.json({ error: "Invalid id" }, { status: 400 }); | ||
| } | ||
|
|
||
| await connectDB() | ||
| const deleted = await Announcement.findOneAndDelete({ _id: id }) | ||
|
|
||
| await connectDB(); | ||
| const deleted = await Announcement.findOneAndDelete({ | ||
| _id: id, | ||
| teacherId: userId, | ||
| }); | ||
|
|
||
| if (!deleted) { | ||
| return NextResponse.json({ error: 'Not found' }, { status: 404 }) | ||
| return NextResponse.json({ error: "Not found" }, { status: 404 }); | ||
| } | ||
| return NextResponse.json({ success: true }) | ||
|
|
||
| return NextResponse.json({ success: true }); | ||
| } catch (error) { | ||
| if (error instanceof Error) { | ||
| console.error('DELETE /api/announcements/[id] error:', error.message) | ||
| console.error("DELETE /api/announcements/[id] error:", error.message); | ||
| } | ||
| return NextResponse.json({ error: 'Internal server error' }, { status: 500 }) | ||
| return NextResponse.json( | ||
| { error: "Internal server error" }, | ||
| { status: 500 }, | ||
| ); | ||
| } | ||
| } | ||
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,79 +1,116 @@ | ||
| import { auth } from '@clerk/nextjs/server' | ||
| import { NextRequest, NextResponse } from 'next/server' | ||
| import mongoose from 'mongoose' | ||
| import { connectDB } from '@/lib/mongodb' | ||
| import { Assignment } from '@/models/Assignment' | ||
| import { auth } from "@clerk/nextjs/server"; | ||
| import { NextRequest, NextResponse } from "next/server"; | ||
| import mongoose from "mongoose"; | ||
| import { connectDB } from "@/lib/mongodb"; | ||
| import { Assignment } from "@/models/Assignment"; | ||
|
|
||
| const ALLOWED_UPDATE_FIELDS = ['title', 'description', 'dueDate', 'deadline', 'subject', 'class', 'status', 'kanbanStatus', 'maxMarks'] | ||
| const ALLOWED_UPDATE_FIELDS = [ | ||
| "title", | ||
| "description", | ||
| "dueDate", | ||
| "deadline", | ||
| "subject", | ||
| "class", | ||
| "status", | ||
| "kanbanStatus", | ||
| "maxMarks", | ||
| ]; | ||
|
|
||
| export async function PUT(req: NextRequest, ctx: { params: Promise<{ id: string }> }) { | ||
| const { userId } = await auth() | ||
| if (!userId) return NextResponse.json({ error: 'Unauthorized' }, { status: 401 }) | ||
| export async function PUT( | ||
| req: NextRequest, | ||
| ctx: { params: Promise<{ id: string }> }, | ||
| ) { | ||
| const { userId } = await auth(); | ||
| if (!userId) | ||
| return NextResponse.json({ error: "Unauthorized" }, { status: 401 }); | ||
|
|
||
| try { | ||
| const { id } = await ctx.params | ||
| const { id } = await ctx.params; | ||
|
|
||
| // Validate ObjectId | ||
| if (!mongoose.Types.ObjectId.isValid(id)) { | ||
| return NextResponse.json({ error: 'Invalid id' }, { status: 400 }) | ||
| return NextResponse.json({ error: "Invalid id" }, { status: 400 }); | ||
| } | ||
|
|
||
| await connectDB() | ||
| let body | ||
| await connectDB(); | ||
|
|
||
| let body; | ||
| try { | ||
| body = await req.json() | ||
| body = await req.json(); | ||
| } catch { | ||
| return NextResponse.json({ error: 'Invalid JSON in request body' }, { status: 400 }) | ||
| return NextResponse.json( | ||
| { error: "Invalid JSON in request body" }, | ||
| { status: 400 }, | ||
| ); | ||
| } | ||
| if (body === null || typeof body !== "object" || Array.isArray(body)) { | ||
| return NextResponse.json( | ||
| { error: "Invalid JSON in request body" }, | ||
| { status: 400 }, | ||
| ); | ||
| } | ||
|
|
||
| // Sanitize: only allow whitelisted fields | ||
| const sanitizedBody: Record<string, unknown> = {} | ||
| const sanitizedBody: Record<string, unknown> = {}; | ||
| for (const key of ALLOWED_UPDATE_FIELDS) { | ||
| if (key in body) { | ||
| sanitizedBody[key] = body[key] | ||
| sanitizedBody[key] = body[key]; | ||
| } | ||
| } | ||
|
|
||
| const assignment = await Assignment.findOneAndUpdate( | ||
| { _id: id }, | ||
| { _id: id, teacherId: userId }, | ||
| sanitizedBody, | ||
| { new: true } | ||
| ) | ||
| if (!assignment) return NextResponse.json({ error: 'Not found' }, { status: 404 }) | ||
| return NextResponse.json(assignment) | ||
| { new: true, runValidators: true, context: "query" }, | ||
| ); | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
| if (!assignment) | ||
| return NextResponse.json({ error: "Not found" }, { status: 404 }); | ||
| return NextResponse.json(assignment); | ||
| } catch (error) { | ||
| if (error instanceof Error) { | ||
| console.error('PUT /api/assignments/[id] error:', error.message) | ||
| console.error("PUT /api/assignments/[id] error:", error.message); | ||
| } | ||
| return NextResponse.json({ error: 'Internal server error' }, { status: 500 }) | ||
| return NextResponse.json( | ||
| { error: "Internal server error" }, | ||
| { status: 500 }, | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| export async function DELETE(_req: NextRequest, ctx: { params: Promise<{ id: string }> }) { | ||
| const { userId } = await auth() | ||
| if (!userId) return NextResponse.json({ error: 'Unauthorized' }, { status: 401 }) | ||
| export async function DELETE( | ||
| _req: NextRequest, | ||
| ctx: { params: Promise<{ id: string }> }, | ||
| ) { | ||
| const { userId } = await auth(); | ||
| if (!userId) | ||
| return NextResponse.json({ error: "Unauthorized" }, { status: 401 }); | ||
|
|
||
| try { | ||
| const { id } = await ctx.params | ||
| const { id } = await ctx.params; | ||
|
|
||
| // Validate ObjectId | ||
| if (!mongoose.Types.ObjectId.isValid(id)) { | ||
| return NextResponse.json({ error: 'Invalid id' }, { status: 400 }) | ||
| return NextResponse.json({ error: "Invalid id" }, { status: 400 }); | ||
| } | ||
|
|
||
| await connectDB() | ||
| const deleted = await Assignment.findOneAndDelete({ _id: id }) | ||
|
|
||
| await connectDB(); | ||
| const deleted = await Assignment.findOneAndDelete({ | ||
| _id: id, | ||
| teacherId: userId, | ||
| }); | ||
|
|
||
| if (!deleted) { | ||
| return NextResponse.json({ error: 'Not found' }, { status: 404 }) | ||
| return NextResponse.json({ error: "Not found" }, { status: 404 }); | ||
| } | ||
| return NextResponse.json({ success: true }) | ||
|
|
||
| return NextResponse.json({ success: true }); | ||
| } catch (error) { | ||
| if (error instanceof Error) { | ||
| console.error('DELETE /api/assignments/[id] error:', error.message) | ||
| console.error("DELETE /api/assignments/[id] error:", error.message); | ||
| } | ||
| return NextResponse.json({ error: 'Internal server error' }, { status: 500 }) | ||
| return NextResponse.json( | ||
| { error: "Internal server error" }, | ||
| { status: 500 }, | ||
| ); | ||
| } | ||
| } | ||
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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.