Repository navigation
Raise InvalidURL when a request URL has a scheme but no host - #3893
MadhurBagdare wants to merge 2 commits into
Conversation
YashrajMiyatra
left a comment
There was a problem hiding this comment.
Nice catch on validating missing hosts in BaseClient._merge_url before relative URL checks run.
One small test coverage suggestion: pytest.param("http://", id="no-host") was removed from test_get_invalid_url in tests/client/test_async_client.py, while the new test_get_url_with_scheme_and_no_host and test_scheme_without_host_is_not_joined_onto_base_url tests were only added to tests/client/test_client.py. Even though _merge_url is shared via BaseClient, keeping matching AsyncClient test cases in test_async_client.py would maintain parity between the sync and async suites.
Mansi2221
left a comment
There was a problem hiding this comment.
Good fix. The new InvalidURL branch in _merge_url catches URLs like https:/google.com (single slash) and https:///google.com (empty authority) that previously fell through — the URL parser accepted them with an empty host and the client sent nonsense requests.
The test matrix is thoughtful:
http://moves from the existingtest_get_invalid_urlinto the new dedicated test (the old://example.organdinvalid://example.orgstill cover the no-scheme and bad-scheme cases)https:/google.comandhttps:///google.comcover the new casestest_scheme_without_host_is_not_joined_onto_base_urlis a nice behavioral assertion — without the fix, this would silently buildhttps://example.org/sub/https:/google.com, which is a security hazard (CRLF/SSRF adjacent)
One nit: the error message "missing host" is fine but "URL has a scheme but no host" would match the PR title and be slightly more actionable.
LGTM.
|
Added the same missing-host cases on AsyncClient, including the base URL join. |
httpx.get("https:/google.com")andhttpx.get("https:///google.com")raiseUnsupportedProtocol. The scheme is dropped while the URL is merged, and a client with a base URL rewrites the request onto that host instead.A request URL with a scheme and no host should raise
InvalidURL.Fixes #1832
pytest tests/client/test_client.py::test_get_url_with_scheme_and_no_host tests/client/test_client.py::test_scheme_without_host_is_not_joined_onto_base_url