Skip to content

Commit 04eeec0

Browse files
committed
initial commit to skip the body inspection
1 parent fd28e6a commit 04eeec0

File tree

5 files changed

+99
-5
lines changed

5 files changed

+99
-5
lines changed

README.md

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ Further information about nginx third-party add-ons support are available [here]
4545
# Usage
4646

4747
ModSecurity for nginx extends your nginx configuration directives.
48-
It adds four new directives and they are:
48+
It adds six new directives and they are:
4949

5050
modsecurity
5151
-----------
@@ -175,6 +175,60 @@ using the same unique identificator.
175175

176176
String can contain variables.
177177

178+
modsecurity_skip_req_body_filter
179+
-----------------
180+
**syntax:** *modsecurity_skip_req_body_filter on | off*
181+
182+
**context:** *http, server, location*
183+
184+
**default:** *off*
185+
186+
Allows to skip the caching of the request body and subsequently its inspection.
187+
Useful in cases, where `SecRequestBodyAccess` or `ctl:requestBodyAccess` is set, due to, e.g. encrypted data, as the caching causes an unneeded memory overhead.
188+
189+
190+
```nginx
191+
server {
192+
modsecurity on;
193+
modsecurity_rules_file /etc/my_modsecurity_rules.conf;
194+
195+
location / {
196+
root /var/www/html;
197+
}
198+
199+
location = /special/unchecked/path {
200+
# skip the inspection of the request body
201+
modsecurity_skip_req_body_filter on;
202+
}
203+
}
204+
```
205+
206+
modsecurity_skip_res_body_filter
207+
-----------------
208+
**syntax:** *modsecurity_skip_res_body_filter on | off*
209+
210+
**context:** *http, server, location*
211+
212+
**default:** *off*
213+
214+
Allows to skip the caching of the request body and subsequently its inspection.
215+
Useful in cases, where `SecResponseBodyAccess` is set, due to, e.g. encrypted data, as the caching causes an unneeded memory overhead.
216+
217+
```nginx
218+
server {
219+
modsecurity on;
220+
modsecurity_rules_file /etc/my_modsecurity_rules.conf;
221+
222+
location / {
223+
root /var/www/html;
224+
}
225+
226+
location = /special/unchecked/path {
227+
# skip the inspection of the response body
228+
modsecurity_skip_res_body_filter on;
229+
}
230+
}
231+
```
178232

179233
# Contributing
180234

src/ngx_http_modsecurity_body_filter.c

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@ ngx_http_modsecurity_body_filter(ngx_http_request_t *r, ngx_chain_t *in)
3939
{
4040
ngx_chain_t *chain = in;
4141
ngx_http_modsecurity_ctx_t *ctx = NULL;
42+
ngx_http_modsecurity_conf_t *mcf = NULL;
4243
#if defined(MODSECURITY_SANITY_CHECKS) && (MODSECURITY_SANITY_CHECKS)
43-
ngx_http_modsecurity_conf_t *mcf;
4444
ngx_list_part_t *part = &r->headers_out.headers.part;
4545
ngx_table_elt_t *data = part->elts;
4646
ngx_uint_t i = 0;
@@ -50,7 +50,19 @@ ngx_http_modsecurity_body_filter(ngx_http_request_t *r, ngx_chain_t *in)
5050
return ngx_http_next_body_filter(r, in);
5151
}
5252

53-
ctx = ngx_http_modsecurity_get_module_ctx(r);
53+
mcf = ngx_http_get_module_loc_conf(r, ngx_http_modsecurity_module);
54+
55+
if (mcf == NULL){
56+
dd("failed to get configuration");
57+
return NGX_HTTP_INTERNAL_SERVER_ERROR;
58+
}
59+
60+
if (mcf->skip_res_body_filter) {
61+
dd("Skipping response body filter");
62+
return ngx_http_next_body_filter(r, in);
63+
}
64+
65+
ctx = ngx_http_get_module_ctx(r, ngx_http_modsecurity_module);
5466

5567
dd("body filter, recovering ctx: %p", ctx);
5668

