Skip to content

Commit

Permalink
Add HTTP method validation (aio-libs#6533)
Browse files Browse the repository at this point in the history
  • Loading branch information
asvetlov authored Jan 25, 2022
1 parent a1d4dac commit 75fca0b
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGES/6533.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add HTTP method validation.
10 changes: 9 additions & 1 deletion aiohttp/client_reqrep.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,9 @@
from .tracing import Trace


_CONTAINS_CONTROL_CHAR_RE = re.compile(r"[^-!#$%&'*+.^_`|~0-9a-zA-Z]")


def _gen_default_accept_encoding() -> str:
return "gzip, deflate, br" if HAS_BROTLI else "gzip, deflate"

Expand Down Expand Up @@ -208,7 +211,12 @@ def __init__(
proxy_headers: Optional[LooseHeaders] = None,
traces: Optional[List["Trace"]] = None,
):

match = _CONTAINS_CONTROL_CHAR_RE.search(method)
if match:
raise ValueError(
f"Method cannot contain non-token characters {method!r} "
"(found at least {match.group()!r})"
)
assert isinstance(url, URL), url
assert isinstance(proxy, (URL, type(None))), proxy
# FIXME: session is None in tests only, need to fix tests
Expand Down
5 changes: 5 additions & 0 deletions tests/test_client_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,11 @@ def test_method3(make_request: Any) -> None:
assert req.method == "HEAD"


def test_method_invalid(make_request: Any) -> None:
with pytest.raises(ValueError, match="Method cannot contain non-token characters"):
make_request("METHOD WITH\nWHITESPACES", "http://python.org/")


def test_version_1_0(make_request: Any) -> None:
req = make_request("get", "http://python.org/", version="1.0")
assert req.version == (1, 0)
Expand Down
9 changes: 7 additions & 2 deletions tests/test_web_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,10 @@ def test_base_ctor() -> None:

assert "GET" == req.method
assert HttpVersion(1, 1) == req.version
assert req.host == socket.getfqdn()
# MacOS may return CamelCased host name, need .lower()
# FQDN can be wider than host, e.g.
# 'fv-az397-495' in 'fv-az397-495.internal.cloudapp.net'
assert req.host.lower() in socket.getfqdn().lower()
assert "/path/to?a=1&b=2" == req.path_qs
assert "/path/to" == req.path
assert "a=1&b=2" == req.query_string
Expand All @@ -71,7 +74,9 @@ def test_ctor() -> None:
assert "GET" == req.method
assert HttpVersion(1, 1) == req.version
# MacOS may return CamelCased host name, need .lower()
assert req.host.lower() == socket.getfqdn().lower()
# FQDN can be wider than host, e.g.
# 'fv-az397-495' in 'fv-az397-495.internal.cloudapp.net'
assert req.host.lower() in socket.getfqdn().lower()
assert "/path/to?a=1&b=2" == req.path_qs
assert "/path/to" == req.path
assert "a=1&b=2" == req.query_string
Expand Down

0 comments on commit 75fca0b

Please sign in to comment.