fix(fileUtils): validate image mime types and prevent traversal by SH20RAJ · Pull Request #25790 · google-gemini/gemini-cli · GitHub
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

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
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