forked from owasp-modsecurity/ModSecurity-apache
-
Notifications
You must be signed in to change notification settings - Fork 0
/
msc_filters.c
147 lines (121 loc) · 3.98 KB
/
msc_filters.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#include "msc_filters.h"
#include "msc_utils.h"
apr_status_t input_filter(ap_filter_t *f, apr_bucket_brigade *pbbOut,
ap_input_mode_t mode, apr_read_type_e block, apr_off_t nbytes)
{
request_rec *r = f->r;
conn_rec *c = r->connection;
apr_bucket_brigade *pbbTmp;
int ret;
msc_t *msr = (msc_t *)f->ctx;
/* Do we have the context? */
if (msr == NULL)
{
ap_log_error(APLOG_MARK, APLOG_ERR | APLOG_NOERRNO, 0, f->r->server,
"ModSecurity: Internal Error: msr is null in input filter.");
ap_remove_output_filter(f);
return send_error_bucket(msr, f, HTTP_INTERNAL_SERVER_ERROR);
}
pbbTmp = apr_brigade_create(r->pool, c->bucket_alloc);
if (APR_BRIGADE_EMPTY(pbbTmp))
{
ret = ap_get_brigade(f->next, pbbTmp, mode, block, nbytes);
if (mode == AP_MODE_EATCRLF || ret != APR_SUCCESS)
return ret;
}
while (!APR_BRIGADE_EMPTY(pbbTmp))
{
apr_bucket *pbktIn = APR_BRIGADE_FIRST(pbbTmp);
apr_bucket *pbktOut;
const char *data;
apr_size_t len;
apr_size_t n;
int it;
if (APR_BUCKET_IS_EOS(pbktIn))
{
APR_BUCKET_REMOVE(pbktIn);
APR_BRIGADE_INSERT_TAIL(pbbOut, pbktIn);
break;
}
ret=apr_bucket_read(pbktIn, &data, &len, block);
if (ret != APR_SUCCESS)
{
return ret;
}
msc_append_request_body(msr->t, data, len);
it = process_intervention(msr->t, r);
if (it != N_INTERVENTION_STATUS)
{
ap_remove_output_filter(f);
return send_error_bucket(msr, f, it);
}
pbktOut = apr_bucket_heap_create(data, len, 0, c->bucket_alloc);
APR_BRIGADE_INSERT_TAIL(pbbOut, pbktOut);
apr_bucket_delete(pbktIn);
}
return APR_SUCCESS;
}
apr_status_t output_filter(ap_filter_t *f, apr_bucket_brigade *bb_in)
{
request_rec *r = f->r;
msc_t *msr = (msc_t *)f->ctx;
/* Do we have the context? */
if (msr == NULL)
{
ap_log_error(APLOG_MARK, APLOG_ERR | APLOG_NOERRNO, 0, f->r->server,
"ModSecurity: Internal Error: msr is null in output filter.");
ap_remove_output_filter(f);
return send_error_bucket(msr, f, HTTP_INTERNAL_SERVER_ERROR);
}
/* response headers */
{
const apr_array_header_t *arr = NULL;
const apr_table_entry_t *te = NULL;
int i, it;
arr = apr_table_elts(r->err_headers_out);
te = (apr_table_entry_t *)arr->elts;
for (i = 0; i < arr->nelts; i++)
{
const char *key = te[i].key;
const char *val = te[i].val;
msc_add_response_header(msr->t, key, val);
}
arr = apr_table_elts(r->headers_out);
te = (apr_table_entry_t *)arr->elts;
for (i = 0; i < arr->nelts; i++)
{
const char *key = te[i].key;
const char *val = te[i].val;
msc_add_response_header(msr->t, key, val);
}
msc_process_response_headers(msr->t, r->status, "HTTP 1.1");
it = process_intervention(msr->t, r);
if (it != N_INTERVENTION_STATUS)
{
ap_remove_output_filter(f);
return send_error_bucket(msr, f, it);
}
}
/* response body */
{
apr_bucket *pbktIn;
int it;
for (pbktIn = APR_BRIGADE_FIRST(bb_in);
pbktIn != APR_BRIGADE_SENTINEL(bb_in);
pbktIn = APR_BUCKET_NEXT(pbktIn))
{
const char *data;
apr_size_t len;
apr_bucket_read(pbktIn, &data, &len, APR_BLOCK_READ);
msc_append_response_body(msr->t, data, len);
}
msc_process_response_body(msr->t);
it = process_intervention(msr->t, r);
if (it != N_INTERVENTION_STATUS)
{
ap_remove_output_filter(f);
return send_error_bucket(msr, f, it);
}
}
return ap_pass_brigade(f->next, bb_in);
}