Skip to content

Commit

Permalink
Disallow newlines in reason
Browse files Browse the repository at this point in the history
  • Loading branch information
Dreamsorcerer committed Sep 17, 2024
1 parent bf022b3 commit a951374
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 0 deletions.
2 changes: 2 additions & 0 deletions aiohttp/web_exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,8 @@ def __init__(
super().__init__()
if reason is None:
reason = self.default_reason
elif "\n" in reason:
raise ValueError("Reason cannot contain \\n")

if text is None:
if not self.empty_body:
Expand Down
2 changes: 2 additions & 0 deletions aiohttp/web_response.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,8 @@ def set_status(
reason = HTTPStatus(self._status).phrase
except ValueError:
reason = ""
if "\n" in reason:
raise ValueError("Reason cannot contain \\n")
self._reason = reason

@property
Expand Down
4 changes: 4 additions & 0 deletions tests/test_web_exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,10 @@ def test_ctor_all(self) -> None:
assert resp.reason == "Done"
assert resp.status == 200

def test_multiline_reason(self) -> None:
with pytest.raises(ValueError, match=r"Reason cannot contain \\n"):
web.HTTPOk(reason="Bad\r\nInjected-header: foo")

Check failure

Code scanning / CodeQL

Unused exception object Error test

Instantiating an exception, but not raising it, has no effect.

def test_pickle(self) -> None:
resp = web.HTTPOk(
headers={"X-Custom": "value"},
Expand Down
6 changes: 6 additions & 0 deletions tests/test_web_response.py
Original file line number Diff line number Diff line change
Expand Up @@ -1076,6 +1076,12 @@ async def test_render_with_body(buf: Any, writer: Any) -> None:
)


async def test_multiline_reason(buf: Any, writer: Any) -> None:
req = make_request("GET", "/", writer=writer)

Check notice

Code scanning / CodeQL

Unused local variable Note test

Variable req is not used.
with pytest.raises(ValueError, match=r"Reason cannot contain \\n"):
Response(reason="Bad\r\nInjected-header: foo")


async def test_send_set_cookie_header(buf: Any, writer: Any) -> None:
resp = Response()
resp.cookies["name"] = "value"
Expand Down

0 comments on commit a951374

Please sign in to comment.