diff --git a/unit_tests/falco/test_configuration.cpp b/unit_tests/falco/test_configuration.cpp index f65c70aa..2f87f1fd 100644 --- a/unit_tests/falco/test_configuration.cpp +++ b/unit_tests/falco/test_configuration.cpp @@ -278,6 +278,39 @@ TEST(Configuration, configuration_include_files) ASSERT_TRUE(conf.is_defined("base_value_2.id")); ASSERT_EQ(conf.get_scalar("base_value_2.id", 0), 2); + /* Test that empty includes list is accepted */ + const std::string main_conf_yaml_empty_includes = + "includes:\n" + "foo: bar\n" + "base_value:\n" + " id: 1\n" + " name: foo\n"; + outfile.open("main.yaml"); + outfile << main_conf_yaml_empty_includes; + outfile.close(); + + ASSERT_NO_THROW(conf.load_from_file("main.yaml")); + + ASSERT_TRUE(conf.is_defined("foo")); + ASSERT_EQ(conf.get_scalar("foo", ""), "bar"); + ASSERT_TRUE(conf.is_defined("base_value.id")); + ASSERT_EQ(conf.get_scalar("base_value.id", 0), 1); + ASSERT_TRUE(conf.is_defined("base_value.name")); + ASSERT_EQ(conf.get_scalar("base_value.name", ""), "foo"); + + /* Test that empty includes list is accepted */ + const std::string main_conf_yaml_include_itself = + "includes: main.yaml\n" + "foo: bar\n" + "base_value:\n" + " id: 1\n" + " name: foo\n"; + outfile.open("main.yaml"); + outfile << main_conf_yaml_include_itself; + outfile.close(); + + ASSERT_ANY_THROW(conf.load_from_file("main.yaml")); + // Cleanup everything std::filesystem::remove("main.yaml"); std::filesystem::remove("conf_2.yaml"); diff --git a/userspace/falco/yaml_helper.h b/userspace/falco/yaml_helper.h index a21a2462..ccddf410 100644 --- a/userspace/falco/yaml_helper.h +++ b/userspace/falco/yaml_helper.h @@ -109,6 +109,10 @@ public: { include_file_path = config_folder / include_file; } + if (include_file_path == ppath) + { + throw std::runtime_error("Config error: 'includes' directive tried to recursively include main config file: " + path + "."); + } if (std::filesystem::exists(include_file_path) && std::filesystem::is_regular_file(include_file_path)) { auto loaded_nodes = load_from_file_int(include_file_path.string());