Skip to content

Commit 8195361

Browse files
authored
Ensure request method is a valid token
1 parent fe249c9 commit 8195361

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

h11/_events.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
from dataclasses import dataclass, field
1111
from typing import Any, cast, Dict, List, Tuple, Union
1212

13-
from ._abnf import request_target
13+
from ._abnf import method, request_target
1414
from ._headers import Headers, normalize_and_validate
1515
from ._util import bytesify, LocalProtocolError, validate
1616

@@ -25,6 +25,7 @@
2525
"ConnectionClosed",
2626
]
2727

28+
method_re = re.compile(method.encode("ascii"))
2829
request_target_re = re.compile(request_target.encode("ascii"))
2930

3031

@@ -117,6 +118,7 @@ def __init__(
117118
if host_count > 1:
118119
raise LocalProtocolError("Found multiple Host: headers")
119120

121+
validate(method_re, self.method, "Illegal method characters")
120122
validate(request_target_re, self.target, "Illegal target characters")
121123

122124
# This is an unhashable type.

h11/tests/test_events.py

+9
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,15 @@ def test_events() -> None:
8484
method="GET", target=target, headers=[("Host", "a")], http_version="1.1"
8585
)
8686

87+
# Request method is validated
88+
with pytest.raises(LocalProtocolError):
89+
Request(
90+
method="GET / HTTP/1.1",
91+
target=target,
92+
headers=[("Host", "a")],
93+
http_version="1.1",
94+
)
95+
8796
ir = InformationalResponse(status_code=100, headers=[("Host", "a")])
8897
assert ir.status_code == 100
8998
assert ir.headers == [(b"host", b"a")]

0 commit comments

Comments
 (0)