Skip to content

Commit cc6e853

Browse files
committed
Merge pull request #18 from chaizhenhua/remotes/trunk
Added drop action for nginx
2 parents f920303 + 6815d17 commit cc6e853

File tree

4 files changed

+45
-7
lines changed

4 files changed

+45
-7
lines changed

Diff for: apache2/mod_security2.c

+22-5
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,9 @@ unsigned long int DSOLOCAL conn_read_state_limit = 0;
6464

6565
unsigned long int DSOLOCAL conn_write_state_limit = 0;
6666

67+
#if defined(WIN32) || defined(VERSION_NGINX)
68+
int (*modsecDropAction)(request_rec *r) = NULL;
69+
#endif
6770
static int server_limit, thread_limit;
6871

6972
typedef struct {
@@ -250,11 +253,25 @@ int perform_interception(modsec_rec *msr) {
250253
}
251254
}
252255
#else
253-
log_level = 1;
254-
status = HTTP_INTERNAL_SERVER_ERROR;
255-
message = apr_psprintf(msr->mp, "Access denied with code 500%s "
256-
"(Error: Connection drop not implemented on this platform).",
257-
phase_text);
256+
{
257+
if (modsecDropAction == NULL) {
258+
log_level = 1;
259+
status = HTTP_INTERNAL_SERVER_ERROR;
260+
message = apr_psprintf(msr->mp, "Access denied with code 500%s "
261+
"(Error: Connection drop not implemented on this platform.",
262+
phase_text);
263+
} else if (modsecDropAction(msr->r) == 0) {
264+
status = HTTP_FORBIDDEN;
265+
message = apr_psprintf(msr->mp, "Access denied with connection close%s.",
266+
phase_text);
267+
} else {
268+
log_level = 1;
269+
status = HTTP_INTERNAL_SERVER_ERROR;
270+
message = apr_psprintf(msr->mp, "Access denied with code 500%s "
271+
"(Error: Connection drop request failed.",
272+
phase_text);
273+
}
274+
}
258275
#endif
259276
break;
260277

Diff for: nginx/modsecurity/ngx_http_modsecurity.c

+17
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,8 @@ static char *ngx_http_modsecurity_add_handler(ngx_conf_t *cf, ngx_command_t *cmd
8181
static char *ngx_http_modsecurity_pass(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);
8282
static ngx_int_t ngx_http_modsecurity_pass_to_backend(ngx_http_request_t *r);
8383

84+
static int ngx_http_modsecurity_drop_action(request_rec *r);
85+
8486
/* command handled by the module */
8587
static ngx_command_t ngx_http_modsecurity_commands[] = {
8688
{ ngx_string("ModSecurityConfig"),
@@ -223,6 +225,8 @@ ngx_http_modsecurity_init_process(ngx_cycle_t *cycle)
223225

224226
modsecSetLogHook(cycle->log, modsecLog);
225227

228+
modsecSetDropAction(ngx_http_modsecurity_drop_action);
229+
226230
modsecInit();
227231
/* config was already parsed in master process */
228232
// modsecStartConfig();
@@ -1094,3 +1098,16 @@ ngx_http_modsecurity_pass(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
10941098

10951099
return NGX_CONF_OK;
10961100
}
1101+
1102+
static int
1103+
ngx_http_modsecurity_drop_action(request_rec *r)
1104+
{
1105+
ngx_http_modsecurity_ctx_t *ctx;
1106+
ctx = (ngx_http_modsecurity_ctx_t *) apr_table_get(r->notes, NOTE_NGINX_REQUEST_CTX);
1107+
1108+
if (ctx == NULL) {
1109+
return -1;
1110+
}
1111+
ctx->r->connection->error = 1;
1112+
return 0;
1113+
}

Diff for: standalone/api.c

+5-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141

4242
extern void *modsecLogObj;
4343
extern void (*modsecLogHook)(void *obj, int level, char *str);
44-
44+
extern int (*modsecDropAction)(request_rec *r);
4545
apr_status_t (*modsecReadBody)(request_rec *r, char *buf, unsigned int length, unsigned int *readcnt, int *is_eos);
4646
apr_status_t (*modsecReadResponse)(request_rec *r, char *buf, unsigned int length, unsigned int *readcnt, int *is_eos);
4747
apr_status_t (*modsecWriteBody)(request_rec *r, char *buf, unsigned int length);
@@ -528,3 +528,7 @@ void modsecSetWriteBody(apr_status_t (*func)(request_rec *r, char *buf, unsigned
528528
void modsecSetWriteResponse(apr_status_t (*func)(request_rec *r, char *buf, unsigned int length)) {
529529
modsecWriteResponse = func;
530530
}
531+
532+
void modsecSetDropAction(int (*func)(request_rec *r)) {
533+
modsecDropAction = func;
534+
}

Diff for: standalone/api.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ void modsecSetReadBody(apr_status_t (*func)(request_rec *r, char *buf, unsigned
7070
void modsecSetReadResponse(apr_status_t (*func)(request_rec *r, char *buf, unsigned int length, unsigned int *readcnt, int *is_eos));
7171
void modsecSetWriteBody(apr_status_t (*func)(request_rec *r, char *buf, unsigned int length));
7272
void modsecSetWriteResponse(apr_status_t (*func)(request_rec *r, char *buf, unsigned int length));
73-
73+
void modsecSetDropAction(int (*func)(request_rec *r));
7474
int modsecIsResponseBodyAccessEnabled(request_rec *r);
7575

7676
#ifdef __cplusplus

0 commit comments

Comments
 (0)