Skip to content
Myr

Patch `copy_with` (#2185)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

* Patch `copy_with` * Add a new test for `copy_with`

authored by Alan Li · committed by GitHub · parent b07fe7b · Browse files · 2 files+54 added−1 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+54 added−1 removed

  • tests:+49 added−0 removed
  • other:+5 added−1 removed

2 files+54 added−1 removed

modified:httpx/_urls.py+5 added-1 removed
482 unmodified lines
483 # foo://example.com:8042/over/there?name=ferret#nose484 # \_/ \______________/\_________/ \_________/ \__/485 # | | | | |486 # scheme authority path query fragment487 return URL(self._uri_reference.copy_with(**kwargs).unsplit())487 new_url = URL(self)488 new_url._uri_reference = self._uri_reference.copy_with(**kwargs)489 if new_url.is_absolute_url:490 new_url._uri_reference = new_url._uri_reference.normalize()491 return URL(new_url)492493 def copy_set_param(self, key: str, value: typing.Any = None) -> "URL":494 return self.copy_with(params=self.params.set(key, value))495
282 unmodified lines
modified:tests/models/test_url.py+49 added
306 unmodified lines
307 assert url.query == b"a=123"308 assert url.raw_path == b"/some/path?a=123"309310311def test_url_copywith_security():312 """313 Prevent unexpected changes on URL after calling copy_with (CVE-2021-41945)314 """315 url = httpx.URL("https://u:p@[invalid!]//evilHost/path?t=w#tw")316 original_scheme = url.scheme317 original_userinfo = url.userinfo318 original_netloc = url.netloc319 original_raw_path = url.raw_path320 original_query = url.query321 original_fragment = url.fragment322 url = url.copy_with()323 assert url.scheme == original_scheme324 assert url.userinfo == original_userinfo325 assert url.netloc == original_netloc326 assert url.raw_path == original_raw_path327 assert url.query == original_query328 assert url.fragment == original_fragment329330 url = httpx.URL("https://u:p@[invalid!]//evilHost/path?t=w#tw")331 original_scheme = url.scheme332 original_netloc = url.netloc333 original_raw_path = url.raw_path334 original_query = url.query335 original_fragment = url.fragment336 url = url.copy_with(userinfo=b"")337 assert url.scheme == original_scheme338 assert url.userinfo == b""339 assert url.netloc == original_netloc340 assert url.raw_path == original_raw_path341 assert url.query == original_query342 assert url.fragment == original_fragment343344 url = httpx.URL("https://example.com/path?t=w#tw")345 original_userinfo = url.userinfo346 original_netloc = url.netloc347 original_raw_path = url.raw_path348 original_query = url.query349 original_fragment = url.fragment350 bad = "https://xxxx:xxxx@xxxxxxx/xxxxx/xxx?x=x#xxxxx"351 url = url.copy_with(scheme=bad)352 assert url.scheme == bad353 assert url.userinfo == original_userinfo354 assert url.netloc == original_netloc355 assert url.raw_path == original_raw_path356 assert url.query == original_query357 assert url.fragment == original_fragment358359360def test_url_invalid():361 with pytest.raises(httpx.InvalidURL):362 httpx.URL("https://😇/")363
64 unmodified lines