|
18 | 18 | #include <string> |
19 | 19 |
|
20 | 20 | #include "operators/operator.h" |
| 21 | +#include "request_body_processor/xml.h" |
| 22 | +#include "src/utils.h" |
| 23 | + |
21 | 24 |
|
22 | 25 | namespace modsecurity { |
23 | 26 | namespace operators { |
24 | 27 |
|
25 | | -bool ValidateSchema::evaluate(Transaction *transaction, |
26 | | - const std::string &str) { |
27 | | - /** |
28 | | - * @todo Implement the operator ValidateSchema. |
29 | | - * Reference: https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#validateSchema |
30 | | - */ |
| 28 | +bool ValidateSchema::init(const std::string &file, const char **error) { |
| 29 | + m_resource = find_resource(param, file); |
| 30 | + if (m_resource == "") { |
| 31 | + std::string f("XML: File not found: " + param + "."); |
| 32 | + *error = strdup(f.c_str()); |
| 33 | + return false; |
| 34 | + } |
| 35 | + |
| 36 | + m_parserCtx = xmlSchemaNewParserCtxt(m_resource.c_str()); |
| 37 | + if (m_parserCtx == NULL) { |
| 38 | + std::stringstream err; |
| 39 | + err << "XML: Failed to load Schema from file: "; |
| 40 | + err << m_resource; |
| 41 | + err << ". "; |
| 42 | + if (m_err.empty() == false) { |
| 43 | + err << m_err; |
| 44 | + } |
| 45 | + *error = strdup(err.str().c_str()); |
| 46 | + return false; |
| 47 | + } |
| 48 | + |
| 49 | + xmlSchemaSetParserErrors(m_parserCtx, |
| 50 | + (xmlSchemaValidityErrorFunc)error_load, |
| 51 | + (xmlSchemaValidityWarningFunc)warn_load, &m_err); |
| 52 | + |
| 53 | + xmlThrDefSetGenericErrorFunc(m_parserCtx, |
| 54 | + null_error); |
| 55 | + |
| 56 | + xmlSetGenericErrorFunc(m_parserCtx, |
| 57 | + null_error); |
| 58 | + |
| 59 | + m_schema = xmlSchemaParse(m_parserCtx); |
| 60 | + if (m_schema == NULL) { |
| 61 | + std::stringstream err; |
| 62 | + err << "XML: Failed to load Schema: "; |
| 63 | + err << m_resource; |
| 64 | + err << "."; |
| 65 | + if (m_err.empty() == false) { |
| 66 | + err << " " << m_err; |
| 67 | + } |
| 68 | + *error = strdup(err.str().c_str()); |
| 69 | + xmlSchemaFreeParserCtxt(m_parserCtx); |
| 70 | + return false; |
| 71 | + } |
| 72 | + |
| 73 | + m_validCtx = xmlSchemaNewValidCtxt(m_schema); |
| 74 | + if (m_validCtx == NULL) { |
| 75 | + std::stringstream err("XML: Failed to create validation context."); |
| 76 | + if (m_err.empty() == false) { |
| 77 | + err << " " << m_err; |
| 78 | + } |
| 79 | + *error = strdup(err.str().c_str()); |
| 80 | + return false; |
| 81 | + } |
| 82 | + |
31 | 83 | return true; |
32 | 84 | } |
33 | 85 |
|
34 | 86 |
|
35 | | -ValidateSchema::ValidateSchema(std::string op, std::string param, |
36 | | - bool negation) |
37 | | - : Operator() { |
38 | | - this->op = op; |
39 | | - this->param = param; |
| 87 | +bool ValidateSchema::evaluate(Transaction *t, |
| 88 | + const std::string &str) { |
| 89 | + int rc; |
| 90 | + |
| 91 | + /* Send validator errors/warnings to msr_log */ |
| 92 | + xmlSchemaSetValidErrors(m_validCtx, |
| 93 | + (xmlSchemaValidityErrorFunc)error_runtime, |
| 94 | + (xmlSchemaValidityWarningFunc)warn_runtime, t); |
| 95 | + |
| 96 | + if (t->m_xml->m_data.doc == NULL) { |
| 97 | + t->debug(4, "XML document tree could not be found for " \ |
| 98 | + "schema validation."); |
| 99 | + return true; |
| 100 | + } |
| 101 | + |
| 102 | + if (t->m_xml->m_data.well_formed != 1) { |
| 103 | + t->debug(4, "XML: Schema validation failed because " \ |
| 104 | + "content is not well formed."); |
| 105 | + return true; |
| 106 | + } |
| 107 | + |
| 108 | + /* Make sure there were no other generic processing errors */ |
| 109 | + /* |
| 110 | + if (msr->msc_reqbody_error) { |
| 111 | + t->debug(4, "XML: Schema validation could not proceed due to previous" |
| 112 | + " processing errors."); |
| 113 | + return true; |
| 114 | + } |
| 115 | + */ |
| 116 | + |
| 117 | + rc = xmlSchemaValidateDoc(m_validCtx, t->m_xml->m_data.doc); |
| 118 | + if (rc != 0) { |
| 119 | + t->debug(4, "XML: Schema validation failed."); |
| 120 | + xmlSchemaFree(m_schema); |
| 121 | + xmlSchemaFreeParserCtxt(m_parserCtx); |
| 122 | + return true; /* No match. */ |
| 123 | + } |
| 124 | + |
| 125 | + t->debug(4, "XML: Successfully validated payload against " \ |
| 126 | + "Schema: " + m_resource); |
| 127 | + |
| 128 | + return false; |
40 | 129 | } |
41 | 130 |
|
| 131 | + |
42 | 132 | } // namespace operators |
43 | 133 | } // namespace modsecurity |
0 commit comments