Skip to content

Commit 753a5f3

Browse files
committed
Validate content length before trailers
1 parent 77ac36c commit 753a5f3

1 file changed

Lines changed: 7 additions & 18 deletions

File tree

‎src/h2/stream.py‎

Lines changed: 7 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1094,25 +1094,14 @@ def receive_headers(self,
10941094
).stream_ended = cast("StreamEnded", es_events[0])
10951095
events += es_events
10961096

1097-
is_trailers = isinstance(headers_event, TrailersReceived)
1098-
1099-
if is_trailers:
1100-
# A trailers section carries no message content, so a
1101-
# content-length in it is validated for syntax only and must never
1102-
# redefine the expected body length: run the same parse, then put
1103-
# the previous expectation back. The trailers section is the end of
1104-
# the message, so the body must be validated here too, otherwise a
1105-
# stream that ends with trailers escapes the length check
1106-
# entirely.
1107-
expected_content_length = self._expected_content_length
1108-
self._initialize_content_length(headers)
1109-
self._expected_content_length = expected_content_length
1110-
if not end_stream:
1111-
msg = "Trailers must have END_STREAM set"
1112-
raise ProtocolError(msg)
1097+
if isinstance(headers_event, TrailersReceived) and end_stream:
11131098
self._track_content_length(0, end_stream=True)
1114-
else:
1115-
self._initialize_content_length(headers)
1099+
1100+
self._initialize_content_length(headers)
1101+
1102+
if isinstance(headers_event, TrailersReceived) and not end_stream:
1103+
msg = "Trailers must have END_STREAM set"
1104+
raise ProtocolError(msg)
11161105

11171106
hdr_validation_flags = self._build_hdr_validation_flags(events)
11181107
headers_event.headers = self._process_received_headers(

0 commit comments

Comments
 (0)