Skip to content

🔒 [Fix file upload spoofing vulnerability] - #113

Draft
NITISH-R-G wants to merge 2 commits into
mainfrom
fix-file-upload-spoofing-14649357054211411820
Draft

🔒 [Fix file upload spoofing vulnerability]#113
NITISH-R-G wants to merge 2 commits into
mainfrom
fix-file-upload-spoofing-14649357054211411820

Conversation

@NITISH-R-G

@NITISH-R-G NITISH-R-G commented Jul 12, 2026

Copy link
Copy Markdown
Owner

🎯 What: Implemented robust file upload validation by inspecting magic bytes instead of blindly trusting client-provided mimetypes.
⚠️ Risk: Attackers could spoof file.type (e.g. upload an .exe disguised as a .pdf), bypassing rudimentary checks and executing potentially harmful content.
🛡️ Solution: Integrated the file-type library to asynchronously detect MIME types based on actual buffer content. Applied changes simultaneously across api/analyze.ts (Vercel) and server.ts (local dev) with a safe fallback mechanism for text-based formats (CSV, JSON, TXT).


PR created automatically by Jules for task 14649357054211411820 started by @NITISH-R-G

Summary by Sourcery

Harden file upload handling by validating MIME types based on file content instead of trusting client-provided types.

New Features:

  • Introduce a shared detectMimeType helper to infer MIME types from file buffers with safe fallbacks for text formats.

Bug Fixes:

  • Prevent file upload spoofing by rejecting files whose detected MIME type is not in the allowed set across both local and Vercel analyzers.

Build:

  • Add the file-type dependency to support content-based MIME detection.

Replaced trust on client-provided `mimetype` with actual magic bytes
inspection using the `file-type` library for robust security. Validates
both Vercel serverless functions and local express dev server. Added
fallback for plain text formats.

Co-authored-by: google-labs-jules[bot] <161369871+google-labs-jules[bot]@users.noreply.github.com>
@google-labs-jules

Copy link
Copy Markdown
Contributor

👋 Jules, reporting for duty! I'm here to lend a hand with this pull request.

When you start a review, I'll add a 👀 emoji to each comment to let you know I've read it. I'll focus on feedback directed at me and will do my best to stay out of conversations between you and other bots or reviewers to keep the noise down.

I'll push a commit with your requested changes shortly after. Please note there might be a delay between these steps, but rest assured I'm on the job!

For more direct control, you can switch me to Reactive Mode. When this mode is on, I will only act on comments where you specifically mention me with @jules. You can find this option in the Pull Request section of your global Jules UI settings. You can always switch back!

New to Jules? Learn more at jules.google/docs.


For security, I will only act on instructions from the user who triggered this task.

@vercel

vercel Bot commented Jul 12, 2026

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
intelli-credit-v2 Ready Ready Preview, Comment Jul 12, 2026 10:22pm

@greptile-apps greptile-apps Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Your trial has ended. Reactivate Greptile to resume code reviews.

@coderabbitai

coderabbitai Bot commented Jul 12, 2026

Copy link
Copy Markdown

Important

Review skipped

Draft detected.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

⚙️ Run configuration

Configuration used: Organization UI

Review profile: ASSERTIVE

Plan: Pro Plus

Run ID: e54bbfa3-c263-421e-b8be-6e91e88803e5

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix-file-upload-spoofing-14649357054211411820

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@github-actions github-actions Bot added dependencies Pull requests that update a dependency file backend labels Jul 12, 2026
@sourcery-ai

sourcery-ai Bot commented Jul 12, 2026

Copy link
Copy Markdown

Reviewer's Guide

This PR hardens file upload validation by introducing server-side MIME type detection using magic bytes via the file-type library, and enforcing a shared allowlist across both the Vercel edge handler and local Express server.

Sequence diagram for server-side MIME detection and validation during file upload

sequenceDiagram
  actor User
  participant Client
  participant ApiAnalyze
  participant Limits
  participant FileTypeLibrary
  participant AnalysisCore

  User->>Client: Select files
  Client->>ApiAnalyze: POST /api/analyze (files)

  loop For each uploaded file
    ApiAnalyze->>Limits: detectMimeType(buffer, fallbackMimeType)
    Limits->>FileTypeLibrary: fileTypeFromBuffer(buffer)
    FileTypeLibrary-->>Limits: result
    alt result.mime exists
      Limits-->>ApiAnalyze: mimeType
    else
      alt [fallbackMimeType is text/* or application/json]
        Limits-->>ApiAnalyze: fallbackMimeType
      else
        Limits-->>ApiAnalyze: application/octet-stream
      end
    end

    ApiAnalyze->>Limits: isAllowedMimeType(mimeType)
    alt MIME type allowed
      ApiAnalyze->>AnalysisCore: runAnalysis(files, apiMode, bureauApiKey)
      AnalysisCore-->>ApiAnalyze: analysis
    else
      ApiAnalyze-->>Client: 415 Unsupported file type
    end
  end

  ApiAnalyze-->>Client: JSON response (analysis or error)
Loading

File-Level Changes

Change Details Files
Introduce shared, async MIME type detection based on file buffer content with a safe text-format fallback.
  • Add fileTypeFromBuffer-based detectMimeType helper to shared limits module.
  • Return detected MIME type when available, otherwise fall back to trusted text/* or application/json client types.
  • Default to application/octet-stream when neither detection nor safe fallback applies.
api/_lib/limits.ts
Apply server-side MIME validation to uploaded files in the local Express dev server, rejecting disallowed types early.
  • Replace direct use of f.mimetype with detectMimeType on the uploaded buffer.
  • Check each resolved MIME type against isAllowedMimeType and return 415 with structured error when unsupported.
  • Build the AnalyzeInputFile array only for files that pass MIME validation, preserving base64 encoding of buffers.
server.ts
Apply server-side MIME validation to uploaded files in the Vercel API handler using the shared detection helper.
  • Replace reliance on client-provided f.type with detectMimeType over the request body bytes plus a lowercase fallback.
  • Validate each detected MIME type via isAllowedMimeType and return a 415 JSON error for unsupported types.
  • Keep existing analysis flow unchanged for allowed files, using the detected MIME type for downstream logic.
api/analyze.ts
Add the file-type dependency to support buffer-based MIME detection.
  • Declare file-type runtime dependency in package.json.
  • Update package-lock.json to include file-type and its transitive dependencies.
package.json
package-lock.json

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

Replaced trust on client-provided `mimetype` with actual magic bytes
inspection using the `file-type` library for robust security. Validates
both Vercel serverless functions and local express dev server. Added
fallback for plain text formats.

Co-authored-by: google-labs-jules[bot] <161369871+google-labs-jules[bot]@users.noreply.github.com>

@greptile-apps greptile-apps Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Your trial has ended. Reactivate Greptile to resume code reviews.

@sonarqubecloud

Copy link
Copy Markdown

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

backend dependencies Pull requests that update a dependency file

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant