-
Notifications
You must be signed in to change notification settings - Fork 6
fix(security): bound pickle metadata reads in metadata extraction #712
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
mldangelo
wants to merge
5
commits into
main
Choose a base branch
from
codex/fix-dos-vulnerability-in-pickle-extraction
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
Show all changes
5 commits
Select commit
Hold shift + click to select a range
b639d2d
fix(security): bound pickle metadata reads to prevent DoS
mldangelo d3ff557
fix: reject invalid pickle metadata read limits
mldangelo 5f89bff
Merge branch 'main' into review-pr-712
mldangelo 93c38ad
Merge remote-tracking branch 'origin/main' into audit-pr712-mainmerge
mldangelo 56110af
Merge remote-tracking branch 'origin/main' into review-pr712-refresh
mldangelo 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -466,6 +466,27 @@ def __reduce__(self): | |
| assert "REDUCE" in metadata.get("dangerous_opcodes", []) | ||
| assert metadata.get("has_dangerous_opcodes") is True | ||
|
|
||
| @pytest.mark.parametrize( | ||
| ("limit", "expected_error"), | ||
| [ | ||
| (64, "read limit exceeded"), | ||
| (0, "must be greater than 0"), | ||
| (-1, "must be greater than 0"), | ||
| ], | ||
| ) | ||
| def test_pickle_metadata_enforces_read_limit(self, tmp_path: Path, limit: int, expected_error: str) -> None: | ||
| """Ensure pickle metadata extraction rejects oversized and invalid read limits.""" | ||
| from modelaudit.scanners.pickle_scanner import PickleScanner | ||
|
|
||
| pkl_file = tmp_path / "oversized.pkl" | ||
| pkl_file.write_bytes(b"x" * 128) | ||
|
|
||
| scanner = PickleScanner({"max_metadata_pickle_read_size": limit}) | ||
| metadata = scanner.extract_metadata(str(pkl_file)) | ||
|
|
||
| assert "extraction_error" in metadata | ||
| assert expected_error in metadata["extraction_error"] | ||
|
|
||
|
Comment on lines
+469
to
+489
Contributor
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. 🧹 Nitpick | 🔵 Trivial Add a success-path case to this limit matrix. This parametrization only checks failing paths. Add one valid limit case (e.g., Proposed test refinement `@pytest.mark.parametrize`(
- ("limit", "expected_error"),
+ ("limit", "expected_error"),
[
(64, "read limit exceeded"),
(0, "must be greater than 0"),
(-1, "must be greater than 0"),
+ (256, None),
],
)
-def test_pickle_metadata_enforces_read_limit(self, tmp_path: Path, limit: int, expected_error: str) -> None:
+def test_pickle_metadata_enforces_read_limit(
+ self, tmp_path: Path, limit: int, expected_error: str | None
+) -> None:
@@
- assert "extraction_error" in metadata
- assert expected_error in metadata["extraction_error"]
+ if expected_error is None:
+ assert "extraction_error" not in metadata
+ assert metadata.get("pickle_size") == 128
+ else:
+ assert "extraction_error" in metadata
+ assert expected_error in metadata["extraction_error"]🤖 Prompt for AI Agents |
||
| def test_pickle_safe_data_no_dangerous_opcodes(self, tmp_path: Path) -> None: | ||
| """Ensure simple data structures don't trigger dangerous opcode detection.""" | ||
| from modelaudit.scanners.pickle_scanner import PickleScanner | ||
|
|
||
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.
Enforce a hard 10 MiB upper bound for
max_metadata_pickle_read_size.The new validation rejects non-positive values, but any very large positive config still weakens the metadata DoS protection.
🔧 Proposed fix
if max_metadata_read_size <= 0: raise ValueError( f"Invalid pickle metadata read limit: {max_metadata_read_size} (must be greater than 0)" ) + hard_cap = 10 * 1024 * 1024 + if max_metadata_read_size > hard_cap: + raise ValueError( + f"Invalid pickle metadata read limit: {max_metadata_read_size} (must be <= {hard_cap})" + )🤖 Prompt for AI Agents