Skip to content

Commit c425726

Browse files
authored
Merge pull request #360 from fatihusta/disable-error-log
added disable error log support
2 parents fd28e6a + 585dc82 commit c425726

File tree

5 files changed

+68
-5
lines changed

5 files changed

+68
-5
lines changed

.github/nginx/nginx.conf

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,5 +74,18 @@ http {
7474
}
7575
}
7676

77+
server {
78+
listen 80;
79+
server_name modsecurity_use_error_log_off;
80+
81+
modsecurity on;
82+
modsecurity_use_error_log off;
83+
modsecurity_rules_file /home/runner/work/ModSecurity-nginx/ModSecurity-nginx/ModSecurity-nginx/.github/nginx/modsecurity.conf;
84+
root /usr/local/nginx/html/;
85+
86+
location / {
87+
try_files $uri /index.html;
88+
}
89+
}
7790
}
7891

.github/workflows/test.yml

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,28 @@ jobs:
140140
echo "FAIL"
141141
exit 1
142142
fi
143+
- name: Check attack log vhost 2 (modsecurity_use_error_log on(default))
144+
run: |
145+
if ( grep -q "modsectest2" /usr/local/nginx/logs/error.log ); then
146+
echo "OK"
147+
else
148+
echo "FAIL"
149+
exit 1
150+
fi
151+
- name: Check attack log vhost 3 (modsecurity_use_error_log off)
152+
run: |
153+
status=$(curl -sSo /dev/null -w %{http_code} -I -X GET -H "Host: modsecurity_use_error_log_off" "http://localhost/?q=attack")
154+
if [ "${status}" == "403" ]; then
155+
if ( grep -q "modsecurity_use_error_log_off" /usr/local/nginx/logs/error.log ); then
156+
echo "FAIL"
157+
exit 1
158+
else
159+
echo "OK"
160+
fi
161+
else
162+
echo "FAIL"
163+
exit 1
164+
fi
143165
- name: Start Nginx with redir
144166
run: |
145167
sudo killall nginx
@@ -320,4 +342,4 @@ jobs:
320342
md temp
321343
set TEMP=temp
322344
set TEST_NGINX_BINARY=..\objs\nginx.exe
323-
prove modsecurity*.t
345+
prove modsecurity*.t

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,15 @@ using the same unique identificator.
175175

176176
String can contain variables.
177177

178+
modsecurity_use_error_log
179+
-----------
180+
**syntax:** *modsecurity_use_error_log on | off*
181+
182+
**context:** *http, server, location*
183+
184+
**default:** *on*
185+
186+
Turns on or off ModSecurity error log functionality.
178187

179188
# Contributing
180189

src/ngx_http_modsecurity_common.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ typedef struct {
118118
void *rules_set;
119119

120120
ngx_flag_t enable;
121+
ngx_flag_t use_error_log;
121122
#if defined(MODSECURITY_SANITY_CHECKS) && (MODSECURITY_SANITY_CHECKS)
122123
ngx_flag_t sanity_checks_enabled;
123124
#endif

src/ngx_http_modsecurity_module.c

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,7 @@ ngx_http_modsecurity_process_intervention (Transaction *transaction, ngx_http_re
146146
intervention.log = NULL;
147147
intervention.disruptive = 0;
148148
ngx_http_modsecurity_ctx_t *ctx = NULL;
149+
ngx_http_modsecurity_conf_t *mcf;
149150

150151
dd("processing intervention");
151152

@@ -160,12 +161,19 @@ ngx_http_modsecurity_process_intervention (Transaction *transaction, ngx_http_re
160161
return 0;
161162
}
162163

163-
log = intervention.log;
164-
if (intervention.log == NULL) {
165-
log = "(no log message was specified)";
164+
mcf = ngx_http_get_module_loc_conf(r, ngx_http_modsecurity_module);
165+
if (mcf == NULL) {
166+
return NGX_HTTP_INTERNAL_SERVER_ERROR;
166167
}
167168

168-
ngx_log_error(NGX_LOG_ERR, (ngx_log_t *)r->connection->log, 0, "%s", log);
169+
// logging to nginx error log can be disable by setting `modsecurity_use_error_log` to off
170+
if (mcf->use_error_log) {
171+
log = intervention.log;
172+
if (intervention.log == NULL) {
173+
log = "(no log message was specified)";
174+
}
175+
ngx_log_error(NGX_LOG_ERR, (ngx_log_t *)r->connection->log, 0, "%s", log);
176+
}
169177

170178
if (intervention.log != NULL) {
171179
free(intervention.log);
@@ -513,6 +521,14 @@ static ngx_command_t ngx_http_modsecurity_commands[] = {
513521
0,
514522
NULL
515523
},
524+
{
525+
ngx_string("modsecurity_use_error_log"),
526+
NGX_HTTP_LOC_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_MAIN_CONF|NGX_CONF_FLAG,
527+
ngx_conf_set_flag_slot,
528+
NGX_HTTP_LOC_CONF_OFFSET,
529+
offsetof(ngx_http_modsecurity_conf_t, use_error_log),
530+
NULL
531+
},
516532
ngx_null_command
517533
};
518534

@@ -724,6 +740,7 @@ ngx_http_modsecurity_create_conf(ngx_conf_t *cf)
724740
conf->rules_set = msc_create_rules_set();
725741
conf->pool = cf->pool;
726742
conf->transaction_id = NGX_CONF_UNSET_PTR;
743+
conf->use_error_log = NGX_CONF_UNSET;
727744
#if defined(MODSECURITY_SANITY_CHECKS) && (MODSECURITY_SANITY_CHECKS)
728745
conf->sanity_checks_enabled = NGX_CONF_UNSET;
729746
#endif
@@ -763,6 +780,7 @@ ngx_http_modsecurity_merge_conf(ngx_conf_t *cf, void *parent, void *child)
763780

764781
ngx_conf_merge_value(c->enable, p->enable, 0);
765782
ngx_conf_merge_ptr_value(c->transaction_id, p->transaction_id, NULL);
783+
ngx_conf_merge_value(c->use_error_log, p->use_error_log, 1);
766784
#if defined(MODSECURITY_SANITY_CHECKS) && (MODSECURITY_SANITY_CHECKS)
767785
ngx_conf_merge_value(c->sanity_checks_enabled, p->sanity_checks_enabled, 0);
768786
#endif

0 commit comments

Comments
 (0)