Skip to content

Commit 70b0fb6

Browse files
authored
Merge pull request #13560 from rajgaur98/develop
Updated Whiskers for checking invalid tags
2 parents 53e0a47 + bfd83c9 commit 70b0fb6

File tree

3 files changed

+41
-2
lines changed

3 files changed

+41
-2
lines changed

libsolutil/Whiskers.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ using namespace solidity::util;
3434
Whiskers::Whiskers(string _template):
3535
m_template(std::move(_template))
3636
{
37+
checkTemplateValid();
3738
}
3839

3940
Whiskers& Whiskers::operator()(string _parameter, string _value)
@@ -74,6 +75,17 @@ string Whiskers::render() const
7475
return replace(m_template, m_parameters, m_conditions, m_listParameters);
7576
}
7677

78+
void Whiskers::checkTemplateValid() const
79+
{
80+
regex validTemplate("<[#?!\\/]\\+{0,1}[a-zA-Z0-9_$-]+(?:[^a-zA-Z0-9_$>-]|$)");
81+
smatch match;
82+
assertThrow(
83+
!regex_search(m_template, match, validTemplate),
84+
WhiskersError,
85+
"Template contains an invalid/unclosed tag " + match.str()
86+
);
87+
}
88+
7789
void Whiskers::checkParameterValid(string const& _parameter) const
7890
{
7991
static regex validParam("^" + paramRegex() + "$");

libsolutil/Whiskers.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ class Whiskers
9292
private:
9393
// Prevent implicit cast to bool
9494
Whiskers& operator()(std::string _parameter, long long);
95+
void checkTemplateValid() const;
9596
void checkParameterValid(std::string const& _parameter) const;
9697
void checkParameterUnknown(std::string const& _parameter) const;
9798

test/libsolutil/Whiskers.cpp

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,34 @@ BOOST_AUTO_TEST_CASE(conditional_with_else)
8888
BOOST_AUTO_TEST_CASE(broken_conditional_with_else)
8989
{
9090
string templ = "<?b>X<!bY</b>";
91-
BOOST_CHECK_EQUAL(Whiskers(templ)("b", true).render(), "X<!bY");
92-
BOOST_CHECK_EQUAL(Whiskers(templ)("b", false).render(), "");
91+
BOOST_CHECK_THROW(Whiskers{templ}, WhiskersError);
92+
93+
templ = "<?bX<!b>Y</b>";
94+
BOOST_CHECK_THROW(Whiskers{templ}, WhiskersError);
95+
96+
templ = "<?b>X<!b>Y</b";
97+
BOOST_CHECK_THROW(Whiskers{templ}, WhiskersError);
98+
}
99+
100+
BOOST_AUTO_TEST_CASE(broken_conditional_value_with_else)
101+
{
102+
string templ = "<?+b>X<!+bY</+b>";
103+
BOOST_CHECK_THROW(Whiskers{templ}, WhiskersError);
104+
105+
templ = "<?+bX<!+b>Y</+b>";
106+
BOOST_CHECK_THROW(Whiskers{templ}, WhiskersError);
107+
108+
templ = "<?+b>X<!+b>Y</+b";
109+
BOOST_CHECK_THROW(Whiskers{templ}, WhiskersError);
110+
}
111+
112+
BOOST_AUTO_TEST_CASE(broken_list_parameter)
113+
{
114+
string templ = "<#b><a></b";
115+
BOOST_CHECK_THROW(Whiskers{templ}, WhiskersError);
116+
117+
templ = "<#b<a></b>";
118+
BOOST_CHECK_THROW(Whiskers{templ}, WhiskersError);
93119
}
94120

95121
BOOST_AUTO_TEST_CASE(conditional_plus_params)

0 commit comments

Comments
 (0)