diff --git a/src/assay.cc b/src/assay.cc index 3b6e7bd8..8a33f504 100644 --- a/src/assay.cc +++ b/src/assay.cc @@ -664,6 +664,9 @@ int Assay::addResponseHeader(const std::string& key, this->store_variable("RESPONSE_HEADERS:" + key, value); + if (tolower(key) == "content-type") { + this->store_variable("RESPONSE_CONTENT_TYPE", value); + } return 1; } @@ -748,6 +751,10 @@ int Assay::processResponseBody() { store_variable("OUTBOUND_DATA_ERROR", "0"); } + store_variable("RESPONSE_BODY", m_responseBody.str()); + store_variable("RESPONSE_CONTENT_LENGTH", + std::to_string(m_responseBody.str().size())); + this->m_rules->evaluate(ModSecurity::ResponseBodyPhase, this); return 0; } diff --git a/test/test-cases/regression/variable-RESPONSE_BODY.json b/test/test-cases/regression/variable-RESPONSE_BODY.json new file mode 100644 index 00000000..6a6fe2a6 --- /dev/null +++ b/test/test-cases/regression/variable-RESPONSE_BODY.json @@ -0,0 +1,45 @@ +[ + { + "enabled":1, + "version_min":300000, + "title":"Testing Variables :: RESPONSE_BODY", + "client":{ + "ip":"200.249.12.31", + "port":123 + }, + "server":{ + "ip":"200.249.12.31", + "port":80 + }, + "request":{ + "headers":{ + "Host":"localhost", + "User-Agent":"curl/7.38.0", + "Accept":"*/*" + }, + "uri":"/?key=value&key=other_value", + "protocol":"GET", + "http_version":1.1 + }, + "response":{ + "headers":{ + "Date":"Mon, 13 Jul 2015 20:02:41 GMT", + "Last-Modified":"Sun, 26 Oct 2014 22:33:37 GMT", + "Content-Type":"text/html" + }, + "body":[ + "no need." + ] + }, + "expected":{ + "debug_log":"Target value: \"no need.\" \\(Variable: RESPONSE_BODY\\)" + }, + "rules":[ + "SecRuleEngine On", + "SecDebugLog \/tmp\/modsec_debug.log", + "SecDebugLogLevel 9", + "SecRule RESPONSE_BODY \"@contains test \" \"phase:4,pass,t:trim\"" + ] + } +] + diff --git a/test/test-cases/regression/variable-RESPONSE_CONTENT_LENGTH.json b/test/test-cases/regression/variable-RESPONSE_CONTENT_LENGTH.json new file mode 100644 index 00000000..fde10a27 --- /dev/null +++ b/test/test-cases/regression/variable-RESPONSE_CONTENT_LENGTH.json @@ -0,0 +1,45 @@ +[ + { + "enabled":1, + "version_min":300000, + "title":"Testing Variables :: RESPONSE_CONTENT_LENGTH", + "client":{ + "ip":"200.249.12.31", + "port":123 + }, + "server":{ + "ip":"200.249.12.31", + "port":80 + }, + "request":{ + "headers":{ + "Host":"localhost", + "User-Agent":"curl/7.38.0", + "Accept":"*/*" + }, + "uri":"/?key=value&key=other_value", + "protocol":"GET", + "http_version":1.1 + }, + "response":{ + "headers":{ + "Date":"Mon, 13 Jul 2015 20:02:41 GMT", + "Last-Modified":"Sun, 26 Oct 2014 22:33:37 GMT", + "Content-Type":"text/html" + }, + "body":[ + "no need." + ] + }, + "expected":{ + "debug_log":"Target value: \"9\" \\(Variable: RESPONSE_CONTENT_LENGTH\\)" + }, + "rules":[ + "SecRuleEngine On", + "SecDebugLog \/tmp\/modsec_debug.log", + "SecDebugLogLevel 9", + "SecRule RESPONSE_CONTENT_LENGTH \"@contains test \" \"phase:4,pass,t:trim\"" + ] + } +] + diff --git a/test/test-cases/regression/variable-RESPONSE_CONTENT_TYPE.json b/test/test-cases/regression/variable-RESPONSE_CONTENT_TYPE.json new file mode 100644 index 00000000..109086fc --- /dev/null +++ b/test/test-cases/regression/variable-RESPONSE_CONTENT_TYPE.json @@ -0,0 +1,45 @@ +[ + { + "enabled":1, + "version_min":300000, + "title":"Testing Variables :: RESPONSE_CONTENT_TYPE", + "client":{ + "ip":"200.249.12.31", + "port":123 + }, + "server":{ + "ip":"200.249.12.31", + "port":80 + }, + "request":{ + "headers":{ + "Host":"localhost", + "User-Agent":"curl/7.38.0", + "Accept":"*/*" + }, + "uri":"/?key=value&key=other_value", + "protocol":"GET", + "http_version":1.1 + }, + "response":{ + "headers":{ + "Date":"Mon, 13 Jul 2015 20:02:41 GMT", + "Last-Modified":"Sun, 26 Oct 2014 22:33:37 GMT", + "Content-Type":"text/html" + }, + "body":[ + "no need." + ] + }, + "expected":{ + "debug_log":"Target value: \"text/html\" \\(Variable: RESPONSE_CONTENT_TYPE\\)" + }, + "rules":[ + "SecRuleEngine On", + "SecDebugLog \/tmp\/modsec_debug.log", + "SecDebugLogLevel 9", + "SecRule RESPONSE_CONTENT_TYPE \"@contains test \" \"phase:3,pass,t:trim\"" + ] + } +] +