-
Notifications
You must be signed in to change notification settings - Fork 117
fix(ask-ai): reject oversized payloads before parsing to prevent memory exhaustion #502
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -198,8 +198,24 @@ async function callGroqWithFallback( | |
| /** | ||
| * Main AI Solver API Route. | ||
| */ | ||
| // Maximum allowed Content-Length for this route in bytes. | ||
| // imageBase64 payloads are read entirely into memory before the slice, so a | ||
| // large body allocates the full payload on the server heap before any truncation | ||
| // occurs. Enforcing this limit early keeps memory usage predictable under load. | ||
| const MAX_BODY_BYTES = 512 * 1024; // 512 KB | ||
|
|
||
| export async function POST(req: Request) { | ||
| try { | ||
| // Reject oversized payloads before parsing JSON to avoid loading | ||
| // a multi-megabyte body into memory only to discard most of it. | ||
| const contentLength = parseInt(req.headers.get('content-length') ?? '0', 10); | ||
| if (contentLength > MAX_BODY_BYTES) { | ||
| return NextResponse.json( | ||
| { error: 'Request payload too large. Maximum size is 512 KB.' }, | ||
| { status: 413 } | ||
|
Comment on lines
+205
to
+215
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🟠 Architect Review — HIGH The new 512 KB server-side body limit for /api/ask-ai reduces the maximum accepted image payload well below what the Ask AI UI promises ("Max 10MB") and below what the client currently enforces, so typical camera-sized uploads that previously worked will now be rejected with 413 while the UI still suggests larger files are acceptable. Suggestion: Align the backend limit with the product flow by either increasing the server limit or, if 512 KB is required, adding client-side file-size enforcement/compression plus updated UX copy and explicit handling of 413 responses in src/app/ask-ai/page.tsx so users are warned before upload and see a clear size-related error. Fix in Cursor | Fix in VSCode Claude (Use Cmd/Ctrl + Click for best experience) Prompt for AI Agent 🤖This is an **Architect / Logical Review** comment left during a code review. These reviews are first-class, important findings — not optional suggestions. Do NOT dismiss this as a 'big architectural change' just because the title says architect review; most of these can be resolved with a small, localized fix once the intent is understood.
**Path:** src/app/api/ask-ai/route.ts
**Line:** 205:215
**Comment:**
*HIGH: The new 512 KB server-side body limit for /api/ask-ai reduces the maximum accepted image payload well below what the Ask AI UI promises ("Max 10MB") and below what the client currently enforces, so typical camera-sized uploads that previously worked will now be rejected with 413 while the UI still suggests larger files are acceptable.
Validate the correctness of the flagged issue. If correct, How can I resolve this? If you propose a fix, implement it and please make it concise.
If a suggested approach is provided above, use it as the authoritative instruction. If no explicit code suggestion is given, you MUST still draft and apply your own minimal, localized fix — do not punt back with 'no suggestion provided, review manually'. Keep the change as small as possible: add a guard clause, gate on a loading state, reorder an await, wrap in a conditional, etc. Do not refactor surrounding code or expand scope beyond the finding.
Once fix is implemented, also check other comments on the same PR, and ask user if the user wants to fix the rest of the comments as well. if said yes, then fetch all the comments validate the correctness and implement a minimal fix |
||
| ); | ||
| } | ||
|
|
||
| const user = await currentUser(); | ||
|
|
||
| if (!user) { | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Suggestion: This size guard trusts the client-supplied
Content-Lengthheader, so a request sent with chunked transfer encoding (noContent-Length) or a missing header bypasses the check and still reachesreq.json(), allowing large bodies to be fully buffered in memory. Enforce the limit based on actual bytes read from the request stream (or reject requests without a valid length for this endpoint) so oversized payloads cannot bypass the protection. [security]Severity Level: Major⚠️
Steps of Reproduction ✅
Fix in Cursor | Fix in VSCode Claude
(Use Cmd/Ctrl + Click for best experience)
Prompt for AI Agent 🤖