Skip to content

Add opt-in rfl::MaxDepth<N> processor for recursion-depth DoS protection - #130

Draft
clockblocker wants to merge 1 commit into
mainfrom
feat-max-depth
Draft

clockblocker wants to merge 1 commit into
mainfrom
feat-max-depth

Conversation

@clockblocker

@clockblocker clockblocker commented Nov 28, 2026 •

Copy link
Copy Markdown
Owner

Summary

Adds an opt-in rfl::MaxDepth<N> processor that bounds container and struct nesting depth during deserialization, returning an rfl::Error instead of overflowing the C++ call stack on deeply nested untrusted inputs (such as rfl::Generic or recursive rfl::Box<Node> structs).

  • Zero overhead when unused: Guarded behind if constexpr (internal::max_depth_v<ProcessorsType> > 0) — emits zero instructions and zero TLS lookups when rfl::MaxDepth is not passed.
  • Format-agnostic: Enforced at compound parse boundaries (VectorParser, MapParser, NamedTupleParser::read_view / read_view_with_default, ParserArray, TupleParser, FieldVariantParser) so it applies uniformly across JSON, YAML, TOML, XML, CBOR, MsgPack, etc.
  • Safe & composable: Uses a thread-local RAII ParseDepthGuard (exception-safe and thread-safe, matching #72 ActiveNodeGuard), supports nested rfl::Processors<...>, and static_asserts against MaxDepth<0> or duplicate MaxDepth processors.
  • Docs & tests: Updated docs/content/processors.md and added tests/json/test_max_depth.cpp (exact depth boundaries, 5,000-level nested arrays & recursive structs, DefaultIfMissing, maps, std::array, std::tuple, and post-error recovery).

Motivation

Underlying DOM parsers like yyjson parse thousands of levels of nested arrays/objects iteratively on the heap without crashing, after which reflect-cpp's recursive C++ Parser::read overflows the call stack (SIGSEGV) on untrusted input (rfl::Generic or recursive rfl::Box<Node> structs). rfl::MaxDepth<N> closes that stack-overflow DoS vector in reflect-cpp's own layer (same crash class as #72's YAML cyclic-alias guard).

Verification

  • Strict standalone headers (-Wall -Wextra -Wpedantic -Wshadow -Wconversion -Werror): rfl/MaxDepth.hpp and rfl/internal/max_depth.hpp passed; 0 relative-include errors.
  • Compile-fail static_assert verified for MaxDepth<0> and duplicate MaxDepth.
  • tests/json/test_max_depth.cpp under -fsanitize=undefined -fno-sanitize-recover=all: 3/3 passed (including 5,000-level deep arrays & recursive rfl::Box<Node> structs).

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