Skip to content

Commit 8c8d2d4

Browse files
committed
add a directive to proxied client's ip address
1 parent fb678c5 commit 8c8d2d4

File tree

3 files changed

+26
-2
lines changed

3 files changed

+26
-2
lines changed

src/ngx_http_modsecurity_common.h

+1
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@ typedef struct {
126126
#endif
127127

128128
ngx_http_complex_value_t *transaction_id;
129+
ngx_flag_t proxy_protocol_ip;
129130
} ngx_http_modsecurity_conf_t;
130131

131132

src/ngx_http_modsecurity_module.c

+10
Original file line numberDiff line numberDiff line change
@@ -514,6 +514,14 @@ static ngx_command_t ngx_http_modsecurity_commands[] = {
514514
0,
515515
NULL
516516
},
517+
{
518+
ngx_string("modsecurity_proxy_protocol_ip"),
519+
NGX_HTTP_LOC_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_MAIN_CONF|NGX_CONF_FLAG,
520+
ngx_conf_set_flag_slot,
521+
NGX_HTTP_LOC_CONF_OFFSET,
522+
offsetof(ngx_http_modsecurity_conf_t, proxy_protocol_ip),
523+
NULL
524+
},
517525
ngx_null_command
518526
};
519527

@@ -725,6 +733,7 @@ ngx_http_modsecurity_create_conf(ngx_conf_t *cf)
725733
conf->rules_set = msc_create_rules_set();
726734
conf->pool = cf->pool;
727735
conf->transaction_id = NGX_CONF_UNSET_PTR;
736+
conf->proxy_protocol_ip = NGX_CONF_UNSET;
728737
#if defined(MODSECURITY_SANITY_CHECKS) && (MODSECURITY_SANITY_CHECKS)
729738
conf->sanity_checks_enabled = NGX_CONF_UNSET;
730739
#endif
@@ -764,6 +773,7 @@ ngx_http_modsecurity_merge_conf(ngx_conf_t *cf, void *parent, void *child)
764773

765774
ngx_conf_merge_value(c->enable, p->enable, 0);
766775
ngx_conf_merge_ptr_value(c->transaction_id, p->transaction_id, NULL);
776+
ngx_conf_merge_value(c->proxy_protocol_ip, p->proxy_protocol_ip, 0);
767777
#if defined(MODSECURITY_SANITY_CHECKS) && (MODSECURITY_SANITY_CHECKS)
768778
ngx_conf_merge_value(c->sanity_checks_enabled, p->sanity_checks_enabled, 0);
769779
#endif

src/ngx_http_modsecurity_rewrite.c

+15-2
Original file line numberDiff line numberDiff line change
@@ -78,10 +78,23 @@ ngx_http_modsecurity_rewrite_handler(ngx_http_request_t *r)
7878
* erliest phase that nginx allow us to attach those kind of hooks.
7979
*
8080
*/
81-
int client_port = ngx_inet_get_port(connection->sockaddr);
81+
int client_port;
82+
83+
if (mcf->proxy_protocol_ip && connection->proxy_protocol) {
84+
client_port = connection->proxy_protocol->src_port;
85+
} else {
86+
client_port = ngx_inet_get_port(connection->sockaddr);
87+
}
8288
int server_port = ngx_inet_get_port(connection->local_sockaddr);
8389

84-
const char *client_addr = ngx_str_to_char(addr_text, r->pool);
90+
const char *client_addr;
91+
92+
if (mcf->proxy_protocol_ip && connection->proxy_protocol) {
93+
client_addr = ngx_str_to_char(connection->proxy_protocol->src_addr, r->pool);
94+
} else {
95+
client_addr = ngx_str_to_char(addr_text, r->pool);
96+
}
97+
8598
if (client_addr == (char*)-1) {
8699
return NGX_HTTP_INTERNAL_SERVER_ERROR;
87100
}

0 commit comments

Comments
 (0)