fix: add missing upload mime type validation#7
Conversation
|
@JustinBenito can you please review it. |
|
Hi , I can see a lot changes , can add what was the existing backend , what you have changed and why do you think that would better ? |
What Was the Existing Backend?Backend API (
|
|
@sharathlingam, the project is running on a free tier in Vercel. I doubt that adding strict validation and restricting the upload will eat compute. Do you have any other approach to solve the issue? |
What about we validate the mime type only in the client side and revert the server side validation to the previous state, the one which validates only the size and the file type? |
| const ALLOWED_TYPES = ['image/jpeg', 'image/jpg', 'image/png', 'image/gif', 'image/webp']; | ||
| const MAX_FILE_SIZE = 10 * 1024 * 1024; | ||
| import { nanoid } from "nanoid"; | ||
| import { type NextRequest, NextResponse } from "next/server"; | ||
| import { generatePresignedUpload } from "@/lib/r2-client"; | ||
| import { validateImageFile } from "@/lib/utils"; | ||
|
|
||
| export async function POST(request: NextRequest) { | ||
| try { | ||
| const body = await request.json(); | ||
| const { contentType, size } = body; | ||
|
|
||
| if (!contentType) { | ||
| return NextResponse.json({ error: 'Content type is required' }, { status: 400 }); | ||
| } | ||
|
|
||
| if (!ALLOWED_TYPES.includes(contentType)) { | ||
| return NextResponse.json( | ||
| { error: 'Invalid file type. Only JPG, PNG, GIF, and WebP are allowed.' }, | ||
| { status: 400 } | ||
| ); | ||
| } | ||
|
|
||
| if (size > MAX_FILE_SIZE) { | ||
| return NextResponse.json( | ||
| { error: 'File too large. Maximum size is 10MB.' }, | ||
| { status: 400 } | ||
| ); | ||
| } |
There was a problem hiding this comment.
isnt this validation already?
Fixes: #3