Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 30 additions & 2 deletions include/rfl/yaml/Reader.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

#include "../Result.hpp"
#include "../always_false.hpp"
#include "../common.hpp"

namespace rfl {
namespace yaml {
Expand Down Expand Up @@ -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) {}
Expand Down Expand Up @@ -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<size_t>(pos) < yaml_str_.size() &&
yaml_str_[static_cast<size_t>(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);
Expand Down Expand Up @@ -159,6 +179,10 @@ struct Reader {
template <class ArrayReader>
std::optional<Error> 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;
Expand All @@ -182,6 +206,10 @@ struct Reader {
template <class ObjectReader>
std::optional<Error> 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<std::string>();
Expand Down
1 change: 1 addition & 0 deletions src/reflectcpp_yaml.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
48 changes: 48 additions & 0 deletions src/rfl/yaml/Reader.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#include "rfl/yaml/Reader.hpp"

#include <vector>

namespace rfl::yaml {

namespace {

std::vector<YAML::Node>& active_nodes() noexcept {
static thread_local std::vector<YAML::Node> nodes;
return nodes;
}

bool is_active_node(const std::vector<YAML::Node>& _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
84 changes: 84 additions & 0 deletions tests/yaml/test_recursive_alias.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
#include <gtest/gtest.h>

#include <optional>
#include <rfl.hpp>
#include <rfl/yaml.hpp>
#include <string>
#include <vector>

namespace test_yaml_recursive_alias {

using Path = std::vector<rfl::ErrorDetail::PathSegment>;

struct Endpoint {
std::string host;
int port;
};

struct Cluster {
Endpoint defaults;
Endpoint primary;
Endpoint secondary;
};

struct TreeNode {
std::string name;
std::optional<rfl::Box<TreeNode>> 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<Cluster>(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<TreeNode>(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<rfl::Generic>(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<TreeNode>(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