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_request_body
------------------------
**syntax:** *modsecurity_request_body on | off*

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

**default:** *on*

Turns on or off the request body inspection.

When turned off, the connector does not read and buffer the request body and
does not feed it to ModSecurity, so `REQUEST_BODY`, `ARGS_POST`, `FILES` and
the other body related variables are empty. Phase 2 rules are still executed
(with `SecRequestBodyAccess On`; libmodsecurity skips phase 2 entirely when it
is `Off`), hence rules matching on `ARGS`, `REQUEST_HEADERS` and friends keep
working. The request body is then read by the content handler with nginx's
own settings, which means that directives such as `proxy_request_buffering off`
are effective again.

As no body reaches ModSecurity, `SecRequestBodyLimit`,
`SecRequestBodyNoFilesLimit` and `SecRequestBodyLimitAction` are not enforced
in such a location; only nginx's `client_max_body_size` limits the body size.

The body processors still run, on an empty body. The URLENCODED, MULTIPART
and JSON processors accept it, but the XML processor fails to parse an empty
document and sets `REQBODY_ERROR`, so with the recommended rules 200000 and
200002 every XML request to such a location is rejected with 400. Remove rule
200000 there: `modsecurity_rules 'SecRuleRemoveById 200000';`.

The setting of the location where the access phase first runs applies to the
whole request: an internal redirect (for example `try_files`, `index` or a
named location) to a location with `on` does not re-enable body inspection,
so set `off` only where every redirect target may also skip it.

This is useful for locations handling large uploads, encrypted payloads, or
for locations where `SecRequestBodyAccess Off` is already set: libmodsecurity
copies the request body regardless of `SecRequestBodyAccess`, so turning this
directive off is the only way to avoid that cost.

This directive is a per-location opt-out. It does not make the connector
inspect the body while it is streamed to the backend: with `on`, the whole
body is still buffered before inspection.

```nginx
server {
modsecurity on;
modsecurity_rules_file /etc/my_modsecurity_rules.conf;

location /upload {
modsecurity_request_body off;
proxy_request_buffering off;
proxy_pass http://backend;
}
}
```

# 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
28 changes: 16 additions & 12 deletions src/ngx_http_modsecurity_access.c
Original file line number Diff line number Diff line change
Expand Up @@ -305,12 +305,11 @@ ngx_http_modsecurity_access_handler(ngx_http_request_t *r)
return NGX_HTTP_INTERNAL_SERVER_ERROR;
}

if (ctx->request_body_processed) {
// should we use r->internal or r->filter_finalize?
if (ctx->intervention_triggered) {
return NGX_DECLINED;
}

if (ctx->intervention_triggered) {
if (ctx->request_body_processed) {
return NGX_DECLINED;
}

Expand All @@ -322,7 +321,10 @@ ngx_http_modsecurity_access_handler(ngx_http_request_t *r)
return NGX_DONE;
}

if (ctx->body_requested == 0)
if (mcf->request_body == 0) {
dd("request body inspection disabled by modsecurity_request_body off");

} else if (ctx->body_requested == 0)
{
ngx_int_t rc = NGX_OK;

Expand Down Expand Up @@ -370,14 +372,12 @@ ngx_http_modsecurity_access_handler(ngx_http_request_t *r)
if (ctx->waiting_more_body == 0)
{
int ret = 0;
int already_inspected = 0;
ngx_chain_t *chain = NULL;

dd("request body is ready to be processed");

r->write_event_handler = ngx_http_core_run_phases;

ngx_chain_t *chain = r->request_body->bufs;

/**
* TODO: Speed up the analysis by sending chunk while they arrive.
*
Expand All @@ -387,7 +387,10 @@ ngx_http_modsecurity_access_handler(ngx_http_request_t *r)
* function.
*/

if (r->request_body->temp_file != NULL) {
if (mcf->request_body == 0 || r->request_body == NULL) {
dd("request body is not inspected");

} else if (r->request_body->temp_file != NULL) {
ngx_str_t file_path = r->request_body->temp_file->file.name;
const char *file_name = ngx_str_to_char(file_path, r->pool);
if (file_name == (char*)-1) {
Expand All @@ -400,13 +403,12 @@ ngx_http_modsecurity_access_handler(ngx_http_request_t *r)
dd("request body inspection: file -- %s", file_name);

msc_request_body_from_file(ctx->modsec_transaction, file_name);

already_inspected = 1;
} else {
dd("inspection request body in memory.");
chain = r->request_body->bufs;
}

while (chain && !already_inspected)
while (chain)
{
u_char *data = chain->buf->pos;

Expand All @@ -427,6 +429,7 @@ ngx_http_modsecurity_access_handler(ngx_http_request_t *r)
*/
ret = ngx_http_modsecurity_process_intervention(ctx->modsec_transaction, r, 0);
if (ret > 0) {
ctx->intervention_triggered = 1;
return ret;
}
}
Expand All @@ -448,8 +451,9 @@ ngx_http_modsecurity_access_handler(ngx_http_request_t *r)
ret = ngx_http_modsecurity_process_intervention(ctx->modsec_transaction, r, 0);
if (r->error_page) {
return NGX_DECLINED;
}
}
if (ret > 0) {
ctx->intervention_triggered = 1;
return ret;
}
}
Expand Down
1 change: 1 addition & 0 deletions src/ngx_http_modsecurity_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ typedef struct {

ngx_flag_t enable;
ngx_flag_t use_error_log;
ngx_flag_t request_body;
#if defined(MODSECURITY_SANITY_CHECKS) && (MODSECURITY_SANITY_CHECKS)
ngx_flag_t sanity_checks_enabled;
#endif
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_request_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, request_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->request_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->request_body, p->request_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