Skip to content

Commit 53f57b7

Browse files
committed
dns_server: fix DOH Get issue.
1 parent 2c9ca2e commit 53f57b7

File tree

3 files changed

+39
-8
lines changed

3 files changed

+39
-8
lines changed

src/dns_server.c

+21-2
Original file line numberDiff line numberDiff line change
@@ -7759,6 +7759,7 @@ static int _dns_server_tcp_process_one_request(struct dns_server_conn_tcp_client
77597759
int len = 0;
77607760
struct http_head *http_head = NULL;
77617761
uint8_t *http_decode_data = NULL;
7762+
char *base64_query = NULL;
77627763

77637764
/* Handling multiple requests */
77647765
for (;;) {
@@ -7806,12 +7807,26 @@ static int _dns_server_tcp_process_one_request(struct dns_server_conn_tcp_client
78067807
goto errout;
78077808
}
78087809

7809-
const char *base64_query = http_head_get_params_value(http_head, "dns");
7810-
if (base64_query == NULL) {
7810+
const char *dns_query = http_head_get_params_value(http_head, "dns");
7811+
if (dns_query == NULL) {
78117812
tlog(TLOG_DEBUG, "query is null.");
78127813
goto errout;
78137814
}
78147815

7816+
if (base64_query == NULL) {
7817+
base64_query = malloc(DNS_IN_PACKSIZE);
7818+
if (base64_query == NULL) {
7819+
tlog(TLOG_DEBUG, "malloc failed.");
7820+
goto errout;
7821+
}
7822+
}
7823+
7824+
if (urldecode(base64_query, DNS_IN_PACKSIZE, dns_query) < 0) {
7825+
tlog(TLOG_DEBUG, "urldecode query failed.");
7826+
goto errout;
7827+
}
7828+
7829+
78157830
if (http_decode_data == NULL) {
78167831
http_decode_data = malloc(DNS_IN_PACKSIZE);
78177832
if (http_decode_data == NULL) {
@@ -7887,6 +7902,10 @@ static int _dns_server_tcp_process_one_request(struct dns_server_conn_tcp_client
78877902
free(http_decode_data);
78887903
}
78897904

7905+
if (base64_query) {
7906+
free(base64_query);
7907+
}
7908+
78907909
if ((ret == RECV_ERROR_FAIL || ret == RECV_ERROR_INVALID_PACKET) &&
78917910
tcpclient->head.type == DNS_CONN_TYPE_HTTPS_CLIENT) {
78927911
_dns_server_reply_http_error(tcpclient, 400, "Bad Request", "Bad Request");

src/util.c

+17-5
Original file line numberDiff line numberDiff line change
@@ -336,14 +336,14 @@ int getsocket_inet(int fd, struct sockaddr *addr, socklen_t *addr_len)
336336
switch (addr_store.ss_family) {
337337
case AF_INET: {
338338
struct sockaddr_in *addr_in = NULL;
339-
addr_in = (struct sockaddr_in *)addr;
339+
addr_in = (struct sockaddr_in *)&addr_store;
340340
addr_in->sin_family = AF_INET;
341341
*addr_len = sizeof(struct sockaddr_in);
342342
memcpy(addr, addr_in, sizeof(struct sockaddr_in));
343343
} break;
344344
case AF_INET6: {
345345
struct sockaddr_in6 *addr_in6 = NULL;
346-
addr_in6 = (struct sockaddr_in6 *)addr;
346+
addr_in6 = (struct sockaddr_in6 *)&addr_store;
347347
if (IN6_IS_ADDR_V4MAPPED(&addr_in6->sin6_addr)) {
348348
struct sockaddr_in addr_in4;
349349
memset(&addr_in4, 0, sizeof(addr_in4));
@@ -552,9 +552,10 @@ int parse_uri(const char *value, char *scheme, char *host, int *port, char *path
552552
return parse_uri_ext(value, scheme, NULL, NULL, host, port, path);
553553
}
554554

555-
void urldecode(char *dst, const char *src)
555+
int urldecode(char *dst, int dst_maxlen, const char *src)
556556
{
557557
char a, b;
558+
int len = 0;
558559
while (*src) {
559560
if ((*src == '%') && ((a = src[1]) && (b = src[2])) && (isxdigit(a) && isxdigit(b))) {
560561
if (a >= 'a') {
@@ -584,8 +585,15 @@ void urldecode(char *dst, const char *src)
584585
} else {
585586
*dst++ = *src++;
586587
}
588+
589+
len++;
590+
if (len >= dst_maxlen - 1) {
591+
return -1;
592+
}
587593
}
588594
*dst++ = '\0';
595+
596+
return len;
589597
}
590598

591599
int parse_uri_ext(const char *value, char *scheme, char *user, char *password, char *host, int *port, char *path)
@@ -635,11 +643,15 @@ int parse_uri_ext(const char *value, char *scheme, char *user, char *password, c
635643
*sep = '\0';
636644
sep = sep + 1;
637645
if (password) {
638-
urldecode(password, sep);
646+
if (urldecode(password, 128, sep) < 0) {
647+
return -1;
648+
}
639649
}
640650
}
641651
if (user) {
642-
urldecode(user, user_password);
652+
if (urldecode(user, 128, user_password) < 0) {
653+
return -1;
654+
}
643655
}
644656
} else {
645657
host_part = user_pass_host_part;

src/util.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ int parse_uri(const char *value, char *scheme, char *host, int *port, char *path
8888

8989
int parse_uri_ext(const char *value, char *scheme, char *user, char *password, char *host, int *port, char *path);
9090

91-
void urldecode(char *dst, const char *src);
91+
int urldecode(char *dst, int dst_maxlen, const char *src);
9292

9393
int set_fd_nonblock(int fd, int nonblock);
9494

0 commit comments

Comments
 (0)