Skip to content

Commit 18c72cb

Browse files
committed
fixed bug that using proxy_connect_address wihtout nginx variable triggers segfault
1 parent 215545c commit 18c72cb

File tree

2 files changed

+68
-5
lines changed

2 files changed

+68
-5
lines changed

ngx_http_proxy_connect_module.c

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1483,7 +1483,7 @@ ngx_http_proxy_connect_address(ngx_conf_t *cf, ngx_command_t *cmd,
14831483
ngx_int_t rc;
14841484
ngx_str_t *value;
14851485
ngx_http_complex_value_t cv;
1486-
ngx_http_proxy_connect_address_t **paddress, *address;
1486+
ngx_http_proxy_connect_address_t **paddress, *address;
14871487
ngx_http_compile_complex_value_t ccv;
14881488

14891489
paddress = (ngx_http_proxy_connect_address_t **) (p + cmd->offset);
@@ -1712,10 +1712,14 @@ ngx_http_proxy_connect_set_address(ngx_http_request_t *r,
17121712
}
17131713

17141714
if (address->value == NULL) {
1715-
u->peer.sockaddr = address->addr->sockaddr;
1716-
u->peer.socklen = address->addr->socklen;
1717-
u->peer.name = &address->addr->name;
1715+
17181716
u->resolved->naddrs = 1;
1717+
u->resolved->addrs = NULL;
1718+
u->resolved->sockaddr = address->addr->sockaddr;
1719+
u->resolved->socklen = address->addr->socklen;
1720+
u->resolved->name = address->addr->name;
1721+
u->resolved->host = address->addr->name;
1722+
17191723
return NGX_OK;
17201724
}
17211725

t/http_proxy_connect.t

Lines changed: 60 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ use Test::Nginx;
2121
select STDERR; $| = 1;
2222
select STDOUT; $| = 1;
2323

24-
my $t = Test::Nginx->new()->has(qw/http proxy/)->plan(9);
24+
my $t = Test::Nginx->new()->has(qw/http proxy/)->plan(10);
2525

2626
$t->write_file_expand('nginx.conf', <<'EOF');
2727
@@ -120,6 +120,65 @@ $t->stop();
120120

121121
###############################################################################
122122

123+
$t->write_file_expand('nginx.conf', <<'EOF');
124+
125+
%%TEST_GLOBALS%%
126+
127+
daemon off;
128+
129+
events {
130+
}
131+
132+
http {
133+
%%TEST_GLOBALS_HTTP%%
134+
135+
log_format connect '$remote_addr - $remote_user [$time_local] "$request" '
136+
'$status $body_bytes_sent var:$connect_host-$connect_port-$connect_addr';
137+
138+
access_log %%TESTDIR%%/connect.log connect;
139+
140+
server {
141+
listen 8081;
142+
listen 8082;
143+
access_log off;
144+
location / {
145+
return 200 "hello $remote_addr $server_port\n";
146+
}
147+
}
148+
149+
server {
150+
listen 127.0.0.1:8080;
151+
server_name localhost;
152+
153+
# forward proxy for CONNECT method
154+
155+
proxy_connect;
156+
proxy_connect_allow all;
157+
158+
proxy_connect_connect_timeout 10s;
159+
proxy_connect_read_timeout 10s;
160+
proxy_connect_send_timeout 10s;
161+
proxy_connect_send_lowat 0;
162+
163+
proxy_connect_address 127.0.0.1:8082;
164+
# proxy_connect_bind 127.0.0.3;
165+
166+
location / {
167+
proxy_pass http://127.0.0.1:8081;
168+
}
169+
}
170+
}
171+
172+
EOF
173+
174+
175+
$t->run();
176+
like(http_connect_request('address.com', '8081', '/'), qr/hello 127.0.0.1 8082/, 'set remote address without nginx variable');
177+
$t->stop();
178+
179+
###############################################################################
180+
181+
123182
sub http_connect_request {
124183
my ($host, $port, $url) = @_;
125184
my $r = http_connect($host, $port, <<EOF);

0 commit comments

Comments
 (0)