Repository navigation
fix: return a standard error body for non-JSON error responses - #1509
Merged
jshcodes merged 1 commit intoSep 23, 2026
Merged
Conversation
ManuelFCastillo
requested review from
alhumaw,
crowdstrikedcs and
jshcodes
as code owners
August 31, 2026 03:41
jshcodes
approved these changes
Sep 23, 2026
jshcodes
left a comment
Member
There was a problem hiding this comment.
Thank you for your contribution!! 🙇
Member
Error responses are not always JSON. A proxy, load balancer or WAF in
front of the API can answer with an HTML error page, an empty body or an
unexpected content type. Those payloads reach calc_content_return() as
raw bytes, and the error inspection block then calls .get() on them:
if resp.status_code >= 400:
_errors = returned.get("body", {}).get("errors", [])
raising AttributeError: 'bytes' object has no attribute 'get'. The
exception is caught by the general handler in perform_request(), wrapped
in an SDKError and returned as a generated 500, so the real status code
and every response header, including X-Cs-TraceId, are discarded.
Normalize a non-dictionary payload into the documented error format
before the block inspects it, so the status code and headers survive and
the returned body is always a dictionary.
The guard is nested inside the existing status_code >= 400 branch, so
successful binary responses (RTR file get, sensor downloads, report
exports) are untouched and still return bytes.
Adds tests/test_non_json_error_responses.py: 14 tests built on stubbed
requests.Response objects, no credentials required.
Fixes CrowdStrike#1508
Co-Authored-By: Claude Opus 5 <[email protected]>
jshcodes
force-pushed
the
fix/non-json-error-response-1508
branch
from
September 23, 2026 14:51
0c9aca2 to
4c1d807
Compare
2 tasks done
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.
Return a standard error body for non-JSON error responses
Error responses are not always JSON. A proxy, load balancer or WAF sitting in front of the API can answer with an HTML error page, an empty body or an unexpected content type. Those payloads reach
calc_content_return()as raw bytes, and the error inspection block then calls.get()on them:which raises
AttributeError: 'bytes' object has no attribute 'get'. The exception is caught by the general handler inperform_request(), wrapped in anSDKError(whose code defaults to 500) and returned as a generated error, so the real status code and every response header, includingX-Cs-TraceId, are discarded. That is the loss reported in #1508: the caller cannot tell a 429 from a 502 from a 503, and Support has no trace ID to research.Reproduction
Four conditions reach the failing line on
main(1.6.5). All four requirestatus_code >= 400and a body that is not JSON ortext/plain:text/htmlgateway pageAttributeErrorContent-TypeAttributeErrorapplication/octet-streamAttributeErrortext/htmlrate limit pageAttributeErrorEnd to end through the operation named in the issue, before the change:
{'status_code': 500, 'headers': {}, 'body': {'errors': [{'message': "'bytes' object has no attribute 'get'", 'code': 500}], 'resources': []}}and after:
{'status_code': 502, 'headers': {'Content-Type': 'text/html', 'X-Cs-Traceid': 'trace-abc-123'}, 'body': {'errors': [{'code': 502, 'message': '<html>...502 Bad Gateway...</html>'}], 'resources': []}}Both Service Classes and
APIHarnessV2reach this through the same function, so both were affected and both are fixed.Behaviour change
A response with
status_code >= 400whose body is not JSON ortext/plainpreviously raised, and now returns the documentedstatus_code/headers/bodydictionary with a Falcon shapederrorslist.The guard is nested inside the existing
status_code >= 400branch, so successful binary responses are untouched. RTR file get, sensor downloads and report exports still return raw bytes. There is a unit test asserting this.Relationship to #1428
PR #1428 is open against the same function for a different issue (#1154). I checked it out and ran the reproduction against it: it resolves the two
text/htmlcases as a side effect of adding atext/htmlbranch, and leaves the empty body andapplication/octet-streamcases raising, because it does not change the.get()call. The two changes are complementary rather than competing, and I do not believe they conflict textually. Happy to rebase on it, fold this into it, or step aside if you would rather see that one land first.Unit test coverage
tests/test_non_json_error_responses.pyis new: 14 tests built on stubbedrequests.Responseobjects, requiring no credentials.$ coverage run --source=src --omit=src/falconpy/debug.py -m pytest tests/test_non_json_error_responses.py -q 14 passed, 1 warning in 0.14s Name Stmts Miss Cover ------------------------------------------------------------- src/falconpy/_constant/__init__.py 13 0 100% src/falconpy/_util/_functions.py 475 244 49%Every line added by this PR is covered by this file alone. The 49% figure is this one test file in isolation; the rest of the module is exercised by the credentialed suite, which does not run on fork pull requests (the unit testing workflows are gated on
head.repo.full_name == github.repository), so I could not produce a full series result. Removing the guard and rerunning the new file fails 8 of 14, each with the originalAttributeError.Bandit analysis
$ bandit -r src/falconpy Total lines of code: 155107 Total issues (by severity): Undefined: 0 Low: 0 Medium: 0 High: 0flake8 src/falconpy --countreports 0.pylinton the changed module scores 9.80 against a 9.79 baseline onmain.Issues resolved
Other
build_error_body_from_payload()decodes witherrors="replace"so an error page that is not valid UTF-8 cannot raise from inside error handling, and truncates atMAX_ERROR_PAYLOAD_LENGTH(4096) so a large HTML page is not embedded whole into an error message._util/__init__.pysince nothing outside_functions.pyuses it. Happy to export it if you would prefer.