diff --git a/src/ngx_http_modsecurity_module.c b/src/ngx_http_modsecurity_module.c index d3d9624d..b6fdcfde 100644 --- a/src/ngx_http_modsecurity_module.c +++ b/src/ngx_http_modsecurity_module.c @@ -234,7 +234,7 @@ ngx_http_modsecurity_process_intervention (Transaction *transaction, ngx_http_re dd("intervention -- calling log handler manually with code: %d", intervention.status); ngx_http_modsecurity_log_handler(r); ctx->logged = 1; - } + } if (r->header_sent) { @@ -286,6 +286,12 @@ ngx_http_modsecurity_create_ctx(ngx_http_request_t *r) mmcf = ngx_http_get_module_main_conf(r, ngx_http_modsecurity_module); mcf = ngx_http_get_module_loc_conf(r, ngx_http_modsecurity_module); + if (mcf->rules_set == NULL) { + ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, + "modsec connector: no context when mcf->rules_set == NULL"); + return NULL; + } + dd("creating transaction with the following rules: '%p' -- ms: '%p'", mcf->rules_set, mmcf->modsec); if (mcf->transaction_id) { @@ -361,6 +367,19 @@ ngx_conf_set_rules(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) return NGX_CONF_ERROR; } + // Init the rules_set + if (mcf->rules_set == NULL) { + mcf->rules_set = msc_create_rules_set(); + ngx_pool_cleanup_t *cln = ngx_pool_cleanup_add(cf->pool, 0); + if (cln == NULL) { + dd("failed to create the ModSecurity configuration cleanup"); + return NGX_CONF_ERROR; + } + + cln->handler = ngx_http_modsecurity_cleanup_rules; + cln->data = conf; + } + old_pool = ngx_http_modsecurity_pcre_malloc_init(cf->pool); res = msc_rules_add(mcf->rules_set, rules, &error); ngx_http_modsecurity_pcre_malloc_done(old_pool); @@ -395,6 +414,19 @@ ngx_conf_set_rules_file(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) return NGX_CONF_ERROR; } + // Init the rules_set + if (mcf->rules_set == NULL) { + mcf->rules_set = msc_create_rules_set(); + ngx_pool_cleanup_t *cln = ngx_pool_cleanup_add(cf->pool, 0); + if (cln == NULL) { + dd("failed to create the ModSecurity configuration cleanup"); + return NGX_CONF_ERROR; + } + + cln->handler = ngx_http_modsecurity_cleanup_rules; + cln->data = conf; + } + old_pool = ngx_http_modsecurity_pcre_malloc_init(cf->pool); res = msc_rules_add_file(mcf->rules_set, rules_set, &error); ngx_http_modsecurity_pcre_malloc_done(old_pool); @@ -434,6 +466,19 @@ ngx_conf_set_rules_remote(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) return NGX_CONF_ERROR; } + // Init the rules_set + if (mcf->rules_set == NULL) { + mcf->rules_set = msc_create_rules_set(); + ngx_pool_cleanup_t *cln = ngx_pool_cleanup_add(cf->pool, 0); + if (cln == NULL) { + dd("failed to create the ModSecurity configuration cleanup"); + return NGX_CONF_ERROR; + } + + cln->handler = ngx_http_modsecurity_cleanup_rules; + cln->data = conf; + } + old_pool = ngx_http_modsecurity_pcre_malloc_init(cf->pool); res = msc_rules_add_remote(mcf->rules_set, rules_remote_key, rules_remote_server, &error); ngx_http_modsecurity_pcre_malloc_done(old_pool); @@ -697,7 +742,6 @@ ngx_http_modsecurity_init_main_conf(ngx_conf_t *cf, void *conf) static void * ngx_http_modsecurity_create_conf(ngx_conf_t *cf) { - ngx_pool_cleanup_t *cln; ngx_http_modsecurity_conf_t *conf; conf = (ngx_http_modsecurity_conf_t *) ngx_pcalloc(cf->pool, @@ -720,7 +764,6 @@ ngx_http_modsecurity_create_conf(ngx_conf_t *cf) */ conf->enable = NGX_CONF_UNSET; - conf->rules_set = msc_create_rules_set(); conf->pool = cf->pool; conf->transaction_id = NGX_CONF_UNSET_PTR; conf->use_error_log = NGX_CONF_UNSET; @@ -728,15 +771,6 @@ ngx_http_modsecurity_create_conf(ngx_conf_t *cf) conf->sanity_checks_enabled = NGX_CONF_UNSET; #endif - cln = ngx_pool_cleanup_add(cf->pool, 0); - if (cln == NULL) { - dd("failed to create the ModSecurity configuration cleanup"); - return NGX_CONF_ERROR; - } - - cln->handler = ngx_http_modsecurity_cleanup_rules; - cln->data = conf; - dd ("conf created at: '%p'", conf); return conf; @@ -774,7 +808,17 @@ ngx_http_modsecurity_merge_conf(ngx_conf_t *cf, void *parent, void *child) dd("CHILD RULES"); msc_rules_dump(c->rules_set); #endif + if (p->rules_set == NULL) { + return NGX_CONF_OK; + } + if (c->rules_set == NULL) { + c->rules_set = p->rules_set; + return NGX_CONF_OK; + } rules = msc_rules_merge(c->rules_set, p->rules_set, &error); + if (rules < 0) { + return strdup(error); + } if (rules < 0) { return strdup(error); @@ -812,6 +856,11 @@ ngx_http_modsecurity_cleanup_rules(void *data) mcf = (ngx_http_modsecurity_conf_t *) data; + if (mcf->rules_set == NULL) { + dd("cleanup loc conf -- RuleSet is NULL"); + return; + } + dd("deleting a loc conf -- RuleSet is: \"%p\"", mcf->rules_set); old_pool = ngx_http_modsecurity_pcre_malloc_init(mcf->pool);