Skip to content

Commit 7f3a14f

Browse files
committed
forbid GET requests with body content
Signed-off-by: Luminita Voicu <[email protected]>
1 parent 83dcfdd commit 7f3a14f

File tree

2 files changed

+16
-8
lines changed

2 files changed

+16
-8
lines changed

src/request.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,9 @@ impl Request {
236236
None
237237
}
238238
content_length => {
239+
if request_line.method == Method::Get {
240+
return Err(RequestError::InvalidRequest);
241+
}
239242
// Multiplication is safe because `CRLF_LEN` is a small constant.
240243
// Addition is also safe because `headers_end` started out as the result
241244
// of `find(<something>, CRLFCRLF)`, then `CRLF_LEN` was subtracted from it.
@@ -456,6 +459,15 @@ mod tests {
456459
RequestError::InvalidRequest
457460
);
458461

462+
// Test for invalid Request (`GET` requests should have no body).
463+
let request_bytes = b"GET /machine-config HTTP/1.1\r\n\
464+
Content-Length: 13\r\n\
465+
Content-Type: application/json\r\n\r\nwhatever body";
466+
assert_eq!(
467+
Request::try_from(request_bytes).unwrap_err(),
468+
RequestError::InvalidRequest
469+
);
470+
459471
// Test for a request with the headers we are looking for.
460472
let request = Request::try_from(
461473
b"PATCH http://localhost/home HTTP/1.1\r\n\

src/server.rs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -754,8 +754,7 @@ mod tests {
754754
second_socket
755755
.write_all(
756756
b"GET /machine-config HTTP/1.1\r\n\
757-
Content-Length: 20\r\n\
758-
Content-Type: application/json\r\n\r\nwhatever second body",
757+
Content-Type: application/json\r\n\r\n",
759758
)
760759
.unwrap();
761760

@@ -766,8 +765,7 @@ mod tests {
766765
second_server_request.request,
767766
Request::try_from(
768767
b"GET /machine-config HTTP/1.1\r\n\
769-
Content-Length: 20\r\n\
770-
Content-Type: application/json\r\n\r\nwhatever second body"
768+
Content-Type: application/json\r\n\r\n"
771769
)
772770
.unwrap()
773771
);
@@ -980,8 +978,7 @@ mod tests {
980978
second_socket
981979
.write_all(
982980
b"GET /machine-config HTTP/1.1\r\n\
983-
Content-Length: 20\r\n\
984-
Content-Type: application/json\r\n\r\nwhatever second body",
981+
Content-Type: application/json\r\n\r\n",
985982
)
986983
.unwrap();
987984

@@ -992,8 +989,7 @@ mod tests {
992989
second_server_request.request,
993990
Request::try_from(
994991
b"GET /machine-config HTTP/1.1\r\n\
995-
Content-Length: 20\r\n\
996-
Content-Type: application/json\r\n\r\nwhatever second body"
992+
Content-Type: application/json\r\n\r\n"
997993
)
998994
.unwrap()
999995
);

0 commit comments

Comments
 (0)