Skip to content

Commit 789e2f1

Browse files
authored
Disallow invalid header characters (#725)
* Disallow invalid header characters * Linting * Fix escape sequence
1 parent 81f2136 commit 789e2f1

File tree

1 file changed

+9
-0
lines changed

1 file changed

+9
-0
lines changed

uvicorn/protocols/http/httptools_impl.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import asyncio
22
import http
33
import logging
4+
import re
45
import urllib
56

67
import httptools
@@ -13,6 +14,9 @@
1314
is_ssl,
1415
)
1516

17+
HEADER_RE = re.compile(b'[\x00-\x1F\x7F()<>@,;:[]={} \t\\"]')
18+
HEADER_VALUE_RE = re.compile(b"[\x00-\x1F\x7F]")
19+
1620

1721
def _get_status_line(status_code):
1822
try:
@@ -459,6 +463,11 @@ async def send(self, message):
459463
content = [STATUS_LINE[status_code]]
460464

461465
for name, value in headers:
466+
if HEADER_RE.search(name):
467+
raise RuntimeError("Invalid HTTP header name.")
468+
if HEADER_VALUE_RE.search(value):
469+
raise RuntimeError("Invalid HTTP header value.")
470+
462471
name = name.lower()
463472
if name == b"content-length" and self.chunked_encoding is None:
464473
self.expected_content_length = int(value.decode())

0 commit comments

Comments
 (0)