Add missing security headers to next config#883
Conversation
|
Someone is attempting to deploy a commit to the Karan Mani Tripathi 's projects Team on Vercel. A member of the Team first needs to authorize it. |
|
CodeAnt AI is reviewing your PR. |
Thanks for using CodeAnt! 🎉We're free for open-source projects. if you're enjoying it, help us grow by sharing. Share on X · |
❌ PR Rejected — Issue Assignment Check FailedHi @saurabhhhcodes! This PR has been closed because you are not assigned to the issue(s) it references:
What to do:
|
|
Caution Review failedThe pull request is closed. ℹ️ Recent review info⚙️ Run configurationConfiguration used: Path: .coderabbit.yaml Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (5)
WalkthroughThe PR adds multiple security headers to Next.js responses and validates video generation image URLs by inspecting fetched file signatures before the request proceeds to job creation. ChangesSecurity header configuration
Video image payload validation
Estimated code review effort: 3 (Moderate) | ~25 minutes Sequence Diagram(s)sequenceDiagram
participant Client
participant VideoGenerateRoute
participant validateVideoImageUrl
participant ImageURL
Client->>VideoGenerateRoute: POST imageUrl
VideoGenerateRoute->>validateVideoImageUrl: validate image URL
validateVideoImageUrl->>ImageURL: fetch image bytes
ImageURL-->>validateVideoImageUrl: response payload
validateVideoImageUrl-->>VideoGenerateRoute: validation result
VideoGenerateRoute-->>Client: 422 error or continue processing
Possibly related issues
Possibly related PRs
Suggested reviewers: ✨ Finishing Touches🧪 Generate unit tests (beta)
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. Comment |
|
|
||
| export async function validateVideoImageUrl(imageUrl: string): Promise<VideoImageValidationResult> { | ||
| try { | ||
| const response = await fetch(imageUrl, { cache: 'no-store' }); |
There was a problem hiding this comment.
Suggestion: This fetches a user-controlled URL directly from the server without any SSRF guardrails (no scheme restriction to HTTPS only, no private-network/localhost/IP blocking, no allowlist). An attacker can submit internal URLs and make the backend probe internal services/metadata endpoints. Add strict outbound URL validation (protocol + hostname/IP checks) before issuing the request. [ssrf]
Severity Level: Critical 🚨
❌ /api/video/generate can fetch attacker-chosen internal URLs.
❌ Backend may reach cloud metadata or admin services.
⚠️ Potential pivot to internal network reconnaissance.Steps of Reproduction ✅
1. Start the DoubtDesk Next.js app so that the API route handler in
`src/app/api/video/generate/route.ts:23-110` is active, exposing `POST
/api/video/generate`.
2. From a client, send `POST /api/video/generate` with JSON body `{ "content": null,
"imageUrl": "http://127.0.0.1:8080/internal-status" }`. The body is parsed and validated
by `parseAndValidateRequest` in `src/lib/validations/validate.ts:5-27` using
`generateVideoSchema` in `src/lib/validations/video.ts:4-7`, which applies `safeUrl` from
`src/lib/validations/common.ts:3-5` (only syntax + length checks, no SSRF filtering).
3. In `src/app/api/video/generate/route.ts:46-48`, the handler sees a non-null
`data.imageUrl` and calls `validateVideoImageUrl(data.imageUrl)`, passing the
attacker-controlled URL to `validateVideoImageUrl` implemented in
`src/lib/video/image-validation.ts:49-82`.
4. Inside `validateVideoImageUrl`, line `51 const response = await fetch(imageUrl, {
cache: 'no-store' });` issues a server-side `fetch` to
`http://127.0.0.1:8080/internal-status` (or any attacker-chosen internal/metadata
endpoint). There are no scheme, hostname, or private-network restrictions, so the backend
will attempt to reach internal services, demonstrating an SSRF capability even though only
a validation result (not the full response body) is returned to the client.(Use Cmd/Ctrl + Click for best experience)
Prompt for AI Agent 🤖
This is a comment left during a code review.
**Path:** src/lib/video/image-validation.ts
**Line:** 51:51
**Comment:**
*Ssrf: This fetches a user-controlled URL directly from the server without any SSRF guardrails (no scheme restriction to HTTPS only, no private-network/localhost/IP blocking, no allowlist). An attacker can submit internal URLs and make the backend probe internal services/metadata endpoints. Add strict outbound URL validation (protocol + hostname/IP checks) before issuing the request.
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.
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 bytes = new Uint8Array(await response.arrayBuffer()); |
There was a problem hiding this comment.
Suggestion: This reads the entire remote response into memory with no size cap, so a large payload can cause excessive memory usage or process instability before validation completes. Enforce a strict maximum download size (via Content-Length checks plus streamed byte limits) and reject oversized payloads early. [performance]
Severity Level: Major ⚠️
❌ Large remote images can OOM Next.js video API.
⚠️ Video generation requests can stall under resource exhaustion.
⚠️ Resource spikes may degrade other API endpoints.Steps of Reproduction ✅
1. Run the DoubtDesk Next.js backend so that the video generation API handler in
`src/app/api/video/generate/route.ts:23-110` is exposed as `POST /api/video/generate`.
2. Host a very large file (e.g., >500MB) at an attacker-controlled URL such as
`https://attacker.example/huge.bin`, and send `POST /api/video/generate` with body `{
"content": null, "imageUrl": "https://attacker.example/huge.bin" }`. The request body is
accepted by `parseAndValidateRequest` (`src/lib/validations/validate.ts:5-27`) and
`generateVideoSchema` (`src/lib/validations/video.ts:4-7`) as long as the URL is
syntactically valid.
3. In `src/app/api/video/generate/route.ts:46-48`, the handler calls
`validateVideoImageUrl(data.imageUrl)`. Inside `validateVideoImageUrl`
(`src/lib/video/image-validation.ts:49-82`), line `51 const response = await
fetch(imageUrl, { cache: 'no-store' });` starts downloading the large file from the remote
server.
4. At `src/lib/video/image-validation.ts:61`, `const bytes = new Uint8Array(await
response.arrayBuffer());` reads the entire response body into an `ArrayBuffer` with no
`Content-Length` check and no byte-size cap, unlike the explicit `AI_IMAGE_MAX_BYTES`
limit enforced in `src/lib/ai/ai-image-validation.ts:1-3,102-110`. For very large
payloads, this can cause excessive memory usage or process instability (OOM or severe GC
thrashing) in the Next.js server handling `/api/video/generate`.(Use Cmd/Ctrl + Click for best experience)
Prompt for AI Agent 🤖
This is a comment left during a code review.
**Path:** src/lib/video/image-validation.ts
**Line:** 61:61
**Comment:**
*Performance: This reads the entire remote response into memory with no size cap, so a large payload can cause excessive memory usage or process instability before validation completes. Enforce a strict maximum download size (via `Content-Length` checks plus streamed byte limits) and reject oversized payloads early.
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.
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|
CodeAnt AI finished reviewing your PR. |
User description
Closes #874\n\nAdds the security headers called out in the issue to the global Next.js headers config: CSP, nosniff, referrer policy, permissions policy, and HSTS in production.\n\nValidation:\n- git diff --check\n- node --check next.config.mjs
CodeAnt-AI Description
Add security headers and reject invalid video image uploads
What Changed
Impact
✅ Safer page loading✅ Fewer failed video jobs from bad image uploads✅ Clearer upload errors💡 Usage Guide
Checking Your Pull Request
Every time you make a pull request, our system automatically looks through it. We check for security issues, mistakes in how you're setting up your infrastructure, and common code problems. We do this to make sure your changes are solid and won't cause any trouble later.
Talking to CodeAnt AI
Got a question or need a hand with something in your pull request? You can easily get in touch with CodeAnt AI right here. Just type the following in a comment on your pull request, and replace "Your question here" with whatever you want to ask:
This lets you have a chat with CodeAnt AI about your pull request, making it easier to understand and improve your code.
Example
Preserve Org Learnings with CodeAnt
You can record team preferences so CodeAnt AI applies them in future reviews. Reply directly to the specific CodeAnt AI suggestion (in the same thread) and replace "Your feedback here" with your input:
This helps CodeAnt AI learn and adapt to your team's coding style and standards.
Example
Retrigger review
Ask CodeAnt AI to review the PR again, by typing:
Check Your Repository Health
To analyze the health of your code repository, visit our dashboard at https://app.codeant.ai. This tool helps you identify potential issues and areas for improvement in your codebase, ensuring your repository maintains high standards of code health.
Summary by CodeRabbit
New Features
Bug Fixes