From 71fdf3e7e65097e324b85906e6e98eafe0e52733 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 +++++++++++++++++++++++++++++++++++++ 3 files changed, 79 insertions(+), 2 deletions(-) create mode 100644 src/rfl/yaml/Reader.cpp diff --git a/include/rfl/yaml/Reader.hpp b/include/rfl/yaml/Reader.hpp index c8625c04a..4ab9e62ae 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 { @@ -117,8 +118,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 +162,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 +189,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(); @@ -221,6 +232,23 @@ struct Reader { } private: + /// @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 The YAML document as a string view, used for context and error /// reporting. std::string_view yaml_str_; 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