Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix unicode errors #7044

Merged
merged 5 commits into from
Nov 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGES/7044.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Avoid raising UnicodeDecodeError in multipart and in HTTP headers parsing.
6 changes: 3 additions & 3 deletions aiohttp/http_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ def parse_headers(
if len(bname) > self.max_field_size:
raise LineTooLong(
"request header name {}".format(
bname.decode("utf8", "xmlcharrefreplace")
bname.decode("utf8", "backslashreplace")
Dreamsorcerer marked this conversation as resolved.
Show resolved Hide resolved
),
str(self.max_field_size),
str(len(bname)),
Expand All @@ -176,7 +176,7 @@ def parse_headers(
if header_length > self.max_field_size:
raise LineTooLong(
"request header field {}".format(
bname.decode("utf8", "xmlcharrefreplace")
bname.decode("utf8", "backslashreplace")
),
str(self.max_field_size),
str(header_length),
Expand All @@ -197,7 +197,7 @@ def parse_headers(
if header_length > self.max_field_size:
raise LineTooLong(
"request header field {}".format(
bname.decode("utf8", "xmlcharrefreplace")
bname.decode("utf8", "backslashreplace")
),
str(self.max_field_size),
str(header_length),
Expand Down
7 changes: 6 additions & 1 deletion aiohttp/multipart.py
Original file line number Diff line number Diff line change
Expand Up @@ -462,8 +462,13 @@ async def form(self, *, encoding: Optional[str] = None) -> List[Tuple[str, str]]
real_encoding = encoding
else:
real_encoding = self.get_charset(default="utf-8")
try:
decoded_data = data.rstrip().decode(real_encoding)
except UnicodeDecodeError:
raise ValueError("data cannot be decoded with %s encoding" % real_encoding)

return parse_qsl(
data.rstrip().decode(real_encoding),
decoded_data,
keep_blank_values=True,
encoding=real_encoding,
)
Expand Down
8 changes: 8 additions & 0 deletions tests/test_http_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,14 @@ def test_parse_headers(parser: Any) -> None:
assert not msg.upgrade


def test_parse_headers_longline(parser: Any) -> None:
invalid_unicode_byte = b"\xd9"
header_name = b"Test" + invalid_unicode_byte + b"Header" + b"A" * 8192
text = b"GET /test HTTP/1.1\r\n" + header_name + b": test\r\n" + b"\r\n" + b"\r\n"
with pytest.raises((http_exceptions.LineTooLong, http_exceptions.BadHttpMessage)):
parser.feed_data(text)


def test_parse(parser: Any) -> None:
text = b"GET /test HTTP/1.1\r\n\r\n"
messages, upgrade, tail = parser.feed_data(text)
Expand Down
15 changes: 15 additions & 0 deletions tests/test_multipart.py
Original file line number Diff line number Diff line change
Expand Up @@ -599,6 +599,21 @@ async def test_read_form(self, newline: Any) -> None:
result = await obj.form()
assert [("foo", "bar"), ("foo", "baz"), ("boo", "")] == result

async def test_read_form_invalid_utf8(self, newline: Any) -> None:
invalid_unicode_byte = b"\xff"
data = invalid_unicode_byte + b"%s--:--" % newline
with Stream(data) as stream:
obj = aiohttp.BodyPartReader(
BOUNDARY,
{CONTENT_TYPE: "application/x-www-form-urlencoded"},
stream,
_newline=newline,
)
with pytest.raises(
ValueError, match="data cannot be decoded with utf-8 encoding"
):
await obj.form()
ret2libc marked this conversation as resolved.
Show resolved Hide resolved

async def test_read_form_encoding(self, newline: Any) -> None:
data = b"foo=bar&foo=baz&boo=%s--:--" % newline
with Stream(data) as stream:
Expand Down