fix: consolidated bug fixes and security improvements#91
Open
Conversation
Greptile OverviewGreptile SummaryThis PR consolidates critical security fixes and robustness improvements from multiple previous PRs into a single cohesive change. The changes address security vulnerabilities, prevent resource exhaustion, and improve error handling across the codebase. Key ChangesSecurity Hardening
Memory and Resource Management
Data Durability
Error Handling
Numeric Safety
TestingAll changes include comprehensive test coverage, including tests for edge cases like path traversal attempts, quote bypasses, and multi-byte UTF-8 handling. Confidence Score: 5/5
|
| Filename | Overview |
|---|---|
| src/cortex-tui/src/mcp_storage.rs | Added path traversal prevention by sanitizing server names before filesystem operations |
| src/cortex-tui/src/session/storage.rs | Added path traversal prevention and fsync after writes for crash safety and durability |
| src/cortex-tui/src/external_editor.rs | Replaced predictable temp file names with cryptographically secure random names to prevent symlink attacks |
| src/cortex-shell-snapshot/src/snapshot.rs | Added shell escaping for paths containing single quotes to prevent shell injection in restore scripts |
| src/cortex-engine/src/tools/response_store.rs | New bounded storage for tool responses with automatic cleanup to prevent unbounded memory growth |
| src/cortex-engine/src/validation.rs | Added command normalization to prevent validation bypasses via quotes, paths, and extra whitespace |
| src/cortex-common/src/file_locking.rs | Added automatic cleanup of stale lock entries to prevent memory leaks in long-running processes |
| src/cortex-engine/src/streaming.rs | Added buffer size limits and saturating conversions for token counts to prevent overflow and unbounded growth |
| src/cortex-mcp-server/src/server.rs | Fixed TOCTOU race condition by holding write lock during state check and modification |
| src/cortex-plugins/src/registry.rs | Fixed TOCTOU race using HashMap entry API for atomic check-and-insert operations |
Sequence Diagram
sequenceDiagram
participant User
participant TUI
participant Storage
participant Engine
participant MCP
Note over TUI,Storage: Security Layer
User->>TUI: Request external editor
TUI->>TUI: Generate cryptographically secure temp file (TOCTOU fix)
TUI-->>User: Opens editor safely
User->>TUI: Request session save
TUI->>Storage: Sanitize session ID (path traversal prevention)
Storage->>Storage: Write with atomic rename
Storage->>Storage: fsync() for crash safety
Storage-->>TUI: Session saved durably
User->>Engine: Execute tool with large dataset
Engine->>Engine: Check ToolResponseStore bounds (MAX_STORE_SIZE)
Engine->>Engine: Evict oldest if at capacity
Engine-->>User: Response (memory bounded)
User->>Engine: Stream processing
Engine->>Engine: Check buffer limits (MAX_BUFFER_SIZE)
Engine->>Engine: Saturating token conversions
Engine-->>User: Stream events (overflow prevented)
User->>MCP: Initialize server
MCP->>MCP: Atomic state check-and-transition (TOCTOU fix)
MCP-->>User: Server initialized
Note over Engine: Command Validation
User->>Engine: Execute bash command
Engine->>Engine: Normalize command (bypass prevention)
Engine->>Engine: Check against blocked patterns
alt Command allowed
Engine-->>User: Execute safely
else Command blocked
Engine-->>User: Validation error
end
This PR consolidates all bug fixes and security improvements from PRs #69-88 into a single cohesive change. ## Categories ### Security Fixes - Path traversal prevention in MCP and session storage - Shell injection prevention in restore scripts - Secure random temp files for external editor - TOCTOU race condition fixes ### TUI Improvements - Overflow prevention for u16 conversions - Cursor positioning fixes in selection lists - Unicode width handling for popups - Empty section handling in help browser ### Error Handling - Graceful semaphore and init failure handling - Improved error propagation in middleware - Better client access error handling - SystemTime operation safety ### Memory and Storage - Cache size limits to prevent unbounded growth - File lock cleanup for memory leak prevention - fsync after critical writes for durability - Bounded ToolResponseStore with automatic cleanup ### Protocol Robustness - Buffer size limits for StreamProcessor - ToolState transition validation - State machine documentation ### Numeric Safety - Saturating operations to prevent overflow/underflow - Safe UTF-8 string slicing throughout codebase ### Tools - Parameter alias support for backward compatibility - Handler name consistency fixes ## Files Modified Multiple files across cortex-tui, cortex-engine, cortex-exec, cortex-common, cortex-protocol, cortex-storage, cortex-mcp-server, and other crates. Closes #69, #70, #71, #73, #75, #80, #82, #87, #88
1e4adb3 to
535de7b
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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 consolidates all bug fixes and security improvements from PRs #69-88 into a single cohesive change.
Security Fixes
TUI Improvements
Error Handling
Memory and Storage
Protocol Robustness
Numeric Safety
Tools
Files Modified
Multiple files across cortex-tui, cortex-engine, cortex-exec, cortex-common,
cortex-protocol, cortex-storage, cortex-mcp-server, and other crates.
Closes #69, #70, #71, #73, #75, #80, #82, #87, #88