Having a better error handler for the highlight feature

This commit is contained in:
Felipe Zimmerle 2018-10-11 10:01:13 -03:00
parent a586809db5
commit 665b54f5c4
No known key found for this signature in database
GPG Key ID: E6DFB08CE8B11277
2 changed files with 54 additions and 13 deletions

View File

@ -86,8 +86,12 @@ static void *process_request(void *data) {
modsecTransaction->processURI(request_uri, "GET", "1.1"); modsecTransaction->processURI(request_uri, "GET", "1.1");
usleep(10); usleep(10);
modsecTransaction->addRequestHeader("Host", /*
"net.tutsplus.com"); for (auto &i : m_requestHeaders) {
modsecTransaction->addRequestHeader(i.first,
i.second);
}
*/
modsecTransaction->processRequestHeaders(); modsecTransaction->processRequestHeaders();
modsecTransaction->processRequestBody(); modsecTransaction->processRequestBody();
modsecTransaction->addResponseHeader("HTTP/1.1", modsecTransaction->addResponseHeader("HTTP/1.1",
@ -109,14 +113,15 @@ static void *process_request(void *data) {
class ReadingLogsViaRuleMessage { class ReadingLogsViaRuleMessage {
public: public:
ReadingLogsViaRuleMessage(char *request_header, ReadingLogsViaRuleMessage(
std::unordered_multimap<std::string, std::string> requestHeaders,
char *request_uri, char *request_uri,
char *request_body, char *request_body,
char *response_headers, char *response_headers,
char *response_body, char *response_body,
char *ip, char *ip,
std::string rules) : std::string rules) :
m_request_header(request_header), m_requestHeaders(requestHeaders),
m_request_uri(request_uri), m_request_uri(request_uri),
m_request_body(request_body), m_request_body(request_body),
m_response_headers(response_headers), m_response_headers(response_headers),
@ -245,7 +250,7 @@ end:
} }
protected: protected:
char *m_request_header; std::unordered_multimap<std::string, std::string> m_requestHeaders;
char *m_request_uri; char *m_request_uri;
char *m_request_body; char *m_request_body;
char *m_response_headers; char *m_response_headers;

View File

@ -207,11 +207,37 @@ void ModSecurity::serverLog(void *data, std::shared_ptr<RuleMessage> rm) {
if (m_logProperties & RuleMessageLogProperty) { if (m_logProperties & RuleMessageLogProperty) {
const void *a = static_cast<const void *>(rm.get()); const void *a = static_cast<const void *>(rm.get());
if (m_logProperties & IncludeFullHighlightLogProperty) { if (m_logProperties & IncludeFullHighlightLogProperty) {
processContentOffset(rm->m_buf.c_str(), rm->m_buf.size(), const char *err = NULL;
rm->m_reference.c_str(), &rm->m_highlightJSON, NULL); const char *buf = NULL;
m_logCb(data, a); size_t z;
return; int ret = processContentOffset(rm->m_buf.c_str(), rm->m_buf.size(),
rm->m_reference.c_str(), &rm->m_highlightJSON, &err);
if (ret < 0) {
#ifdef WITH_YAJL
yajl_gen g;
g = yajl_gen_alloc(NULL);
if (g == NULL) {
rm->m_highlightJSON.append(err);
goto out;
} }
yajl_gen_config(g, yajl_gen_beautify, 1);
yajl_gen_map_open(g);
yajl_gen_string(g, reinterpret_cast<const unsigned char*>("error"),
strlen("error"));
yajl_gen_string(g, reinterpret_cast<const unsigned char*>(err),
strlen(err));
yajl_gen_map_close(g);
yajl_gen_get_buf(g, (const unsigned char**)&buf, &z);
&rm->m_highlightJSON.append(buf);
yajl_gen_free(g);
#else
rm->m_highlightJSON.append(err);
#endif
}
}
out:
m_logCb(data, a); m_logCb(data, a);
return; return;
} }
@ -236,7 +262,7 @@ int ModSecurity::processContentOffset(const char *content, size_t len,
g = yajl_gen_alloc(NULL); g = yajl_gen_alloc(NULL);
if (g == NULL) { if (g == NULL) {
*err = "Failed to allocate memory for the JSON creation."; *err = strdup("Failed to allocate memory for the JSON creation.");
return -1; return -1;
} }
@ -279,7 +305,12 @@ int ModSecurity::processContentOffset(const char *content, size_t len,
yajl_gen_map_close(g); yajl_gen_map_close(g);
if (stoi(startingAt) >= len) { if (stoi(startingAt) >= len) {
*err = "Offset is out of the content limits."; std::stringstream e;
e << "Offset for the variables are out of the content limits. " \
"Trying to read position " << startingAt.c_str() << " from a buffer "\
"with only " << len << " bytes. Buffer: " << content \
<< std::endl;
*err = strdup(e.str().c_str());
return -1; return -1;
} }
@ -363,7 +394,12 @@ int ModSecurity::processContentOffset(const char *content, size_t len,
yajl_gen_map_close(g); yajl_gen_map_close(g);
if (stoi(startingAt) >= varValue.size()) { if (stoi(startingAt) >= varValue.size()) {
*err = "Offset is out of the variable limits."; std::stringstream e;
e << "Offset for the operator is out of the variable limit. " \
"Trying to read " << startingAt.c_str() << " from a buffer with " \
"only " << std::to_string(varValue.size()) << " bytes. Buffer: " \
"" << varValue << std::endl;
*err = strdup(e.str().c_str());
return -1; return -1;
} }
yajl_gen_string(g, yajl_gen_string(g,
@ -394,7 +430,7 @@ int ModSecurity::processContentOffset(const char *content, size_t len,
yajl_gen_free(g); yajl_gen_free(g);
#else #else
*err = "Without YAJL support, we cannot generate JSON."; *err = strdup("Without YAJL support, we cannot generate JSON.");
return -1; return -1;
#endif #endif
return 0; return 0;