Skip to content

Commit c232194

Browse files
defanatorFelipe Zimmerle
authored and
Felipe Zimmerle
committed
Introduced modsecurity_transaction_id directive
1 parent 38971aa commit c232194

File tree

2 files changed

+65
-3
lines changed

2 files changed

+65
-3
lines changed

src/ngx_http_modsecurity_common.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,8 @@ typedef struct {
9595
Rules *rules_set;
9696

9797
void *pool;
98+
99+
ngx_http_complex_value_t *transaction_id;
98100
} ngx_http_modsecurity_conf_t;
99101

100102

src/ngx_http_modsecurity_module.c

Lines changed: 63 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,7 @@ ngx_http_modsecurity_create_ctx(ngx_http_request_t *r)
238238
ngx_http_modsecurity_conf_t *loc_cf = NULL;
239239
ngx_http_modsecurity_conf_t *cf = NULL;
240240
ngx_pool_cleanup_t *cln = NULL;
241+
ngx_str_t s;
241242

242243
ctx = ngx_pcalloc(r->pool, sizeof(ngx_http_modsecurity_ctx_t));
243244
if (ctx == NULL)
@@ -250,7 +251,15 @@ ngx_http_modsecurity_create_ctx(ngx_http_request_t *r)
250251

251252
dd("creating transaction with the following rules: '%p' -- ms: '%p'", loc_cf->rules_set, cf->modsec);
252253

253-
ctx->modsec_transaction = msc_new_transaction(cf->modsec, loc_cf->rules_set, r->connection->log);
254+
if (loc_cf->transaction_id) {
255+
if (ngx_http_complex_value(r, loc_cf->transaction_id, &s) != NGX_OK) {
256+
return NGX_CONF_ERROR;
257+
}
258+
ctx->modsec_transaction = msc_new_transaction_with_id(cf->modsec, loc_cf->rules_set, (char *) s.data, r->connection->log);
259+
260+
} else {
261+
ctx->modsec_transaction = msc_new_transaction(cf->modsec, loc_cf->rules_set, r->connection->log);
262+
}
254263

255264
dd("transaction created");
256265

@@ -352,6 +361,36 @@ char *ngx_conf_set_rules_remote(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
352361
}
353362

354363

364+
char *ngx_conf_set_transaction_id(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) {
365+
ngx_str_t *value;
366+
ngx_http_complex_value_t cv;
367+
ngx_http_compile_complex_value_t ccv;
368+
ngx_http_modsecurity_conf_t *mcf = conf;
369+
370+
value = cf->args->elts;
371+
372+
ngx_memzero(&ccv, sizeof(ngx_http_compile_complex_value_t));
373+
374+
ccv.cf = cf;
375+
ccv.value = &value[1];
376+
ccv.complex_value = &cv;
377+
ccv.zero = 1;
378+
379+
if (ngx_http_compile_complex_value(&ccv) != NGX_OK) {
380+
return NGX_CONF_ERROR;
381+
}
382+
383+
mcf->transaction_id = ngx_palloc(cf->pool, sizeof(ngx_http_complex_value_t));
384+
if (mcf->transaction_id == NULL) {
385+
return NGX_CONF_ERROR;
386+
}
387+
388+
*mcf->transaction_id = cv;
389+
390+
return NGX_CONF_OK;
391+
}
392+
393+
355394
static ngx_command_t ngx_http_modsecurity_commands[] = {
356395
{
357396
ngx_string("modsecurity"),
@@ -385,6 +424,14 @@ static ngx_command_t ngx_http_modsecurity_commands[] = {
385424
offsetof(ngx_http_modsecurity_conf_t, enable),
386425
NULL
387426
},
427+
{
428+
ngx_string("modsecurity_transaction_id"),
429+
NGX_HTTP_LOC_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_MAIN_CONF|NGX_CONF_1MORE,
430+
ngx_conf_set_transaction_id,
431+
NGX_HTTP_LOC_CONF_OFFSET,
432+
0,
433+
NULL
434+
},
388435
ngx_null_command
389436
};
390437

@@ -542,19 +589,30 @@ static void *ngx_http_modsecurity_create_conf(ngx_conf_t *cf)
542589
{
543590
ngx_pool_cleanup_t *cln = NULL;
544591
ngx_http_modsecurity_conf_t *conf = (ngx_http_modsecurity_conf_t *)
545-
ngx_palloc(cf->pool, sizeof(ngx_http_modsecurity_conf_t));
592+
ngx_pcalloc(cf->pool, sizeof(ngx_http_modsecurity_conf_t));
546593

547594
if (conf == NULL)
548595
{
549596
dd("Failed to allocate space for ModSecurity configuration");
550597
return NGX_CONF_ERROR;
551598
}
552599

600+
/*
601+
* set by ngx_pcalloc():
602+
*
603+
* conf->modsec = NULL;
604+
* conf->enable = 0;
605+
* conf->sanity_checks_enabled = 0;
606+
* conf->rules_set = NULL;
607+
* conf->pool = NULL;
608+
* conf->transaction_id = NULL;
609+
*/
610+
553611
conf->enable = NGX_CONF_UNSET;
554612
conf->sanity_checks_enabled = NGX_CONF_UNSET;
555613
conf->rules_set = msc_create_rules_set();
556-
conf->modsec = NULL;
557614
conf->pool = cf->pool;
615+
conf->transaction_id = NGX_CONF_UNSET_PTR;
558616

559617
cln = ngx_pool_cleanup_add(cf->pool, 0);
560618
if (cln == NULL) {
@@ -587,6 +645,7 @@ ngx_http_modsecurity_merge_srv_conf(ngx_conf_t *cf, void *parent, void *child)
587645

588646
ngx_conf_merge_value(c->enable, p->enable, 0);
589647
ngx_conf_merge_value(c->sanity_checks_enabled, p->sanity_checks_enabled, 0);
648+
ngx_conf_merge_ptr_value(c->transaction_id, p->transaction_id, NULL);
590649

591650
#if defined(MODSECURITY_DDEBUG) && (MODSECURITY_DDEBUG)
592651
dd("PARENT RULES");
@@ -630,6 +689,7 @@ ngx_http_modsecurity_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child)
630689

631690
ngx_conf_merge_value(c->enable, p->enable, 0);
632691
ngx_conf_merge_value(c->sanity_checks_enabled, p->sanity_checks_enabled, 0);
692+
ngx_conf_merge_ptr_value(c->transaction_id, p->transaction_id, NULL);
633693

634694
#if defined(MODSECURITY_DDEBUG) && (MODSECURITY_DDEBUG)
635695
dd("PARENT RULES");

0 commit comments

Comments
 (0)