Skip to content

Commit

Permalink
Added check for negative value for HTTP Content-Length header
Browse files Browse the repository at this point in the history
  • Loading branch information
allanpark committed May 16, 2023
1 parent 9088b00 commit 4663090
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 2 deletions.
4 changes: 3 additions & 1 deletion mongoose.c
Original file line number Diff line number Diff line change
Expand Up @@ -1463,7 +1463,9 @@ int mg_http_parse(const char *s, size_t len, struct mg_http_message *hm) {
mg_http_parse_headers(s, end, hm->headers,
sizeof(hm->headers) / sizeof(hm->headers[0]));
if ((cl = mg_http_get_header(hm, "Content-Length")) != NULL) {
hm->body.len = (size_t) mg_to64(*cl);
int64_t content_len = mg_to64(*cl);
if(content_len < 0) return -1;
hm->body.len = (size_t) content_len;
hm->message.len = (size_t) req_len + hm->body.len;
}

Expand Down
4 changes: 3 additions & 1 deletion src/http.c
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,9 @@ int mg_http_parse(const char *s, size_t len, struct mg_http_message *hm) {
mg_http_parse_headers(s, end, hm->headers,
sizeof(hm->headers) / sizeof(hm->headers[0]));
if ((cl = mg_http_get_header(hm, "Content-Length")) != NULL) {
hm->body.len = (size_t) mg_to64(*cl);
int64_t content_len = mg_to64(*cl);
if(content_len < 0) return -1;
hm->body.len = (size_t) content_len;
hm->message.len = (size_t) req_len + hm->body.len;
}

Expand Down
4 changes: 4 additions & 0 deletions test/unit_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -790,6 +790,10 @@ static void test_http_server(void) {
ASSERT(fetch(&mgr, buf, url, "GET /..ddot HTTP/1.0\n\n") == 301);
ASSERT(fetch(&mgr, buf, url, "GET /..ddot/ HTTP/1.0\n\n") == 200);
ASSERT(cmpbody(buf, "hi\n") == 0);
ASSERT(fetch(&mgr, buf, url, "GET /a.txt HTTP/1.0\n"
"Content-Length: -123\n\n") == 0);
ASSERT(fetch(&mgr, buf, url, "POST /a.txt HTTP/1.0\n"
"Content-Length: -123\n\n") == 0);

{
extern char *mg_http_etag(char *, size_t, size_t, time_t);
Expand Down

0 comments on commit 4663090

Please sign in to comment.