Skip to content

Commit af17ac8

Browse files
committed
initial commit to skip the body inspection
1 parent b94f2d3 commit af17ac8

5 files changed

Lines changed: 332 additions & 5 deletions

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
-----------
@@ -184,6 +184,60 @@ modsecurity_use_error_log
184184
**default:** *on*
185185

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

188242
# Contributing
189243

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
@@ -124,6 +124,8 @@ typedef struct {
124124
#endif
125125

126126
ngx_http_complex_value_t *transaction_id;
127+
ngx_flag_t skip_req_body_filter;
128+
ngx_flag_t skip_res_body_filter;
127129
} ngx_http_modsecurity_conf_t;
128130

129131

src/ngx_http_modsecurity_module.c

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -527,6 +527,19 @@ static ngx_command_t ngx_http_modsecurity_commands[] = {
527527
ngx_conf_set_flag_slot,
528528
NGX_HTTP_LOC_CONF_OFFSET,
529529
offsetof(ngx_http_modsecurity_conf_t, use_error_log),
530+
ngx_string("modsecurity_skip_req_body_filter"),
531+
NGX_HTTP_LOC_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_MAIN_CONF|NGX_CONF_FLAG,
532+
ngx_conf_set_flag_slot,
533+
NGX_HTTP_LOC_CONF_OFFSET,
534+
offsetof(ngx_http_modsecurity_conf_t, skip_req_body_filter),
535+
NULL
536+
},
537+
{
538+
ngx_string("modsecurity_skip_res_body_filter"),
539+
NGX_HTTP_LOC_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_MAIN_CONF|NGX_CONF_FLAG,
540+
ngx_conf_set_flag_slot,
541+
NGX_HTTP_LOC_CONF_OFFSET,
542+
offsetof(ngx_http_modsecurity_conf_t, skip_res_body_filter),
530543
NULL
531544
},
532545
ngx_null_command
@@ -724,6 +737,8 @@ ngx_http_modsecurity_create_conf(ngx_conf_t *cf)
724737
conf->pool = cf->pool;
725738
conf->transaction_id = NGX_CONF_UNSET_PTR;
726739
conf->use_error_log = NGX_CONF_UNSET;
740+
conf->skip_req_body_filter = NGX_CONF_UNSET;
741+
conf->skip_res_body_filter = NGX_CONF_UNSET;
727742
#if defined(MODSECURITY_SANITY_CHECKS) && (MODSECURITY_SANITY_CHECKS)
728743
conf->sanity_checks_enabled = NGX_CONF_UNSET;
729744
#endif
@@ -764,6 +779,8 @@ ngx_http_modsecurity_merge_conf(ngx_conf_t *cf, void *parent, void *child)
764779
ngx_conf_merge_value(c->enable, p->enable, 0);
765780
ngx_conf_merge_ptr_value(c->transaction_id, p->transaction_id, NULL);
766781
ngx_conf_merge_value(c->use_error_log, p->use_error_log, 1);
782+
ngx_conf_merge_value(c->skip_req_body_filter, p->skip_req_body_filter, 0);
783+
ngx_conf_merge_value(c->skip_res_body_filter, p->skip_res_body_filter, 0);
767784
#if defined(MODSECURITY_SANITY_CHECKS) && (MODSECURITY_SANITY_CHECKS)
768785
ngx_conf_merge_value(c->sanity_checks_enabled, p->sanity_checks_enabled, 0);
769786
#endif

0 commit comments

Comments
 (0)