Skip to content

feat: improved XMLArgs processing #3363

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 31 commits into from
May 4, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
9e41a53
Finish XMLArgs processing in v3
airween Apr 20, 2025
f62de58
Added new cc and h files
airween Apr 20, 2025
8ae8374
Fix cppcheck errors
airween Apr 20, 2025
b42602f
Fix more cppcheck warning
airween Apr 20, 2025
3e95614
Add nullptr check conditions
airween Apr 20, 2025
029684c
Add nullptr check conditions
airween Apr 20, 2025
22fee12
Change owner in legal text
airween Apr 26, 2025
e367876
Update comment
airween Apr 27, 2025
3dc9fe9
Update comment
airween Apr 27, 2025
90be54e
Update error message
airween Apr 27, 2025
f0aa070
Update comment
airween Apr 27, 2025
eedfed8
Update error message
airween Apr 27, 2025
5b1c6fb
Update comment
airween Apr 27, 2025
fedc709
Update comment
airween Apr 27, 2025
0fcd257
Update comment
airween Apr 27, 2025
bbe7eda
Update explanation
airween Apr 27, 2025
159f612
Update comment
airween Apr 27, 2025
2000f4c
Update comment
airween Apr 27, 2025
0bf6020
Add explanation
airween Apr 27, 2025
72de7e8
Update comment
airween Apr 27, 2025
0c7ea21
Update comment
airween Apr 27, 2025
6742930
Update comment
airween Apr 27, 2025
8947346
Update comment
airween Apr 27, 2025
2135c89
Update comment
airween Apr 27, 2025
91a45e7
Update error message
airween Apr 27, 2025
0b62b7e
Align debug messages to fix regression tests
airween Apr 27, 2025
bf707de
Change directive format to strict camel case
airween Apr 28, 2025
e8dc60e
Change node value's parsing to concatenate instead of copy it every time
airween Apr 28, 2025
89442ed
Change directives in tests; add multibyte test case
airween Apr 28, 2025
d228ea6
Update comment
airween May 1, 2025
a3876e3
Avoid unvanted content parse (whitespaces between tags)
airween May 2, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 39 additions & 1 deletion headers/modsecurity/rules_set_properties.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@
to = (from == PropertyNotSetBodyLimitAction) ? default : from; \
}

#define merge_xmlargparse_value(to, from, default) \
if (to == PropertyNotSetConfigXMLParseXmlIntoArgs) { \
to = (from == PropertyNotSetConfigXMLParseXmlIntoArgs) ? default : from; \
}

#ifdef __cplusplus

