Skip to content

Commit

Permalink
Disallow arbitrary sequence types in version (#7835)
Browse files Browse the repository at this point in the history
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
  • Loading branch information
Dreamsorcerer and pre-commit-ci[bot] authored Nov 13, 2023
1 parent 0bf1091 commit 1e86b77
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGES/7835.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fixed arbitrary sequence types being allowed to inject headers via version parameter -- by :user:`Dreamsorcerer`
4 changes: 2 additions & 2 deletions aiohttp/client_reqrep.py
Original file line number Diff line number Diff line change
Expand Up @@ -644,8 +644,8 @@ async def send(self, conn: "Connection") -> "ClientResponse":
self.headers[hdrs.CONNECTION] = connection

# status + headers
status_line = "{0} {1} HTTP/{2[0]}.{2[1]}".format(
self.method, path, self.version
status_line = "{0} {1} HTTP/{v.major}.{v.minor}".format(
self.method, path, v=self.version
)
await writer.write_headers(status_line, self.headers)

Expand Down
20 changes: 17 additions & 3 deletions tests/test_client_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
Fingerprint,
_gen_default_accept_encoding,
)
from aiohttp.http import HttpVersion
from aiohttp.test_utils import make_mocked_coro


Expand Down Expand Up @@ -590,18 +591,18 @@ async def test_connection_header(loop: Any, conn: Any) -> None:
req.headers.clear()

req.keep_alive.return_value = True
req.version = (1, 1)
req.version = HttpVersion(1, 1)
req.headers.clear()
await req.send(conn)
assert req.headers.get("CONNECTION") is None

req.version = (1, 0)
req.version = HttpVersion(1, 0)
req.headers.clear()
await req.send(conn)
assert req.headers.get("CONNECTION") == "keep-alive"

req.keep_alive.return_value = False
req.version = (1, 1)
req.version = HttpVersion(1, 1)
req.headers.clear()
await req.send(conn)
assert req.headers.get("CONNECTION") == "close"
Expand Down Expand Up @@ -1112,6 +1113,19 @@ async def gen():
resp.close()


async def test_bad_version(loop: Any, conn: Any) -> None:
req = ClientRequest(
"GET",
URL("http://python.org"),
loop=loop,
headers={"Connection": "Close"},
version=("1", "1\r\nInjected-Header: not allowed"),
)

with pytest.raises(AttributeError):
await req.send(conn)


async def test_custom_response_class(loop: Any, conn: Any) -> None:
class CustomResponse(ClientResponse):
def read(self, decode=False):
Expand Down

0 comments on commit 1e86b77

Please sign in to comment.