Skip to content

Reuse already initialized rule set for each location #291

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 1 commit 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
73 changes: 61 additions & 12 deletions src/ngx_http_modsecurity_module.c
Original file line number Diff line number Diff line change
@@ -221,7 +221,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)
{
@@ -273,6 +273,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) {
@@ -327,6 +333,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);
@@ -361,6 +380,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);
@@ -400,6 +432,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);
@@ -669,7 +714,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,
@@ -692,22 +736,12 @@ 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;
#if defined(MODSECURITY_SANITY_CHECKS) && (MODSECURITY_SANITY_CHECKS)
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;
@@ -744,7 +778,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);
@@ -782,6 +826,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);