Skip to content

Commit 2de9b2a

Browse files
committed
Test on the logging generation
Avoid to print the '%' character. This commit should be reverted before production
1 parent 7d2a4ee commit 2de9b2a

File tree

1 file changed

+38
-1
lines changed

1 file changed

+38
-1
lines changed

src/ngx_http_modsecurity_log.c

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,51 @@
2020

2121
#include "ngx_http_modsecurity_common.h"
2222

23+
24+
// FIXME: This function should be replaced, it is being used just for test.
25+
char *
26+
str_replace ( const char *string, const char *substr, const char *replacement ){
27+
char *tok = NULL;
28+
char *newstr = NULL;
29+
char *oldstr = NULL;
30+
char *head = NULL;
31+
32+
/* if either substr or replacement is NULL, duplicate string a let caller handle it */
33+
if ( substr == NULL || replacement == NULL ) return strdup (string);
34+
newstr = strdup (string);
35+
head = newstr;
36+
while ( (tok = strstr ( head, substr ))){
37+
oldstr = newstr;
38+
newstr = malloc ( strlen ( oldstr ) - strlen ( substr ) + strlen ( replacement ) + 1 );
39+
/*failed to alloc mem, free old string and return NULL */
40+
if ( newstr == NULL ){
41+
free (oldstr);
42+
return NULL;
43+
}
44+
memcpy ( newstr, oldstr, tok - oldstr );
45+
memcpy ( newstr + (tok - oldstr), replacement, strlen ( replacement ) );
46+
memcpy ( newstr + (tok - oldstr) + strlen( replacement ), tok + strlen ( substr ), strlen ( oldstr ) - strlen ( substr ) - ( tok - oldstr ) );
47+
memset ( newstr + strlen ( oldstr ) - strlen ( substr ) + strlen ( replacement ) , 0, 1 );
48+
/* move back head right after the last replacement */
49+
head = newstr + (tok - oldstr) + strlen( replacement );
50+
free (oldstr);
51+
}
52+
return newstr;
53+
}
54+
55+
2356
void
2457
ngx_http_modsecurity_log(void *log, const char* msg)
2558
{
59+
char *new_log = NULL;
60+
2661
if (log == NULL) {
2762
return;
2863
}
2964

30-
ngx_log_error(NGX_LOG_INFO, (ngx_log_t *)log, 0, msg);
65+
new_log = str_replace(msg, "%", "_");
66+
ngx_log_error(NGX_LOG_INFO, (ngx_log_t *)log, 0, new_log);
67+
free(new_log);
3168
}
3269

3370

0 commit comments

Comments
 (0)