Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 02249da

Browse files
mlevogianniskolotourosthanosgn
committedJun 3, 2024··
Fix phase 2 and 3 audit logging in case of internal redirect
Phase 2 and 3 log entries were not logged in the audit log in case of an internal redirect. Only phase 1 and 4 ones were logged, the former because early logging was explicitly enabled and the latter because the internal redirect does not work in this phase. This commit unconditionally enables early logging in all ModSecurity phases. Since the Nginx log phase may not be executed in case of an intervention, the process intervention function is the only place which is guaranteed to call the log handler in such a case. Co-authored-by: Dimitris Kolotouros <[email protected]> Co-authored-by: Thanos Giannopoulos <[email protected]>
1 parent d90eed5 commit 02249da

7 files changed

+13
-15
lines changed
 

‎src/ngx_http_modsecurity_body_filter.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ ngx_http_modsecurity_body_filter(ngx_http_request_t *r, ngx_chain_t *in)
147147
int ret;
148148

149149
msc_append_response_body(ctx->modsec_transaction, data, chain->buf->last - data);
150-
ret = ngx_http_modsecurity_process_intervention(ctx->modsec_transaction, r, 0);
150+
ret = ngx_http_modsecurity_process_intervention(ctx->modsec_transaction, r);
151151
if (ret > 0) {
152152
return ngx_http_filter_finalize_request(r,
153153
&ngx_http_modsecurity_module, ret);
@@ -165,7 +165,7 @@ ngx_http_modsecurity_body_filter(ngx_http_request_t *r, ngx_chain_t *in)
165165

166166
/* XXX: I don't get how body from modsec being transferred to nginx's buffer. If so - after adjusting of nginx's
167167
XXX: body we can proceed to adjust body size (content-length). see xslt_body_filter() for example */
168-
ret = ngx_http_modsecurity_process_intervention(ctx->modsec_transaction, r, 0);
168+
ret = ngx_http_modsecurity_process_intervention(ctx->modsec_transaction, r);
169169
if (ret > 0) {
170170
return ret;
171171
}

‎src/ngx_http_modsecurity_common.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ typedef struct {
137137
extern ngx_module_t ngx_http_modsecurity_module;
138138

139139
/* ngx_http_modsecurity_module.c */
140-
int ngx_http_modsecurity_process_intervention (Transaction *transaction, ngx_http_request_t *r, ngx_int_t early_log);
140+
int ngx_http_modsecurity_process_intervention (Transaction *transaction, ngx_http_request_t *r);
141141
ngx_http_modsecurity_ctx_t *ngx_http_modsecurity_create_ctx(ngx_http_request_t *r);
142142
char *ngx_str_to_char(ngx_str_t a, ngx_pool_t *p);
143143
#if (NGX_PCRE2)

‎src/ngx_http_modsecurity_header_filter.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -528,7 +528,7 @@ ngx_http_modsecurity_header_filter(ngx_http_request_t *r)
528528
old_pool = ngx_http_modsecurity_pcre_malloc_init(r->pool);
529529
msc_process_response_headers(ctx->modsec_transaction, status, http_response_ver);
530530
ngx_http_modsecurity_pcre_malloc_done(old_pool);
531-
ret = ngx_http_modsecurity_process_intervention(ctx->modsec_transaction, r, 0);
531+
ret = ngx_http_modsecurity_process_intervention(ctx->modsec_transaction, r);
532532
if (r->error_page) {
533533
return ngx_http_next_header_filter(r);
534534
}

‎src/ngx_http_modsecurity_log.c

+1
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ ngx_http_modsecurity_log_handler(ngx_http_request_t *r)
7878
old_pool = ngx_http_modsecurity_pcre_malloc_init(r->pool);
7979
msc_process_logging(ctx->modsec_transaction);
8080
ngx_http_modsecurity_pcre_malloc_done(old_pool);
81+
ctx->logged = 1;
8182

8283
return NGX_OK;
8384
}

‎src/ngx_http_modsecurity_module.c

+3-6
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ ngx_inline char *ngx_str_to_char(ngx_str_t a, ngx_pool_t *p)
137137

138138

139139
int
140-
ngx_http_modsecurity_process_intervention (Transaction *transaction, ngx_http_request_t *r, ngx_int_t early_log)
140+
ngx_http_modsecurity_process_intervention (Transaction *transaction, ngx_http_request_t *r)
141141
{
142142
char *log = NULL;
143143
ModSecurityIntervention intervention;
@@ -222,11 +222,8 @@ ngx_http_modsecurity_process_intervention (Transaction *transaction, ngx_http_re
222222
*/
223223
msc_update_status_code(ctx->modsec_transaction, intervention.status);
224224

225-
if (early_log) {
226-
dd("intervention -- calling log handler manually with code: %d", intervention.status);
227-
ngx_http_modsecurity_log_handler(r);
228-
ctx->logged = 1;
229-
}
225+
dd("intervention -- calling log handler manually with code: %d", intervention.status);
226+
ngx_http_modsecurity_log_handler(r);
230227

231228
if (r->header_sent)
232229
{

‎src/ngx_http_modsecurity_pre_access.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ ngx_http_modsecurity_pre_access_handler(ngx_http_request_t *r)
195195
* it may ask for a intervention in consequence of that.
196196
*
197197
*/
198-
ret = ngx_http_modsecurity_process_intervention(ctx->modsec_transaction, r, 0);
198+
ret = ngx_http_modsecurity_process_intervention(ctx->modsec_transaction, r);
199199
if (ret > 0) {
200200
return ret;
201201
}
@@ -214,7 +214,7 @@ ngx_http_modsecurity_pre_access_handler(ngx_http_request_t *r)
214214
msc_process_request_body(ctx->modsec_transaction);
215215
ngx_http_modsecurity_pcre_malloc_done(old_pool);
216216

217-
ret = ngx_http_modsecurity_process_intervention(ctx->modsec_transaction, r, 0);
217+
ret = ngx_http_modsecurity_process_intervention(ctx->modsec_transaction, r);
218218
if (r->error_page) {
219219
return NGX_DECLINED;
220220
}

‎src/ngx_http_modsecurity_rewrite.c

+3-3
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ ngx_http_modsecurity_rewrite_handler(ngx_http_request_t *r)
117117
*
118118
*/
119119
dd("Processing intervention with the connection information filled in");
120-
ret = ngx_http_modsecurity_process_intervention(ctx->modsec_transaction, r, 1);
120+
ret = ngx_http_modsecurity_process_intervention(ctx->modsec_transaction, r);
121121
if (ret > 0) {
122122
ctx->intervention_triggered = 1;
123123
return ret;
@@ -166,7 +166,7 @@ ngx_http_modsecurity_rewrite_handler(ngx_http_request_t *r)
166166
ngx_http_modsecurity_pcre_malloc_done(old_pool);
167167

168168
dd("Processing intervention with the transaction information filled in (uri, method and version)");
169-
ret = ngx_http_modsecurity_process_intervention(ctx->modsec_transaction, r, 1);
169+
ret = ngx_http_modsecurity_process_intervention(ctx->modsec_transaction, r);
170170
if (ret > 0) {
171171
ctx->intervention_triggered = 1;
172172
return ret;
@@ -215,7 +215,7 @@ ngx_http_modsecurity_rewrite_handler(ngx_http_request_t *r)
215215
msc_process_request_headers(ctx->modsec_transaction);
216216
ngx_http_modsecurity_pcre_malloc_done(old_pool);
217217
dd("Processing intervention with the request headers information filled in");
218-
ret = ngx_http_modsecurity_process_intervention(ctx->modsec_transaction, r, 1);
218+
ret = ngx_http_modsecurity_process_intervention(ctx->modsec_transaction, r);
219219
if (r->error_page) {
220220
return NGX_DECLINED;
221221
}

0 commit comments

Comments
 (0)
Please sign in to comment.