Skip to content

Guard rfl::yaml::Reader against recursive aliases and out-of-bounds literal marks - #801

Open
clockblocker wants to merge 1 commit into
munich-data-engineering:mainfrom
clockblocker:yaml-recursive-alias-guard
Open

clockblocker wants to merge 1 commit into
munich-data-engineering:mainfrom
clockblocker:yaml-recursive-alias-guard

Conversation

@clockblocker

@clockblocker clockblocker commented Nov 27, 2026 •

Copy link
Copy Markdown
Contributor

AI-authored below this line.

Problem

Cyclic YAML anchors and aliases (such as &self referenced via *self) cause rfl::yaml::Reader to recurse indefinitely until stack overflow. In addition, literal-block newline trimming indexes yaml_str_[_var.node_.Mark().pos] without bounds checking, reading out of bounds when a node carries a synthetic mark (pos == -1) or out-of-range offset.

# Cyclic alias — Before: stack-overflow crash
#                After:  rfl::Error("Recursive YAML alias detected.") at path ["child"]
&self
name: loop
child: *self

# Non-cyclic (DAG) alias reuse — still succeeds
defaults: &def
  host: localhost
  port: 8080
primary: *def
secondary: *def

Summary

  • Tracks active ancestor nodes across read_object and read_array using a thread-local RAII guard, rejecting direct and mutual cycles with an rfl::Error while preserving non-cyclic DAG alias reuse and const Reader thread safety.
  • Bounds-checks YAML::Mark::pos against [0, yaml_str_.size()) before indexing into the source buffer for literal block scalars.

Real-world motivation

Project Reference Context
openmoq/moqx LoaderTest.cpp#L605-L625, YamlDuplicateKeys.cpp#L23-L24 Guards against self-referencing YAML aliases causing infinite recursion during config loading.

Testing

  • Verified with the existing test suite plus standalone regression tests in clockblocker/refl-cpp-workbench/queue-tests/yaml/test_recursive_alias.cpp covering non-cyclic DAG alias reuse, direct mapping/sequence cycles, mutually recursive aliases with error paths, and out-of-bounds YAML::Mark::pos handling (kept out of the PR diff to keep it minimal; happy to pull them in if preferred).

clockblocker added a commit to clockblocker/reflect-cpp that referenced this pull request Nov 29, 2026
…y skipping

Parent: munich-data-engineering#801 (`yaml::Reader::read_object` recursive alias guard).

`rfl::yaml::Reader::read_object` previously wrapped `p.first.as<std::string>()`
and `_object_reader.read(...)` in `catch (std::exception& e) { continue; }`,
silently dropping mapping entries whose keys were sequences, maps, or aliases
to collections (e.g. `[1, 2]: foo` or `{a: 1}: bar`). This caused silent data
loss in maps / `rfl::Generic` and silently bypassed `rfl::NoExtraFields`.

Validate the mapping key inside the `try` block and return an explicit
`rfl::Error` ("YAML mapping key must be a scalar.") whenever a key is
neither a scalar nor YAML null, propagating any `yaml-cpp` exception as an
`rfl::Error` instead of skipping. Move `_object_reader.read(...)` outside
the `try` block (matching all other structured readers) and pass a zero-copy
`std::string_view` directly from `p.first.Scalar()` (or `"null"` for YAML
null keys `~` / `null` / empty `: val`). Scalar aliases and merge keys (`<<`)
continue to work identically.

Addresses downstream workaround in `openmoq/moqx`
(`src/config/YamlDuplicateKeys.cpp:23-111`, `test/config/LoaderTest.cpp:542-650`).
clockblocker added a commit to clockblocker/reflect-cpp that referenced this pull request Nov 29, 2026
…of-bounds literal marks

Tracks active ancestor YAML nodes via a thread-local RAII guard across
`read_object` and `read_array` so cyclic YAML aliases return an `rfl::Error`
instead of overflowing the stack (while preserving non-cyclic DAG aliases and
`const Reader` thread safety), and bounds-checks `YAML::Mark::pos` before
indexing `_yaml_str`.