@@ -63,8 +75,7 @@ ngx_http_modsecurity_body_filter(ngx_http_request_t *r, ngx_chain_t *in)
6375
}
6476

6577
#if defined(MODSECURITY_SANITY_CHECKS) && (MODSECURITY_SANITY_CHECKS)
66-
mcf = ngx_http_get_module_loc_conf(r, ngx_http_modsecurity_module);
67-
if (mcf != NULL && mcf->sanity_checks_enabled != NGX_CONF_UNSET)
78+
if (mcf->sanity_checks_enabled != NGX_CONF_UNSET)
6879
{
6980
#if 0
7081
dd("dumping stored ctx headers");

src/ngx_http_modsecurity_common.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,8 @@ typedef struct {
123123
#endif
124124

125125
ngx_http_complex_value_t *transaction_id;
126+
ngx_flag_t skip_req_body_filter;
127+
ngx_flag_t skip_res_body_filter;
126128
} ngx_http_modsecurity_conf_t;
127129

128130

src/ngx_http_modsecurity_module.c

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -513,6 +513,22 @@ static ngx_command_t ngx_http_modsecurity_commands[] = {
513513
0,
514514
NULL
515515
},
516+
{
517+
ngx_string("modsecurity_skip_req_body_filter"),
518+
NGX_HTTP_LOC_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_MAIN_CONF|NGX_CONF_FLAG,
519+
ngx_conf_set_flag_slot,
520+
NGX_HTTP_LOC_CONF_OFFSET,
521+
offsetof(ngx_http_modsecurity_conf_t, skip_req_body_filter),
522+
NULL
523+
},
524+
{
525+
ngx_string("modsecurity_skip_res_body_filter"),
526+
NGX_HTTP_LOC_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_MAIN_CONF|NGX_CONF_FLAG,
527+
ngx_conf_set_flag_slot,
528+
NGX_HTTP_LOC_CONF_OFFSET,
529+
offsetof(ngx_http_modsecurity_conf_t, skip_res_body_filter),
530+
NULL
531+
},
516532
ngx_null_command
517533
};
518534

@@ -724,6 +740,8 @@ ngx_http_modsecurity_create_conf(ngx_conf_t *cf)
724740
conf->rules_set = msc_create_rules_set();
725741
conf->pool = cf->pool;
726742
conf->transaction_id = NGX_CONF_UNSET_PTR;
743+
conf->skip_req_body_filter = NGX_CONF_UNSET;
744+
conf->skip_res_body_filter = NGX_CONF_UNSET;
727745
#if defined(MODSECURITY_SANITY_CHECKS) && (MODSECURITY_SANITY_CHECKS)
728746
conf->sanity_checks_enabled = NGX_CONF_UNSET;
729747
#endif
@@ -763,6 +781,8 @@ ngx_http_modsecurity_merge_conf(ngx_conf_t *cf, void *parent, void *child)
763781

764782
ngx_conf_merge_value(c->enable, p->enable, 0);
765783
ngx_conf_merge_ptr_value(c->transaction_id, p->transaction_id, NULL);
784+
ngx_conf_merge_value(c->skip_req_body_filter, p->skip_req_body_filter, 0);
785+
ngx_conf_merge_value(c->skip_res_body_filter, p->skip_res_body_filter, 0);
766786
#if defined(MODSECURITY_SANITY_CHECKS) && (MODSECURITY_SANITY_CHECKS)
767787
ngx_conf_merge_value(c->sanity_checks_enabled, p->sanity_checks_enabled, 0);
768788
#endif

src/ngx_http_modsecurity_pre_access.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,13 @@ ngx_http_modsecurity_pre_access_handler(ngx_http_request_t *r)
5858
dd("ModSecurity not enabled... returning");
5959
return NGX_DECLINED;
6060
}
61+
62+
if(mcf->skip_req_body_filter == 1)
63+
{
64+
dd("Skipping request body filter");
65+
return NGX_DECLINED;
66+
}
67+
6168
/*
6269
* FIXME:
6370
* In order to perform some tests, let's accept everything.

0 commit comments

Comments
 (0)