Skip to content
Open
Show file tree
Hide file tree
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
58 changes: 57 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ Further information about building nginx third-party modules is available [here]
# Usage

ModSecurity for nginx extends your nginx configuration directives.
It adds six new directives and they are:
It adds the following directives:

modsecurity
-----------
Expand Down Expand Up @@ -211,6 +211,62 @@ modsecurity_use_error_log

Turns on or off ModSecurity error log functionality.

modsecurity_response_body
-------------------------
**syntax:** *modsecurity_response_body on | off*

**context:** *http, server, location*

**default:** *on*

Controls whether the connector buffers response bodies and hands them to
ModSecurity. It does not enable inspection on its own: whether the body is
inspected at all is still decided by `SecResponseBodyAccess` and
`SecResponseBodyMimeType`.

When turned off the connector no longer asks nginx to keep the response body
in memory, so with `sendfile` enabled file buffers are sent straight from the
file instead of being read into memory first, and it no longer copies the
response buffers into ModSecurity. The setting of the main request's location at
the time ModSecurity processes the response headers applies to the whole
response, including subrequests (SSI includes, `add_after_body`,
`auth_request`) and an `error_page` served after a ModSecurity intervention.

ModSecurity still runs phase 4 on the last buffer, but against an empty body
(ModSecurity itself skips phase 4 altogether when `SecResponseBodyAccess` is
`Off` or the response Content-Type is not listed in
`SecResponseBodyMimeType`):

* `RESPONSE_BODY` is empty and `RESPONSE_CONTENT_LENGTH` is `0`.
* Rules that look for content in the response body silently stop matching.
This includes the OWASP CRS outbound data leakage rules
(`RESPONSE-95x-*.conf`) that inspect `RESPONSE_BODY`.
* Rules that match on the absence of content now match on every response,
for instance `RESPONSE_BODY "!@contains </html>"`, `RESPONSE_BODY "@rx ^$"`
or `RESPONSE_CONTENT_LENGTH "@lt 100"`. If such a rule is disruptive, it
fires after the response header has already been passed on, so the client
gets an empty or truncated response instead of the error page.
* The audit log has no part E (response body).
* Phase 4 rules that do not look at the body, for instance rules on
`RESPONSE_STATUS` or on variables set in earlier phases, keep working.

Turning it off is recommended for locations serving static files or large
downloads, and wherever `SecResponseBodyAccess Off` is in use: ModSecurity
still copies the response bodies of the configured MIME types and the
connector has no way to query that setting, so this directive is the only
way to avoid the cost.

```nginx
server {
sendfile on;
modsecurity on;
modsecurity_rules_file /etc/my_modsecurity_rules.conf;
location /static/ {
modsecurity_response_body off;
}
}
```

# Contributing

