|
27 | 27 | SSLError, |
28 | 28 | ) |
29 | 29 | from urllib3.response import ( # type: ignore[attr-defined] |
| 30 | + _MAX_CHUNK_LINE_LENGTH, |
30 | 31 | BaseHTTPResponse, |
31 | 32 | BytesQueueBuffer, |
32 | 33 | HTTPResponse, |
@@ -1890,6 +1891,63 @@ def test_invalid_chunk_length(self) -> None: |
1890 | 1891 | assert isinstance(orig_ex, InvalidChunkLength) |
1891 | 1892 | assert orig_ex.length == fp.BAD_LENGTH_LINE.encode() |
1892 | 1893 |
|
| 1894 | + def test_chunk_size_line_too_long(self) -> None: |
| 1895 | + # A malicious server can send a chunk-size line with no newline. urllib3's |
| 1896 | + # streaming path must reject it with a bounded read rather than buffering |
| 1897 | + # the whole line (memory exhaustion). See GHSA / CVE for chunked DoS. |
| 1898 | + stream = [b"foooo"] |
| 1899 | + fp = MockChunkedEncodingLongChunkSizeLine(stream) |
| 1900 | + r = httplib.HTTPResponse(MockSock) # type: ignore[arg-type] |
| 1901 | + r.fp = fp # type: ignore[assignment] |
| 1902 | + r.chunked = True |
| 1903 | + r.chunk_left = None |
| 1904 | + resp = HTTPResponse( |
| 1905 | + r, preload_content=False, headers={"transfer-encoding": "chunked"} |
| 1906 | + ) |
| 1907 | + with pytest.raises(ProtocolError) as ctx: |
| 1908 | + next(resp.read_chunked()) |
| 1909 | + |
| 1910 | + assert "chunk size line exceeded maximum allowed length" in str(ctx.value) |
| 1911 | + |
| 1912 | + @pytest.mark.parametrize( |
| 1913 | + "trailer_length", |
| 1914 | + [ |
| 1915 | + _MAX_CHUNK_LINE_LENGTH - 1, |
| 1916 | + _MAX_CHUNK_LINE_LENGTH, |
| 1917 | + _MAX_CHUNK_LINE_LENGTH + 1, |
| 1918 | + ], |
| 1919 | + ) |
| 1920 | + @pytest.mark.parametrize("line_ending", [b"\r\n", b""]) |
| 1921 | + def test_chunk_trailer_line_length( |
| 1922 | + self, trailer_length: int, line_ending: bytes |
| 1923 | + ) -> None: |
| 1924 | + # The line limit includes its CRLF, but not the final blank line. |
| 1925 | + trailer = b"X:" + b"x" * (trailer_length - 2 - len(line_ending)) + line_ending |
| 1926 | + fp = BytesIO(b"3\r\nfoo\r\n0\r\n" + trailer + (b"\r\n" if line_ending else b"")) |
| 1927 | + r = httplib.HTTPResponse(MockSock, method="GET") # type: ignore[arg-type] |
| 1928 | + r.fp = fp # type: ignore[assignment] |
| 1929 | + resp = HTTPResponse( |
| 1930 | + r, |
| 1931 | + preload_content=False, |
| 1932 | + headers={"transfer-encoding": "chunked"}, |
| 1933 | + original_response=r, |
| 1934 | + ) |
| 1935 | + chunks = resp.read_chunked() |
| 1936 | + assert next(chunks) == b"foo" |
| 1937 | + |
| 1938 | + if trailer_length > _MAX_CHUNK_LINE_LENGTH: |
| 1939 | + with pytest.raises( |
| 1940 | + ProtocolError, |
| 1941 | + match="Response chunk trailer line exceeded maximum allowed length", |
| 1942 | + ): |
| 1943 | + next(chunks) |
| 1944 | + else: |
| 1945 | + assert list(chunks) == [] |
| 1946 | + |
| 1947 | + assert fp.closed |
| 1948 | + assert r.isclosed() |
| 1949 | + assert resp.closed |
| 1950 | + |
1893 | 1951 | def test_truncated_before_chunk(self) -> None: |
1894 | 1952 | stream = [b"foooo", b"bbbbaaaaar"] |
1895 | 1953 | fp = MockChunkedNoChunks(stream) |
@@ -2150,7 +2208,19 @@ def pop_current_chunk(self, amt: int = -1, till_crlf: bool = False) -> bytes: |
2150 | 2208 | return chunk_part |
2151 | 2209 |
|
2152 | 2210 | def readline(self, amt: int = -1) -> bytes: |
2153 | | - return self.pop_current_chunk(amt, till_crlf=amt < 0) |
| 2211 | + # Emulate a real file object's readline(size): return bytes up to and |
| 2212 | + # including the first newline, or at most ``amt`` bytes when ``amt >= 0``, |
| 2213 | + # whichever comes first. |
| 2214 | + if len(self.cur_chunk) <= 0: |
| 2215 | + self.cur_chunk = self._pop_new_chunk() |
| 2216 | + line = self.cur_chunk |
| 2217 | + newline_index = line.find(b"\n") |
| 2218 | + if newline_index != -1: |
| 2219 | + line = line[: newline_index + 1] |
| 2220 | + if 0 <= amt < len(line): |
| 2221 | + line = line[:amt] |
| 2222 | + self.cur_chunk = self.cur_chunk[len(line) :] |
| 2223 | + return line |
2154 | 2224 |
|
2155 | 2225 | def read(self, amt: int = -1) -> bytes: |
2156 | 2226 | return self.pop_current_chunk(amt) |
@@ -2178,6 +2248,14 @@ def _encode_chunk(self, chunk: bytes) -> bytes: |
2178 | 2248 | return f"{self.BAD_LENGTH_LINE}{chunk.decode()}\r\n".encode() |
2179 | 2249 |
|
2180 | 2250 |
|
| 2251 | +class MockChunkedEncodingLongChunkSizeLine(MockChunkedEncodingResponse): |
| 2252 | + def _encode_chunk(self, chunk: bytes) -> bytes: |
| 2253 | + # A chunk-size line far longer than _MAX_CHUNK_LINE_LENGTH with no |
| 2254 | + # newline, simulating a malicious server that never terminates the |
| 2255 | + # size line (memory-exhaustion attack). |
| 2256 | + return b"f" * (2**16 + 1024) |
| 2257 | + |
| 2258 | + |
2181 | 2259 | class MockChunkedEncodingWithoutCRLFOnEnd(MockChunkedEncodingResponse): |
2182 | 2260 | def _encode_chunk(self, chunk: bytes) -> bytes: |
2183 | 2261 | return "{:X}\r\n{}{}".format( |
|
0 commit comments