Skip to content

Raise InvalidURL when a request URL has a scheme but no host - #3893

Open
MadhurBagdare wants to merge 2 commits into
encode:masterfrom
MadhurBagdare:fix-invalid-url-missing-host
Open

MadhurBagdare wants to merge 2 commits into
encode:masterfrom
MadhurBagdare:fix-invalid-url-missing-host

Conversation

@MadhurBagdare

Copy link
Copy Markdown

httpx.get("https:/google.com") and httpx.get("https:///google.com") raise UnsupportedProtocol. 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

@YashrajMiyatra YashrajMiyatra left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 Mansi2221 left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 existing test_get_invalid_url into the new dedicated test (the old ://example.org and invalid://example.org still cover the no-scheme and bad-scheme cases)
  • https:/google.com and https:///google.com cover the new cases
  • test_scheme_without_host_is_not_joined_onto_base_url is a nice behavioral assertion — without the fix, this would silently build https://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.

@MadhurBagdare

Copy link
Copy Markdown
Author

Added the same missing-host cases on AsyncClient, including the base URL join.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

Inconsistent Handling of Invalid Urls

3 participants