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

Add HTTP method validation #6533

Merged
merged 6 commits into from Jan 25, 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/6533.feature
@@ -0,0 +1 @@
Add HTTP method validation.
10 changes: 9 additions & 1 deletion aiohttp/client_reqrep.py
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
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
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