Skip to content

Commit 78d8173

Browse files
Fix flake8 D301 Use r""" if any backslashes in a docstring
1 parent 659bc5a commit 78d8173

File tree

1 file changed

+38
-2
lines changed

1 file changed

+38
-2
lines changed

tests/test_cookie_helpers.py

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
SimpleCookie,
1010
_unquote as simplecookie_unquote,
1111
)
12+
import typing
13+
from unittest.mock import patch
1214

1315
import pytest
1416

@@ -1163,7 +1165,7 @@ def test_parse_set_cookie_headers_ctl_chars_from_octal(
11631165

11641166

11651167
def test_parse_set_cookie_headers_literal_ctl_chars() -> None:
1166-
"""Ensure literal control characters in a cookie value don't crash the parser.
1168+
r"""Ensure literal control characters in a cookie value don't crash the parser.
11671169
11681170
If the raw header itself contains a control character (e.g. BEL \\x07),
11691171
both the decoded value and coded_value are unsalvageable. The parser
@@ -1650,7 +1652,7 @@ def test_parse_cookie_header_empty_key_in_fallback(
16501652

16511653

16521654
def test_parse_cookie_header_literal_ctl_chars() -> None:
1653-
"""Ensure literal control characters in a cookie value don't crash the parser.
1655+
r"""Ensure literal control characters in a cookie value don't crash the parser.
16541656
16551657
If the raw header itself contains a control character (e.g. BEL \\x07),
16561658
the cookie is unsalvageable. The parser should gracefully skip it.
@@ -1850,3 +1852,37 @@ def test_unquote_compatibility_with_simplecookie(test_value: str) -> None:
18501852
f"our={_unquote(test_value)!r}, "
18511853
f"SimpleCookie={simplecookie_unquote(test_value)!r}"
18521854
)
1855+
1856+
1857+
@pytest.fixture
1858+
def mock_strict_morsel() -> typing.Iterator[None]:
1859+
original_setstate = Morsel.__setstate__ # type: ignore[attr-defined]
1860+
1861+
def _mock_setstate(self: Morsel[str], state: dict[str, str]) -> None:
1862+
if any(ord(c) < 32 for c in state.get("value", "")):
1863+
raise CookieError()
1864+
original_setstate(self, state)
1865+
1866+
with patch(
1867+
"aiohttp._cookie_helpers.Morsel.__setstate__",
1868+
autospec=True,
1869+
side_effect=_mock_setstate,
1870+
):
1871+
yield
1872+
1873+
1874+
def test_cookie_helpers_cve_fallback(mock_strict_morsel: None) -> None:
1875+
m: Morsel[str] = Morsel()
1876+
assert helpers._safe_set_morsel_state(m, "k", "v\n", "v\\012") is True
1877+
assert m.value == "v\\012"
1878+
1879+
assert helpers._safe_set_morsel_state(Morsel(), "k", "v\n", "v\n") is False
1880+
1881+
cookie: Morsel[str] = Morsel()
1882+
cookie._key, cookie._value, cookie._coded_value = "k", "v\n", "v\n" # type: ignore[attr-defined]
1883+
assert preserve_morsel_with_coded_value(cookie) is cookie
1884+
1885+
assert parse_cookie_header("f=b\x07r;") == []
1886+
assert parse_cookie_header("f=b\x07r") == []
1887+
assert parse_cookie_header("f=\"b\x07r\";") == []
1888+
assert parse_set_cookie_headers(["f=\"b\x07r\";"]) == []

0 commit comments

Comments
 (0)