Skip to content

Commit 6d88375

Browse files
committed
modsecurity: body filter module (closes owasp-modsecurity#84)
1 parent 2ee4c91 commit 6d88375

5 files changed

+103
-217
lines changed

config

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ if test -n "$ngx_module_link"; then
110110
ngx_module_type=HTTP_FILTER
111111
ngx_module_name="$ngx_addon_name"
112112
ngx_module_srcs="$ngx_addon_dir/src/ngx_http_modsecurity_module.c \
113-
$ngx_addon_dir/src/ngx_http_modsecurity_pre_access.c \
113+
$ngx_addon_dir/src/ngx_http_modsecurity_request_body_filter.c \
114114
$ngx_addon_dir/src/ngx_http_modsecurity_header_filter.c \
115115
$ngx_addon_dir/src/ngx_http_modsecurity_log.c \
116116
$ngx_addon_dir/src/ngx_http_modsecurity_rewrite.c \
@@ -140,7 +140,7 @@ else
140140
NGX_ADDON_SRCS="\
141141
$NGX_ADDON_SRCS \
142142
$ngx_addon_dir/src/ngx_http_modsecurity_module.c \
143-
$ngx_addon_dir/src/ngx_http_modsecurity_pre_access.c \
143+
$ngx_addon_dir/src/ngx_http_modsecurity_request_body_filter.c \
144144
$ngx_addon_dir/src/ngx_http_modsecurity_header_filter.c \
145145
$ngx_addon_dir/src/ngx_http_modsecurity_log.c \
146146
$ngx_addon_dir/src/ngx_http_modsecurity_rewrite.c \

src/ngx_http_modsecurity_common.h

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -75,11 +75,8 @@
7575
typedef struct {
7676
Transaction *modsec_transaction;
7777

78-
unsigned waiting_more_body:1;
79-
unsigned body_requested:1;
8078
unsigned logged:1;
8179
unsigned intervention_triggered:1;
82-
unsigned pre_access_processed:1;
8380
} ngx_http_modsecurity_ctx_t;
8481

8582

@@ -119,8 +116,7 @@ void ngx_http_modsecurity_header_filter_init(void);
119116
void ngx_http_modsecurity_log(void *log, const void* data);
120117
ngx_int_t ngx_http_modsecurity_log_handler(ngx_http_request_t *r);
121118

122-
/* ngx_http_modsecurity_pre_access.c */
123-
ngx_int_t ngx_http_modsecurity_pre_access_handler(ngx_http_request_t *r);
119+
void ngx_http_modsecurity_request_body_filter_init(void);
124120

125121
/* ngx_http_modsecurity_rewrite.c */
126122
ngx_int_t ngx_http_modsecurity_rewrite_handler(ngx_http_request_t *r);

src/ngx_http_modsecurity_module.c

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -541,19 +541,13 @@ ngx_http_modsecurity_init(ngx_conf_t *cf)
541541

542542
*h = ngx_http_modsecurity_rewrite_handler;
543543

544-
h = ngx_array_push(&cmcf->phases[NGX_HTTP_PREACCESS_PHASE].handlers);
545-
if (h == NULL) {
546-
return NGX_ERROR;
547-
}
548-
549-
*h = ngx_http_modsecurity_pre_access_handler;
550-
551544
h = ngx_array_push(&cmcf->phases[NGX_HTTP_LOG_PHASE].handlers);
552545
if (h == NULL) {
553546
return NGX_ERROR;
554547
}
555548
*h = ngx_http_modsecurity_log_handler;
556549

550+
ngx_http_modsecurity_request_body_filter_init();
557551
ngx_http_modsecurity_header_filter_init();
558552

559553
return NGX_OK;

src/ngx_http_modsecurity_pre_access.c

Lines changed: 0 additions & 203 deletions
This file was deleted.
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
/*
2+
* ModSecurity connector for nginx, http://www.modsecurity.org/
3+
* Copyright (c) 2015 Trustwave Holdings, Inc. (http://www.trustwave.com/)
4+
*
5+
* You may not use this file except in compliance with
6+
* the License. You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* If any of the files related to licensing are missing or if you have any
11+
* other questions related to licensing please contact Trustwave Holdings, Inc.
12+
* directly using the email address security@modsecurity.org.
13+
*
14+
*/
15+
16+
#ifndef MODSECURITY_DDEBUG
17+
#define MODSECURITY_DDEBUG 0
18+
#endif
19+
#include "ddebug.h"
20+
21+
#include "ngx_http_modsecurity_common.h"
22+
23+
24+
static ngx_int_t ngx_http_modsecurity_request_body_filter(
25+
ngx_http_request_t *r, ngx_chain_t *in);
26+
27+
28+
static ngx_http_request_body_filter_pt ngx_http_next_request_body_filter;
29+
30+
31+
void
32+
ngx_http_modsecurity_request_body_filter_init(void)
33+
{
34+
ngx_http_next_request_body_filter = ngx_http_top_request_body_filter;
35+
ngx_http_top_request_body_filter = ngx_http_modsecurity_request_body_filter;
36+
}
37+
38+
39+
static ngx_int_t
40+
ngx_http_modsecurity_request_body_filter(ngx_http_request_t *r, ngx_chain_t *in)
41+
{
42+
ngx_int_t rc, rcms;
43+
ngx_pool_t *old_pool;
44+
ngx_uint_t last;
45+
ngx_http_modsecurity_ctx_t *ctx;
46+
47+
if (r != r->main || r->internal) {
48+
return NGX_DECLINED;
49+
}
50+
51+
ctx = ngx_http_get_module_ctx(r, ngx_http_modsecurity_module);
52+
53+
if (ctx == NULL) {
54+
// module is off
55+
return ngx_http_next_request_body_filter(r, in);
56+
}
57+
58+
rc = ngx_http_next_request_body_filter(r, in);
59+
60+
last = 0;
61+
62+
while (in) {
63+
if (in->buf->last_buf) {
64+
last = 1;
65+
}
66+
67+
msc_append_request_body(ctx->modsec_transaction,
68+
in->buf->pos,
69+
in->buf->last - in->buf->pos);
70+
71+
/**
72+
* ModSecurity may perform stream inspection on this buffer,
73+
* it may ask for a intervention in consequence of that.
74+
*
75+
*/
76+
rcms = ngx_http_modsecurity_process_intervention(
77+
ctx->modsec_transaction, r, 0);
78+
if (rcms > 0) {
79+
return rcms;
80+
}
81+
82+
in = in->next;
83+
}
84+
85+
if (last) {
86+
old_pool = ngx_http_modsecurity_pcre_malloc_init(r->pool);
87+
msc_process_request_body(ctx->modsec_transaction);
88+
ngx_http_modsecurity_pcre_malloc_done(old_pool);
89+
90+
rcms = ngx_http_modsecurity_process_intervention(
91+
ctx->modsec_transaction, r, 0);
92+
if (rcms > 0) {
93+
return rcms;
94+
}
95+
}
96+
97+
return rc;
98+
}
99+

0 commit comments

Comments
 (0)