Skip to content

Commit 49cf8b8

Browse files
Felipe Zimmerledaniilyar
Felipe Zimmerle
authored andcommitted
Nginx fixes
Refactoring on the nginx module, including: - Better handling larger posts; - Now using nginx echo module during the regression tests. - Better interacting with neginx chain rules - Separation of the request handling and content filters. - Better handling nginx sessions and resource counts to allow a more efficient garbage collector. - Handling both http/1.0 and 1.1, including keep-alive. - Tests are now capable to test nginx as a proxy or end-server. - Tested agains nginx 1.6 and 1.7. - Better dealing with chunked request body
1 parent efd59ba commit 49cf8b8

File tree

1 file changed

+42
-16
lines changed

1 file changed

+42
-16
lines changed

nginx/modsecurity/ngx_http_modsecurity.c

Lines changed: 42 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
/* Those are defined twice, lets keep it defined just once by `undef`
2222
* the first one.
2323
*/
24+
*/
2425
#undef CR
2526
#undef LF
2627
#undef CRLF
@@ -228,7 +229,6 @@ ngx_pstrdup0(ngx_pool_t *pool, ngx_str_t *src)
228229
return dst;
229230
}
230231

231-
232232
/*
233233
* MultiplyDeBruijnBitPosition
234234
* http://graphics.stanford.edu/~seander/bithacks.html#ZerosOnRightMultLookup
@@ -563,6 +563,10 @@ ngx_http_modsecurity_load_request_body(ngx_http_request_t *r)
563563
ngx_http_modsecurity_ctx_t *ctx;
564564
ngx_chain_t *chain;
565565

566+
ngx_log_debug(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
567+
"ModSec: loading request body.");
568+
569+
566570
ngx_log_debug(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
567571
"ModSec: loading request body.");
568572

@@ -598,7 +602,6 @@ ngx_http_modsecurity_load_request_body(ngx_http_request_t *r)
598602

599603
return NGX_OK;
600604
}
601-
602605
static ngx_inline ngx_int_t
603606
ngx_http_modsecurity_save_request_body(ngx_http_request_t *r)
604607
{
@@ -607,7 +610,6 @@ ngx_http_modsecurity_save_request_body(ngx_http_request_t *r)
607610
apr_off_t content_length;
608611
ngx_buf_t *buf;
609612
#endif
610-
611613
ctx = ngx_http_get_module_ctx(r, ngx_http_modsecurity);
612614

613615
#ifdef MOVE_REQUEST_CHAIN_TO_MODSEC
@@ -619,10 +621,8 @@ ngx_http_modsecurity_save_request_body(ngx_http_request_t *r)
619621
apr_brigade_cleanup(ctx->brigade);
620622
buf->last += content_length;
621623
r->header_in = buf;
622-
623624
if (r->headers_in.content_length) {
624625
ngx_str_t *str = NULL;
625-
626626
str = &r->headers_in.content_length->value;
627627
str->data = ngx_palloc(r->pool, NGX_OFF_T_LEN);
628628
if (str->data == NULL) {
@@ -631,18 +631,31 @@ ngx_http_modsecurity_save_request_body(ngx_http_request_t *r)
631631
}
632632
str->len = ngx_snprintf(str->data, NGX_OFF_T_LEN, "%O",
633633
content_length) - str->data;
634-
635-
}
636-
637634
r->headers_in.content_length_n = content_length;
638-
639635
ngx_log_debug(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
640636
"ModSec: Content length: %O, Content length n: %O", content_length,
641637
r->headers_in.content_length_n);
642638
#else
643639
apr_brigade_cleanup(ctx->brigade);
644640
#endif
645641

642+
if (r->headers_in.content_length) {
643+
ngx_str_t *str = NULL;
644+
645+
str = &r->headers_in.content_length->value;
646+
str->data = ngx_palloc(r->pool, NGX_OFF_T_LEN);
647+
if (str->data == NULL) {
648+
ngx_http_finalize_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
649+
return NGX_OK;
650+
}
651+
str->len = ngx_snprintf(str->data, NGX_OFF_T_LEN, "%O", content_length) - str->data;
652+
653+
}
654+
655+
656+
r->headers_in.content_length_n = content_length;
657+
658+
ngx_log_debug(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, "ModSec: Content length: %O, Content length n: %O", content_length, r->headers_in.content_length_n);
646659
return NGX_OK;
647660
}
648661

@@ -1151,19 +1164,25 @@ ngx_http_modsecurity_handler(ngx_http_request_t *r) {
11511164
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
11521165
"ModSec: ModSecurity is not enabled or not a main request.");
11531166

1167+
ctx = ngx_http_modsecurity_create_ctx(r);
1168+
11541169
return NGX_DECLINED;
1170+
if (ngx_http_set_pool_ctx(r, ctx, ngx_http_modsecurity) != NGX_OK) {
1171+
return NGX_ERROR;
11551172
}
11561173

11571174
ctx = ngx_http_get_module_ctx(r, ngx_http_modsecurity);
11581175

11591176
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
11601177
"ModSec: Recovering ctx: %p", ctx);
1178+
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
1179+
"ModSec: ctx is null, nothing we can do, returning.");
11611180

11621181
if (ctx == NULL) {
11631182
ctx = ngx_http_modsecurity_create_ctx(r);
11641183

11651184
ngx_http_set_ctx(r, ctx, ngx_http_modsecurity);
1166-
1185+
"ModSec: ctx is now: %p / count: %d", ctx, r->main->count);
11671186
if (ngx_http_set_pool_ctx(r, ctx, ngx_http_modsecurity) != NGX_OK) {
11681187
return NGX_ERROR;
11691188
}
@@ -1181,19 +1200,17 @@ ngx_http_modsecurity_handler(ngx_http_request_t *r) {
11811200
ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
11821201
"ModSec: ctx is now: %p / count: %d", ctx, r->main->count);
11831202

1184-
if (modsecContextState(ctx->req) == MODSEC_DISABLED) {
11851203
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
11861204
"ModSec: ModSecurity was disabled, returning....", ctx);
11871205

1188-
return NGX_DECLINED;
1189-
}
11901206
if (ctx->waiting_more_body == 1) {
11911207
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
11921208
"ModSec: waiting for more data before proceed. / count: %d",
11931209
r->main->count);
11941210

11951211
return NGX_DONE;
11961212
}
1213+
ngx_http_modsecurity_request_read);
11971214

11981215
if (ctx->body_requested == 0) {
11991216
ngx_log_debug(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
@@ -1221,9 +1238,16 @@ ngx_http_modsecurity_handler(ngx_http_request_t *r) {
12211238

12221239
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
12231240
"ModSec: request is ready to be processed.");
1224-
rc = ngx_http_modsecurity_process_request(r);
1241+
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
1242+
"ModSec: chuncked? %d", r->chunked);
1243+
ngx_http_modsecurity_process_request(r);
12251244
ctx->request_processed = 1;
1245+
}
12261246

1247+
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
1248+
"ModSec: request is ready to be processed.");
1249+
rc = ngx_http_modsecurity_process_request(r);
1250+
ctx->request_processed = 1;
12271251
if (rc == NGX_ERROR || rc >= NGX_HTTP_SPECIAL_RESPONSE) {
12281252
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
12291253
"ModSec: returning a special response after process " \
@@ -1232,8 +1256,10 @@ ngx_http_modsecurity_handler(ngx_http_request_t *r) {
12321256
return rc;
12331257
}
12341258

1259+
void
1260+
ngx_http_modsecurity_request_read(ngx_http_request_t *r)
1261+
{
12351262

1236-
}
12371263

12381264
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
12391265
"ModSec: returning NGX_DECLINED." );
@@ -1435,7 +1461,7 @@ ngx_http_modsecurity_body_filter(ngx_http_request_t *r, ngx_chain_t *in)
14351461
}
14361462

14371463
return ngx_http_next_body_filter(r, out);
1438-
1464+
#endif
14391465
return NGX_OK;
14401466
}
14411467
#endif

0 commit comments

Comments
 (0)