fix(security): consolidated path traversal prevention for MCP and session storage#87
fix(security): consolidated path traversal prevention for MCP and session storage#87
Conversation
…sion storage ## Summary This PR consolidates **2 security fixes** for path traversal vulnerabilities. ### Included PRs: - #81: Prevent path traversal in session storage via session_id sanitization - #83: Prevent path traversal in MCP storage via server name sanitization ### Key Changes: - Add sanitize_session_id() function that replaces dangerous characters - Add validate_session_id() for pre-validation of untrusted input - Add sanitize_server_name() function for MCP server names - Add validate_server_name() for pre-validation of MCP server names - Only alphanumeric, hyphen, and underscore characters are allowed ### Files Modified: - src/cortex-tui/src/session/storage.rs - src/cortex-tui/src/mcp_storage.rs Closes #81, #83
Greptile OverviewGreptile SummaryThis PR consolidates two critical security fixes that prevent path traversal vulnerabilities in both MCP server storage and session storage. Key Security Improvements:
Security Analysis: Testing:
Confidence Score: 5/5
|
| Filename | Overview |
|---|---|
| src/cortex-tui/src/mcp_storage.rs | Added sanitize_server_name() and validate_server_name() functions to prevent path traversal; applied sanitization in server_path(); comprehensive tests included |
| src/cortex-tui/src/session/storage.rs | Added sanitize_session_id() and validate_session_id() functions to prevent path traversal; applied sanitization in session_dir(); comprehensive tests included |
Sequence Diagram
sequenceDiagram
participant User
participant API as save_server/session_dir
participant Sanitize as sanitize_*
participant FS as Filesystem
User->>API: Provide name/ID (potentially malicious)
Note over User,API: e.g., "../../../etc/passwd"
API->>Sanitize: sanitize_server_name(name) / sanitize_session_id(id)
Note over Sanitize: Replace non-alphanumeric chars<br/>(except - and _) with _
Sanitize->>Sanitize: "../../../etc" → "________etc"
Sanitize-->>API: Return sanitized string
API->>API: Construct path with sanitized value
Note over API: base_dir.join(sanitized_name)
API->>FS: Access file with safe path
Note over FS: Path is confined to base_dir<br/>No traversal possible
FS-->>API: Success
API-->>User: Operation complete
|
Closing this PR to consolidate into a single mega-PR combining all bug fixes. The changes will be included in a new consolidated PR. |
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
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
Summary
This PR consolidates 2 security fixes for path traversal vulnerabilities.
Included PRs:
Key Changes:
Files Modified:
Closes #81, #83