Skip to content

Commit b0c71bd

Browse files
committed
modsecurity: remove sanity checks
1 parent 9a8ed61 commit b0c71bd

File tree

5 files changed

+2
-222
lines changed

5 files changed

+2
-222
lines changed

src/ddebug.h

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,6 @@
1717
* #define MODSECURITY_DDEBUG 1
1818
*/
1919

20-
/*
21-
* Setting MODSECURITY_SANITY_CHECKS will help you in the debug process. By
22-
* defining MODSECURITY_SANITY_CHECKS a set of functions will be executed in
23-
* order to make sure the well behavior of ModSecurity, letting you know (via
24-
* debug_logs) if something unexpected happens.
25-
*
26-
* If performance is not a concern, it is safe to keep it set.
27-
*
28-
*/
29-
#ifndef MODSECURITY_SANITY_CHECKS
30-
#define MODSECURITY_SANITY_CHECKS 0
31-
#endif
32-
3320
#if defined(MODSECURITY_DDEBUG) && (MODSECURITY_DDEBUG)
3421

3522
# if (NGX_HAVE_VARIADIC_MACROS)

src/ngx_http_modsecurity_body_filter.c

Lines changed: 2 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,8 @@ ngx_int_t
3434
ngx_http_modsecurity_body_filter(ngx_http_request_t *r, ngx_chain_t *in)
3535
{
3636
ngx_chain_t *chain = in;
37-
ngx_http_modsecurity_ctx_t *ctx = NULL;
38-
#if defined(MODSECURITY_SANITY_CHECKS) && (MODSECURITY_SANITY_CHECKS)
39-
ngx_http_modsecurity_conf_t *mcf;
40-
ngx_list_part_t *part = &r->headers_out.headers.part;
41-
ngx_table_elt_t *data = part->elts;
42-
ngx_uint_t i = 0;
43-
#endif
37+
38+
ngx_http_modsecurity_ctx_t *ctx;
4439

4540
if (in == NULL) {
4641
return ngx_http_next_body_filter(r, in);
@@ -56,84 +51,6 @@ ngx_http_modsecurity_body_filter(ngx_http_request_t *r, ngx_chain_t *in)
5651
return ngx_http_next_body_filter(r, in);
5752
}
5853

59-
#if defined(MODSECURITY_SANITY_CHECKS) && (MODSECURITY_SANITY_CHECKS)
60-
mcf = ngx_http_get_module_loc_conf(r, ngx_http_modsecurity_module);
61-
if (mcf != NULL && mcf->sanity_checks_enabled != NGX_CONF_UNSET)
62-
{
63-
#if 0
64-
dd("dumping stored ctx headers");
65-
for (i = 0; i < ctx->sanity_headers_out->nelts; i++)
66-
{
67-
ngx_http_modsecurity_header_t *vals = ctx->sanity_headers_out->elts;
68-
ngx_str_t *s2 = &vals[i].name, *s3 = &vals[i].value;
69-
dd(" dump[%d]: name = '%.*s', value = '%.*s'", (int)i,
70-
(int)s2->len, (char*)s2->data,
71-
(int)s3->len, (char*)s3->data);
72-
}
73-
#endif
74-
/*
75-
* Identify if there is a header that was not inspected by ModSecurity.
76-
*/
77-
int worth_to_fail = 0;
78-
79-
for (i = 0; ; i++)
80-
{
81-
int found = 0;
82-
ngx_uint_t j = 0;
83-
ngx_table_elt_t *s1;
84-
ngx_http_modsecurity_header_t *vals;
85-
86-
if (i >= part->nelts)
87-
{
88-
if (part->next == NULL) {
89-
break;
90-
}
91-
92-
part = part->next;
93-
data = part->elts;
94-
i = 0;
95-
}
96-
97-
vals = ctx->sanity_headers_out->elts;
98-
s1 = &data[i];
99-
100-
/*
101-
* Headers that were inspected by ModSecurity.
102-
*/
103-
while (j < ctx->sanity_headers_out->nelts)
104-
{
105-
ngx_str_t *s2 = &vals[j].name;
106-
ngx_str_t *s3 = &vals[j].value;
107-
108-
if (s1->key.len == s2->len && ngx_strncmp(s1->key.data, s2->data, s1->key.len) == 0)
109-
{
110-
if (s1->value.len == s3->len && ngx_strncmp(s1->value.data, s3->data, s1->value.len) == 0)
111-
{
112-
found = 1;
113-
break;
114-
}
115-
}
116-
j++;
117-
}
118-
if (!found) {
119-
dd("header: `%.*s' with value: `%.*s' was not inspected by ModSecurity",
120-
(int) s1->key.len,
121-
(const char *) s1->key.data,
122-
(int) s1->value.len,
123-
(const char *) s1->value.data);
124-
worth_to_fail++;
125-
}
126-
}
127-
128-
if (worth_to_fail)
129-
{
130-
dd("%d header(s) were not inspected by ModSecurity, so exiting", worth_to_fail);
131-
return ngx_http_filter_finalize_request(r,
132-
&ngx_http_modsecurity_module, NGX_HTTP_INTERNAL_SERVER_ERROR);
133-
}
134-
}
135-
#endif
136-
13754
int is_request_processed = 0;
13855
for (; chain != NULL; chain = chain->next)
13956
{

src/ngx_http_modsecurity_common.h

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -71,27 +71,10 @@
7171
#define MODSECURITY_NGINX_WHOAMI "ModSecurity-nginx v" \
7272
MODSECURITY_NGINX_VERSION
7373

74-
typedef struct {
75-
ngx_str_t name;
76-
ngx_str_t value;
77-
} ngx_http_modsecurity_header_t;
78-
7974

8075
typedef struct {
8176
Transaction *modsec_transaction;
8277

83-
#if defined(MODSECURITY_SANITY_CHECKS) && (MODSECURITY_SANITY_CHECKS)
84-
/*
85-
* Should be filled with the headers that were sent to ModSecurity.
86-
*
87-
* The idea is to compare this set of headers with the headers that were
88-
* sent to the client. This check was placed because we don't have control
89-
* over other modules, thus, we may partially inspect the headers.
90-
*
91-
*/
92-
ngx_array_t *sanity_headers_out;
93-
#endif
94-
9578
unsigned waiting_more_body:1;
9679
unsigned body_requested:1;
9780
unsigned processed:1;
@@ -109,12 +92,7 @@ typedef struct {
10992
typedef struct {
11093
/* RulesSet or Rules */
11194
void *rules_set;
112-
11395
ngx_flag_t enable;
114-
#if defined(MODSECURITY_SANITY_CHECKS) && (MODSECURITY_SANITY_CHECKS)
115-
ngx_flag_t sanity_checks_enabled;
116-
#endif
117-
11896
ngx_http_complex_value_t *transaction_id;
11997
} ngx_http_modsecurity_conf_t;
12098

@@ -150,9 +128,6 @@ ngx_int_t ngx_http_modsecurity_body_filter(ngx_http_request_t *r, ngx_chain_t *i
150128

151129
/* ngx_http_modsecurity_header_filter.c */
152130
void ngx_http_modsecurity_header_filter_init(void);
153-
#if defined(MODSECURITY_SANITY_CHECKS) && (MODSECURITY_SANITY_CHECKS)
154-
int ngx_http_modsecurity_store_ctx_header(ngx_http_request_t *r, ngx_str_t *name, ngx_str_t *value);
155-
#endif
156131

157132
/* ngx_http_modsecurity_log.c */
158133
void ngx_http_modsecurity_log(void *log, const void* data);

src/ngx_http_modsecurity_header_filter.c

Lines changed: 0 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -103,46 +103,6 @@ ngx_http_modsecurity_header_out_t ngx_http_modsecurity_headers_out[] = {
103103
};
104104

105105

106-
#if defined(MODSECURITY_SANITY_CHECKS) && (MODSECURITY_SANITY_CHECKS)
107-
int
108-
ngx_http_modsecurity_store_ctx_header(ngx_http_request_t *r, ngx_str_t *name, ngx_str_t *value)
109-
{
110-
ngx_http_modsecurity_ctx_t *ctx;
111-
ngx_http_modsecurity_conf_t *mcf;
112-
ngx_http_modsecurity_header_t *hdr;
113-
114-
ctx = ngx_http_get_module_ctx(r, ngx_http_modsecurity_module);
115-
if (ctx == NULL || ctx->sanity_headers_out == NULL) {
116-
return NGX_ERROR;
117-
}
118-
119-
mcf = ngx_http_get_module_loc_conf(r, ngx_http_modsecurity_module);
120-
if (mcf == NULL || mcf->sanity_checks_enabled == NGX_CONF_UNSET)
121-
{
122-
return NGX_OK;
123-
}
124-
125-
hdr = ngx_array_push(ctx->sanity_headers_out);
126-
if (hdr == NULL) {
127-
return NGX_ERROR;
128-
}
129-
130-
hdr->name.data = ngx_pnalloc(r->pool, name->len);
131-
hdr->value.data = ngx_pnalloc(r->pool, value->len);
132-
if (hdr->name.data == NULL || hdr->value.data == NULL) {
133-
return NGX_ERROR;
134-
}
135-
136-
ngx_memcpy(hdr->name.data, name->data, name->len);
137-
hdr->name.len = name->len;
138-
ngx_memcpy(hdr->value.data, value->data, value->len);
139-
hdr->value.len = value->len;
140-
141-
return NGX_OK;
142-
}
143-
#endif
144-
145-
146106
static ngx_int_t
147107
ngx_http_modsecurity_resolv_header_server(ngx_http_request_t *r, ngx_str_t name, off_t offset)
148108
{
@@ -170,10 +130,6 @@ ngx_http_modsecurity_resolv_header_server(ngx_http_request_t *r, ngx_str_t name,
170130
value.len = h->value.len;
171131
}
172132

173-
#if defined(MODSECURITY_SANITY_CHECKS) && (MODSECURITY_SANITY_CHECKS)
174-
ngx_http_modsecurity_store_ctx_header(r, &name, &value);
175-
#endif
176-
177133
return msc_add_n_response_header(ctx->modsec_transaction,
178134
(const unsigned char *) name.data,
179135
name.len,
@@ -199,10 +155,6 @@ ngx_http_modsecurity_resolv_header_date(ngx_http_request_t *r, ngx_str_t name, o
199155
date.len = h->value.len;
200156
}
201157

202-
#if defined(MODSECURITY_SANITY_CHECKS) && (MODSECURITY_SANITY_CHECKS)
203-
ngx_http_modsecurity_store_ctx_header(r, &name, &date);
204-
#endif
205-
206158
return msc_add_n_response_header(ctx->modsec_transaction,
207159
(const unsigned char *) name.data,
208160
name.len,
@@ -226,9 +178,6 @@ ngx_http_modsecurity_resolv_header_content_length(ngx_http_request_t *r, ngx_str
226178
value.data = (unsigned char *)buf;
227179
value.len = strlen(buf);
228180

229-
#if defined(MODSECURITY_SANITY_CHECKS) && (MODSECURITY_SANITY_CHECKS)
230-
ngx_http_modsecurity_store_ctx_header(r, &name, &value);
231-
#endif
232181
return msc_add_n_response_header(ctx->modsec_transaction,
233182
(const unsigned char *) name.data,
234183
name.len,
@@ -249,11 +198,6 @@ ngx_http_modsecurity_resolv_header_content_type(ngx_http_request_t *r, ngx_str_t
249198

250199
if (r->headers_out.content_type.len > 0)
251200
{
252-
253-
#if defined(MODSECURITY_SANITY_CHECKS) && (MODSECURITY_SANITY_CHECKS)
254-
ngx_http_modsecurity_store_ctx_header(r, &name, &r->headers_out.content_type);
255-
#endif
256-
257201
return msc_add_n_response_header(ctx->modsec_transaction,
258202
(const unsigned char *) name.data,
259203
name.len,
@@ -283,10 +227,6 @@ ngx_http_modsecurity_resolv_header_last_modified(ngx_http_request_t *r, ngx_str_
283227
value.data = buf;
284228
value.len = (int)(p-buf);
285229

286-
#if defined(MODSECURITY_SANITY_CHECKS) && (MODSECURITY_SANITY_CHECKS)
287-
ngx_http_modsecurity_store_ctx_header(r, &name, &value);
288-
#endif
289-
290230
return msc_add_n_response_header(ctx->modsec_transaction,
291231
(const unsigned char *) name.data,
292232
name.len,
@@ -319,10 +259,6 @@ ngx_http_modsecurity_resolv_header_connection(ngx_http_request_t *r, ngx_str_t n
319259
value.data = buf;
320260
value.len = strlen((char *)buf);
321261

322-
#if defined(MODSECURITY_SANITY_CHECKS) && (MODSECURITY_SANITY_CHECKS)
323-
ngx_http_modsecurity_store_ctx_header(r, &name2, &value);
324-
#endif
325-
326262
msc_add_n_response_header(ctx->modsec_transaction,
327263
(const unsigned char *) name2.data,
328264
name2.len,
@@ -336,10 +272,6 @@ ngx_http_modsecurity_resolv_header_connection(ngx_http_request_t *r, ngx_str_t n
336272
value.data = (u_char *) connection;
337273
value.len = strlen(connection);
338274

339-
#if defined(MODSECURITY_SANITY_CHECKS) && (MODSECURITY_SANITY_CHECKS)
340-
ngx_http_modsecurity_store_ctx_header(r, &name, &value);
341-
#endif
342-
343275
return msc_add_n_response_header(ctx->modsec_transaction,
344276
(const unsigned char *) name.data,
345277
name.len,
@@ -357,10 +289,6 @@ ngx_http_modsecurity_resolv_header_transfer_encoding(ngx_http_request_t *r, ngx_
357289

358290
ctx = ngx_http_get_module_ctx(r, ngx_http_modsecurity_module);
359291

360-
#if defined(MODSECURITY_SANITY_CHECKS) && (MODSECURITY_SANITY_CHECKS)
361-
ngx_http_modsecurity_store_ctx_header(r, &name, &value);
362-
#endif
363-
364292
return msc_add_n_response_header(ctx->modsec_transaction,
365293
(const unsigned char *) name.data,
366294
name.len,
@@ -384,10 +312,6 @@ ngx_http_modsecurity_resolv_header_vary(ngx_http_request_t *r, ngx_str_t name, o
384312

385313
ctx = ngx_http_get_module_ctx(r, ngx_http_modsecurity_module);
386314

387-
#if defined(MODSECURITY_SANITY_CHECKS) && (MODSECURITY_SANITY_CHECKS)
388-
ngx_http_modsecurity_store_ctx_header(r, &name, &value);
389-
#endif
390-
391315
return msc_add_n_response_header(ctx->modsec_transaction,
392316
(const unsigned char *) name.data,
393317
name.len,
@@ -474,10 +398,6 @@ ngx_http_modsecurity_header_filter(ngx_http_request_t *r)
474398
i = 0;
475399
}
476400

477-
#if defined(MODSECURITY_SANITY_CHECKS) && (MODSECURITY_SANITY_CHECKS)
478-
ngx_http_modsecurity_store_ctx_header(r, &data[i].key, &data[i].value);
479-
#endif
480-
481401
/*
482402
* Doing this ugly cast here, explanation on the request_header
483403
*/

src/ngx_http_modsecurity_module.c

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -218,10 +218,6 @@ ngx_http_modsecurity_process_intervention (Transaction *transaction, ngx_http_re
218218
r->headers_out.location = location;
219219
r->headers_out.location->hash = 1;
220220

221-
#if defined(MODSECURITY_SANITY_CHECKS) && (MODSECURITY_SANITY_CHECKS)
222-
ngx_http_modsecurity_store_ctx_header(r, &location->key, &location->value);
223-
#endif
224-
225221
return intervention.status;
226222
}
227223

@@ -286,13 +282,6 @@ ngx_http_modsecurity_create_ctx(ngx_http_request_t *r, ModSecurity *modsec,
286282

287283
dd("transaction created");
288284

289-
#if defined(MODSECURITY_SANITY_CHECKS) && (MODSECURITY_SANITY_CHECKS)
290-
ctx->sanity_headers_out = ngx_array_create(r->pool, 12, sizeof(ngx_http_modsecurity_header_t));
291-
if (ctx->sanity_headers_out == NULL) {
292-
return NULL;
293-
}
294-
#endif
295-
296285
cln = ngx_pool_cleanup_add(r->pool, sizeof(ngx_http_modsecurity_ctx_t));
297286
if (cln == NULL) {
298287
return NULL;
@@ -635,16 +624,12 @@ ngx_http_modsecurity_create_conf(ngx_conf_t *cf)
635624
* set by ngx_pcalloc():
636625
*
637626
* conf->enable = 0;
638-
* conf->sanity_checks_enabled = 0;
639627
* conf->rules_set = NULL;
640628
* conf->transaction_id = NULL;
641629
*/
642630

643631
conf->enable = NGX_CONF_UNSET;
644632
conf->transaction_id = NGX_CONF_UNSET_PTR;
645-
#if defined(MODSECURITY_SANITY_CHECKS) && (MODSECURITY_SANITY_CHECKS)
646-
conf->sanity_checks_enabled = NGX_CONF_UNSET;
647-
#endif
648633

649634
return conf;
650635
}
@@ -661,10 +646,6 @@ ngx_http_modsecurity_merge_conf(ngx_conf_t *cf, void *parent, void *child)
661646

662647
ngx_conf_merge_value(conf->enable, prev->enable, 0);
663648
ngx_conf_merge_ptr_value(conf->transaction_id, prev->transaction_id, NULL);
664-
#if defined(MODSECURITY_SANITY_CHECKS) && (MODSECURITY_SANITY_CHECKS)
665-
ngx_conf_merge_value(conf->sanity_checks_enabled,
666-
prev->sanity_checks_enabled, 0);
667-
#endif
668649

669650
if (prev->rules_set != NULL) {
670651
if (conf->rules_set != NULL) {

0 commit comments

Comments
 (0)