Upstream PR: munich-data-engineering#801
Downstream receipts:
- openmoq/moqx explicitly documents self-referencing YAML alias infinite
  recursion and tests adversarial cyclic YAML configs:
  https://github.com/openmoq/moqx/blob/60bab7f13925acbaf578670223e660d71e891547/src/config/YamlDuplicateKeys.cpp#L23-L24
  https://github.com/openmoq/moqx/blob/60bab7f13925acbaf578670223e660d71e891547/test/config/LoaderTest.cpp#L605-L625
- FANUC-CORPORATION/fanuc_driver parses YAML configs via `rfl::yaml::load`:
  https://github.com/FANUC-CORPORATION/fanuc_driver/blob/da5db3f611cdc9dffe687e056c4e79bea584bf5c/fanuc_libs/gpio_config/src/gpio_config.cpp#L18
clockblocker added a commit to clockblocker/reflect-cpp that referenced this pull request Nov 29, 2026
…y skipping

Parent: munich-data-engineering#801 (`yaml::Reader::read_object` recursive alias guard).

`rfl::yaml::Reader::read_object` previously wrapped `p.first.as<std::string>()`
and `_object_reader.read(...)` in `catch (std::exception& e) { continue; }`,
silently dropping mapping entries whose keys were sequences, maps, or aliases
to collections (e.g. `[1, 2]: foo` or `{a: 1}: bar`). This caused silent data
loss in maps / `rfl::Generic` and silently bypassed `rfl::NoExtraFields`.

Validate the mapping key inside the `try` block and return an explicit
`rfl::Error` ("YAML mapping key must be a scalar.") whenever a key is
neither a scalar nor YAML null, propagating any `yaml-cpp` exception as an
`rfl::Error` instead of skipping. Move `_object_reader.read(...)` outside
the `try` block (matching all other structured readers) and pass a zero-copy
`std::string_view` directly from `p.first.Scalar()` (or `"null"` for YAML
null keys `~` / `null` / empty `: val`). Scalar aliases and merge keys (`<<`)
continue to work identically.

Addresses downstream workaround in `openmoq/moqx`
(`src/config/YamlDuplicateKeys.cpp:23-111`, `test/config/LoaderTest.cpp:542-650`).
clockblocker added a commit to clockblocker/reflect-cpp that referenced this pull request Nov 30, 2026
…of-bounds literal marks

Tracks active ancestor YAML nodes via a thread-local RAII guard across
`read_object` and `read_array` so cyclic YAML aliases return an `rfl::Error`
instead of overflowing the stack (while preserving non-cyclic DAG aliases and
`const Reader` thread safety), and bounds-checks `YAML::Mark::pos` before
indexing `_yaml_str`.

Upstream PR: munich-data-engineering#801
Downstream receipts:
- openmoq/moqx explicitly documents self-referencing YAML alias infinite
  recursion and tests adversarial cyclic YAML configs:
  https://github.com/openmoq/moqx/blob/60bab7f13925acbaf578670223e660d71e891547/src/config/YamlDuplicateKeys.cpp#L23-L24
  https://github.com/openmoq/moqx/blob/60bab7f13925acbaf578670223e660d71e891547/test/config/LoaderTest.cpp#L605-L625
- FANUC-CORPORATION/fanuc_driver parses YAML configs via `rfl::yaml::load`:
  https://github.com/FANUC-CORPORATION/fanuc_driver/blob/da5db3f611cdc9dffe687e056c4e79bea584bf5c/fanuc_libs/gpio_config/src/gpio_config.cpp#L18
clockblocker added a commit to clockblocker/reflect-cpp that referenced this pull request Nov 30, 2026
…y skipping

Parent: munich-data-engineering#801 (`yaml::Reader::read_object` recursive alias guard).

