Skip to content

fix: use incremental hashing in CatalogDescriptor.get_hash() - #3907

Open
Quratulain-bilal wants to merge 3 commits into
github:mainfrom
Quratulain-bilal:fix/integrations-chunked-hash
Open

fix: use incremental hashing in CatalogDescriptor.get_hash()#3907
Quratulain-bilal wants to merge 3 commits into
github:mainfrom
Quratulain-bilal:fix/integrations-chunked-hash

Conversation

@Quratulain-bilal

Copy link
Copy Markdown
Contributor

Problem

CatalogDescriptor.get_hash() called fh.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

  • Verified hash output matches the previous implementation for the same file

… 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.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Comment thread src/specify_cli/integrations/catalog.py
…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

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review details

  • Files reviewed: 2/2 changed files
  • Comments generated: 1
  • Review effort level: Balanced

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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants