Skip to content

Commit 2c31056

Browse files
committed
[+] Fix: Enhance request parsing to avoid hq parsing error
1 parent 79a8a2b commit 2c31056

File tree

1 file changed

+22
-2
lines changed

1 file changed

+22
-2
lines changed

demo/xqc_hq_request.c

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -231,13 +231,27 @@ ssize_t
231231
xqc_hq_parse_req(xqc_hq_request_t *hqr, char *res, size_t sz, uint8_t *fin)
232232
{
233233
char method[16] = {0};
234-
int ret = sscanf(hqr->req_recv_buf, "%s %s", method, res);
234+
char fmt[32] = {0};
235+
size_t method_cap = sizeof(method) - 1;
236+
size_t res_cap;
237+
int ret;
238+
size_t request_line_len;
239+
240+
if (sz <= 1) {
241+
PRINT_LOG("|invalid resource buffer size|sz:%zu|", sz);
242+
return -XQC_EPROTO;
243+
}
244+
245+
res_cap = sz - 1;
246+
snprintf(fmt, sizeof(fmt), "%%%zus %%%zus", method_cap, res_cap);
247+
248+
ret = sscanf((char *)hqr->req_recv_buf, fmt, method, res);
235249
if (ret <= 0) {
236250
PRINT_LOG("|parse hq request failed: %s", hqr->req_recv_buf);
237251
return -XQC_EPROTO;
238252
}
239253

240-
int request_line_len = strlen(method) + strlen(res) + 1; /* method + ' ' + path */
254+
request_line_len = strlen(method) + strlen(res) + 1; /* method + ' ' + path */
241255
if (request_line_len + 2 <= hqr->recv_buf_len
242256
&& (*(hqr->req_recv_buf + request_line_len) == '\r')
243257
&& (*(hqr->req_recv_buf + request_line_len + 1) == '\n'))
@@ -284,6 +298,12 @@ xqc_hq_request_recv_req(xqc_hq_request_t *hqr, char *res_buf, size_t buf_sz, uin
284298
} while (read > 0 && !hqr->fin);
285299

286300

301+
if (hqr->recv_cnt >= hqr->recv_buf_len) {
302+
PRINT_LOG("|hq request too long|len:%zu|", hqr->recv_cnt);
303+
return -XQC_EPROTO;
304+
}
305+
hqr->req_recv_buf[hqr->recv_cnt] = '\0';
306+
287307
if (NULL == hqr->resource_buf) {
288308
hqr->resource_buf = xqc_malloc(XQC_HQ_REQUEST_RESOURCE_MAX_LEN);
289309
if (NULL == hqr->resource_buf) {

0 commit comments

Comments
 (0)