Skip to content

Commit ae30826

Browse files
committed
Picked up code from PR #273
1 parent 62639fa commit ae30826

7 files changed

+47
-18
lines changed

‎src/ngx_http_modsecurity_body_filter.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ ngx_http_modsecurity_body_filter(ngx_http_request_t *r, ngx_chain_t *in)
5555
}
5656

5757
/* get context for request */
58-
ctx=ngx_http_get_module_ctx(r, ngx_http_modsecurity_module);
58+
ctx=ngx_http_modsecurity_get_module_ctx(r);
5959
dd("body filter, recovering ctx: %p", ctx);
6060

6161
if (ctx==NULL||r->filter_finalize||ctx->response_body_filtered){

‎src/ngx_http_modsecurity_common.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ typedef struct{
102102
unsignedresponse_body_filtered:1;
103103
unsignedlogged:1;
104104
unsignedintervention_triggered:1;
105+
unsignedrequest_body_processed:1;
105106
} ngx_http_modsecurity_ctx_t;
106107

107108

@@ -142,6 +143,7 @@ extern ngx_module_t ngx_http_modsecurity_module;
142143
/* ngx_http_modsecurity_module.c */
143144
intngx_http_modsecurity_process_intervention (Transaction*transaction, ngx_http_request_t*r, ngx_int_tearly_log);
144145
ngx_http_modsecurity_ctx_t*ngx_http_modsecurity_create_ctx(ngx_http_request_t*r);
146+
ngx_http_modsecurity_ctx_t*ngx_http_modsecurity_get_module_ctx(ngx_http_request_t*r);
145147
char*ngx_str_to_char(ngx_str_ta, ngx_pool_t*p);
146148
#if (NGX_PCRE2)
147149
#definengx_http_modsecurity_pcre_malloc_init(x) NULL

‎src/ngx_http_modsecurity_header_filter.c‎

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ ngx_http_modsecurity_store_ctx_header(ngx_http_request_t *r, ngx_str_t *name, ng
109109
ngx_http_modsecurity_conf_t*mcf;
110110
ngx_http_modsecurity_header_t*hdr;
111111

112-
ctx=ngx_http_get_module_ctx(r, ngx_http_modsecurity_module);
112+
ctx=ngx_http_modsecurity_get_module_ctx(r);
113113
if (ctx==NULL||ctx->sanity_headers_out==NULL){
114114
returnNGX_ERROR;
115115
}
@@ -152,7 +152,7 @@ ngx_http_modsecurity_resolv_header_server(ngx_http_request_t *r, ngx_str_t name,
152152
ngx_str_tvalue;
153153

154154
clcf=ngx_http_get_module_loc_conf(r, ngx_http_core_module);
155-
ctx=ngx_http_get_module_ctx(r, ngx_http_modsecurity_module);
155+
ctx=ngx_http_modsecurity_get_module_ctx(r);
156156

157157
if (r->headers_out.server==NULL){
158158
if (clcf->server_tokens){
@@ -186,7 +186,7 @@ ngx_http_modsecurity_resolv_header_date(ngx_http_request_t *r, ngx_str_t name, o
186186
ngx_http_modsecurity_ctx_t*ctx=NULL;
187187
ngx_str_tdate;
188188

189-
ctx=ngx_http_get_module_ctx(r, ngx_http_modsecurity_module);
189+
ctx=ngx_http_modsecurity_get_module_ctx(r);
190190

191191
if (r->headers_out.date==NULL){
192192
date.data=ngx_cached_http_time.data;
@@ -216,7 +216,7 @@ ngx_http_modsecurity_resolv_header_content_length(ngx_http_request_t *r, ngx_str
216216
ngx_str_tvalue;
217217
charbuf[NGX_INT64_LEN+2];
218218

219-
ctx=ngx_http_get_module_ctx(r, ngx_http_modsecurity_module);
219+
ctx=ngx_http_modsecurity_get_module_ctx(r);
220220

221221
if (r->headers_out.content_length_n>0)
222222
{
@@ -243,7 +243,7 @@ ngx_http_modsecurity_resolv_header_content_type(ngx_http_request_t *r, ngx_str_t
243243
{
244244
ngx_http_modsecurity_ctx_t*ctx=NULL;
245245

246-
ctx=ngx_http_get_module_ctx(r, ngx_http_modsecurity_module);
246+
ctx=ngx_http_modsecurity_get_module_ctx(r);
247247

248248
if (r->headers_out.content_type.len>0)
249249
{
@@ -270,7 +270,7 @@ ngx_http_modsecurity_resolv_header_last_modified(ngx_http_request_t *r, ngx_str_
270270
u_charbuf[1024], *p;
271271
ngx_str_tvalue;
272272

273-
ctx=ngx_http_get_module_ctx(r, ngx_http_modsecurity_module);
273+
ctx=ngx_http_modsecurity_get_module_ctx(r);
274274

275275
if (r->headers_out.last_modified_time==-1){
276276
return1;
@@ -302,7 +302,7 @@ ngx_http_modsecurity_resolv_header_connection(ngx_http_request_t *r, ngx_str_t n
302302
ngx_str_tvalue;
303303

304304
clcf=ngx_http_get_module_loc_conf(r, ngx_http_core_module);
305-
ctx=ngx_http_get_module_ctx(r, ngx_http_modsecurity_module);
305+
ctx=ngx_http_modsecurity_get_module_ctx(r);
306306

307307
if (r->headers_out.status==NGX_HTTP_SWITCHING_PROTOCOLS){
308308
connection="upgrade";
@@ -353,7 +353,7 @@ ngx_http_modsecurity_resolv_header_transfer_encoding(ngx_http_request_t *r, ngx_
353353
if (r->chunked){
354354
ngx_str_tvalue=ngx_string("chunked");
355355

356-
ctx=ngx_http_get_module_ctx(r, ngx_http_modsecurity_module);
356+
ctx=ngx_http_modsecurity_get_module_ctx(r);
357357

358358
#if defined(MODSECURITY_SANITY_CHECKS) && (MODSECURITY_SANITY_CHECKS)
359359
ngx_http_modsecurity_store_ctx_header(r, &name, &value);
@@ -380,7 +380,7 @@ ngx_http_modsecurity_resolv_header_vary(ngx_http_request_t *r, ngx_str_t name, o
380380
if (r->gzip_vary&&clcf->gzip_vary){
381381
ngx_str_tvalue=ngx_string("Accept-Encoding");
382382

383-
ctx=ngx_http_get_module_ctx(r, ngx_http_modsecurity_module);
383+
ctx=ngx_http_modsecurity_get_module_ctx(r);
384384

385385
#if defined(MODSECURITY_SANITY_CHECKS) && (MODSECURITY_SANITY_CHECKS)
386386
ngx_http_modsecurity_store_ctx_header(r, &name, &value);
@@ -422,7 +422,7 @@ ngx_http_modsecurity_header_filter(ngx_http_request_t *r)
422422

423423
/* XXX: if NOT_MODIFIED, do we need to process it at all? see xslt_header_filter() */
424424

425-
ctx=ngx_http_get_module_ctx(r, ngx_http_modsecurity_module);
425+
ctx=ngx_http_modsecurity_get_module_ctx(r);
426426

427427
dd("header filter, recovering ctx: %p", ctx);
428428

‎src/ngx_http_modsecurity_log.c‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,13 +60,13 @@ ngx_http_modsecurity_log_handler(ngx_http_request_t *r)
6060
return NGX_OK;
6161
}
6262
*/
63-
ctx=ngx_http_get_module_ctx(r, ngx_http_modsecurity_module);
63+
ctx=ngx_http_modsecurity_get_module_ctx(r);
6464

6565
dd("recovering ctx: %p", ctx);
6666

6767
if (ctx==NULL){
68-
dd("something really bad happened here. returning NGX_ERROR");
69-
returnNGX_ERROR;
68+
dd("ModSecurity not enabled or error occurred");
69+
returnNGX_OK;
7070
}
7171

7272
if (ctx->logged){

‎src/ngx_http_modsecurity_module.c‎

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ ngx_http_modsecurity_process_intervention (Transaction *transaction, ngx_http_re
149149

150150
dd("processing intervention");
151151

152-
ctx=ngx_http_get_module_ctx(r, ngx_http_modsecurity_module);
152+
ctx=ngx_http_modsecurity_get_module_ctx(r);
153153
if (ctx==NULL)
154154
{
155155
returnNGX_HTTP_INTERNAL_SERVER_ERROR;
@@ -314,6 +314,27 @@ ngx_http_modsecurity_create_ctx(ngx_http_request_t *r)
314314
returnctx;
315315
}
316316

317+
ngx_inlinengx_http_modsecurity_ctx_t*
318+
ngx_http_modsecurity_get_module_ctx(ngx_http_request_t*r)
319+
{
320+
ngx_http_modsecurity_ctx_t*ctx;
321+
ctx=ngx_http_get_module_ctx(r, ngx_http_modsecurity_module);
322+
if (ctx==NULL){
323+
/*
324+
* refer <nginx>/src/http/modules/ngx_http_realip_module.c
325+
* if module context was reset, the original address
326+
* can still be found in the cleanup handler
327+
*/
328+
ngx_pool_cleanup_t*cln;
329+
for (cln=r->pool->cleanup; cln; cln=cln->next){
330+
if (cln->handler==ngx_http_modsecurity_cleanup){
331+
ctx=cln->data;
332+
break;
333+
}
334+
}
335+
}
336+
returnctx;
337+
}
317338

318339
char*
319340
ngx_conf_set_rules(ngx_conf_t*cf, ngx_command_t*cmd, void*conf)

‎src/ngx_http_modsecurity_pre_access.c‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ ngx_http_modsecurity_request_read(ngx_http_request_t *r)
2727
{
2828
ngx_http_modsecurity_ctx_t*ctx;
2929

30-
ctx=ngx_http_get_module_ctx(r, ngx_http_modsecurity_module);
30+
ctx=ngx_http_modsecurity_get_module_ctx(r);
3131

3232
#if defined(nginx_version) &&nginx_version >= 8011
3333
r->main->count--;
@@ -70,7 +70,7 @@ ngx_http_modsecurity_pre_access_handler(ngx_http_request_t *r)
7070
}
7171
*/
7272

73-
ctx=ngx_http_get_module_ctx(r, ngx_http_modsecurity_module);
73+
ctx=ngx_http_modsecurity_get_module_ctx(r);
7474

7575
dd("recovering ctx: %p", ctx);
7676

@@ -80,6 +80,11 @@ ngx_http_modsecurity_pre_access_handler(ngx_http_request_t *r)
8080
returnNGX_HTTP_INTERNAL_SERVER_ERROR;
8181
}
8282

83+
if (ctx->request_body_processed){
84+
// should we use r->internal or r->filter_finalize?
85+
returnNGX_DECLINED;
86+
}
87+
8388
if (ctx->intervention_triggered){
8489
returnNGX_DECLINED;
8590
}
@@ -212,6 +217,7 @@ ngx_http_modsecurity_pre_access_handler(ngx_http_request_t *r)
212217

213218
old_pool=ngx_http_modsecurity_pcre_malloc_init(r->pool);
214219
msc_process_request_body(ctx->modsec_transaction);
220+
ctx->request_body_processed=1;
215221
ngx_http_modsecurity_pcre_malloc_done(old_pool);
216222

217223
ret=ngx_http_modsecurity_process_intervention(ctx->modsec_transaction, r, 0);

‎src/ngx_http_modsecurity_rewrite.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ ngx_http_modsecurity_rewrite_handler(ngx_http_request_t *r)
4646

4747
dd("catching a new _rewrite_ phase handler");
4848

49-
ctx=ngx_http_get_module_ctx(r, ngx_http_modsecurity_module);
49+
ctx=ngx_http_modsecurity_get_module_ctx(r);
5050

5151
dd("recovering ctx: %p", ctx);
5252

0 commit comments

Comments
(0)