Fix: Prevent Null Pointer Dereference in HTTPAdapter methods #7056
+97
−5
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
This PR addresses a robustness issue in the
HTTPAdapterclass whereNonevalues inPreparedRequestobjects could causeTypeErrorandAttributeErrorexceptions. The fix adds defensive null checks to prevent null pointer dereference errors when processing requests with uninitialized or None attributes.Problem
PreparedRequest.__init__()initializes bothself.urlandself.headerstoNone. Several methods inHTTPAdapterdid not handle theseNonevalues defensively, leading to potential crashes:request_url()method: Calledurlparse(request.url)andselect_proxy(request.url, proxies)without checking ifrequest.urlisNone_urllib3_request_context()function: Calledurlparse(request.url)without null checkssend()method: Evaluated"Content-Length" in request.headerswhenrequest.headerscould beNone, causingTypeError: argument of type 'NoneType' is not iterableSolution
Added defensive null handling using the
oroperator with appropriate fallback values:request.url or ""- Falls back to empty string for URL parsing operationsrequest.headers or {}- Falls back to empty dict for membership checksChanges Made
src/requests/adapters.py_urllib3_request_context(): Changedurlparse(request.url)→urlparse(request.url or "")request_url(): Added null safety for:select_proxy(request.url or "", proxies)urlparse(request.url or "").schemeurldefragauth(request.url or "")send(): Changed"Content-Length" in request.headers→"Content-Length" in (request.headers or {})tests/test_adapters.pyAdded comprehensive test coverage (103 new lines):
test_request_url_with_none_url()- Verifies graceful handling of None URLtest_request_url_with_none_url_and_proxy()- Tests None URL with proxy configurationtest_send_with_none_headers()- Ensures None headers don't cause TypeErrortest_urllib3_request_context_with_none_url()- Validates None URL handling in context builderImpact
Testing
All new tests pass and verify that the methods handle
Nonevalues gracefully without raisingTypeErrororAttributeErrorexceptions. The fixes ensure robust behavior even whenPreparedRequestobjects are not fully initialized.