namespace modsecurity {
Expand Down Expand Up @@ -177,6 +182,7 @@ class RulesSetProperties {
m_secRequestBodyAccess(PropertyNotSetConfigBoolean),
m_secResponseBodyAccess(PropertyNotSetConfigBoolean),
m_secXMLExternalEntity(PropertyNotSetConfigBoolean),
m_secXMLParseXmlIntoArgs(PropertyNotSetConfigXMLParseXmlIntoArgs),
m_tmpSaveUploadedFiles(PropertyNotSetConfigBoolean),
m_uploadKeepFiles(PropertyNotSetConfigBoolean),
m_debugLog(new DebugLog()),
Expand All @@ -191,6 +197,7 @@ class RulesSetProperties {
m_secRequestBodyAccess(PropertyNotSetConfigBoolean),
m_secResponseBodyAccess(PropertyNotSetConfigBoolean),
m_secXMLExternalEntity(PropertyNotSetConfigBoolean),
m_secXMLParseXmlIntoArgs(PropertyNotSetConfigXMLParseXmlIntoArgs),
m_tmpSaveUploadedFiles(PropertyNotSetConfigBoolean),
m_uploadKeepFiles(PropertyNotSetConfigBoolean),
m_debugLog(debugLog),
Expand Down Expand Up @@ -218,14 +225,27 @@ class RulesSetProperties {

/**
*
*
* The ConfigBoolean enumerator defines the states for configuration boolean values.
* The default value is PropertyNotSetConfigBoolean.
*/
enum ConfigBoolean {
TrueConfigBoolean,
FalseConfigBoolean,
PropertyNotSetConfigBoolean
};

/**
*
* The ConfigXMLParseXmlIntoArgs enumerator defines the states for the configuration
* XMLParseXmlIntoArgs values.
* The default value is PropertyNotSetConfigXMLParseXmlIntoArgs.
*/
enum ConfigXMLParseXmlIntoArgs {
TrueConfigXMLParseXmlIntoArgs,
FalseConfigXMLParseXmlIntoArgs,
OnlyArgsConfigXMLParseXmlIntoArgs,
PropertyNotSetConfigXMLParseXmlIntoArgs
};

/**
*
Expand Down Expand Up @@ -338,6 +358,19 @@ class RulesSetProperties {
}
}

static std::string configXMLParseXmlIntoArgsString(ConfigXMLParseXmlIntoArgs i) {
switch (i) {
case TrueConfigXMLParseXmlIntoArgs:
return "True";
case FalseConfigXMLParseXmlIntoArgs:
return "False";
case OnlyArgsConfigXMLParseXmlIntoArgs:
return "OnlyArgs";
case PropertyNotSetConfigXMLParseXmlIntoArgs:
default:
return "Not set";
}
}

static int mergeProperties(RulesSetProperties *from,
RulesSetProperties *to, std::ostringstream *err) {
Expand All @@ -357,6 +390,10 @@ class RulesSetProperties {
from->m_secXMLExternalEntity,
PropertyNotSetConfigBoolean);

merge_xmlargparse_value(to->m_secXMLParseXmlIntoArgs,
from->m_secXMLParseXmlIntoArgs,
PropertyNotSetConfigXMLParseXmlIntoArgs);

merge_boolean_value(to->m_uploadKeepFiles,
from->m_uploadKeepFiles,
PropertyNotSetConfigBoolean);
Expand Down Expand Up @@ -464,6 +501,7 @@ class RulesSetProperties {
ConfigBoolean m_secRequestBodyAccess;
ConfigBoolean m_secResponseBodyAccess;
ConfigBoolean m_secXMLExternalEntity;
ConfigXMLParseXmlIntoArgs m_secXMLParseXmlIntoArgs;
ConfigBoolean m_tmpSaveUploadedFiles;
ConfigBoolean m_uploadKeepFiles;
ConfigDouble m_argumentsLimit;
Expand Down
1 change: 1 addition & 0 deletions headers/modsecurity/transaction.h
Original file line number Diff line number Diff line change
Expand Up @@ -619,6 +619,7 @@ class Transaction : public TransactionAnchoredVariables, public TransactionSecMa
RequestBodyProcessor::JSON *m_json;

int m_secRuleEngine;
int m_secXMLParseXmlIntoArgs;

std::string m_variableDuration;
std::map<std::string, std::string> m_variableEnvs;
Expand Down
1 change: 1 addition & 0 deletions src/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ ACTIONS = \
actions/chain.cc \
actions/ctl/audit_log_parts.cc \
actions/ctl/audit_engine.cc \
actions/ctl/parse_xml_into_args.cc \
actions/ctl/rule_engine.cc \
actions/ctl/request_body_processor_json.cc \
actions/ctl/request_body_processor_xml.cc \
Expand Down
63 changes: 63 additions & 0 deletions src/actions/ctl/parse_xml_into_args.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* ModSecurity, http://www.modsecurity.org/
* Copyright (c) 2025 OWASP ModSecurity project
*
* You may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* If any of the files related to licensing are missing or if you have any
* other questions related to licensing please contact OWASP.
* directly using the email address [email protected].
*
*/

#include "src/actions/ctl/parse_xml_into_args.h"

#include <iostream>
#include <string>

#include "modsecurity/rules_set_properties.h"
#include "modsecurity/rules_set.h"
#include "modsecurity/transaction.h"

namespace modsecurity {
namespace actions {
namespace ctl {


bool ParseXmlIntoArgs::init(std::string *error) {
std::string what(m_parser_payload, 17, m_parser_payload.size() - 17);

if (what == "on") {
m_secXMLParseXmlIntoArgs = RulesSetProperties::TrueConfigXMLParseXmlIntoArgs;
} else if (what == "off") {
m_secXMLParseXmlIntoArgs = RulesSetProperties::FalseConfigXMLParseXmlIntoArgs;
} else if (what == "onlyargs") {
m_secXMLParseXmlIntoArgs = RulesSetProperties::OnlyArgsConfigXMLParseXmlIntoArgs;
} else {
error->assign("Internal error. Expected: On, Off or OnlyArgs; " \
"got: " + m_parser_payload);
return false;
}

return true;
}

bool ParseXmlIntoArgs::evaluate(RuleWithActions *rule, Transaction *transaction) {
std::stringstream a;
a << "Setting SecParseXmlIntoArgs to ";
a << modsecurity::RulesSetProperties::configXMLParseXmlIntoArgsString(m_secXMLParseXmlIntoArgs);
a << " as requested by a ctl:parseXmlIntoArgs action";

ms_dbg_a(transaction, 8, a.str());

transaction->m_secXMLParseXmlIntoArgs = m_secXMLParseXmlIntoArgs;
return true;
}


} // namespace ctl
} // namespace actions
} // namespace modsecurity
48 changes: 48 additions & 0 deletions src/actions/ctl/parse_xml_into_args.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* ModSecurity, http://www.modsecurity.org/
* Copyright (c) 2025 OWASP ModSecurity Project
*
* You may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* If any of the files related to licensing are missing or if you have any
* other questions related to licensing please contact OWASP.
* directly using the email address [email protected]
*
*/

#include <string>

#include "modsecurity/rules_set_properties.h"
#include "modsecurity/actions/action.h"
#include "modsecurity/transaction.h"


#ifndef SRC_ACTIONS_CTL_PARSE_XML_INTO_ARGS_H_
#define SRC_ACTIONS_CTL_PARSE_XML_INTO_ARGS_H_

namespace modsecurity {
namespace actions {
namespace ctl {


class ParseXmlIntoArgs : public Action {
public:
explicit ParseXmlIntoArgs(const std::string &action)
: Action(action),
m_secXMLParseXmlIntoArgs(RulesSetProperties::PropertyNotSetConfigXMLParseXmlIntoArgs) { }

bool init(std::string *error) override;
bool evaluate(RuleWithActions *rule, Transaction *transaction) override;

RulesSetProperties::ConfigXMLParseXmlIntoArgs m_secXMLParseXmlIntoArgs;
};


} // namespace ctl
} // namespace actions
} // namespace modsecurity

#endif // SRC_ACTIONS_CTL_PARSE_XML_INTO_ARGS_H_
Loading
Loading