Repository navigation
Binärantworten nur mit Typ und Größe loggen - #4164
Conversation
5ef1f68 to
00eaba7
Compare
00eaba7 to
b1cb82c
Compare
b1cb82c to
5247bd7
Compare
There was a problem hiding this comment.
🟡 Changes recommended
A moderate active finding remains because unlisted binary media types can still be decoded and written to DEBUG logs, so the change does not reliably prevent binary-content logging.
1 open issue
What changed in this PR
This pull request updates shared HTTP response logging to log recognized binary responses by content type and size instead of their decoded body, with accompanying regression tests.
Changes:
- Adds binary-response detection based on MIME types and ZIP signatures.
- Preserves text-response logging and existing HTTP error handling.
- Adds tests for binary, text, disabled-debug, and error-response behavior.
- Moderate issue: Unlisted binary media types can still fall through to
response.textand expose decoded binary contents in DEBUG logs.
| File | Description |
|---|---|
packages/modules/common/req.py |
Implements metadata-only logging for recognized binary responses; binary media types outside the whitelist may still be logged as decoded text. |
packages/modules/common/req_test.py |
Adds regression coverage for binary ZIP handling, text logging, debug-level behavior, and error responses. |
Review effort: Lite
Ein Bot darf einen PR nicht akzeptieren.
becea21 to
e722152
Compare
|
@sourcery-ai review |
Reviewer's GuideImplementationAdd a response logger that logs textual bodies as before but logs binary responses by content type and size, including ZIP files mislabeled as text.
TestsTest textual and binary response logging, ZIP detection, disabled debug logging, and preservation of HTTP error responses.
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
There was a problem hiding this comment.
Hey - I've found 3 issues
Prompt for AI Agents
Please address the comments from this code review:
## Individual Comments
### Comment 1
<location path="packages/modules/common/req.py" line_range="58" />
<code_context>
+ content_type in ('application/json', 'application/xml', 'application/x-www-form-urlencoded') or
+ (content_type.startswith('application/') and content_type.endswith(('+json', '+xml'))))
+ # The portal can omit/mislabel the content type of ZIP downloads.
+ if not text or response.content.startswith((b'PK\x03\x04', b'PK\x05\x06', b'PK\x07\x08')):
+ log.debug("Get-Response: binary data (%s, %s bytes)", content_type or 'unknown type', len(response.content))
+ else:
</code_context>
<issue_to_address>
**Binary payloads are written to logs**
When non-ZIP binary data is labeled with a text/* content type, `_log_response` trusts the text media type and logs `response.text`, decoding and emitting the binary payload instead of metadata; callers see the binary payload in debug logs.
Detect non-ZIP binary payloads before logging `response.text` and log only their type and size.
</issue_to_address>
### Comment 2
<location path="packages/modules/common/req.py" line_range="56" />
<code_context>
+ content_type = response.headers.get('Content-Type', '').split(';', 1)[0].strip().lower()
+ text = (content_type.startswith('text/') or
+ content_type in ('application/json', 'application/xml', 'application/x-www-form-urlencoded') or
+ (content_type.startswith('application/') and content_type.endswith(('+json', '+xml'))))
+ # The portal can omit/mislabel the content type of ZIP downloads.
+ if not text or response.content.startswith((b'PK\x03\x04', b'PK\x05\x06', b'PK\x07\x08')):
</code_context>
<issue_to_address>
**Text response bodies disappear from logs**
When a response uses an unlisted textual application/* media type such as `application/javascript`, `_log_response` classifies the response as binary and logs only its type and size, so callers cannot inspect the body in debug logs.
Classify textual application/* media types as text instead of limiting them to the listed types and JSON/XML suffixes.
</issue_to_address>
### Comment 3
<location path="packages/modules/common/req.py" line_range="58" />
<code_context>
+ content_type in ('application/json', 'application/xml', 'application/x-www-form-urlencoded') or
+ (content_type.startswith('application/') and content_type.endswith(('+json', '+xml'))))
+ # The portal can omit/mislabel the content type of ZIP downloads.
+ if not text or response.content.startswith((b'PK\x03\x04', b'PK\x05\x06', b'PK\x07\x08')):
+ log.debug("Get-Response: binary data (%s, %s bytes)", content_type or 'unknown type', len(response.content))
+ else:
</code_context>
<issue_to_address>
**Prefixed ZIP contents enter logs**
When a ZIP download has a prepended executable and is labeled with a text-like Content-Type, `_log_response` recognizes ZIP data only when a ZIP signature starts at byte zero, so a self-extracting ZIP with a prepended executable and a text-like Content-Type falls through to `response.text` and its binary body appears in debug logs.
Detect ZIP archives with a format-aware check that supports prepended data before logging their contents.
</issue_to_address>| content_type in ('application/json', 'application/xml', 'application/x-www-form-urlencoded') or | ||
| (content_type.startswith('application/') and content_type.endswith(('+json', '+xml')))) | ||
| # The portal can omit/mislabel the content type of ZIP downloads. | ||
| if not text or response.content.startswith((b'PK\x03\x04', b'PK\x05\x06', b'PK\x07\x08')): |
There was a problem hiding this comment.
🟡 Medium · Binary payloads are written to logs
When non-ZIP binary data is labeled with a text/* content type, _log_response trusts the text media type and logs response.text, decoding and emitting the binary payload instead of metadata; callers see the binary payload in debug logs.
Detect non-ZIP binary payloads before logging response.text and log only their type and size.
Prompt for AI agents
In `packages/modules/common/req.py` at line 58:
**Binary payloads are written to logs**
When non-ZIP binary data is labeled with a text/* content type, `_log_response` trusts the text media type and logs `response.text`, decoding and emitting the binary payload instead of metadata; callers see the binary payload in debug logs.
Detect non-ZIP binary payloads before logging `response.text` and log only their type and size.| content_type = response.headers.get('Content-Type', '').split(';', 1)[0].strip().lower() | ||
| text = (content_type.startswith('text/') or | ||
| content_type in ('application/json', 'application/xml', 'application/x-www-form-urlencoded') or | ||
| (content_type.startswith('application/') and content_type.endswith(('+json', '+xml')))) |
There was a problem hiding this comment.
🟡 Medium · Text response bodies disappear from logs
When a response uses an unlisted textual application/* media type such as application/javascript, _log_response classifies the response as binary and logs only its type and size, so callers cannot inspect the body in debug logs.
Classify textual application/* media types as text instead of limiting them to the listed types and JSON/XML suffixes.
Prompt for AI agents
In `packages/modules/common/req.py` at line 56:
**Text response bodies disappear from logs**
When a response uses an unlisted textual application/* media type such as `application/javascript`, `_log_response` classifies the response as binary and logs only its type and size, so callers cannot inspect the body in debug logs.
Classify textual application/* media types as text instead of limiting them to the listed types and JSON/XML suffixes.| content_type in ('application/json', 'application/xml', 'application/x-www-form-urlencoded') or | ||
| (content_type.startswith('application/') and content_type.endswith(('+json', '+xml')))) | ||
| # The portal can omit/mislabel the content type of ZIP downloads. | ||
| if not text or response.content.startswith((b'PK\x03\x04', b'PK\x05\x06', b'PK\x07\x08')): |
There was a problem hiding this comment.
🟡 Medium · Prefixed ZIP contents enter logs
When a ZIP download has a prepended executable and is labeled with a text-like Content-Type, _log_response recognizes ZIP data only when a ZIP signature starts at byte zero, so a self-extracting ZIP with a prepended executable and a text-like Content-Type falls through to response.text and its binary body appears in debug logs.
Detect ZIP archives with a format-aware check that supports prepended data before logging their contents.
Prompt for AI agents
In `packages/modules/common/req.py` at line 58:
**Prefixed ZIP contents enter logs**
When a ZIP download has a prepended executable and is labeled with a text-like Content-Type, `_log_response` recognizes ZIP data only when a ZIP signature starts at byte zero, so a self-extracting ZIP with a prepended executable and a text-like Content-Type falls through to `response.text` and its binary body appears in debug logs.
Detect ZIP archives with a format-aware check that supports prepended data before logging their contents.
references #4148 (review) Punkt 2
Summary by Sourcery
Log binary HTTP responses using only their content type and size while preserving text response logging.
Bug Fixes:
Enhancements:
Tests: