Skip to content

Commit 4296e72

Browse files
committed
Validate Content-Length format in ClientRequest
1 parent d60dba4 commit 4296e72

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

aiohttp/client_reqrep.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -755,7 +755,13 @@ def _get_content_length(self) -> int | None:
755755

756756
content_length_hdr = self.headers[hdrs.CONTENT_LENGTH]
757757
if not _DIGITS_RE.fullmatch(content_length_hdr):
758+
<<<<<<< HEAD
758759
raise ValueError(f"Invalid Content-Length header: {content_length_hdr!r}")
760+
=======
761+
raise ValueError(
762+
f"Invalid Content-Length header: {content_length_hdr!r}"
763+
)
764+
>>>>>>> c59955e89 (Validate Content-Length format in ClientRequest)
759765
return int(content_length_hdr)
760766

761767
@property

tests/test_web_functional.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,29 @@ async def handler(request: web.Request) -> web.Response:
196196
resp.release()
197197

198198

199+
async def test_content_length_invalid(
200+
aiohttp_client: AiohttpClient,
201+
) -> None:
202+
async def handler(request: web.Request) -> web.Response:
203+
body = await request.read()
204+
return web.Response(body=body)
205+
206+
app = web.Application()
207+
app.router.add_post("/", handler)
208+
client = await aiohttp_client(app)
209+
210+
resp = await client.post("/", data=b"hello world", headers={CONTENT_LENGTH: "11"})
211+
assert resp.status == 200
212+
assert await resp.read() == b"hello world"
213+
resp.release()
214+
215+
with pytest.raises(ValueError, match="Invalid Content-Length header"):
216+
await client.post("/", data=b"hello world", headers={CONTENT_LENGTH: "-100"})
217+
218+
with pytest.raises(ValueError, match="Invalid Content-Length header"):
219+
await client.post("/", data=b"hello world", headers={CONTENT_LENGTH: " 100"})
220+
221+
199222
@pytest.mark.skipif(sys.version_info < (3, 11), reason="Needs Task.cancelling()")
200223
async def test_cancel_shutdown(aiohttp_client: AiohttpClient) -> None:
201224
async def handler(request: web.Request) -> web.Response:

0 commit comments

Comments
 (0)