Tesseract.js OCR Missing File Type Validation - #915
Conversation
|
@Kirtan-pc 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 · |
|
Warning Review limit reached
Next review available in: 19 minutes Enable usage-based reviews in Billing to review now. Otherwise, wait until the next included review is available. How can I continue?After more reviews become available, a review can be triggered using the To avoid repeated limits, reduce automatic review volume by pausing incremental auto-reviews earlier, using label-based review opt-in, excluding WIP or generated PR titles, or requesting reviews manually when the PR is ready. If your team needs uninterrupted high-volume reviews, an organization admin can enable usage-based reviews. How do review limits work?CodeRabbit enforces per-developer PR review limits for each organization. Most developers receive the normal plan review availability. For paid Pro and Pro+ PR reviews, CodeRabbit uses adaptive limits for sustained high-volume activity. When a developer's recent PR review activity reaches the 95th percentile or higher among CodeRabbit users, additional reviews become available more gradually as earlier reviews age out of the rolling window. Please refer docs for additional details. Review details⚙️ Run configurationConfiguration used: Path: .coderabbit.yaml Review profile: CHILL Plan: Pro Run ID: ⛔ Files ignored due to path filters (1)
📒 Files selected for processing (3)
✨ 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 |
|
CodeAnt AI finished reviewing your PR. |
|
Closing because this issue is already implemented by PR #778 |
|
This pull request has been closed because the requested features or bug fixes are already implemented in the repository. |
User description
Description
Add server-side magic byte verification to the image upload validation in the AI chat endpoint. Previously, only the client-supplied
Content-Type/ data URI prefix was checked, which can be trivially spoofed. Now the actual decoded file bytes are compared against known image signatures using thefile-typepackage.Related Issue
Closes #873
Type of Change
Problem
The
validateAiImageDataUrlfunction insrc/lib/ai/ai-image-validation.tsonly validated the client-supplied data URI prefix:A malicious client could send:
This spoofed data URI would pass validation, and the payload would be forwarded to Groq's vision API. While the primary risk here is resource waste and potential parser crashes rather than code execution, the lack of content verification violates defense-in-depth principles.
Fix
1. Magic byte verification (
src/lib/ai/ai-image-validation.ts)validateMagicBytes()function that decodes the base64 payload and runsfileTypeFromBuffer()from thefile-typepackagefile-typereads the file's magic bytes (first 2-8 bytes) to determine the actual format:89 50 4E 47 0D 0A 1A 0AFF D8 FF52 49 46 46 xx xx xx xx 57 45 42 50(RIFF....WEBP)422 IMAGE_MIME_MISMATCHif the declared MIME doesn't match the detected format422 INVALID_IMAGE_PAYLOADif the bytes don't match any known format2.
validateAiImageDataUrlis now asyncThe function signature changed from:
to:
This is necessary because
fileTypeFromBufferreturns a Promise.3. Updated caller (
src/app/api/ask-ai/route.ts)The ask-ai route's inline regex check was replaced with a call to the shared
validateAiImageDataUrl()function, so all validation (format, MIME, size, magic bytes) happens in one place.Before (ask-ai route — inline regex only)
After (ask-ai route — full validation)
Files Changed
src/lib/ai/ai-image-validation.tsvalidateMagicBytes()usingfile-typepackage;validateAiImageDataUrlis nowasyncsrc/app/api/ask-ai/route.tsvalidateAiImageDataUrl()callpackage.jsonfile-typedependencyHow Has This Been Tested?
npm run devnpx tsc --noEmitpasses)Checklist
npx tsc --noEmit)anytypes)mainCodeAnt-AI Description
Reject spoofed image uploads in AI chat
What Changed
Impact
✅ Fewer rejected AI image requests later in the flow✅ Clearer image upload errors✅ Less chance of malformed files reaching vision requests💡 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.