-
Notifications
You must be signed in to change notification settings - Fork 0
π‘οΈ Sentinel: [CRITICAL] Fix TOCTOU vulnerability and unbound memory DoS in file reads #62
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -103,19 +103,27 @@ def record_attempts(self, attempts: list[Attempt]) -> None: | |||||||||
| self._save_storage(storage) | ||||||||||
|
|
||||||||||
| def _load_storage(self) -> dict[str, object]: | ||||||||||
| if not self._file_path.exists(): | ||||||||||
| if not self._file_path.is_file(): | ||||||||||
| return {"items": [], "attempts": []} | ||||||||||
| if self._file_path.stat().st_size > 10 * 1024 * 1024: | ||||||||||
| raise ValueError( | ||||||||||
| f"Practice repository file {self._file_path} exceeds 10MB size limit" | ||||||||||
| ) | ||||||||||
|
|
||||||||||
| try: | ||||||||||
| content = self._file_path.read_text(encoding="utf-8") | ||||||||||
| if not content.strip(): | ||||||||||
| return {"items": [], "attempts": []} | ||||||||||
| parsed = json.loads(content) | ||||||||||
| return parsed if isinstance(parsed, dict) else {"items": [], "attempts": []} | ||||||||||
| with self._file_path.open("r", encoding="utf-8") as f: | ||||||||||
| content = f.read(10 * 1024 * 1024 + 1) | ||||||||||
| if len(content) > 10 * 1024 * 1024: | ||||||||||
|
Comment on lines
+111
to
+112
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Similar to the other adapters, let's replace the magic number
Suggested change
|
||||||||||
| raise ValueError( | ||||||||||
| f"Practice repository file {self._file_path} " | ||||||||||
| "exceeds 10MB size limit" | ||||||||||
| ) | ||||||||||
|
|
||||||||||
| if not content.strip(): | ||||||||||
| return {"items": [], "attempts": []} | ||||||||||
|
|
||||||||||
| parsed = json.loads(content) | ||||||||||
| return ( | ||||||||||
| parsed | ||||||||||
| if isinstance(parsed, dict) | ||||||||||
| else {"items": [], "attempts": []} | ||||||||||
| ) | ||||||||||
| except (OSError, json.JSONDecodeError): | ||||||||||
| return {"items": [], "attempts": []} | ||||||||||
|
|
||||||||||
|
|
||||||||||
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -40,27 +40,30 @@ def reset_progress(self, user_id: str) -> None: | |||||||||
|
|
||||||||||
| def _load_storage(self) -> dict[str, LessonProgress]: | ||||||||||
| """Load all persisted progress payloads.""" | ||||||||||
| if not self._file_path.exists(): | ||||||||||
| if not self._file_path.is_file(): | ||||||||||
| return {} | ||||||||||
| if self._file_path.stat().st_size > 10 * 1024 * 1024: | ||||||||||
| raise ValueError( | ||||||||||
| f"Progress repository file {self._file_path} exceeds 10MB size limit" | ||||||||||
| ) | ||||||||||
|
|
||||||||||
| try: | ||||||||||
| content = self._file_path.read_text(encoding="utf-8") | ||||||||||
| if not content.strip(): | ||||||||||
| return {} | ||||||||||
|
|
||||||||||
| data = json.loads(content) | ||||||||||
| if not isinstance(data, dict): | ||||||||||
| return {} | ||||||||||
|
|
||||||||||
| normalized: dict[str, LessonProgress] = {} | ||||||||||
| for user_id, progress in data.items(): | ||||||||||
| if isinstance(user_id, str) and isinstance(progress, dict): | ||||||||||
| normalized[user_id] = cast(LessonProgress, progress) | ||||||||||
| return normalized | ||||||||||
| with self._file_path.open("r", encoding="utf-8") as f: | ||||||||||
| content = f.read(10 * 1024 * 1024 + 1) | ||||||||||
| if len(content) > 10 * 1024 * 1024: | ||||||||||
|
Comment on lines
+48
to
+49
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. To maintain consistency and avoid magic numbers, please consider defining
Suggested change
|
||||||||||
| raise ValueError( | ||||||||||
| f"Progress repository file {self._file_path} " | ||||||||||
| "exceeds 10MB size limit" | ||||||||||
| ) | ||||||||||
|
|
||||||||||
| if not content.strip(): | ||||||||||
| return {} | ||||||||||
|
|
||||||||||
| data = json.loads(content) | ||||||||||
| if not isinstance(data, dict): | ||||||||||
| return {} | ||||||||||
|
|
||||||||||
| normalized: dict[str, LessonProgress] = {} | ||||||||||
| for user_id, progress in data.items(): | ||||||||||
| if isinstance(user_id, str) and isinstance(progress, dict): | ||||||||||
| normalized[user_id] = cast(LessonProgress, progress) | ||||||||||
| return normalized | ||||||||||
| except (json.JSONDecodeError, OSError): | ||||||||||
| return {} | ||||||||||
|
|
||||||||||
|
|
||||||||||
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -34,14 +34,20 @@ def save(self, snapshot: ProgressSnapshot) -> None: | |||||||||
| self._save_payload(progress_snapshot_to_payload(snapshot)) | ||||||||||
|
|
||||||||||
| def _load_payload(self) -> dict[str, object]: | ||||||||||
| if not self._file_path.exists(): | ||||||||||
| if not self._file_path.is_file(): | ||||||||||
| return {} | ||||||||||
| if self._file_path.stat().st_size > 10 * 1024 * 1024: | ||||||||||
| raise ValueError( | ||||||||||
| f"Progress snapshot file {self._file_path} exceeds 10MB size limit" | ||||||||||
| ) | ||||||||||
|
|
||||||||||
| 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: | ||||||||||
|
Comment on lines
+42
to
+43
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's replace the hardcoded size limit
Suggested change
|
||||||||||
| raise ValueError( | ||||||||||
| f"Progress snapshot file {self._file_path} " | ||||||||||
| "exceeds 10MB size limit" | ||||||||||
| ) | ||||||||||
| if not content.strip(): | ||||||||||
| return {} | ||||||||||
| parsed = json.loads(content) | ||||||||||
| except (OSError, json.JSONDecodeError): | ||||||||||
| return {} | ||||||||||
| return parsed if isinstance(parsed, dict) else {} | ||||||||||
|
|
||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To improve maintainability and avoid magic numbers, it's a good practice to define the file size limit as a module-level constant. This makes it easier to find and update the value if needed, and ensures consistency across the codebase.
You could define
_MAX_FILE_SIZE_BYTES = 10 * 1024 * 1024at the top of the file and use it here.