Skip to content

feat: set correct hostname in log produced by Nginx #353

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions src/ngx_http_modsecurity_rewrite.c
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,45 @@ ngx_http_modsecurity_rewrite_handler(ngx_http_request_t *r)
return NGX_HTTP_INTERNAL_SERVER_ERROR;
}

#if defined(MODSECURITY_CHECK_VERSION)
#if MODSECURITY_VERSION_NUM >= 30130100
ngx_str_t hostname;
hostname.len = 0;
// first check if Nginx received a Host header and it's usable
// (i.e. not empty)
// if yes, we can use that
if (r->headers_in.server.len > 0) {
hostname.len = r->headers_in.server.len;
hostname.data = r->headers_in.server.data;
}
else {
// otherwise we try to use the server config, namely the
// server_name $SERVER_NAME
// directive
// for eg. in default config, server_name is "_"
// possible all requests without a Host header will be
// handled by this server block
ngx_http_core_srv_conf_t *cscf;
cscf = ngx_http_get_module_srv_conf(r, ngx_http_core_module);
if (cscf->server_name.len > 0) {
hostname.len = cscf->server_name.len;
hostname.data = cscf->server_name.data;
}
}
if (hostname.len > 0) {
const char *host_name = ngx_str_to_char(hostname, r->pool);
if (host_name == (char*)-1 || host_name == NULL) {
return NGX_HTTP_INTERNAL_SERVER_ERROR;
}
else {
// set the hostname in the transaction
// this function is only available in ModSecurity 3.0.13 and later
msc_set_request_hostname(ctx->modsec_transaction, (const unsigned char *)host_name);
}
}
#endif
#endif

ngx_str_t s;
u_char addr[NGX_SOCKADDR_STRLEN];
s.len = NGX_SOCKADDR_STRLEN;
Expand Down