Skip to content

fix(fileUtils): validate image mime types and prevent traversal#25790

Open
SH20RAJ wants to merge 3 commits intogoogle-gemini:mainfrom
SH20RAJ:fix-issue-24817
Open

fix(fileUtils): validate image mime types and prevent traversal#25790
SH20RAJ wants to merge 3 commits intogoogle-gemini:mainfrom
SH20RAJ:fix-issue-24817

Conversation

@SH20RAJ
Copy link
Copy Markdown
Contributor

@SH20RAJ SH20RAJ commented Apr 22, 2026

This PR enhances image file validation by verifying MIME types and ensuring path safety. Replaces #24886.

SH20RAJ added 3 commits April 7, 2026 19:35
…oogle-gemini#24817)

- Throw clear errors for unsupported image formats (PNG, JPEG, WEBP, HEIC, HEIF only)
- Enforce 20MB size limit for images to avoid failures on upload
- Improves user feedback when including images in messages

Fixes google-gemini#24817
@gemini-code-assist
Copy link
Copy Markdown
Contributor

Summary of Changes

Hello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request improves the security and robustness of the file processing utility. By introducing path validation and strict content checks for images, it prevents unauthorized file access and ensures that only supported, appropriately sized image files are processed by the system.

Highlights

  • Path Traversal Prevention: Added a check using isWithinRoot to ensure that files being accessed are strictly within the project directory.
  • Image Validation: Implemented MIME type verification for images to restrict processing to supported formats (PNG, JPEG, WEBP, HEIC, HEIF).
  • File Size Constraints: Added a 20MB size limit for image files to prevent processing excessively large assets.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request enhances file processing security and validation by implementing a project root boundary check and specific constraints for image files, including format verification and a 20MB size limit. A high-severity security issue was identified where the root check could be bypassed via symbolic links; it is recommended to resolve paths to their real paths before validation to prevent path traversal.

endLine?: number,
): Promise<ProcessedFileReadResult> {
try {
if (!isWithinRoot(filePath, rootDirectory)) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

security-high high

The isWithinRoot check is vulnerable to path traversal via symbolic links. path.resolve() does not resolve symlinks, allowing an attacker to create a symlink within the project root that points to a file outside the root (e.g., /etc/passwd). This bypasses the isWithinRoot validation, enabling subsequent file operations to read sensitive files. To prevent this, ensure both the file path and the root directory are resolved to their real paths using resolveToRealPath before comparison, ensuring consistent path resolution across the repository.

Suggested change
if (!isWithinRoot(filePath, rootDirectory)) {
if (!isWithinRoot(resolveToRealPath(filePath), resolveToRealPath(rootDirectory))) {
References
  1. When requesting file access permissions, resolve symbolic links first to display the actual path being accessed, preventing potential path traversal vulnerabilities.
  2. Ensure consistent path resolution by using a single, robust function (e.g., resolveToRealPath) for all related path validations.
  3. Utility functions that perform file system operations should validate their path inputs internally to prevent path traversal vulnerabilities.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant