Skip to content

Commit d1501a0

Browse files
ubnt-intrepidseanmonstar
authored andcommitted
fix(server): prohibit the length headers on successful CONNECT
Closes #1783
1 parent 8c345d5 commit d1501a0

File tree

1 file changed

+10
-6
lines changed

1 file changed

+10
-6
lines changed

src/proto/h1/role.rs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -233,13 +233,18 @@ impl Http1Transaction for Server {
233233
);
234234
debug_assert!(!msg.title_case_headers, "no server config for title case headers");
235235

236+
let mut wrote_len = false;
237+
236238
// hyper currently doesn't support returning 1xx status codes as a Response
237239
// This is because Service only allows returning a single Response, and
238240
// so if you try to reply with a e.g. 100 Continue, you have no way of
239241
// replying with the latter status code response.
240-
let is_upgrade = msg.head.subject == StatusCode::SWITCHING_PROTOCOLS
241-
|| (msg.req_method == &Some(Method::CONNECT) && msg.head.subject.is_success());
242-
let (ret, mut is_last) = if is_upgrade {
242+
let (ret, mut is_last) = if msg.head.subject == StatusCode::SWITCHING_PROTOCOLS {
243+
(Ok(()), true)
244+
} else if msg.req_method == &Some(Method::CONNECT) && msg.head.subject.is_success() {
245+
// Sending content-length or transfer-encoding header on 2xx response
246+
// to CONNECT is forbidden in RFC 7231.
247+
wrote_len = true;
243248
(Ok(()), true)
244249
} else if msg.head.subject.is_informational() {
245250
warn!("response with 1xx status code not supported");
@@ -282,13 +287,12 @@ impl Http1Transaction for Server {
282287
}
283288

284289
let mut encoder = Encoder::length(0);
285-
let mut wrote_len = false;
286290
let mut wrote_date = false;
287291
'headers: for (name, mut values) in msg.head.headers.drain() {
288292
match name {
289293
header::CONTENT_LENGTH => {
290294
if wrote_len {
291-
warn!("transfer-encoding and content-length both found, canceling");
295+
warn!("unexpected content-length found, canceling");
292296
rewind(dst);
293297
return Err(::Error::new_header());
294298
}
@@ -397,7 +401,7 @@ impl Http1Transaction for Server {
397401
},
398402
header::TRANSFER_ENCODING => {
399403
if wrote_len {
400-
warn!("transfer-encoding and content-length both found, canceling");
404+
warn!("unexpected transfer-encoding found, canceling");
401405
rewind(dst);
402406
return Err(::Error::new_header());
403407
}

0 commit comments

Comments
 (0)