fix(tui): prevent path traversal in session storage via session_id sanitization#81
fix(tui): prevent path traversal in session storage via session_id sanitization#81
Conversation
…nitization The session_dir() function was vulnerable to path traversal attacks where a malicious session_id like '../../../etc/passwd' could escape the sessions directory and access arbitrary files. Changes: - Add sanitize_session_id() function that replaces dangerous characters - Add validate_session_id() for pre-validation of untrusted input - Only alphanumeric, hyphen, and underscore characters are allowed - Path separators and other special chars are replaced with underscores - Add comprehensive unit tests for path traversal prevention Security Impact: Prevents directory traversal attacks that could lead to unauthorized file access or manipulation outside the sessions directory. Fixes: issue #5404
Greptile OverviewGreptile SummaryThis PR introduces security hardening to prevent path traversal attacks in the session storage system by sanitizing session IDs before they're used in filesystem paths. Key Changes:
Security Impact: Issues Found:
Confidence Score: 4/5
Sequence DiagramsequenceDiagram
participant CLI as CLI/User Input
participant Manager as SessionManager
participant Storage as SessionStorage
participant Sanitize as sanitize_session_id()
participant FS as File System
CLI->>Manager: load(session_id)
Manager->>Storage: load_meta(session_id)
Storage->>Storage: session_dir(session_id)
Storage->>Sanitize: sanitize_session_id(session_id)
Note over Sanitize: Replace non-alphanumeric<br/>chars (except - and _)<br/>with underscores
Sanitize-->>Storage: sanitized_id
Storage->>Storage: base_dir.join(sanitized_id)
Note over Storage: Path is now safe,<br/>stays under base_dir
Storage->>FS: read meta.json from safe path
FS-->>Storage: metadata content
Storage-->>Manager: SessionMeta
Manager-->>CLI: Session loaded
|
| /// # Panics | ||
| /// | ||
| /// This function does not panic but will return an invalid path if | ||
| /// the session_id contains disallowed characters. Use `validate_session_id` | ||
| /// before calling this function for untrusted input. |
There was a problem hiding this comment.
documentation states function "does not panic" and mentions returning "an invalid path", but this is misleading. the function always sanitizes and returns a valid path under base_dir. consider updating the documentation to be clearer
| /// # Panics | |
| /// | |
| /// This function does not panic but will return an invalid path if | |
| /// the session_id contains disallowed characters. Use `validate_session_id` | |
| /// before calling this function for untrusted input. | |
| /// # Security | |
| /// | |
| /// The session_id is sanitized to prevent path traversal attacks. | |
| /// Only alphanumeric characters, hyphens, and underscores are allowed. | |
| /// Any other characters are automatically replaced with underscores. | |
| /// | |
| /// Returns a path under `base_dir` with the sanitized session_id. |
Prompt To Fix With AI
This is a comment left during a code review.
Path: src/cortex-tui/src/session/storage.rs
Line: 89:93
Comment:
documentation states function "does not panic" and mentions returning "an invalid path", but this is misleading. the function always sanitizes and returns a valid path under `base_dir`. consider updating the documentation to be clearer
```suggestion
/// # Security
///
/// The session_id is sanitized to prevent path traversal attacks.
/// Only alphanumeric characters, hyphens, and underscores are allowed.
/// Any other characters are automatically replaced with underscores.
///
/// Returns a path under `base_dir` with the sanitized session_id.
```
How can I resolve this? If you propose a fix, please make it concise.
Additional Comments (1)
Prompt To Fix With AIThis is a comment left during a code review.
Path: src/cortex-tui/src/session/storage.rs
Line: 276:277
Comment:
potential issue: directory names are not validated before being passed to `load_meta()`. if an attacker manually creates directories with malicious names like `../../../etc`, they could be processed here. consider validating with `validate_session_id()` before calling `load_meta()`
```suggestion
&& let Some(session_id) = path.file_name().and_then(|n| n.to_str())
&& validate_session_id(session_id)
&& let Ok(meta) = self.load_meta(session_id)
```
How can I resolve this? If you propose a fix, please make it concise. |
|
Closing to consolidate: This path traversal fix will be merged with PR #83 (MCP storage path traversal) into a consolidated security PR for path traversal prevention. |
…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
Summary
Prevent path traversal attacks in session storage by sanitizing session IDs.
Problem
The session_dir() function was vulnerable to path traversal attacks where a malicious session_id like '../../../etc/passwd' could escape the sessions directory.
Solution
Testing
Added unit tests:
Related Issue
Fixes issue #5404