diff --git a/.jules/sentinel.md b/.jules/sentinel.md index 3ae152f..0406c04 100644 --- a/.jules/sentinel.md +++ b/.jules/sentinel.md @@ -2,3 +2,8 @@ **Vulnerability:** Unbounded memory consumption DoS risk due to JSON file parsing without size limits in file-backed repositories and stores. **Learning:** `json.loads(file.read_text())` reads the entire file content into memory. This can lead to out-of-memory errors or denial-of-service if an attacker or misconfiguration provides an excessively large file. The application relies heavily on file-backed adapters (e.g., `JsonFilePracticeRepository`, `JsonFileProgressSnapshotStore`, `CheckpointStore`). **Prevention:** Implement a strict file size limit check using `path.stat().st_size` (e.g., standardized at 10MB or `10 * 1024 * 1024` bytes) before reading the file content into memory in all file-based adapters. + +## 2025-03-23 - [TOCTOU and Device File Size Limit Bypass] +**Vulnerability:** File size checks using `path.stat().st_size` can be bypassed if the path points to a device file (like `/dev/zero`, which reports a size of 0). Additionally, there is a Time-of-Check to Time-of-Use (TOCTOU) vulnerability where a file could be modified between the `stat()` check and the `read_text()` call. +**Learning:** Checking file size before reading is insufficient on its own. Device files bypass `st_size` checks, and files can grow between the size check and the read operation, leading to unbounded memory consumption or application hangs. +**Prevention:** Always verify a path is a regular file first using `path.is_file()`. Then, instead of reading the whole file with `read_text()`, open the file and use a bounded read (e.g., `f.read(limit + 1)`) and check if the returned content length exceeds the limit. diff --git a/src/python_learning_orchestrated/adapters/checkpoint_store.py b/src/python_learning_orchestrated/adapters/checkpoint_store.py index 0d9c5d5..4f3b9c4 100644 --- a/src/python_learning_orchestrated/adapters/checkpoint_store.py +++ b/src/python_learning_orchestrated/adapters/checkpoint_store.py @@ -233,9 +233,15 @@ def _to_int(value: object, default: int) -> int: def _read_json(path: Path) -> dict[str, object]: if not path.exists(): return {} - if path.stat().st_size > 10 * 1024 * 1024: - raise ValueError(f"Checkpoint file {path} exceeds 10MB size limit") - parsed = json.loads(path.read_text(encoding="utf-8")) + if not path.is_file(): + raise ValueError(f"Checkpoint file {path} is not a regular file") + + with path.open("r", encoding="utf-8") as f: + content = f.read(10 * 1024 * 1024 + 1) + if len(content) > 10 * 1024 * 1024: + raise ValueError(f"Checkpoint file {path} exceeds 10MB size limit") + + parsed = json.loads(content) return parsed if isinstance(parsed, dict) else {} diff --git a/src/python_learning_orchestrated/adapters/json_file_practice_repository.py b/src/python_learning_orchestrated/adapters/json_file_practice_repository.py index 15da61b..c4412de 100644 --- a/src/python_learning_orchestrated/adapters/json_file_practice_repository.py +++ b/src/python_learning_orchestrated/adapters/json_file_practice_repository.py @@ -105,13 +105,19 @@ def record_attempts(self, attempts: list[Attempt]) -> None: def _load_storage(self) -> dict[str, object]: if not self._file_path.exists(): return {"items": [], "attempts": []} - if self._file_path.stat().st_size > 10 * 1024 * 1024: + if not self._file_path.is_file(): raise ValueError( - f"Practice repository file {self._file_path} exceeds 10MB size limit" + f"Practice repository file {self._file_path} is not a regular file" ) try: - content = self._file_path.read_text(encoding="utf-8") + with self._file_path.open("r", encoding="utf-8") as f: + content = f.read(10 * 1024 * 1024 + 1) + if len(content) > 10 * 1024 * 1024: + raise ValueError( + f"Practice repository file {self._file_path} " + "exceeds 10MB size limit" + ) if not content.strip(): return {"items": [], "attempts": []} parsed = json.loads(content) diff --git a/src/python_learning_orchestrated/adapters/json_file_progress_repository.py b/src/python_learning_orchestrated/adapters/json_file_progress_repository.py index 7c956af..8de05bd 100644 --- a/src/python_learning_orchestrated/adapters/json_file_progress_repository.py +++ b/src/python_learning_orchestrated/adapters/json_file_progress_repository.py @@ -42,13 +42,20 @@ def _load_storage(self) -> dict[str, LessonProgress]: """Load all persisted progress payloads.""" if not self._file_path.exists(): return {} - if self._file_path.stat().st_size > 10 * 1024 * 1024: + if not self._file_path.is_file(): raise ValueError( - f"Progress repository file {self._file_path} exceeds 10MB size limit" + f"Progress repository file {self._file_path} is not a regular file" ) try: - content = self._file_path.read_text(encoding="utf-8") + with self._file_path.open("r", encoding="utf-8") as f: + content = f.read(10 * 1024 * 1024 + 1) + if len(content) > 10 * 1024 * 1024: + raise ValueError( + f"Progress repository file {self._file_path} " + "exceeds 10MB size limit" + ) + if not content.strip(): return {} diff --git a/src/python_learning_orchestrated/adapters/json_file_progress_snapshot_store.py b/src/python_learning_orchestrated/adapters/json_file_progress_snapshot_store.py index ac34e1c..040d1b1 100644 --- a/src/python_learning_orchestrated/adapters/json_file_progress_snapshot_store.py +++ b/src/python_learning_orchestrated/adapters/json_file_progress_snapshot_store.py @@ -36,12 +36,20 @@ def save(self, snapshot: ProgressSnapshot) -> None: def _load_payload(self) -> dict[str, object]: if not self._file_path.exists(): return {} - if self._file_path.stat().st_size > 10 * 1024 * 1024: + if not self._file_path.is_file(): raise ValueError( - f"Progress snapshot file {self._file_path} exceeds 10MB size limit" + f"Progress snapshot file {self._file_path} is not a regular file" ) + try: - parsed = json.loads(self._file_path.read_text(encoding="utf-8")) + with self._file_path.open("r", encoding="utf-8") as f: + content = f.read(10 * 1024 * 1024 + 1) + if len(content) > 10 * 1024 * 1024: + raise ValueError( + f"Progress snapshot file {self._file_path} " + "exceeds 10MB size limit" + ) + parsed = json.loads(content) except (OSError, json.JSONDecodeError): return {} return parsed if isinstance(parsed, dict) else {}