`rfl::yaml::Reader::read_object` previously wrapped `p.first.as<std::string>()`
and `_object_reader.read(...)` in `catch (std::exception& e) { continue; }`,
silently dropping mapping entries whose keys were sequences, maps, or aliases
to collections (e.g. `[1, 2]: foo` or `{a: 1}: bar`). This caused silent data
loss in maps / `rfl::Generic` and silently bypassed `rfl::NoExtraFields`.

Validate the mapping key inside the `try` block and return an explicit
`rfl::Error` ("YAML mapping key must be a scalar.") whenever a key is
neither a scalar nor YAML null, propagating any `yaml-cpp` exception as an
`rfl::Error` instead of skipping. Move `_object_reader.read(...)` outside
the `try` block (matching all other structured readers) and pass a zero-copy
`std::string_view` directly from `p.first.Scalar()` (or `"null"` for YAML
null keys `~` / `null` / empty `: val`). Scalar aliases and merge keys (`<<`)
continue to work identically.

Addresses downstream workaround in `openmoq/moqx`
(`src/config/YamlDuplicateKeys.cpp:23-111`, `test/config/LoaderTest.cpp:542-650`).
@clockblocker
clockblocker force-pushed the yaml-recursive-alias-guard branch from 5ce6574 to 1996644 Compare November 30, 2026 20:35
@clockblocker
clockblocker force-pushed the yaml-recursive-alias-guard branch from 1996644 to 71fdf3e Compare December 1, 2026 06:05
clockblocker added a commit to clockblocker/reflect-cpp that referenced this pull request Dec 1, 2026
…of-bounds literal marks

Tracks active ancestor YAML nodes via a thread-local RAII guard across
`read_object` and `read_array` so cyclic YAML aliases return an `rfl::Error`
instead of overflowing the stack (while preserving non-cyclic DAG aliases and
`const Reader` thread safety), and bounds-checks `YAML::Mark::pos` before
indexing `_yaml_str`.

Upstream PR: munich-data-engineering#801
Downstream receipts:
- openmoq/moqx explicitly documents self-referencing YAML alias infinite
  recursion and tests adversarial cyclic YAML configs:
  https://github.com/openmoq/moqx/blob/60bab7f13925acbaf578670223e660d71e891547/src/config/YamlDuplicateKeys.cpp#L23-L24
  https://github.com/openmoq/moqx/blob/60bab7f13925acbaf578670223e660d71e891547/test/config/LoaderTest.cpp#L605-L625
- FANUC-CORPORATION/fanuc_driver parses YAML configs via `rfl::yaml::load`:
  https://github.com/FANUC-CORPORATION/fanuc_driver/blob/da5db3f611cdc9dffe687e056c4e79bea584bf5c/fanuc_libs/gpio_config/src/gpio_config.cpp#L18
clockblocker added a commit to clockblocker/reflect-cpp that referenced this pull request Dec 1, 2026
…y skipping

Parent: munich-data-engineering#801 (`yaml::Reader::read_object` recursive alias guard).

`rfl::yaml::Reader::read_object` previously wrapped `p.first.as<std::string>()`
and `_object_reader.read(...)` in `catch (std::exception& e) { continue; }`,
silently dropping mapping entries whose keys were sequences, maps, or aliases
to collections (e.g. `[1, 2]: foo` or `{a: 1}: bar`). This caused silent data
loss in maps / `rfl::Generic` and silently bypassed `rfl::NoExtraFields`.

Validate the mapping key inside the `try` block and return an explicit
`rfl::Error` ("YAML mapping key must be a scalar.") whenever a key is
neither a scalar nor YAML null, propagating any `yaml-cpp` exception as an
`rfl::Error` instead of skipping. Move `_object_reader.read(...)` outside
the `try` block (matching all other structured readers) and pass a zero-copy
`std::string_view` directly from `p.first.Scalar()` (or `"null"` for YAML
null keys `~` / `null` / empty `: val`). Scalar aliases and merge keys (`<<`)
continue to work identically.

Addresses downstream workaround in `openmoq/moqx`
(`src/config/YamlDuplicateKeys.cpp:23-111`, `test/config/LoaderTest.cpp:542-650`).
…iteral 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.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant