From 5ce6574283e959aaa0cbca05c9215e2897dc12cb Mon Sep 17 00:00:00 2001 From: clockblocker Date: Wed, 25 Nov 2026 12:04:19 +0100 Subject: [PATCH] Guard rfl::yaml::Reader against recursive aliases and out-of-bounds literal marks Track ancestor YAML nodes via a thread-local RAII guard (ActiveNodeGuard in src/rfl/yaml/Reader.cpp) across read_object and read_array so cyclic YAML aliases return an rfl::Error instead of overflowing the stack, while keeping const Reader instances thread-safe and allowing non-cyclic (DAG) alias reuse. Also bounds-check YAML::Mark::pos before indexing yaml_str_ when trimming trailing newlines on literal block scalars. --- include/rfl/yaml/Reader.hpp | 32 ++++++++++- src/reflectcpp_yaml.cpp | 1 + src/rfl/yaml/Reader.cpp | 48 +++++++++++++++++ tests/yaml/test_recursive_alias.cpp | 84 +++++++++++++++++++++++++++++ 4 files changed, 163 insertions(+), 2 deletions(-) create mode 100644 src/rfl/yaml/Reader.cpp create mode 100644 tests/yaml/test_recursive_alias.cpp diff --git a/include/rfl/yaml/Reader.hpp b/include/rfl/yaml/Reader.hpp index c8625c04a..737f40656 100644 --- a/include/rfl/yaml/Reader.hpp +++ b/include/rfl/yaml/Reader.hpp @@ -18,6 +18,7 @@ #include "../Result.hpp" #include "../always_false.hpp" +#include "../common.hpp" namespace rfl { namespace yaml { @@ -59,6 +60,23 @@ struct Reader { static constexpr bool has_custom_constructor = (requires(InputVarType var) { T::from_yaml_obj(var); }); + /// @brief RAII guard tracking ancestor YAML nodes on the current thread to + /// reject cyclic aliases while keeping const Reader instances thread-safe and + /// allowing non-cyclic (DAG) alias reuse. + class RFL_API ActiveNodeGuard { + public: + explicit ActiveNodeGuard(const YAML::Node& _node); + ~ActiveNodeGuard(); + ActiveNodeGuard(const ActiveNodeGuard&) = delete; + ActiveNodeGuard& operator=(const ActiveNodeGuard&) = delete; + + bool is_cycle() const noexcept { return is_cycle_; } + + private: + bool is_cycle_; + bool pushed_; + }; + /// @brief Constructs a Reader from a YAML string view. /// @param _yaml_str The YAML document as a string view. Reader(const std::string_view& _yaml_str) noexcept : yaml_str_(_yaml_str) {} @@ -117,8 +135,10 @@ struct Reader { // // This is only done for literal blocks which doesn't have tags or // anchors - if (_var.node_.Tag() == "!" && - yaml_str_[_var.node_.Mark().pos] == '|') { + const auto pos = _var.node_.Mark().pos; + if (_var.node_.Tag() == "!" && pos >= 0 && + static_cast(pos) < yaml_str_.size() && + yaml_str_[static_cast(pos)] == '|') { auto last_non_new_line = result.find_last_not_of("\r\n"); if (last_non_new_line != std::string::npos) { result = result.substr(0, last_non_new_line + 1); @@ -159,6 +179,10 @@ struct Reader { template std::optional read_array(const ArrayReader& _array_reader, const InputArrayType& _arr) const noexcept { + const ActiveNodeGuard guard(_arr.node_); + if (guard.is_cycle()) { + return Error("Recursive YAML alias detected."); + } if constexpr (requires { _array_reader.reserve(_arr.node_.size()); }) { if (auto err = _array_reader.reserve(_arr.node_.size())) { return err; @@ -182,6 +206,10 @@ struct Reader { template std::optional read_object(const ObjectReader& _object_reader, const InputObjectType& _obj) const noexcept { + const ActiveNodeGuard guard(_obj.node_); + if (guard.is_cycle()) { + return Error("Recursive YAML alias detected."); + } for (const auto& p : _obj.node_) { try { const auto k = p.first.as(); diff --git a/src/reflectcpp_yaml.cpp b/src/reflectcpp_yaml.cpp index 3f3f1beab..8e6b2aad2 100644 --- a/src/reflectcpp_yaml.cpp +++ b/src/reflectcpp_yaml.cpp @@ -29,4 +29,5 @@ SOFTWARE. // Also, this speeds up compile time, compared to multiple separate .cpp files // compilation. +#include "rfl/yaml/Reader.cpp" #include "rfl/yaml/Writer.cpp" diff --git a/src/rfl/yaml/Reader.cpp b/src/rfl/yaml/Reader.cpp new file mode 100644 index 000000000..7aae58866 --- /dev/null +++ b/src/rfl/yaml/Reader.cpp @@ -0,0 +1,48 @@ +#include "rfl/yaml/Reader.hpp" + +#include + +namespace rfl::yaml { + +namespace { + +std::vector& active_nodes() noexcept { + static thread_local std::vector nodes; + return nodes; +} + +bool is_active_node(const std::vector& _nodes, + const YAML::Node& _node) noexcept { + if (!_node.IsDefined()) { + return false; + } + for (const auto& active : _nodes) { + if (active.is(_node)) { + return true; + } + } + return false; +} + +} // namespace + +Reader::ActiveNodeGuard::ActiveNodeGuard(const YAML::Node& _node) + : is_cycle_(false), pushed_(false) { + auto& nodes = active_nodes(); + if (is_active_node(nodes, _node)) { + is_cycle_ = true; + return; + } + if (_node.IsDefined()) { + nodes.push_back(_node); + pushed_ = true; + } +} + +Reader::ActiveNodeGuard::~ActiveNodeGuard() { + if (pushed_) { + active_nodes().pop_back(); + } +} + +} // namespace rfl::yaml diff --git a/tests/yaml/test_recursive_alias.cpp b/tests/yaml/test_recursive_alias.cpp new file mode 100644 index 000000000..e434f50ae --- /dev/null +++ b/tests/yaml/test_recursive_alias.cpp @@ -0,0 +1,84 @@ +#include + +#include +#include +#include +#include +#include + +namespace test_yaml_recursive_alias { + +using Path = std::vector; + +struct Endpoint { + std::string host; + int port; +}; + +struct Cluster { + Endpoint defaults; + Endpoint primary; + Endpoint secondary; +}; + +struct TreeNode { + std::string name; + std::optional> child; +}; + +TEST(yaml, test_non_recursive_alias_allowed) { + const std::string yaml_str = + "defaults: &def\n" + " host: localhost\n" + " port: 8080\n" + "primary: *def\n" + "secondary: *def\n"; + + const auto result = rfl::yaml::read(yaml_str); + ASSERT_TRUE(result) << result.error().what(); + EXPECT_EQ(result->primary.host, "localhost"); + EXPECT_EQ(result->primary.port, 8080); + EXPECT_EQ(result->secondary.host, "localhost"); + EXPECT_EQ(result->secondary.port, 8080); +} + +TEST(yaml, test_recursive_mapping_alias_rejected) { + const std::string yaml_str = + "&self\n" + "name: loop\n" + "child: *self\n"; + + const auto result = rfl::yaml::read(yaml_str); + ASSERT_FALSE(result); + const auto details = result.error().details(); + ASSERT_EQ(details.size(), 1) << result.error().what(); + EXPECT_EQ(details[0].path, (Path{"child"})); + EXPECT_EQ(details[0].message, "Recursive YAML alias detected."); +} + +TEST(yaml, test_recursive_sequence_alias_rejected) { + const std::string yaml_str = + "&seq\n" + "- *seq\n"; + + const auto result = rfl::yaml::read(yaml_str); + ASSERT_FALSE(result); +} + +TEST(yaml, test_mutual_recursive_alias_rejected) { + const std::string yaml_str = + "&a\n" + "name: a\n" + "child: &b\n" + " name: b\n" + " child: *a\n"; + + const auto result = rfl::yaml::read(yaml_str); + ASSERT_FALSE(result); + const auto details = result.error().details(); + ASSERT_EQ(details.size(), 1) << result.error().what(); + EXPECT_EQ(details[0].path, (Path{"child", "child"})); + EXPECT_EQ(details[0].message, "Recursive YAML alias detected."); +} + +} // namespace test_yaml_recursive_alias