Skip to content

Commit 1f93cf5

Browse files
committed
adding check for upstream name whether equaling the host
1 parent 53ac5d6 commit 1f93cf5

File tree

2 files changed

+32
-25
lines changed

2 files changed

+32
-25
lines changed

src/ngx_stream_upsync_module.c

Lines changed: 30 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ static ngx_int_t ngx_stream_upsync_check_index(
219219
ngx_stream_upsync_server_t *upsync_server);
220220
static ngx_int_t ngx_stream_upsync_consul_parse_json(void *upsync_server);
221221
static ngx_int_t ngx_stream_upsync_etcd_parse_json(void *upsync_server);
222-
static ngx_int_t ngx_stream_upsync_check_key(u_char *key);
222+
static ngx_int_t ngx_stream_upsync_check_key(u_char *key, ngx_str_t host);
223223
static void *ngx_stream_upsync_servers(ngx_cycle_t *cycle,
224224
ngx_stream_upsync_server_t *upsync_server, ngx_flag_t flag);
225225
static void *ngx_stream_upsync_addrs(ngx_pool_t *pool, u_char *sockaddr);
@@ -712,7 +712,7 @@ ngx_stream_upsync_check_index(ngx_stream_upsync_server_t *upsync_server)
712712
NGX_INDEX_HEARDER_LEN) == 0) {
713713
p = ngx_strchr(state.headers[i][1], '\r');
714714
*p = '\0';
715-
index = strtoull((char *)state.headers[i][1],
715+
index = ngx_strtoull((char *)state.headers[i][1],
716716
(char **)NULL, 10);
717717
break;
718718
}
@@ -736,7 +736,7 @@ ngx_stream_upsync_check_index(ngx_stream_upsync_server_t *upsync_server)
736736
NGX_INDEX_ETCD_HEARDER_LEN) == 0) {
737737
p = ngx_strchr(state.headers[i][1], '\r');
738738
*p = '\0';
739-
index = strtoull((char *)state.headers[i][1],
739+
index = ngx_strtoull((char *)state.headers[i][1],
740740
(char **)NULL, 10);
741741
break;
742742
}
@@ -1153,17 +1153,12 @@ ngx_stream_upsync_consul_parse_json(void *data)
11531153
{
11541154
cJSON *temp1 = cJSON_GetObjectItem(server_next, "Key");
11551155
if (temp1 != NULL && temp1->valuestring != NULL) {
1156-
p = (u_char *)ngx_strrchr(temp1->valuestring, '/');
1157-
if (p == NULL) {
1158-
ngx_log_error(NGX_LOG_ERR, ngx_cycle->log, 0,
1159-
"consul_upsync_parse_json: %s key format is "
1160-
"illegal, contains no slash ('/')",
1161-
temp1->valuestring);
1162-
continue;
1163-
} else if (ngx_stream_upsync_check_key(p) != NGX_OK) {
1156+
if (ngx_stream_upsync_check_key((u_char *)temp1->valuestring,
1157+
upsync_server->host) != NGX_OK) {
11641158
continue;
11651159
}
11661160

1161+
p = (u_char *)ngx_strrchr(temp1->valuestring, '/');
11671162
upstream_conf = ngx_array_push(&ctx->upstream_conf);
11681163
ngx_memzero(upstream_conf, sizeof(*upstream_conf));
11691164
ngx_sprintf(upstream_conf->sockaddr, "%*s", ngx_strlen(p + 1), p + 1);
@@ -1400,16 +1395,12 @@ ngx_stream_upsync_etcd_parse_json(void *data)
14001395
{
14011396
cJSON *temp0 = cJSON_GetObjectItem(server_next, "key");
14021397
if (temp0 != NULL && temp0->valuestring != NULL) {
1403-
p = (u_char *)ngx_strrchr(temp0->valuestring, '/');
1404-
if (p == NULL) {
1405-
ngx_log_error(NGX_LOG_ERR, ngx_cycle->log, 0,
1406-
"etcd_upsync_parse_json: %s key format is illegal,"
1407-
"contains no slash ('/')", temp0->valuestring);
1408-
continue;
1409-
} else if (ngx_stream_upsync_check_key(p) != NGX_OK) {
1398+
if (ngx_stream_upsync_check_key((u_char *)temp0->valuestring,
1399+
upsync_server->host) != NGX_OK) {
14101400
continue;
14111401
}
14121402

1403+
p = (u_char *)ngx_strrchr(temp0->valuestring, '/');
14131404
upstream_conf = ngx_array_push(&ctx->upstream_conf);
14141405
ngx_memzero(upstream_conf, sizeof(*upstream_conf));
14151406
ngx_sprintf(upstream_conf->sockaddr, "%*s", ngx_strlen(p + 1), p + 1);
@@ -1552,30 +1543,44 @@ ngx_stream_upsync_etcd_parse_json(void *data)
15521543

15531544

15541545
static ngx_int_t
1555-
ngx_stream_upsync_check_key(u_char *key)
1546+
ngx_stream_upsync_check_key(u_char *key, ngx_str_t host)
15561547
{
1557-
u_char *last, *ip_p, *port_p;
1548+
u_char *last, *ip_p, *port_p, *s_p;
15581549
ngx_int_t port;
15591550

1560-
port_p = (u_char *)ngx_strchr(key, ':');
1551+
s_p = (u_char *)ngx_strrchr(key, '/');
1552+
if (s_p == NULL) {
1553+
ngx_log_error(NGX_LOG_ERR, ngx_cycle->log, 0,
1554+
"upsync_parse_json: %s key format is illegal, "
1555+
"contains no slash ('/')", key);
1556+
return NGX_ERROR;
1557+
}
1558+
if (*(s_p - host.len - 1) != '/') {
1559+
return NGX_ERROR;
1560+
}
1561+
if (ngx_strncmp((s_p - host.len), host.data, host.len) != 0) {
1562+
return NGX_ERROR;
1563+
}
1564+
1565+
port_p = (u_char *)ngx_strchr(s_p, ':');
15611566
if (port_p == NULL) {
15621567
ngx_log_error(NGX_LOG_ERR, ngx_cycle->log, 0,
1563-
"upsync_check_key: has no port in %s", key);
1568+
"upsync_check_key: has no port in %s", s_p);
15641569
return NGX_ERROR;
15651570
}
15661571

1567-
ip_p = key + 1;
1572+
ip_p = s_p + 1;
15681573
if (ngx_inet_addr(ip_p, port_p - ip_p) == INADDR_NONE) {
15691574
ngx_log_error(NGX_LOG_ERR, ngx_cycle->log, 0,
1570-
"upsync_check_key: invalid ip in %s", key);
1575+
"upsync_check_key: invalid ip in %s", s_p);
15711576
return NGX_ERROR;
15721577
}
15731578

15741579
last = ip_p + ngx_strlen(ip_p);
15751580
port = ngx_atoi(port_p + 1, last - port_p - 1);
15761581
if (port < 1 || port > 65535) {
15771582
ngx_log_error(NGX_LOG_ERR, ngx_cycle->log, 0,
1578-
"upsync_check_key: invalid port in %s", key);
1583+
"upsync_check_key: invalid port in %s", s_p);
15791584
return NGX_ERROR;
15801585
}
15811586

src/ngx_stream_upsync_module.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
#define ngx_fopen(path, mode) fopen(path, mode)
1818
#define ngx_fclose(fp) fclose(fp)
1919

20+
#define ngx_strtoull(nptr, endptr, base) strtoull((const char *) nptr, \
21+
(char **) endptr, (int) base)
2022

2123
#define NGX_INDEX_HEARDER "X-Consul-Index"
2224
#define NGX_INDEX_HEARDER_LEN 14

0 commit comments

Comments
 (0)