tighten MQTT and HTTP handlers

This commit is contained in:
Sergio R. Caprile committed 2026-12-02 14:31:43 -03:00
1 parent 948f16bab3
commit 24f5519463
4 files changed
+36 -16

No files matched your search

+10 -8
View File
@@ -5385,7 +5385,7 @@ static void http_cb(struct mg_connection *c, int ev, void *ev_data) {
while (c->pfn_data == NULL && ofs < c->recv.len) {
const char *buf = (char *) c->recv.buf + ofs;
int n = mg_http_parse(buf, c->recv.len - ofs, &hm);
struct mg_str *te; // Transfer - encoding header
struct mg_str *hcl, *te; // Content-Length and Transfer-encoding headers
bool is_chunked = false, is_http_1_0 = false;
size_t old_len = c->recv.len;
if (n < 0) {
@@ -5405,8 +5405,11 @@ static void http_cb(struct mg_connection *c, int ev, void *ev_data) {
c->pfn = NULL;
return;
}
if (ev == MG_EV_CLOSE) { // If client did not set Content-Length
hm.message.len = c->recv.len - ofs; // and closes now, deliver MSG
hcl = mg_http_get_header(&hm, "Content-Length");
if (ev == MG_EV_CLOSE && (hcl == NULL || c->is_client)) {
// - a server responded to HEAD with optional Content-Length (no body)
// - a client sent no Content-Length and closed --> deliver MSG
hm.message.len = c->recv.len - ofs;
hm.body.len = hm.message.len - (size_t) (hm.body.buf - hm.message.buf);
}
is_http_1_0 =
@@ -5420,7 +5423,7 @@ static void http_cb(struct mg_connection *c, int ev, void *ev_data) {
mg_error(c, "Invalid Transfer-Encoding"); // See #2460
return;
}
} else if (mg_http_get_header(&hm, "Content-length") == NULL) {
} else if (hcl == NULL) {
// #2593: HTTP packets must contain either Transfer-Encoding or
// Content-length
bool is_response = mg_ncasecmp(hm.method.buf, "HTTP/", 5) == 0;
@@ -8573,9 +8576,8 @@ int mg_mqtt_parse(const uint8_t *buf, size_t len, uint8_t version,
m->ack = p[1];
p += 2;
if (version == 5) {
if (p >= end) return MQTT_MALFORMED;
len_len = (uint32_t) decode_varint(p, (size_t) (end - p), &tmp);
if (!len_len) return MQTT_MALFORMED;
if (len_len == 0) return MQTT_MALFORMED;
m->props_size = (size_t) tmp;
m->props_start = (size_t) (p + len_len - buf);
p += len_len + m->props_size;
@@ -8606,9 +8608,9 @@ int mg_mqtt_parse(const uint8_t *buf, size_t len, uint8_t version,
p += 2;
}
if (p > end) return MQTT_MALFORMED;
if (version == 5 && p + 2 < end) {
if (version == 5) {
len_len = (uint32_t) decode_varint(p, (size_t) (end - p), &tmp);
if (!len_len) return MQTT_MALFORMED;
if (len_len == 0) return MQTT_MALFORMED;
m->props_size = (size_t) tmp;
m->props_start = (size_t) (p + len_len - buf);
p += len_len + m->props_size;
+7 -4
View File
@@ -1152,7 +1152,7 @@ static void http_cb(struct mg_connection *c, int ev, void *ev_data) {
while (c->pfn_data == NULL && ofs < c->recv.len) {
const char *buf = (char *) c->recv.buf + ofs;
int n = mg_http_parse(buf, c->recv.len - ofs, &hm);
struct mg_str *te; // Transfer - encoding header
struct mg_str *hcl, *te; // Content-Length and Transfer-encoding headers
bool is_chunked = false, is_http_1_0 = false;
size_t old_len = c->recv.len;
if (n < 0) {
@@ -1172,8 +1172,11 @@ static void http_cb(struct mg_connection *c, int ev, void *ev_data) {
c->pfn = NULL;
return;
}
if (ev == MG_EV_CLOSE) { // If client did not set Content-Length
hm.message.len = c->recv.len - ofs; // and closes now, deliver MSG
hcl = mg_http_get_header(&hm, "Content-Length");
if (ev == MG_EV_CLOSE && (hcl == NULL || c->is_client)) {
// - a server responded to HEAD with optional Content-Length (no body)
// - a client sent no Content-Length and closed --> deliver MSG
hm.message.len = c->recv.len - ofs;
hm.body.len = hm.message.len - (size_t) (hm.body.buf - hm.message.buf);
}
is_http_1_0 =
@@ -1187,7 +1190,7 @@ static void http_cb(struct mg_connection *c, int ev, void *ev_data) {
mg_error(c, "Invalid Transfer-Encoding"); // See #2460
return;
}
} else if (mg_http_get_header(&hm, "Content-length") == NULL) {
} else if (hcl == NULL) {
// #2593: HTTP packets must contain either Transfer-Encoding or
// Content-length
bool is_response = mg_ncasecmp(hm.method.buf, "HTTP/", 5) == 0;
+3 -4
View File
@@ -440,9 +440,8 @@ int mg_mqtt_parse(const uint8_t *buf, size_t len, uint8_t version,
m->ack = p[1];
p += 2;
if (version == 5) {
if (p >= end) return MQTT_MALFORMED;
len_len = (uint32_t) decode_varint(p, (size_t) (end - p), &tmp);
if (!len_len) return MQTT_MALFORMED;
if (len_len == 0) return MQTT_MALFORMED;
m->props_size = (size_t) tmp;
m->props_start = (size_t) (p + len_len - buf);
p += len_len + m->props_size;
@@ -473,9 +472,9 @@ int mg_mqtt_parse(const uint8_t *buf, size_t len, uint8_t version,
p += 2;
}
if (p > end) return MQTT_MALFORMED;
if (version == 5 && p + 2 < end) {
if (version == 5) {
len_len = (uint32_t) decode_varint(p, (size_t) (end - p), &tmp);
if (!len_len) return MQTT_MALFORMED;
if (len_len == 0) return MQTT_MALFORMED;
m->props_size = (size_t) tmp;
m->props_start = (size_t) (p + len_len - buf);
p += len_len + m->props_size;
+16
View File
@@ -817,6 +817,12 @@ static void test_mqtt_parse(void) {
struct mg_mqtt_prop prop;
// MQTT5 PUBLISH with Subscription Identifier = 5
const uint8_t sub_id_pkt[] = {0x30, 0x06, 0x00, 0x01, 'x', 0x02, 0x0B, 0x05};
// MQTT5 PUBLISH with no properties and no payload
const uint8_t pub_empty[] = {0x30, 0x04, 0x00, 0x01, 'x', 0x00};
// MQTT5 PUBLISH with no properties and a one-byte payload
const uint8_t pub_byte[] = {0x30, 0x05, 0x00, 0x01, 'x', 0x00, 'y'};
// MQTT5 PUBLISH without its mandatory Property Length
const uint8_t pub_no_props[] = {0x30, 0x03, 0x00, 0x01, 'x'};
// MQTT5 CONNACK with Server Keep Alive = 60
const uint8_t connack_pkt[] = {0x20, 0x06, 0x00, 0x00, 0x03, 0x13, 0x00, 0x3C};
@@ -825,6 +831,13 @@ static void test_mqtt_parse(void) {
ASSERT(mg_mqtt_parse(sub_id_pkt, sizeof(sub_id_pkt), 5, &mm) == MQTT_OK);
mg_mqtt_next_prop(&mm, &prop, 0);
ASSERT(mg_mqtt_parse(pub_empty, sizeof(pub_empty), 5, &mm) == MQTT_OK);
ASSERT(mm.props_size == 0 && mm.data.len == 0);
ASSERT(mg_mqtt_parse(pub_byte, sizeof(pub_byte), 5, &mm) == MQTT_OK);
ASSERT(mm.props_size == 0 && mm.data.len == 1 && mm.data.buf[0] == 'y');
ASSERT(mg_mqtt_parse(pub_no_props, sizeof(pub_no_props), 5, &mm) ==
MQTT_MALFORMED);
ASSERT(mg_mqtt_parse(connack_pkt, sizeof(connack_pkt), 5, &mm) == MQTT_OK);
ASSERT(mm.ack == 0);
ASSERT(mg_mqtt_next_prop(&mm, &prop, 0) > 0);
@@ -1277,6 +1290,9 @@ static void test_http_server(void) {
"POST /body HTTP/1.1\r\n"
"Content-Length: 4\r\n\r\nkuku") == 200);
ASSERT(cmpbody(buf, "kuku") == 0);
ASSERT(fetch(&mgr, buf, url, "%s",
"POST /body HTTP/1.1\r\n"
"Content-Length: 4\r\n\r\nku") == 0);
ASSERT(fetch(&mgr, buf, url, "GET /ssi HTTP/1.1\r\n\r\n") == 301);
ASSERT(fetch(&mgr, buf, url, "GET /ssi/ HTTP/1.1\r\n\r\n") == 200);