@@ -183,8 +183,10 @@ ngx_http_modsecurity_merge_loc_conf(ngx_conf_t *cf, void *parent,
183
183
ngx_conf_merge_ptr_value (conf -> config , prev -> config , NULL );
184
184
185
185
if (conf -> enable && conf -> config == NULL ) {
186
- ngx_conf_log_error (NGX_LOG_EMERG , cf , 0 ,
187
- "ModSecurity: enabled in %V:%ui while no config file is specified " ,
186
+ ngx_log_error (NGX_LOG_EMERG , cf -> log , 0 ,
187
+ "\"ModSecurityEnabled\" in %V:%ui is set to \"on\""
188
+ " while directive \"ModSecurityConfig\" is not found"
189
+ " in the same location" ,
188
190
conf -> file , conf -> line );
189
191
return NGX_CONF_ERROR ;
190
192
}
@@ -224,6 +226,7 @@ modsec_pcre_free(void *ptr)
224
226
static ngx_int_t
225
227
ngx_http_modsecurity_preconfiguration (ngx_conf_t * cf )
226
228
{
229
+ server_rec * s ;
227
230
228
231
/* XXX: temporary hack, nginx uses pcre as well and hijacks these two */
229
232
pcre_malloc = modsec_pcre_malloc ;
@@ -232,9 +235,20 @@ ngx_http_modsecurity_preconfiguration(ngx_conf_t *cf)
232
235
modsecSetLogHook (cf -> log , modsecLog );
233
236
modsecSetDropAction (ngx_http_modsecurity_drop_action );
234
237
235
- modsecInit ();
236
- modsecStartConfig ();
238
+ s = modsecInit ();
239
+ if (s == NULL ) {
240
+ return NGX_ERROR ;
241
+ }
242
+
243
+ /* set host name */
244
+ s -> server_hostname = ngx_palloc (cf -> pool , ngx_cycle -> hostname .len + 1 );
245
+ if (s -> server_hostname == NULL ) {
246
+ return NGX_ERROR ;
247
+ }
248
+ ngx_memcpy (s -> server_hostname , ngx_cycle -> hostname .data , ngx_cycle -> hostname .len );
249
+ s -> server_hostname [ ngx_cycle -> hostname .len ] = '\0' ;
237
250
251
+ modsecStartConfig ();
238
252
return NGX_OK ;
239
253
}
240
254
@@ -344,31 +358,27 @@ ngx_http_modsecurity_handler(ngx_http_request_t *r)
344
358
ngx_http_modsecurity_ctx_t * ctx ;
345
359
ngx_int_t rc ;
346
360
347
- ngx_log_debug0 (NGX_LOG_DEBUG_HTTP , r -> connection -> log , 0 , "modSecurity: handler" );
348
-
349
361
cf = ngx_http_get_module_loc_conf (r , ngx_http_modsecurity );
350
362
351
363
/* Process only main request */
352
364
if (r != r -> main || r -> internal || !cf -> enable ) {
353
365
return NGX_DECLINED ;
354
366
}
355
367
368
+ ngx_log_debug0 (NGX_LOG_DEBUG_HTTP , r -> connection -> log , 0 , "modSecurity: handler" );
369
+
356
370
ctx = ngx_http_modsecurity_create_ctx (r );
357
371
if (ctx == NULL ) {
358
372
return NGX_ERROR ;
359
373
}
360
374
ngx_http_set_ctx (r , ctx , ngx_http_modsecurity );
361
375
362
376
/* processing request headers */
363
- ngx_log_debug0 (NGX_LOG_DEBUG_HTTP , r -> connection -> log , 0 , "modSecurity: process request headers" );
364
-
365
377
rc = modsecProcessRequestHeaders (ctx -> req );
366
-
378
+ ngx_log_debug1 ( NGX_LOG_DEBUG_HTTP , r -> connection -> log , 0 , "ModSecurity: modsecProcessRequestHeaders %d" , rc );
367
379
if (rc == DECLINED ) {
368
380
if (r -> method == NGX_HTTP_POST ) {
369
381
/* Processing POST request body, should we process PUT? */
370
- ngx_log_debug0 (NGX_LOG_DEBUG_HTTP , r -> connection -> log , 0 , "modSecurity: method POST" );
371
-
372
382
rc = ngx_http_read_client_request_body (r , ngx_http_modsecurity_body_handler );
373
383
if (rc >= NGX_HTTP_SPECIAL_RESPONSE ) {
374
384
return rc ;
@@ -377,12 +387,11 @@ ngx_http_modsecurity_handler(ngx_http_request_t *r)
377
387
return NGX_DONE ;
378
388
}
379
389
/* other method */
380
- ngx_log_debug0 ( NGX_LOG_DEBUG_HTTP , r -> connection -> log , 0 , "modSecurity: process request body" );
381
- rc = modsecProcessRequestBody ( ctx -> req );
390
+ rc = modsecProcessRequestBody ( ctx -> req );
391
+ ngx_log_debug1 ( NGX_LOG_DEBUG_HTTP , r -> connection -> log , 0 , "ModSecurity: modsecProcessRequestBody %d" , rc );
382
392
}
383
393
384
394
if (rc != DECLINED ) {
385
- ngx_log_debug1 (NGX_LOG_DEBUG_HTTP , r -> connection -> log , 0 , "ModSecurity: status: %d, need action" , rc );
386
395
387
396
/* Nginx and Apache share same response code */
388
397
if (rc < NGX_HTTP_SPECIAL_RESPONSE || rc >= 600 ) {
@@ -412,7 +421,7 @@ ngx_http_modsecurity_body_handler(ngx_http_request_t *r)
412
421
return ;
413
422
}
414
423
415
- ngx_log_debug0 (NGX_LOG_DEBUG_HTTP , r -> connection -> log , 0 , "modSecurity: process request body" );
424
+ ngx_log_debug0 (NGX_LOG_DEBUG_HTTP , r -> connection -> log , 0 , "modSecurity: body handler " );
416
425
417
426
ctx = ngx_http_get_module_ctx (r , ngx_http_modsecurity );
418
427
@@ -426,9 +435,9 @@ ngx_http_modsecurity_body_handler(ngx_http_request_t *r)
426
435
modsecSetBodyBrigade (ctx -> req , ctx -> brigade );
427
436
428
437
rc = modsecProcessRequestBody (ctx -> req );
438
+ ngx_log_debug1 (NGX_LOG_DEBUG_HTTP , r -> connection -> log , 0 , "ModSecurity: modsecProcessRequestBody %d" , rc );
429
439
430
440
if (rc != DECLINED ) {
431
- ngx_log_debug1 (NGX_LOG_DEBUG_HTTP , r -> connection -> log , 0 , "ModSecurity: status: %d, need action" , rc );
432
441
433
442
/* Nginx and Apache share same response code */
434
443
if (rc < NGX_HTTP_SPECIAL_RESPONSE || rc >= 600 ) {
@@ -474,21 +483,17 @@ ngx_http_modsecurity_header_filter(ngx_http_request_t *r) {
474
483
ngx_http_modsecurity_loc_conf_t * cf ;
475
484
ngx_http_modsecurity_ctx_t * ctx ;
476
485
const char * lang ;
486
+ ngx_int_t rc ;
477
487
478
488
cf = ngx_http_get_module_loc_conf (r , ngx_http_modsecurity );
479
489
ctx = ngx_http_get_module_ctx (r , ngx_http_modsecurity );
480
-
490
+
481
491
if (r != r -> main || r -> internal || !cf -> enable || ctx -> complete ) {
482
492
return ngx_http_next_header_filter (r );
483
493
}
484
-
485
- if (r -> method == NGX_HTTP_HEAD || r -> header_only
486
- || !modsecIsResponseBodyAccessEnabled (ctx -> req ) ) {
487
- /* TODO: RESPONSE HEADERS PHASE
488
- */
489
- return ngx_http_next_header_filter (r );
490
- }
491
-
494
+
495
+ ngx_log_debug0 (NGX_LOG_DEBUG_HTTP , r -> connection -> log , 0 , "modSecurity: header filter" );
496
+
492
497
/* copy headers_out */
493
498
if (ngx_list_copy_to_apr_table (& r -> headers_out .headers ,
494
499
ctx -> req -> headers_out ,
@@ -507,6 +512,22 @@ ngx_http_modsecurity_header_filter(ngx_http_request_t *r) {
507
512
* (const char * * )apr_array_push (ctx -> req -> content_languages ) = lang ;
508
513
}
509
514
515
+ if (r -> method == NGX_HTTP_HEAD || r -> header_only ) {
516
+
517
+ ctx -> complete = 1 ;
518
+ rc = modsecProcessResponse (ctx -> req );
519
+ ngx_log_debug1 (NGX_LOG_DEBUG_HTTP , r -> connection -> log , 0 , "ModSecurity: modsecProcessResponse %d" , rc );
520
+ if (rc == DECLINED || rc == APR_SUCCESS ) {
521
+ return ngx_http_next_header_filter (r );
522
+ }
523
+
524
+ if (rc < NGX_HTTP_SPECIAL_RESPONSE || rc >= 600 ) {
525
+ rc = NGX_HTTP_INTERNAL_SERVER_ERROR ;
526
+ }
527
+
528
+ return rc ;
529
+ }
530
+
510
531
return NGX_OK ;
511
532
}
512
533
@@ -520,7 +541,7 @@ ngx_http_modsecurity_body_filter(ngx_http_request_t *r, ngx_chain_t *in)
520
541
521
542
cf = ngx_http_get_module_loc_conf (r , ngx_http_modsecurity );
522
543
ctx = ngx_http_get_module_ctx (r , ngx_http_modsecurity );
523
-
544
+
524
545
if (r != r -> main || r -> internal || !cf -> enable || ctx -> complete ) {
525
546
return ngx_http_next_body_filter (r , in );
526
547
}
@@ -532,10 +553,8 @@ ngx_http_modsecurity_body_filter(ngx_http_request_t *r, ngx_chain_t *in)
532
553
}
533
554
534
555
rc = move_chain_to_brigade (in , ctx -> brigade , r -> pool );
535
- if (rc == NGX_ERROR ) {
536
- return NGX_ERROR ;
537
- } else if (rc == NGX_AGAIN ) {
538
- return NGX_AGAIN ;
556
+ if (rc != NGX_OK ) {
557
+ return rc ;
539
558
}
540
559
541
560
/* last buf has been saved */
@@ -544,6 +563,7 @@ ngx_http_modsecurity_body_filter(ngx_http_request_t *r, ngx_chain_t *in)
544
563
modsecSetResponseBrigade (ctx -> req , ctx -> brigade );
545
564
546
565
rc = modsecProcessResponse (ctx -> req );
566
+ ngx_log_debug1 (NGX_LOG_DEBUG_HTTP , r -> connection -> log , 0 , "ModSecurity: modsecProcessResponse %d" , rc );
547
567
548
568
if (rc == DECLINED || rc == APR_SUCCESS ) {
549
569
@@ -565,22 +585,17 @@ ngx_http_modsecurity_body_filter(ngx_http_request_t *r, ngx_chain_t *in)
565
585
rc = ngx_http_next_header_filter (r );
566
586
567
587
if (rc == NGX_ERROR || rc > NGX_OK ) {
568
- return rc ;
588
+ return ngx_http_filter_finalize_request ( r , & ngx_http_modsecurity , rc ) ;
569
589
}
570
590
571
- rc = ngx_http_next_body_filter (r , in );
572
- if (rc == NGX_ERROR || rc >= NGX_HTTP_SPECIAL_RESPONSE ) {
573
- return rc ;
574
- }
575
-
576
- return NGX_OK ;
591
+ return ngx_http_next_body_filter (r , in );
577
592
}
578
593
579
594
if (rc < NGX_HTTP_SPECIAL_RESPONSE || rc >= 600 ) {
580
595
rc = NGX_HTTP_INTERNAL_SERVER_ERROR ;
581
596
}
582
597
583
- return rc ; /* ngx_http_filter_finalize_request(r, &ngx_http_modsecurity, rc); */
598
+ return ngx_http_filter_finalize_request (r , & ngx_http_modsecurity , rc );
584
599
}
585
600
586
601
@@ -709,7 +724,8 @@ ngx_http_modsecurity_config(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
709
724
710
725
msg = modsecProcessConfig (mscf -> config , (const char * )value [1 ].data , NULL );
711
726
if (msg != NULL ) {
712
- ngx_conf_log_error (NGX_LOG_EMERG , cf , 0 , "modSecurity: modsecProcessConfig() %s" , msg );
727
+ ngx_log_error (NGX_LOG_EMERG , cf -> log , 0 , "ModSecurityConfig in %s:%ui: %s" ,
728
+ cf -> conf_file -> file .name .data , cf -> conf_file -> line , msg );
713
729
return NGX_CONF_ERROR ;
714
730
}
715
731
0 commit comments