Skip to content

Commit b001eba

Browse files
committed
fix: Ensure HTTP Host Header format compliance
- Non-default ports are explicitly included in the Host header - Default ports (443 for HTTPS) are omitted - IPv6 addresses are properly enclosed in square brackets (`[ ]`) per RFC 3986
1 parent b969ee6 commit b001eba

File tree

4 files changed

+21
-2
lines changed

4 files changed

+21
-2
lines changed

Diff for: src/dns_client.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -1135,7 +1135,7 @@ static int _dns_client_server_add(char *server_ip, char *server_host, int port,
11351135
if (server_host) {
11361136
safe_strncpy(flag_https->httphost, server_host, DNS_MAX_CNAME_LEN);
11371137
} else {
1138-
safe_strncpy(flag_https->httphost, server_ip, DNS_MAX_CNAME_LEN);
1138+
set_http_host(server_ip, port, DEFAULT_DNS_HTTPS_PORT, flag_https->httphost);
11391139
}
11401140
}
11411141
sock_type = SOCK_STREAM;

Diff for: src/dns_conf.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -1163,7 +1163,7 @@ static int _config_server(int argc, char *argv[], dns_server_type_t type, int de
11631163
}
11641164

11651165
if (server->httphost[0] == '\0') {
1166-
safe_strncpy(server->httphost, server->server, DNS_MAX_CNAME_LEN);
1166+
set_http_host(server->server, server->port, DEFAULT_DNS_HTTPS_PORT, server->httphost);
11671167
}
11681168
}
11691169

Diff for: src/util.c

+17
Original file line numberDiff line numberDiff line change
@@ -2225,6 +2225,23 @@ int parser_mac_address(const char *in_mac, uint8_t mac[6])
22252225
return -1;
22262226
}
22272227

2228+
int set_http_host(const char *uri_host, int port, int default_port, char *host)
2229+
{
2230+
int is_ipv6;
2231+
2232+
if (uri_host == NULL || port <= 0 || host == NULL) {
2233+
return -1;
2234+
}
2235+
2236+
is_ipv6 = check_is_ipv6(uri_host);
2237+
if (port == default_port) {
2238+
snprintf(host, DNS_MAX_CNAME_LEN, "%s%s%s", is_ipv6 == 0 ? "[" : "", uri_host, is_ipv6 == 0 ? "]" : "");
2239+
} else {
2240+
snprintf(host, DNS_MAX_CNAME_LEN, "%s%s%s:%d", is_ipv6 == 0 ? "[" : "", uri_host, is_ipv6 == 0 ? "]" : "", port);
2241+
}
2242+
return 0;
2243+
}
2244+
22282245
#if defined(DEBUG) || defined(TEST)
22292246
struct _dns_read_packet_info {
22302247
int data_len;

Diff for: src/util.h

+2
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,8 @@ void daemon_close_stdfds(void);
184184

185185
int write_file(const char *filename, void *data, int data_len);
186186

187+
int set_http_host(const char *uri_host, int port, int default_port, char *host);
188+
187189
int dns_packet_save(const char *dir, const char *type, const char *from, const void *packet, int packet_len);
188190

189191
int dns_packet_debug(const char *packet_file);

0 commit comments

Comments
 (0)