Skip to content

Commit 5e12a69

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

File tree

1 file changed

+37
-2
lines changed

1 file changed

+37
-2
lines changed

tests/test_cookie_helpers.py

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

1314
import pytest
1415

@@ -1163,7 +1164,7 @@ def test_parse_set_cookie_headers_ctl_chars_from_octal(
11631164

11641165

11651166
def test_parse_set_cookie_headers_literal_ctl_chars() -> None:
1166-
"""Ensure literal control characters in a cookie value don't crash the parser.
1167+
r"""Ensure literal control characters in a cookie value don't crash the parser.
11671168
11681169
If the raw header itself contains a control character (e.g. BEL \\x07),
11691170
both the decoded value and coded_value are unsalvageable. The parser
@@ -1650,7 +1651,7 @@ def test_parse_cookie_header_empty_key_in_fallback(
16501651

16511652

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

0 commit comments

Comments
 (0)