fix: use incremental hashing in CatalogDescriptor.get_hash() - #3907
Open
Quratulain-bilal wants to merge 3 commits into
Open
fix: use incremental hashing in CatalogDescriptor.get_hash()#3907Quratulain-bilal wants to merge 3 commits into
Quratulain-bilal wants to merge 3 commits into
Conversation
… loading entire file into memory CatalogDescriptor.get_hash() called fh.read() which loads the entire file into memory before hashing. Use incremental hashlib.sha256().update() with 64 KiB chunks to prevent unbounded memory allocation on large catalog descriptor files.
Contributor
There was a problem hiding this comment.
Pull request overview
Updates catalog descriptor hashing to avoid loading entire files into memory.
Changes:
- Streams descriptor data into SHA-256 using 64 KiB chunks.
- Preserves the existing
sha256:<digest>output format.
Show a summary per file
| File | Description |
|---|---|
src/specify_cli/integrations/catalog.py |
Implements incremental descriptor hashing. |
Review details
Tip
Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
- Files reviewed: 1/1 changed files
- Comments generated: 1
- Review effort level: Balanced
…scriptor.get_hash() Add test_get_hash_incremental_chunking that: - Creates a 200 KiB+ YAML descriptor to force multiple 64 KiB chunks - Verifies the digest matches hashlib.sha256(content).hexdigest() - Rejects any unbounded f.read() call by patching builtins.open - Asserts at least 3 reads of <= 65536 bytes occurred
Comment on lines
+674
to
+689
| def tracking_open(path, *args, **kwargs): | ||
| fh = original_open(path, *args, **kwargs) | ||
| if (args and args[0] == "rb") or kwargs.get("mode") == "rb": | ||
| orig_read = fh.read | ||
|
|
||
| def tracking_read(n=-1): | ||
| if n == -1 or n is None: | ||
| raise RuntimeError( | ||
| "f.read() called without size limit — " | ||
| "use bounded chunked reads instead" | ||
| ) | ||
| read_sizes.append(n) | ||
| return orig_read(n) | ||
|
|
||
| fh.read = tracking_read | ||
| return fh |
Assigning to fh.read on a _io.BufferedReader raises AttributeError because the attribute is read-only. Wrap the file in a proxy that intercepts read() while delegating everything else to the real file.
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Problem
CatalogDescriptor.get_hash()calledfh.read()which loads the entire file into memory before hashing. A large or maliciously sized catalog descriptor file could cause unbounded memory allocation.Fix
Use incremental
hashlib.sha256().update()with 64 KiB chunks.Testing