Skip to content

Commit 9503920

Browse files
jschmidt-icingajulianbrost
authored andcommitted
Add test-cases for checking permissions in filter exprs
1 parent c7775d1 commit 9503920

File tree

3 files changed

+367
-1
lines changed

3 files changed

+367
-1
lines changed

test/CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ set(base_test_SOURCES
8888
icinga-notification.cpp
8989
icinga-perfdata.cpp
9090
methods-pluginnotificationtask.cpp
91+
remote-filterutility.cpp
9192
remote-configpackageutility.cpp
9293
remote-url.cpp
9394
${base_OBJS}
@@ -226,6 +227,7 @@ add_boost_test(base
226227
config_apply/gettargetservices_noindexer_service
227228
config_ops/simple
228229
config_ops/advanced
230+
config_ops/sandboxed_ticket_salt
229231
icinga_checkresult/host_1attempt
230232
icinga_checkresult/host_2attempts
231233
icinga_checkresult/host_3attempts
@@ -273,6 +275,8 @@ add_boost_test(base
273275
icinga_perfdata/empty_warn_crit_min_max
274276
methods_pluginnotificationtask/truncate_long_output
275277
remote_configpackageutility/ValidateName
278+
remote_filterutility/safe_function_permissions
279+
remote_filterutility/variable_expression_permissions
276280
remote_url/id_and_path
277281
remote_url/parameters
278282
remote_url/get_and_set

test/config-ops.cpp

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66

77
using namespace icinga;
88

9-
BOOST_AUTO_TEST_SUITE(config_ops)
9+
BOOST_AUTO_TEST_SUITE(config_ops,
10+
*boost::unit_test::label("config"))
1011

1112
BOOST_AUTO_TEST_CASE(simple)
1213
{
@@ -243,4 +244,44 @@ BOOST_AUTO_TEST_CASE(advanced)
243244
BOOST_CHECK(func->Invoke() == 3);
244245
}
245246

247+
BOOST_AUTO_TEST_CASE(sandboxed_ticket_salt)
248+
{
249+
ScriptFrame frame(true, new Namespace);
250+
std::unique_ptr<Expression> expr;
251+
252+
auto ns = ScriptGlobal::GetGlobals();
253+
ns->Set("TicketSalt", "testvalue");
254+
255+
expr = ConfigCompiler::CompileText("<test>", "TicketSalt");
256+
BOOST_CHECK_EQUAL(expr->Evaluate(frame).GetValue(), "testvalue");
257+
258+
expr = ConfigCompiler::CompileText("<test>", "globals.TicketSalt");
259+
BOOST_CHECK_EQUAL(expr->Evaluate(frame).GetValue(), "testvalue");
260+
261+
expr = ConfigCompiler::CompileText("<test>", "*&TicketSalt");
262+
BOOST_CHECK_EQUAL(expr->Evaluate(frame).GetValue(), "testvalue");
263+
264+
expr = ConfigCompiler::CompileText("<test>", "globals.TicketSalt = {{{other}}}");
265+
BOOST_CHECK_NO_THROW(expr->Evaluate(frame));
266+
267+
frame.Sandboxed = true;
268+
ns->Set("TicketSalt", "testvalue", false);
269+
270+
// Accessing TicketSalt in a sandboxed context is like trying to access a variable that doesn't exist.
271+
// In case of direct access, it will throw a ScriptError.
272+
expr = ConfigCompiler::CompileText("<test>", "TicketSalt");
273+
BOOST_CHECK_THROW(expr->Evaluate(frame).GetValue(), ScriptError);
274+
275+
// In case of other ways of accessing it, like through the global scope, it evaluates to Empty
276+
expr = ConfigCompiler::CompileText("<test>", "globals.TicketSalt");
277+
BOOST_CHECK_EQUAL(expr->Evaluate(frame).GetValue(), "");
278+
279+
// Same for (the different ways of) trying to access it via a reference.
280+
expr = ConfigCompiler::CompileText("<test>", "*&TicketSalt");
281+
BOOST_CHECK_EQUAL(expr->Evaluate(frame).GetValue(), "");
282+
283+
expr = ConfigCompiler::CompileText("<test>", "globals.TicketSalt = {{{other}}}");
284+
BOOST_CHECK_THROW(expr->Evaluate(frame), ScriptError);
285+
}
286+
246287
BOOST_AUTO_TEST_SUITE_END()

0 commit comments

Comments
 (0)