Updated Whiskers for checking invalid tags

For Example, raise an error if it contains invalid tags like
<?ba<!b</b
This commit is contained in:
Rajkumar gaur 2022-09-28 08:24:54 +05:30
parent 1c77d30cea
commit bfd83c966a
3 changed files with 41 additions and 2 deletions

View File

@ -34,6 +34,7 @@ using namespace solidity::util;
Whiskers::Whiskers(string _template):
m_template(std::move(_template))
{
checkTemplateValid();
}
Whiskers& Whiskers::operator()(string _parameter, string _value)
@ -74,6 +75,17 @@ string Whiskers::render() const
return replace(m_template, m_parameters, m_conditions, m_listParameters);
}
void Whiskers::checkTemplateValid() const
{
regex validTemplate("<[#?!\\/]\\+{0,1}[a-zA-Z0-9_$-]+(?:[^a-zA-Z0-9_$>-]|$)");
smatch match;
assertThrow(
!regex_search(m_template, match, validTemplate),
WhiskersError,
"Template contains an invalid/unclosed tag " + match.str()
);
}
void Whiskers::checkParameterValid(string const& _parameter) const
{
static regex validParam("^" + paramRegex() + "$");

View File

@ -92,6 +92,7 @@ public:
private:
// Prevent implicit cast to bool
Whiskers& operator()(std::string _parameter, long long);
void checkTemplateValid() const;
void checkParameterValid(std::string const& _parameter) const;
void checkParameterUnknown(std::string const& _parameter) const;

View File

@ -88,8 +88,34 @@ BOOST_AUTO_TEST_CASE(conditional_with_else)
BOOST_AUTO_TEST_CASE(broken_conditional_with_else)
{
string templ = "<?b>X<!bY</b>";
BOOST_CHECK_EQUAL(Whiskers(templ)("b", true).render(), "X<!bY");
BOOST_CHECK_EQUAL(Whiskers(templ)("b", false).render(), "");
BOOST_CHECK_THROW(Whiskers{templ}, WhiskersError);
templ = "<?bX<!b>Y</b>";
BOOST_CHECK_THROW(Whiskers{templ}, WhiskersError);
templ = "<?b>X<!b>Y</b";
BOOST_CHECK_THROW(Whiskers{templ}, WhiskersError);
}
BOOST_AUTO_TEST_CASE(broken_conditional_value_with_else)
{
string templ = "<?+b>X<!+bY</+b>";
BOOST_CHECK_THROW(Whiskers{templ}, WhiskersError);
templ = "<?+bX<!+b>Y</+b>";
BOOST_CHECK_THROW(Whiskers{templ}, WhiskersError);
templ = "<?+b>X<!+b>Y</+b";
BOOST_CHECK_THROW(Whiskers{templ}, WhiskersError);
}
BOOST_AUTO_TEST_CASE(broken_list_parameter)
{
string templ = "<#b><a></b";
BOOST_CHECK_THROW(Whiskers{templ}, WhiskersError);
templ = "<#b<a></b>";
BOOST_CHECK_THROW(Whiskers{templ}, WhiskersError);
}
BOOST_AUTO_TEST_CASE(conditional_plus_params)