fix: add file size validation to prevent memory exhaustion #6157
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Summary
This PR implements file size validation across all file reading operations in Roo-Code to prevent memory exhaustion and performance issues, as identified in #6155.
Changes
1. Created shared file size constants (
src/services/constants/file-limits.ts)MAX_FILE_SIZE_BYTES: 1MB for general filesMAX_CONFIG_FILE_SIZE_BYTES: 100KB for configuration filesMAX_GITIGNORE_FILE_SIZE_BYTES: 50KB for .gitignore filesMAX_CHECKPOINT_FILE_SIZE_BYTES: 5MB for checkpoint filesformatBytes()helper andFileSizeLimitErrorclass2. Added file size validation to:
readFileIfExists()now checks file size before reading3. Added comprehensive unit tests
fs.stat()callsTesting
Notes
nullor skip the file rather than throwing errors, maintaining backward compatibilityFixes #6155
Important
Add file size validation to prevent memory exhaustion by setting limits for different file types and updating services to enforce these limits.
nullor skip files exceeding size limits to maintain backward compatibility.MAX_FILE_SIZE_BYTES,MAX_CONFIG_FILE_SIZE_BYTES,MAX_GITIGNORE_FILE_SIZE_BYTES,MAX_CHECKPOINT_FILE_SIZE_BYTESinfile-limits.ts.FileSizeLimitErrorclass for handling size violations.readFileIfExists()inroo-configto check file size.list-filesfor.gitignorefiles.ShadowCheckpointService.McpHub.list-files-gitignore-size.spec.tsandindex.spec.ts.fs.stat()in tests to simulate file size scenarios.This description was created by
for 40a52ae. You can customize this summary. It will automatically update as commits are pushed.