From b305ff6a4ec3eaa59d78c819b61d3d29b40dc26b Mon Sep 17 00:00:00 2001 From: vyrnsynx <153433026+vyrnsynx@users.noreply.github.com> Date: Mon, 26 Oct 2026 17:24:01 +0800 Subject: [PATCH 1/4] Fix FileResponse 416 on well-formed multi-range requests MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit FileResponse only implements a single byte range. A Range header with multiple byte-range-specs (for example bytes=0-1,4-5) made http_range raise ValueError, and every ValueError was turned into 416. A satisfiable multi-range set must not get 416; ignore Range and return the full file as 200 instead (RFC 9110 §14.2). Malformed ranges still get 416. Fixes #14124. --- CHANGES/14124.bugfix.rst | 3 ++ CONTRIBUTORS.txt | 1 + aiohttp/web_fileresponse.py | 44 +++++++++++++++++++-------- tests/test_web_sendfile_functional.py | 39 ++++++++++++++++++++++++ 4 files changed, 75 insertions(+), 12 deletions(-) create mode 100644 CHANGES/14124.bugfix.rst diff --git a/CHANGES/14124.bugfix.rst b/CHANGES/14124.bugfix.rst new file mode 100644 index 00000000000..625a8308382 --- /dev/null +++ b/CHANGES/14124.bugfix.rst @@ -0,0 +1,3 @@ +Fixed ``FileResponse`` answering a well-formed multi-range request with +``416 Range Not Satisfiable`` instead of ignoring ``Range`` and returning the +full file -- by :user:`vyrnsynx`. diff --git a/CONTRIBUTORS.txt b/CONTRIBUTORS.txt index 494ab59ebda..62f4b119df3 100644 --- a/CONTRIBUTORS.txt +++ b/CONTRIBUTORS.txt @@ -431,6 +431,7 @@ Vladimir Zakharov Vladyslav Bohaichuk Vladyslav Bondar Vojtěch Boček +Vyrn Synx W. Trevor King Wei Lin Weiwei Wang diff --git a/aiohttp/web_fileresponse.py b/aiohttp/web_fileresponse.py index e30bff091e6..b403fec588c 100644 --- a/aiohttp/web_fileresponse.py +++ b/aiohttp/web_fileresponse.py @@ -3,6 +3,7 @@ import math import os import pathlib +import re import sys from collections.abc import Awaitable, Callable from contextlib import suppress @@ -42,6 +43,14 @@ NOSENDFILE: Final[bool] = bool(os.environ.get("AIOHTTP_NOSENDFILE")) +# Well-formed multi-range set (RFC 9110 §14.1). FileResponse only serves a +# single range; a satisfiable multi-range must not become 416 (RFC 9110 §14.2 +# allows ignoring Range and returning 200 with the full representation). +_MULTI_BYTE_RANGE_RE = re.compile( + r"^bytes=(?:\d+-\d*|-\d+)(?:,(?:\d+-\d*|-\d+))+$", + re.ASCII | re.IGNORECASE, +) + CONTENT_TYPES: Final[MimeTypes] = MimeTypes() # File extension to IANA encodings map that will be checked in the order defined. @@ -340,18 +349,29 @@ async def _prepare_open_file( start = rng.start end: int | None = rng.stop except ValueError: - # https://tools.ietf.org/html/rfc7233: - # A server generating a 416 (Range Not Satisfiable) response to - # a byte-range request SHOULD send a Content-Range header field - # with an unsatisfied-range value. - # The complete-length in a 416 response indicates the current - # length of the selected representation. - # - # Will do the same below. Many servers ignore this and do not - # send a Content-Range header with HTTP 416 - self._headers[hdrs.CONTENT_RANGE] = f"bytes */{file_size}" - self.set_status(HTTPRequestRangeNotSatisfiable.status_code) - return await super().prepare(request) + # http_range only accepts a single byte range. A well-formed + # multi-range set (e.g. bytes=0-1,4-5) is still a valid Range + # per RFC 9110; answering 416 would be wrong for a satisfiable + # set. Ignore the header and fall through to a full 200 instead + # (RFC 9110 §14.2 / RFC 7233). Malformed ranges keep 416. + range_hdr = request.headers.get(hdrs.RANGE) + if range_hdr is not None and _MULTI_BYTE_RANGE_RE.fullmatch( + range_hdr + ): + pass + else: + # https://tools.ietf.org/html/rfc7233: + # A server generating a 416 (Range Not Satisfiable) response to + # a byte-range request SHOULD send a Content-Range header field + # with an unsatisfied-range value. + # The complete-length in a 416 response indicates the current + # length of the selected representation. + # + # Will do the same below. Many servers ignore this and do not + # send a Content-Range header with HTTP 416 + self._headers[hdrs.CONTENT_RANGE] = f"bytes */{file_size}" + self.set_status(HTTPRequestRangeNotSatisfiable.status_code) + return await super().prepare(request) # If a range request has been made, convert start, end slice # notation into file pointer offset and count diff --git a/tests/test_web_sendfile_functional.py b/tests/test_web_sendfile_functional.py index 178995d8d1d..85b17cedc50 100644 --- a/tests/test_web_sendfile_functional.py +++ b/tests/test_web_sendfile_functional.py @@ -1032,6 +1032,45 @@ async def handler(request: web.Request) -> web.FileResponse: await client.close() +async def test_static_file_multi_range_ignored( + aiohttp_client: AiohttpClient, sender: _Sender +) -> None: + """A well-formed multi-range request gets 200 with the full file. + + FileResponse only implements a single byte range. Per RFC 9110 §14.2 a + server may ignore Range; answering 416 for a satisfiable multi-range set + is incorrect (see #14124). + """ + filepath = pathlib.Path(__file__).parent / "aiohttp.png" + body = filepath.read_bytes() + + async def handler(request: web.Request) -> web.FileResponse: + return sender(filepath, chunk_size=16) + + app = web.Application() + app.router.add_get("/", handler) + client = await aiohttp_client(app) + + resp = await client.get("/", headers={"Range": "bytes=0-1,4-5"}) + assert resp.status == 200 + assert "Content-Range" not in resp.headers + assert await resp.read() == body + resp.release() + + # Still a multi-range set (suffix + open range); ignore rather than 416. + resp = await client.get("/", headers={"Range": "bytes=-3,0-"}) + assert resp.status == 200 + assert await resp.read() == body + resp.release() + + # Malformed ranges that happen to contain a comma stay 416. + resp = await client.get("/", headers={"Range": "bytes=0-1,a-b"}) + assert resp.status == 416 + resp.release() + + await client.close() + + async def test_static_file_if_unmodified_since_past_with_range( aiohttp_client: AiohttpClient, app_with_static_route: web.Application ) -> None: From ef0574164c4c17563f39b10c2d37713c10d59f31 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 26 Oct 2026 09:26:00 +0000 Subject: [PATCH 2/4] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- aiohttp/web_fileresponse.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/aiohttp/web_fileresponse.py b/aiohttp/web_fileresponse.py index b403fec588c..2a7b35bbe93 100644 --- a/aiohttp/web_fileresponse.py +++ b/aiohttp/web_fileresponse.py @@ -355,9 +355,7 @@ async def _prepare_open_file( # set. Ignore the header and fall through to a full 200 instead # (RFC 9110 §14.2 / RFC 7233). Malformed ranges keep 416. range_hdr = request.headers.get(hdrs.RANGE) - if range_hdr is not None and _MULTI_BYTE_RANGE_RE.fullmatch( - range_hdr - ): + if range_hdr is not None and _MULTI_BYTE_RANGE_RE.fullmatch(range_hdr): pass else: # https://tools.ietf.org/html/rfc7233: From 45cf0cb5bb0875c8e93506c7fa28b207ef6a2272 Mon Sep 17 00:00:00 2001 From: vyrnsynx <153433026+vyrnsynx@users.noreply.github.com> Date: Mon, 26 Oct 2026 18:04:01 +0800 Subject: [PATCH 3/4] Fix mypy possibly-undefined end on multi-range ignore path Initialize start/end before parsing http_range so ignoring a well-formed multi-range header can fall through to a full 200 without leaving those names unbound for mypy. --- aiohttp/web_fileresponse.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/aiohttp/web_fileresponse.py b/aiohttp/web_fileresponse.py index 2a7b35bbe93..4ae8add50f9 100644 --- a/aiohttp/web_fileresponse.py +++ b/aiohttp/web_fileresponse.py @@ -344,10 +344,14 @@ async def _prepare_open_file( # Range header would not be processed, return 200 # if True but Range header missing # return 200 + # Default to no range so a multi-range ignore path can fall + # through to a full 200 without leaving start/end unbound. + start: int | None = None + end: int | None = None try: rng = request.http_range start = rng.start - end: int | None = rng.stop + end = rng.stop except ValueError: # http_range only accepts a single byte range. A well-formed # multi-range set (e.g. bytes=0-1,4-5) is still a valid Range From 55ed25b0f282beb1a3255b3b20c230a0e3d061a1 Mon Sep 17 00:00:00 2001 From: vyrnsynx <153433026+vyrnsynx@users.noreply.github.com> Date: Mon, 26 Oct 2026 18:06:27 +0800 Subject: [PATCH 4/4] Fix mypy: init end with start, avoid no-redef start is already declared at the top of _prepare_open_file; only end needed a matching None default so the multi-range ignore path stays typed without redefining start. --- aiohttp/web_fileresponse.py | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/aiohttp/web_fileresponse.py b/aiohttp/web_fileresponse.py index 4ae8add50f9..369e46f439c 100644 --- a/aiohttp/web_fileresponse.py +++ b/aiohttp/web_fileresponse.py @@ -319,6 +319,7 @@ async def _prepare_open_file( file_mtime: float = st.st_mtime count: int = file_size start: int | None = None + end: int | None = None etag_value = f"{st.st_mtime_ns:x}-{st.st_size:x}" # https://www.rfc-editor.org/info/rfc9110/#name-if-range @@ -344,10 +345,6 @@ async def _prepare_open_file( # Range header would not be processed, return 200 # if True but Range header missing # return 200 - # Default to no range so a multi-range ignore path can fall - # through to a full 200 without leaving start/end unbound. - start: int | None = None - end: int | None = None try: rng = request.http_range start = rng.start