Unbounded recursion in Protobuf.JSON decoding of user-defined self-referential messages
Description
The JSON decoder in the protobuf Hex package (Protobuf.JSON.Decode) does not enforce any recursion-depth limit when decoding user-defined embedded message fields. An unauthenticated network client that submits a deeply nested JSON document to any endpoint decoding a self-referential or cyclic protobuf message type can drive the decoder into unbounded recursion, exhausting process memory and stack until the BEAM process crashes. Sustained or concurrent requests exhaust the node and cause a denial of service. The attacker only needs to reach an application API that calls Protobuf.JSON.decode/3, decode!/3, or from_decoded/3 on a schema containing a message type that references itself.
Root Cause
The decoder threads a state map carrying a depth counter and a recursion_limit through the message-decoding recursion, and guards depth with increase_depth_and_maybe_throw/1. That guard is invoked only in the clauses for the well-known types Google.Protobuf.ListValue and Google.Protobuf.Struct. The generic recursion path for user-defined messages (internal_from_json_data/3 for an arbitrary module -> decode_message/4 -> decode_regular_fields/3 -> decode_value/3 -> decode_singular/3 -> back into internal_from_json_data/3) never increments or checks depth. In particular the embedded-message clause passes state through unchanged, so state.depth remains 0 at every nesting level. A self-referential message type such as message Tree { Tree child = 1; } therefore recurses once per JSON nesting level with no bound. The recursion_limit option, even when explicitly set, has no effect on user-defined types.
# lib/protobuf/json/decode.ex
# Guard is applied only to ListValue (line 151) and Struct (line 165):
# state = increase_depth_and_maybe_throw(state)
#
# The embedded-message sink passes state through unchanged (no depth increment):
defp decode_singular(%{type: module, embedded?: true}, value, state) do
internal_from_json_data(value, module, state)
endImpact
Any Elixir or Erlang service that decodes attacker-influenced JSON into a protobuf message whose schema contains a self-referential or cyclic message type is affected in its default configuration. No authentication, privileges, or user interaction are required: the attack is a single unauthenticated network request. A JSON body of roughly 100 KB expresses about 5,000 nesting levels, and about 1 MB expresses on the order of 90,000 levels, each level allocating a fresh stack frame and heap objects. The demonstrated capability is a deterministic crash of the decoding process through stack and memory exhaustion. Because the BEAM max_heap_size process flag defaults to unlimited, a sufficiently deep document, or a handful of concurrent deep documents, can plausibly exhaust the memory of the whole node and disrupt co-located workloads, not just the single request process. Confidentiality and integrity are not affected.
Proof of Concept
Requires the protobuf Hex package (any release in the affected range) and a compiled
self-referential message module. No network service is required to demonstrate the
decoder behavior; the recursion is entirely a property of Protobuf.JSON.decode/decode!.- Define a self-referential proto3 message and compile it, for example a module
TreeNodewith a single embedded fieldchildof typeTreeNode. - Generate a JSON document nesting the
childfield to a depth well beyond 100, for example 5,000 levels:{"child": {"child": {"child": ... }}}. - Decode it with the JSON decoder and observe that no
Protobuf.DecodeErroris raised for exceeding a depth limit; the decoder recurses through every level.
# Build a deeply nested JSON document for a self-referential TreeNode message.
depth = 5_000
json =
Enum.reduce(1..depth, "{}", fn _level, inner -> "{\"child\":" <> inner <> "}" end)
# The JSON decoder recurses once per level with no depth guard for user-defined
# messages. recursion_limit: 100 does NOT stop it (it only guards Value/ListValue/Struct).
TreeNode.from_json(json, recursion_limit: 100)# Observed behavior: the decode does not raise a recursion-limit error at depth 100.
# It proceeds through all 5,000 levels. At larger depths the process consumes
# hundreds of MB and can crash the process with stack/heap exhaustion, whereas the
# well-known-type path (ListValue/Struct) correctly raises {:recursion_limit_exceeded, 100}.Attack Chain
- Exposure: The application exposes a network endpoint that decodes JSON request bodies into a protobuf message via the public
Protobuf.JSON.decode/3,decode!/3, orfrom_decoded/3API, using a schema that contains a self-referential or cyclic message type. - Control: The attacker fully controls the JSON body, including arbitrary nesting depth. The only constraints are valid JSON structure and field names matching the target schema, both trivially satisfied.
- Path: The value flows through
from_json_data/3->internal_from_json_data/3(generic module clause) ->decode_message/4->decode_regular_fields/3->decode_value/3->decode_singular/3(embedded clause) and back intointernal_from_json_data/3, recursing once per nesting level. - Guard: The depth guard
increase_depth_and_maybe_throw/1is applied only in theGoogle.Protobuf.ListValueandGoogle.Protobuf.Structclauses. The user-defined message path and the embedded-message clause never increment or checkstate.depth, so the guard is absent on this path. - Primitive: Unbounded recursion in the decoding process, one live stack frame plus heap allocation per nesting level, with no limit enforced.
- Result: Deterministic exhaustion of process stack and memory leading to a process crash; sustained or concurrent requests plausibly exhaust the node, producing an unauthenticated denial of service.
Bypass Evidence
The canonical prior advisory GHSA-rv48-qqj5-crxg (CVE-2026-54451) fixed the equivalent unbounded recursion in the binary decoder Protobuf.Decoder, first patched in release 0.16.1. That fix commit modifies only lib/protobuf/decoder.ex, lib/protobuf.ex, the protoc CLI, and tests; it does not touch the JSON decoder at lib/protobuf/json/decode.ex. A separate later change added a recursion_limit option to the JSON decoder, but wired the depth guard only into the Google.Protobuf.ListValue and Google.Protobuf.Struct clauses, leaving user-defined message recursion unguarded. This finding therefore targets an independent code path and remediation boundary, not the already-fixed binary decoder.
Disproof attempts and their outcomes: (1) The hypothesis that the underlying JSON parser (Jason or Elixir's native JSON module) rejects deep nesting first was refuted; neither enforces a configurable depth limit, and the from_decoded/3 API bypasses JSON parsing entirely by accepting pre-parsed data. (2) The hypothesis that the BEAM prevents node-wide impact was refuted; the per-process max_heap_size flag defaults to unlimited. (3) The hypothesis that user-defined schemas cannot express unbounded nesting was refuted; self-referential message types are valid and common, and the project's own binary-decoder fix guards all embedded types precisely because schema shape does not bound depth. (4) The hypothesis that amplification is impractical was refuted; roughly 100 KB of JSON yields about 5,000 levels. The behavioral asymmetry is directly observable: the well-known-type path raises {:recursion_limit_exceeded, 100} while the user-defined path does not.
Affected Versions
Ecosystem: erlang (Hex)Package: protobufConfirmed affected range: >= 0.8.0, <= 0.17.0Latest release checked: 0.17.0 (Hex), source revision at git tag v0.17.0 and current default branchFix status: not fixed
JSON encoding and decoding support was introduced in release 0.8.0, which is the introduction boundary for this path. The vulnerable decode_singular/3 embedded clause and the absence of a depth guard for user-defined messages are present in every release from 0.8.0 through the latest release 0.17.0. The file lib/protobuf/json/decode.ex is identical between the 0.17.0 tag and the current default-branch HEAD, so no unreleased fix exists. The 0.16.1 fix for the related binary-decoder issue does not remediate this JSON path.
Suggested Fix
Enforce a recursion-depth invariant on every recursive message-decoding path in the JSON decoder, not only the Google.Protobuf.ListValue and Google.Protobuf.Struct clauses. The decode_singular/3 embedded-message clause should call increase_depth_and_maybe_throw/1 (threading the updated state) before recursing into internal_from_json_data/3, and the same guard should cover embedded messages reached through repeated fields, map values, and oneof fields, so that decoding raises Protobuf.DecodeError once the configured recursion_limit (default 100) is exceeded for any message type. As an interim mitigation without a code change, operators can bound the accepted request body size and reject or pre-validate the maximum JSON nesting depth at the web or middleware layer before decoding, since no decoder option currently limits recursion for user-defined types.
Reported by zx (GitHub: @manus-pi).