As an open source project we invite (and encourage) anyone from the community to contribute to our project. This may take the form of: new
Expand Down
15 changes: 9 additions & 6 deletions src/ngx_http_modsecurity_body_filter.c
Original file line number Diff line number Diff line change
Expand Up @@ -143,14 +143,17 @@ ngx_http_modsecurity_body_filter(ngx_http_request_t *r, ngx_chain_t *in)
int is_request_processed = 0;
for (; chain != NULL; chain = chain->next)
{
u_char *data = chain->buf->pos;
int ret;

msc_append_response_body(ctx->modsec_transaction, data, chain->buf->last - data);
ret = ngx_http_modsecurity_process_intervention(ctx->modsec_transaction, r, 0);
if (ret > 0) {
return ngx_http_filter_finalize_request(r,
&ngx_http_modsecurity_module, ret);
if (ctx->response_body) {
u_char *data = chain->buf->pos;

msc_append_response_body(ctx->modsec_transaction, data, chain->buf->last - data);
ret = ngx_http_modsecurity_process_intervention(ctx->modsec_transaction, r, 0);
if (ret > 0) {
return ngx_http_filter_finalize_request(r,
&ngx_http_modsecurity_module, ret);
}
}

/* XXX: chain->buf->last_buf || chain->buf->last_in_chain */
Expand Down
2 changes: 2 additions & 0 deletions src/ngx_http_modsecurity_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ typedef struct {
unsigned logged:1;
unsigned intervention_triggered:1;
unsigned request_body_processed:1;
unsigned response_body:1;
} ngx_http_modsecurity_ctx_t;


Expand All @@ -119,6 +120,7 @@ typedef struct {

ngx_flag_t enable;
ngx_flag_t use_error_log;
ngx_flag_t response_body;
#if defined(MODSECURITY_SANITY_CHECKS) && (MODSECURITY_SANITY_CHECKS)
ngx_flag_t sanity_checks_enabled;
#endif
Expand Down
17 changes: 11 additions & 6 deletions src/ngx_http_modsecurity_header_filter.c
Original file line number Diff line number Diff line change
Expand Up @@ -411,6 +411,7 @@ ngx_int_t
ngx_http_modsecurity_header_filter(ngx_http_request_t *r)
{
ngx_http_modsecurity_ctx_t *ctx;
ngx_http_modsecurity_conf_t *mcf;
ngx_list_part_t *part = &r->headers_out.headers.part;
ngx_table_elt_t *data = part->elts;
ngx_uint_t i = 0;
Expand Down Expand Up @@ -448,12 +449,16 @@ ngx_http_modsecurity_header_filter(ngx_http_request_t *r)
return ngx_http_next_header_filter(r);
}

/*
* Lets ask nginx to keep the response body in memory
*
* FIXME: I don't see a reason to keep it `1' when SecResponseBody is disabled.
*/
r->filter_need_in_memory = 1;
mcf = ngx_http_get_module_loc_conf(r->main, ngx_http_modsecurity_module);
ctx->response_body = mcf->response_body;

if (ctx->response_body) {
/*
* Ask nginx to keep the response body in memory so that the body
* filter can feed it to ModSecurity.
*/
r->filter_need_in_memory = 1;
}

ctx->processed = 1;
/*
Expand Down
10 changes: 10 additions & 0 deletions src/ngx_http_modsecurity_module.c
Original file line number Diff line number Diff line change
Expand Up @@ -535,6 +535,14 @@ static ngx_command_t ngx_http_modsecurity_commands[] = {
offsetof(ngx_http_modsecurity_conf_t, use_error_log),
NULL
},
{
ngx_string("modsecurity_response_body"),
NGX_HTTP_LOC_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_MAIN_CONF|NGX_CONF_FLAG,
ngx_conf_set_flag_slot,
NGX_HTTP_LOC_CONF_OFFSET,
offsetof(ngx_http_modsecurity_conf_t, response_body),
NULL
},
ngx_null_command
};

Expand Down Expand Up @@ -730,6 +738,7 @@ ngx_http_modsecurity_create_conf(ngx_conf_t *cf)
conf->pool = cf->pool;
conf->transaction_id = NGX_CONF_UNSET_PTR;
conf->use_error_log = NGX_CONF_UNSET;
conf->response_body = NGX_CONF_UNSET;
#if defined(MODSECURITY_SANITY_CHECKS) && (MODSECURITY_SANITY_CHECKS)
conf->sanity_checks_enabled = NGX_CONF_UNSET;
#endif
Expand Down Expand Up @@ -770,6 +779,7 @@ ngx_http_modsecurity_merge_conf(ngx_conf_t *cf, void *parent, void *child)
ngx_conf_merge_value(c->enable, p->enable, 0);
ngx_conf_merge_ptr_value(c->transaction_id, p->transaction_id, NULL);
ngx_conf_merge_value(c->use_error_log, p->use_error_log, 1);
ngx_conf_merge_value(c->response_body, p->response_body, 1);
#if defined(MODSECURITY_SANITY_CHECKS) && (MODSECURITY_SANITY_CHECKS)
ngx_conf_merge_value(c->sanity_checks_enabled, p->sanity_checks_enabled, 0);
#endif
Expand Down
Loading
Loading