-
Notifications
You must be signed in to change notification settings - Fork 0
π‘οΈ Sentinel: [HIGH] Fix TOCTOU and device file size limit bypass #65
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 | ||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -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 {} | ||||||||||||||||
|
Comment on lines
+244
to
245
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. An empty checkpoint file will cause
Suggested change
|
||||||||||||||||
|
|
||||||||||||||||
|
|
||||||||||||||||
|
|
||||||||||||||||
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -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: | ||||||||||
|
Comment on lines
+115
to
+116
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. The file size limit
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) | ||||||||||
|
|
||||||||||
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -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: | ||||||||||
|
Comment on lines
+52
to
+53
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. The file size limit
Suggested change
|
||||||||||
| raise ValueError( | ||||||||||
| f"Progress repository file {self._file_path} " | ||||||||||
| "exceeds 10MB size limit" | ||||||||||
| ) | ||||||||||
|
|
||||||||||
| if not content.strip(): | ||||||||||
| return {} | ||||||||||
|
|
||||||||||
|
|
||||||||||
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -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: | ||||||||||
|
Comment on lines
+46
to
+47
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. The file size limit
Suggested change
|
||||||||||
| 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 {} | ||||||||||
|
|
||||||||||
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.
The file size limit
10 * 1024 * 1024is hardcoded. This value is duplicated across multiple files (json_file_practice_repository.py,json_file_progress_repository.py,json_file_progress_snapshot_store.py), making it difficult to maintain. Consider defining it as a constant in a shared module (e.g., a newsrc/python_learning_orchestrated/adapters/constants.py) and importing it where needed.For example, in a new
constants.pyfile:Then you can import and use this constant.