-
Notifications
You must be signed in to change notification settings - Fork 301
Add variables to hold processing times for modsecurity phases #278
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
base: master
Are you sure you want to change the base?
Changes from 3 commits
e695492
8052510
75cf7a4
26ac576
481e463
811d9a0
f7cff06
55b41d0
38ec2e3
6ec6bbb
aeb5307
bb939a1
d386de9
7aa12d0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,13 +25,26 @@ | |
#include <ngx_http.h> | ||
|
||
static ngx_int_t ngx_http_modsecurity_init(ngx_conf_t *cf); | ||
static ngx_int_t ngx_http_modsecurity_add_variables(ngx_conf_t *cf); | ||
static void *ngx_http_modsecurity_create_main_conf(ngx_conf_t *cf); | ||
static char *ngx_http_modsecurity_init_main_conf(ngx_conf_t *cf, void *conf); | ||
static void *ngx_http_modsecurity_create_conf(ngx_conf_t *cf); | ||
static char *ngx_http_modsecurity_merge_conf(ngx_conf_t *cf, void *parent, void *child); | ||
static void ngx_http_modsecurity_cleanup_instance(void *data); | ||
static void ngx_http_modsecurity_cleanup_rules(void *data); | ||
|
||
static ngx_int_t ngx_http_modsecurity_req_headers_phase_time(ngx_http_request_t *r, | ||
ngx_http_variable_value_t *v, uintptr_t data); | ||
static ngx_int_t ngx_http_modsecurity_req_body_phase_time(ngx_http_request_t *r, | ||
ngx_http_variable_value_t *v, uintptr_t data); | ||
static ngx_int_t ngx_http_modsecurity_resp_headers_phase_time(ngx_http_request_t *r, | ||
ngx_http_variable_value_t *v, uintptr_t data); | ||
static ngx_int_t ngx_http_modsecurity_resp_body_phase_time(ngx_http_request_t *r, | ||
ngx_http_variable_value_t *v, uintptr_t data); | ||
static ngx_int_t ngx_http_modsecurity_logging_phase_time(ngx_http_request_t *r, | ||
ngx_http_variable_value_t *v, uintptr_t data); | ||
static ngx_int_t ngx_http_modsecurity_time_variable(ngx_http_request_t *r, | ||
ngx_http_variable_value_t *v, uintptr_t data, ngx_msec_int_t usec); | ||
|
||
/* | ||
* PCRE malloc/free workaround, based on | ||
|
@@ -268,6 +281,12 @@ ngx_http_modsecurity_create_ctx(ngx_http_request_t *r) | |
return NULL; | ||
} | ||
|
||
ctx->req_headers_phase_time = -1; | ||
ctx->req_body_phase_time = -1; | ||
ctx->resp_headers_phase_time = -1; | ||
ctx->resp_body_phase_time = -1; | ||
ctx->logging_phase_time = -1; | ||
|
||
mmcf = ngx_http_get_module_main_conf(r, ngx_http_modsecurity_module); | ||
mcf = ngx_http_get_module_loc_conf(r, ngx_http_modsecurity_module); | ||
|
||
|
@@ -490,7 +509,7 @@ static ngx_command_t ngx_http_modsecurity_commands[] = { | |
|
||
|
||
static ngx_http_module_t ngx_http_modsecurity_ctx = { | ||
NULL, /* preconfiguration */ | ||
ngx_http_modsecurity_add_variables, /* preconfiguration */ | ||
ngx_http_modsecurity_init, /* postconfiguration */ | ||
|
||
ngx_http_modsecurity_create_main_conf, /* create main configuration */ | ||
|
@@ -520,6 +539,31 @@ ngx_module_t ngx_http_modsecurity_module = { | |
}; | ||
|
||
|
||
static ngx_http_variable_t ngx_http_modsecurity_vars[] = { | ||
{ ngx_string("modsecurity_req_headers_phase_time"), NULL, | ||
ngx_http_modsecurity_req_headers_phase_time, 0, | ||
NGX_HTTP_VAR_NOCACHEABLE, 0 }, | ||
|
||
{ ngx_string("modsecurity_req_body_phase_time"), NULL, | ||
ngx_http_modsecurity_req_body_phase_time, 0, | ||
NGX_HTTP_VAR_NOCACHEABLE, 0 }, | ||
|
||
{ ngx_string("modsecurity_resp_headers_phase_time"), NULL, | ||
ngx_http_modsecurity_resp_headers_phase_time, 0, | ||
NGX_HTTP_VAR_NOCACHEABLE, 0 }, | ||
|
||
{ ngx_string("modsecurity_resp_body_phase_time"), NULL, | ||
ngx_http_modsecurity_resp_body_phase_time, 0, | ||
NGX_HTTP_VAR_NOCACHEABLE, 0 }, | ||
|
||
{ ngx_string("modsecurity_logging_phase_time"), NULL, | ||
ngx_http_modsecurity_logging_phase_time, 0, | ||
|
||
NGX_HTTP_VAR_NOCACHEABLE, 0 }, | ||
|
||
|
||
ngx_http_null_variable | ||
|
||
}; | ||
|
||
|
||
static ngx_int_t | ||
ngx_http_modsecurity_init(ngx_conf_t *cf) | ||
{ | ||
|
@@ -596,6 +640,23 @@ ngx_http_modsecurity_init(ngx_conf_t *cf) | |
return NGX_OK; | ||
} | ||
|
||
static ngx_int_t | ||
ngx_http_modsecurity_add_variables(ngx_conf_t *cf) { | ||
ngx_http_variable_t *var, *v; | ||
|
||
for (v = ngx_http_modsecurity_vars; v->name.len; v++) { | ||
var = ngx_http_add_variable(cf, &v->name, v->flags); | ||
if (var == NULL) { | ||
return NGX_ERROR; | ||
} | ||
|
||
var->get_handler = v->get_handler; | ||
var->data = v->data; | ||
} | ||
|
||
return NGX_OK; | ||
}; | ||
|
||
|
||
static void * | ||
ngx_http_modsecurity_create_main_conf(ngx_conf_t *cf) | ||
|
@@ -788,4 +849,107 @@ ngx_http_modsecurity_cleanup_rules(void *data) | |
} | ||
|
||
|
||
static ngx_int_t | ||
ngx_http_modsecurity_req_headers_phase_time(ngx_http_request_t *r, | ||
ngx_http_variable_value_t *v, uintptr_t data) | ||
{ | ||
ngx_http_modsecurity_ctx_t *ctx; | ||
|
||
ctx = ngx_http_get_module_ctx(r, ngx_http_modsecurity_module); | ||
if (ctx == NULL) { | ||
return NGX_ERROR; | ||
} | ||
return ngx_http_modsecurity_time_variable(r, v, data, ctx->req_headers_phase_time); | ||
} | ||
|
||
|
||
static ngx_int_t | ||
ngx_http_modsecurity_req_body_phase_time(ngx_http_request_t *r, | ||
ngx_http_variable_value_t *v, uintptr_t data) | ||
{ | ||
ngx_http_modsecurity_ctx_t *ctx; | ||
|
||
ctx = ngx_http_get_module_ctx(r, ngx_http_modsecurity_module); | ||
if (ctx == NULL) { | ||
return NGX_ERROR; | ||
} | ||
return ngx_http_modsecurity_time_variable(r, v, data, ctx->req_body_phase_time); | ||
} | ||
|
||
|
||
static ngx_int_t | ||
ngx_http_modsecurity_resp_headers_phase_time(ngx_http_request_t *r, | ||
ngx_http_variable_value_t *v, uintptr_t data) | ||
{ | ||
ngx_http_modsecurity_ctx_t *ctx; | ||
|
||
ctx = ngx_http_get_module_ctx(r, ngx_http_modsecurity_module); | ||
if (ctx == NULL) { | ||
return NGX_ERROR; | ||
} | ||
return ngx_http_modsecurity_time_variable(r, v, data, ctx->resp_headers_phase_time); | ||
} | ||
|
||
|
||
static ngx_int_t | ||
ngx_http_modsecurity_resp_body_phase_time(ngx_http_request_t *r, | ||
ngx_http_variable_value_t *v, uintptr_t data) | ||
{ | ||
ngx_http_modsecurity_ctx_t *ctx; | ||
|
||
ctx = ngx_http_get_module_ctx(r, ngx_http_modsecurity_module); | ||
if (ctx == NULL) { | ||
return NGX_ERROR; | ||
} | ||
return ngx_http_modsecurity_time_variable(r, v, data, ctx->resp_body_phase_time); | ||
} | ||
|
||
|
||
static ngx_int_t | ||
ngx_http_modsecurity_logging_phase_time(ngx_http_request_t *r, | ||
ngx_http_variable_value_t *v, uintptr_t data) | ||
{ | ||
ngx_http_modsecurity_ctx_t *ctx; | ||
|
||
ctx = ngx_http_get_module_ctx(r, ngx_http_modsecurity_module); | ||
if (ctx == NULL) { | ||
return NGX_ERROR; | ||
} | ||
return ngx_http_modsecurity_time_variable(r, v, data, ctx->logging_phase_time); | ||
} | ||
|
||
|
||
static ngx_int_t | ||
ngx_http_modsecurity_time_variable(ngx_http_request_t *r, | ||
ngx_http_variable_value_t *v, uintptr_t data, ngx_msec_int_t usec) | ||
{ | ||
u_char *p; | ||
|
||
p = ngx_pnalloc(r->pool, NGX_TIME_T_LEN + 7); | ||
if (p == NULL) { | ||
return NGX_ERROR; | ||
} | ||
|
||
if(usec == -1) { | ||
v->len = ngx_sprintf(p, "-") - p; | ||
} else { | ||
v->len = ngx_sprintf(p, "%T.%06M", (time_t) usec / 1000000, usec % 1000000) - p; | ||
} | ||
|
||
v->valid = 1; | ||
v->no_cacheable = 0; | ||
v->not_found = 0; | ||
v->data = p; | ||
|
||
return NGX_OK; | ||
} | ||
|
||
|
||
ngx_msec_int_t | ||
ngx_http_modsecurity_compute_processing_time(struct timeval tv) { | ||
struct timeval current_tv; | ||
ngx_gettimeofday(¤t_tv); | ||
return (ngx_msec_int_t) ((current_tv.tv_sec - tv.tv_sec) * 1000000 + (current_tv.tv_usec - tv.tv_usec)); | ||
}; | ||
|
||
/* vi:set ft=c ts=4 sw=4 et fdm=marker: */ |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -140,6 +140,9 @@ ngx_http_modsecurity_pre_access_handler(ngx_http_request_t *r) | |
int ret = 0; | ||
int already_inspected = 0; | ||
|
||
struct timeval start_tv; | ||
ngx_gettimeofday(&start_tv); | ||
|
||
dd("request body is ready to be processed"); | ||
|
||
r->write_event_handler = ngx_http_core_run_phases; | ||
|
@@ -209,7 +212,11 @@ ngx_http_modsecurity_pre_access_handler(ngx_http_request_t *r) | |
/* XXX: once more -- is body can be modified ? content-length need to be adjusted ? */ | ||
|
||
old_pool = ngx_http_modsecurity_pcre_malloc_init(r->pool); | ||
|
||
|
||
msc_process_request_body(ctx->modsec_transaction); | ||
|
||
ctx->req_body_phase_time = ngx_http_modsecurity_compute_processing_time(start_tv); | ||
|
||
ngx_http_modsecurity_pcre_malloc_done(old_pool); | ||
|
||
ret = ngx_http_modsecurity_process_intervention(ctx->modsec_transaction, r, 0); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -51,6 +51,9 @@ ngx_http_modsecurity_rewrite_handler(ngx_http_request_t *r) | |
if (ctx == NULL) | ||
{ | ||
int ret = 0; | ||
struct timeval start_tv; | ||
|
||
ngx_gettimeofday(&start_tv); | ||
|
||
|
||
ngx_connection_t *connection = r->connection; | ||
/** | ||
|
@@ -206,6 +209,9 @@ ngx_http_modsecurity_rewrite_handler(ngx_http_request_t *r) | |
ngx_http_modsecurity_pcre_malloc_done(old_pool); | ||
dd("Processing intervention with the request headers information filled in"); | ||
ret = ngx_http_modsecurity_process_intervention(ctx->modsec_transaction, r, 1); | ||
|
||
ctx->req_headers_phase_time = ngx_http_modsecurity_compute_processing_time(start_tv); | ||
|
||
if (r->error_page) { | ||
return NGX_DECLINED; | ||
} | ||
|
@@ -215,6 +221,5 @@ ngx_http_modsecurity_rewrite_handler(ngx_http_request_t *r) | |
} | ||
} | ||
|
||
|
||
awmackowiak marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return NGX_DECLINED; | ||
} |
Uh oh!
There was an error while loading. Please reload this page.