-
Notifications
You must be signed in to change notification settings - Fork 0
π‘οΈ Sentinel: [CRITICAL] Fix TOCTOU and DoS via file stat checks #81
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
Open
ivangegovdve-sudo
wants to merge
1
commit into
main
Choose a base branch
from
sentinel-fix-toctou-dos-5613353413879271950
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -103,15 +103,18 @@ 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") | ||
| with open(self._file_path, 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. |
||
| raise ValueError( | ||
| f"Practice repository file {self._file_path} " | ||
| "exceeds 10MB size limit" | ||
| ) | ||
|
|
||
| if not content.strip(): | ||
| return {"items": [], "attempts": []} | ||
| parsed = json.loads(content) | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -40,15 +40,18 @@ 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") | ||
| with open(self._file_path, 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. |
||
| raise ValueError( | ||
| f"Progress repository file {self._file_path} " | ||
| "exceeds 10MB size limit" | ||
| ) | ||
|
|
||
| if not content.strip(): | ||
| return {} | ||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -34,14 +34,18 @@ 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 open(self._file_path, 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. |
||
| 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 {} | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
This implementation can be improved in two ways:
OSErrorfrom file operations orjson.JSONDecodeErrorfrom parsing. This could lead to unhandled exceptions, whereas other adapters in the codebase handle these gracefully. For consistency and robustness, this one should too.10 * 1024 * 1024is a magic number. Defining it as a constant improves readability and makes it easier to change, especially since it's used across multiple files.Here's a suggestion that addresses both points. Ideally, the
MAX_FILE_SIZE_BYTESconstant would be defined at the module level to be shared across all adapters.