Merge pull request #13560 from rajgaur98/develop

Updated Whiskers for checking invalid tags
This commit is contained in:
Kamil Śliwak 2022-10-04 17:33:05 +02:00 committed by GitHub
commit 70b0fb6366
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
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)