Skip to content

Commit 2bb7357

Browse files
committed
New variable $modsecurity_status
This variable can be used for example in access logs to distinguish which requests was blocked by modsecurity
1 parent d2051c9 commit 2bb7357

File tree

2 files changed

+47
-1
lines changed

2 files changed

+47
-1
lines changed

src/ngx_http_modsecurity_common.h

+1
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ typedef struct {
9999
unsigned processed:1;
100100
unsigned logged:1;
101101
unsigned intervention_triggered:1;
102+
int status; // status code from modsecurity
102103
} ngx_http_modsecurity_ctx_t;
103104

104105

src/ngx_http_modsecurity_module.c

+46-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,10 @@ static void *ngx_http_modsecurity_create_conf(ngx_conf_t *cf);
3636
static char *ngx_http_modsecurity_merge_conf(ngx_conf_t *cf, void *parent, void *child);
3737
static void ngx_http_modsecurity_cleanup_instance(void *data);
3838
static void ngx_http_modsecurity_cleanup_rules(void *data);
39+
static ngx_int_t ngx_http_modsecurity_add_variables(ngx_conf_t *cf);
40+
static ngx_int_t ngx_http_modsecurity_status_variable(ngx_http_request_t *r, ngx_http_variable_value_t *v, uintptr_t data);
3941

42+
static ngx_str_t ngx_http_modsecurity_status = ngx_string("modsecurity_status");
4043

4144
/*
4245
* PCRE malloc/free workaround, based on
@@ -223,6 +226,7 @@ ngx_http_modsecurity_process_intervention (Transaction *transaction, ngx_http_re
223226

224227
if (intervention.status != 200)
225228
{
229+
ctx->status = intervention.status;
226230
/**
227231
* FIXME: this will bring proper response code to audit log in case
228232
* when e.g. error_page redirect was triggered, but there still won't be another
@@ -284,6 +288,8 @@ ngx_http_modsecurity_create_ctx(ngx_http_request_t *r)
284288
return NULL;
285289
}
286290

291+
ctx->status = 0;
292+
287293
mmcf = ngx_http_get_module_main_conf(r, ngx_http_modsecurity_module);
288294
mcf = ngx_http_get_module_loc_conf(r, ngx_http_modsecurity_module);
289295

@@ -514,7 +520,7 @@ static ngx_command_t ngx_http_modsecurity_commands[] = {
514520

515521

516522
static ngx_http_module_t ngx_http_modsecurity_ctx = {
517-
NULL, /* preconfiguration */
523+
ngx_http_modsecurity_add_variables, /* preconfiguration */
518524
ngx_http_modsecurity_init, /* postconfiguration */
519525

520526
ngx_http_modsecurity_create_main_conf, /* create main configuration */
@@ -817,4 +823,43 @@ ngx_http_modsecurity_cleanup_rules(void *data)
817823
}
818824

819825

826+
static ngx_int_t
827+
ngx_http_modsecurity_add_variables(ngx_conf_t *cf)
828+
{
829+
ngx_http_variable_t *v;
830+
831+
v = ngx_http_add_variable(cf, &ngx_http_modsecurity_status,
832+
NGX_HTTP_VAR_NOCACHEABLE);
833+
if (v == NULL) {
834+
return NGX_ERROR;
835+
}
836+
837+
v->get_handler = ngx_http_modsecurity_status_variable;
838+
839+
return NGX_OK;
840+
}
841+
842+
843+
static ngx_int_t
844+
ngx_http_modsecurity_status_variable(ngx_http_request_t *r,
845+
ngx_http_variable_value_t *v, uintptr_t data)
846+
{
847+
ngx_http_modsecurity_ctx_t *ctx;
848+
849+
ctx = ngx_http_get_module_ctx(r, ngx_http_modsecurity_module);
850+
if (ctx == NULL || ctx->status == 0) {
851+
v->not_found = 1;
852+
return NGX_OK;
853+
}
854+
855+
v->len = ngx_sprintf(v->data, "%03ui", ctx->status) - v->data;
856+
v->valid = 1;
857+
v->no_cacheable = 0;
858+
v->not_found = 0;
859+
860+
return NGX_OK;
861+
}
862+
863+
864+
820865
/* vi:set ft=c ts=4 sw=4 et fdm=marker: */

0 commit comments

Comments
 (0)