Skip to content

Commit 1d4d37a

Browse files
committed
add patch to skip body inspections
1 parent b94f2d3 commit 1d4d37a

5 files changed

Lines changed: 93 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
-----------
@@ -191,6 +191,60 @@ As an open source project we invite (and encourage) anyone from the community to
191191
functionality, bug fixes, bug reports, beginners user support, and anything else that you
192192
are willing to help with. Thank you.
193193

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

195249
## Providing Patches
196250

src/ngx_http_modsecurity_access.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,11 @@ ngx_http_modsecurity_access_handler(ngx_http_request_t *r)
5656
return NGX_DECLINED;
5757
}
5858

59+
if(mcf->skip_req_body_filter == 1) {
60+
dd("Skipping request body filter");
61+
return NGX_DECLINED;
62+
}
63+
5964
/*
6065
if (r->method != NGX_HTTP_GET &&
6166
r->method != NGX_HTTP_POST && r->method != NGX_HTTP_HEAD) {

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_resp_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_resp_body_filter;
127129
} ngx_http_modsecurity_conf_t;
128130

129131

src/ngx_http_modsecurity_module.c

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -529,6 +529,22 @@ static ngx_command_t ngx_http_modsecurity_commands[] = {
529529
offsetof(ngx_http_modsecurity_conf_t, use_error_log),
530530
NULL
531531
},
532+
{
533+
ngx_string("modsecurity_skip_req_body_filter"),
534+
NGX_HTTP_LOC_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_MAIN_CONF|NGX_CONF_FLAG,
535+
ngx_conf_set_flag_slot,
536+
NGX_HTTP_LOC_CONF_OFFSET,
537+
offsetof(ngx_http_modsecurity_conf_t, skip_req_body_filter),
538+
NULL
539+
},
540+
{
541+
ngx_string("modsecurity_skip_resp_body_filter"),
542+
NGX_HTTP_LOC_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_MAIN_CONF|NGX_CONF_FLAG,
543+
ngx_conf_set_flag_slot,
544+
NGX_HTTP_LOC_CONF_OFFSET,
545+
offsetof(ngx_http_modsecurity_conf_t, skip_resp_body_filter),
546+
NULL
547+
},
532548
ngx_null_command
533549
};
534550

0 commit comments

Comments
 (0)