Skip to content

Commit cfe2356

Browse files
committed
waf feature: use the origin's address & port
1 parent c7651c7 commit cfe2356

File tree

1 file changed

+16
-15
lines changed

1 file changed

+16
-15
lines changed

src/ngx_http_modsecurity_rewrite.c

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@
2323

2424
ngx_int_t ngx_http_modsecurity_process_connection(ngx_http_request_t *r,
2525
ngx_http_modsecurity_ctx_t *ctx,
26-
const char *client_addr, in_port_t client_port);
26+
const char *client_addr, in_port_t client_port,
27+
const char *server_addr, in_port_t server_port);
2728
ngx_int_t ngx_http_modsecurity_process_url(ngx_http_request_t *r,
2829
ngx_http_modsecurity_ctx_t *ctx,
2930
const char *uri, const char *method, const char *http_version);
@@ -58,7 +59,7 @@ ngx_http_modsecurity_rewrite_handler(ngx_http_request_t *r)
5859
return rc;
5960
}
6061

61-
rc = ngx_http_modsecurity_process_connection(r, ctx, NULL, 0);
62+
rc = ngx_http_modsecurity_process_connection(r, ctx, NULL, 0, NULL, 0);
6263
if (rc > 0) {
6364
return rc;
6465
}
@@ -85,11 +86,11 @@ ngx_http_modsecurity_rewrite_handler(ngx_http_request_t *r)
8586
ngx_int_t
8687
ngx_http_modsecurity_process_connection(ngx_http_request_t *r,
8788
ngx_http_modsecurity_ctx_t *ctx,
88-
const char *client_addr, in_port_t client_port)
89+
const char *client_addr, in_port_t client_port,
90+
const char *server_addr, in_port_t server_port)
8991
{
90-
in_port_t server_port;
92+
size_t len;
9193
ngx_int_t rc;
92-
ngx_str_t server_addr;
9394
ngx_pool_t *old_pool;
9495
ngx_connection_t *c;
9596
u_char addr[NGX_SOCKADDR_STRLEN + 1];
@@ -101,21 +102,21 @@ ngx_http_modsecurity_process_connection(ngx_http_request_t *r,
101102
client_port = ngx_inet_get_port(c->sockaddr);
102103
}
103104

104-
// fill c->local_sockaddr
105-
ngx_connection_local_sockaddr(c, NULL, 0);
105+
if (server_addr == NULL) {
106+
// fill c->local_sockaddr
107+
ngx_connection_local_sockaddr(c, NULL, 0);
106108

107-
server_addr.data = addr;
108-
server_addr.len = NGX_SOCKADDR_STRLEN;
109-
110-
server_addr.len = ngx_sock_ntop(c->local_sockaddr, c->local_socklen,
111-
server_addr.data, server_addr.len, 0);
112-
server_addr.data[server_addr.len] = 0;
113-
server_port = ngx_inet_get_port(c->local_sockaddr);
109+
len = ngx_sock_ntop(c->local_sockaddr, c->local_socklen,
110+
addr, NGX_SOCKADDR_STRLEN, 0);
111+
addr[len] = 0;
112+
server_addr = (char *)addr;
113+
server_port = ngx_inet_get_port(c->local_sockaddr);
114+
}
114115

115116
old_pool = ngx_http_modsecurity_pcre_malloc_init(r->pool);
116117
rc = msc_process_connection(ctx->modsec_transaction,
117118
client_addr, client_port,
118-
(char *)server_addr.data, server_port);
119+
server_addr, server_port);
119120
ngx_http_modsecurity_pcre_malloc_done(old_pool);
120121
if (rc != 1){
121122
dd("Was not able to extract connection information.");

0 commit comments

Comments
 (0)