Skip to content

Commit a76a0c9

Browse files
committed
Merge branch 'master' into PR90
2 parents 4114734 + 2a13e33 commit a76a0c9

7 files changed

+41
-3
lines changed

CHANGES

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
v1.0.x - YYYY-MMM-DD (To be released)
22
-------------------------------------
33

4+
- Fix nginx sends response without headers
5+
[Issue #238 - @airween, @defanator]
46
- Fix nginx not clearing body cache (caused by incomplete fix for #187)
57
[Issue #216 - @krewi1, @martinhsv]
68
- Fix config setting not respected: client_body_in_file_only on

src/ngx_http_modsecurity_body_filter.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,10 @@ ngx_http_modsecurity_body_filter(ngx_http_request_t *r, ngx_chain_t *in)
5656
return ngx_http_next_body_filter(r, in);
5757
}
5858

59+
if (ctx->intervention_triggered) {
60+
return ngx_http_next_body_filter(r, in);
61+
}
62+
5963
#if defined(MODSECURITY_SANITY_CHECKS) && (MODSECURITY_SANITY_CHECKS)
6064
mcf = ngx_http_get_module_loc_conf(r, ngx_http_modsecurity_module);
6165
if (mcf != NULL && mcf->sanity_checks_enabled != NGX_CONF_UNSET)

src/ngx_http_modsecurity_common.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ typedef struct {
9898
unsigned body_requested:1;
9999
unsigned processed:1;
100100
unsigned logged:1;
101+
unsigned intervention_triggered:1;
101102
} ngx_http_modsecurity_ctx_t;
102103

103104

src/ngx_http_modsecurity_header_filter.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -430,6 +430,10 @@ ngx_http_modsecurity_header_filter(ngx_http_request_t *r)
430430
return ngx_http_next_header_filter(r);
431431
}
432432

433+
if (ctx->intervention_triggered) {
434+
return ngx_http_next_header_filter(r);
435+
}
436+
433437
/* XXX: can it happen ? already processed i mean */
434438
/* XXX: check behaviour on 'ModSecurity off' */
435439

@@ -525,9 +529,9 @@ ngx_http_modsecurity_header_filter(ngx_http_request_t *r)
525529
ret = ngx_http_modsecurity_process_intervention(ctx->modsec_transaction, r);
526530
if (r->error_page) {
527531
return ngx_http_next_header_filter(r);
528-
}
532+
}
529533
if (ret > 0) {
530-
return ret;
534+
return ngx_http_filter_finalize_request(r, &ngx_http_modsecurity_module, ret);
531535
}
532536

533537
/*

src/ngx_http_modsecurity_pre_access.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,10 @@ ngx_http_modsecurity_pre_access_handler(ngx_http_request_t *r)
7878
return NGX_HTTP_INTERNAL_SERVER_ERROR;
7979
}
8080

81+
if (ctx->intervention_triggered) {
82+
return NGX_DECLINED;
83+
}
84+
8185
if (ctx->waiting_more_body == 1)
8286
{
8387
dd("waiting for more data before proceed. / count: %d",

src/ngx_http_modsecurity_rewrite.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ ngx_http_modsecurity_rewrite_handler(ngx_http_request_t *r)
117117
dd("Processing intervention with the connection information filled in");
118118
ret = ngx_http_modsecurity_process_intervention(ctx->modsec_transaction, r);
119119
if (ret > 0) {
120+
ctx->intervention_triggered = 1;
120121
return ret;
121122
}
122123

@@ -157,6 +158,7 @@ ngx_http_modsecurity_rewrite_handler(ngx_http_request_t *r)
157158
dd("Processing intervention with the transaction information filled in (uri, method and version)");
158159
ret = ngx_http_modsecurity_process_intervention(ctx->modsec_transaction, r);
159160
if (ret > 0) {
161+
ctx->intervention_triggered = 1;
160162
return ret;
161163
}
162164

@@ -208,6 +210,7 @@ ngx_http_modsecurity_rewrite_handler(ngx_http_request_t *r)
208210
return NGX_DECLINED;
209211
}
210212
if (ret > 0) {
213+
ctx->intervention_triggered = 1;
211214
return ret;
212215
}
213216
}

tests/modsecurity.t

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,23 @@ http {
105105
SecRule ARGS "@streq block403" "id:4,phase:4,status:403,block"
106106
';
107107
}
108+
location /early-block {
109+
modsecurity on;
110+
modsecurity_rules '
111+
SecRuleEngine On
112+
SecResponseBodyAccess On
113+
SecDefaultAction "phase:1,log,auditlog,pass"
114+
SecDefaultAction "phase:2,log,auditlog,pass"
115+
SecAction "id:900101,phase:1,nolog,pass,t:none,setvar:tx.trigger_phase1=1"
116+
SecAction "id:900103,phase:1,nolog,pass,t:none,setvar:tx.trigger_phase3=1"
117+
SecAction "id:900105,phase:1,nolog,pass,t:none,setvar:tx.trigger_phase5=1"
118+
SecRule TX:TRIGGER_PHASE1 "@eq 1" "id:901111,phase:1,t:none,deny,log"
119+
SecRule REQUEST_BODY "@rx attack" "id:901121,phase:2,t:none,deny,log"
120+
SecRule TX:TRIGGER_PHASE3 "@eq 1" "id:901131,phase:3,t:none,deny,log"
121+
SecRule RESPONSE_BODY "@rx ok" "id:901141,phase:4,t:none,deny,log"
122+
SecRule TX:TRIGGER_PHASE5 "@eq 1" "id:901151,phase:5,t:none,pass,log,msg:\'This is the phase 5.\'"
123+
';
124+
}
108125
}
109126
}
110127
EOF
@@ -113,9 +130,10 @@ $t->write_file("/phase1", "should be moved/blocked before this.");
113130
$t->write_file("/phase2", "should be moved/blocked before this.");
114131
$t->write_file("/phase3", "should be moved/blocked before this.");
115132
$t->write_file("/phase4", "should not be moved/blocked, headers delivered before phase 4.");
133+
$t->write_file("/early-block", "should be moved/blocked before this.");
116134
$t->run();
117135
$t->todo_alerts();
118-
$t->plan(20);
136+
$t->plan(21);
119137

120138
###############################################################################
121139

@@ -150,3 +168,5 @@ like(http_get('/phase2?what=nothing'), qr/should be moved\/blocked before this./
150168
like(http_get('/phase3?what=nothing'), qr/should be moved\/blocked before this./, 'nothing phase 3');
151169
like(http_get('/phase4?what=nothing'), qr/should not be moved\/blocked, headers delivered before phase 4./, 'nothing phase 4');
152170

171+
# early block (https://github.com/SpiderLabs/ModSecurity-nginx/issues/238)
172+
like(http_get('/early-block'), qr/^HTTP.*403/, 'early block 403 (https://github.com/SpiderLabs/ModSecurity-nginx/issues/238)');

0 commit comments

Comments
 (0)