fix: bound response reads in extension catalog and download - #3775
Open
Quratulain-bilal wants to merge 4 commits into
Open
fix: bound response reads in extension catalog and download#3775Quratulain-bilal wants to merge 4 commits into
Quratulain-bilal wants to merge 4 commits into
Conversation
Replace unbounded esponse.read() calls with ead_response_limited() from _download_security in extensions/__init__.py to prevent denial- of-service via oversized catalog or extension archive responses. Three call sites fixed: - _fetch_single_catalog JSON read (catalog metadata) - _fetch_catalog JSON read (legacy path) - download_extension ZIP read (binary download) All existing mock tests updated to use side_effect with BytesIO.read instead of eturn_value, ensuring compatibility with the chunked read loop in ead_response_limited. Two regression tests added: - test_oversized_catalog_response_rejected - test_oversized_extension_download_rejected
Contributor
There was a problem hiding this comment.
Pull request overview
Bounds extension catalog and archive response reads to reduce memory-exhaustion risks.
Changes:
- Applies shared size-limited response reading.
- Updates HTTP mocks for chunked reads.
Show a summary per file
| File | Description |
|---|---|
src/specify_cli/extensions/__init__.py |
Adds bounded catalog and archive reads. |
tests/test_extensions.py |
Updates response mocks for repeated reads. |
Review details
Tip
Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comments suppressed due to low confidence (3)
src/specify_cli/extensions/init.py:3356
- Oversized catalog responses use the helper's default label, so this path raises
ExtensionError: download exceeds maximum size ...while fetching catalog metadata. Pass a catalog-specific label so the new failure tells users which response was rejected.
read_response_limited(response, max_bytes=MAX_JSON_METADATA_BYTES, error_type=ExtensionError).decode("utf-8")
src/specify_cli/extensions/init.py:3546
- The legacy catalog path also inherits the default
"download"label, producing a misleading error when catalog metadata exceeds the limit. Use a catalog-specific label here as well.
read_response_limited(response, max_bytes=MAX_JSON_METADATA_BYTES, error_type=ExtensionError).decode("utf-8")
src/specify_cli/extensions/init.py:3356
- Decoding before
json.loadschanges the catalog parser's existing behavior: the previous bytes input letjson.loadsdetect UTF-8 BOM and UTF-16/32 JSON, whereas decoding as plain UTF-8 rejects those previously accepted catalog responses. Pass the bounded bytes directly tojson.loadsso this security fix does not also introduce an encoding compatibility break.
catalog_data = json.loads(
read_response_limited(response, max_bytes=MAX_JSON_METADATA_BYTES, error_type=ExtensionError).decode("utf-8")
- Files reviewed: 2/2 changed files
- Comments generated: 3
- Review effort level: Medium
json.loads accepts bytes directly. Removing .decode maintains compatibility with BOM-bearing or UTF-16/32 catalogs.
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.
Summary
Replace unbounded
esponse.read() calls with
ead_response_limited() from _download_security in extensions/init.py to prevent denial-of-service via oversized catalog or extension archive responses.
Changes
Three call sites fixed in \extensions/init.py:
Test updates in \ ests/test_extensions.py:
eturn_value, ensuring compatibility with the chunked read loop in
ead_response_limited\
Motivation
Without bounded reads, a malicious catalog server could send an arbitrarily large JSON response causing the CLI to exhaust memory. Similarly, a compromised extension archive endpoint could serve a multi-gigabyte payload. The
ead_response_limited\ utility already exists in _download_security\ and is used by the integrations and workflow catalog modules - this PR extends the same protection to the extensions module.