Skip to content

Commit bbb44db

Browse files
authored
Bad encoding (#2980)
* Error on bad body length * Error on smuggle attempt
1 parent fee71dd commit bbb44db

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

sanic/http/http1.py

+4
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,10 @@ async def http1_request_header(self): # no cov
221221
name, value = h = name.lower(), value.lstrip()
222222

223223
if name in ("content-length", "transfer-encoding"):
224+
if request_body:
225+
raise ValueError(
226+
"Duplicate Content-Length or Transfer-Encoding"
227+
)
224228
request_body = True
225229
elif name == "connection":
226230
self.keep_alive = value.lower() == "keep-alive"

tests/test_http.py

+26
Original file line numberDiff line numberDiff line change
@@ -165,3 +165,29 @@ def test_invalid_chunk_length(chunk_length, client):
165165

166166
assert b"400 Bad Request" in headers
167167
assert b"Bad chunked encoding" in body
168+
169+
170+
def test_smuggle(client):
171+
client.send(
172+
"""
173+
POST /upload HTTP/1.1
174+
Content-Length: 5
175+
Transfer-Encoding: chunked
176+
Transfer-Encoding: xchunked
177+
178+
5
179+
hello
180+
0
181+
182+
GET / HTTP/1.1
183+
184+
""" # noqa
185+
)
186+
187+
response = client.recv()
188+
num_responses = response.count(b"HTTP/1.1")
189+
assert num_responses == 1
190+
191+
headers, body = response.rsplit(b"\r\n\r\n", 1)
192+
assert b"400 Bad Request" in headers
193+
assert b"Bad Request" in body

0 commit comments

Comments
 (0)