fix: bound nesting depth when deserializing enclosed payloads - #3500
Merged
Conversation
Motivation: Several of the remoting wire formats enclose a serialized payload whose enclosed message is itself a serialized payload (for example Some, Optional, Status.Failure, StatusReply, a Throwable cause, or an ActorSelectionMessage). Each level is parsed separately, so neither a protobuf parser's own nesting limit nor the size of the message bounds how deep the chain can go relative to the stack: the recursion tracks the nesting rather than the number of bytes. A sufficiently deeply nested message fails with a StackOverflowError rather than a serialization error. Modification: Add a per-thread nesting-depth counter (NestedDeserialization). A level is counted where a serializer is actually invoked: Serialization.deserializeByteArray counts one, and in WrappedPayloadSupport.deserializePayload the two branches that call a serializer directly count one each, while the branch that delegates back to Serialization leaves the counting to it. Nesting deeper than pekko.actor.serialization-max-nesting-depth (default 32) is rejected with a NotSerializableException. Ordinary nesting depths are unaffected. Result: An over-nested payload is rejected as an ordinary serialization error instead of exhausting the stack. Tests: - sbt "remote/testOnly org.apache.pekko.remote.serialization.NestedPayloadDepthSpec" - 5 passed - sbt "actor-tests/testOnly org.apache.pekko.serialization.SerializeSpec org.apache.pekko.serialization.WireManifestClassLoadingSpec" - 21 passed - sbt "actor/mimaReportBinaryIssues" "remote/mimaReportBinaryIssues" - no issues - scalafmt on the changed Scala sources References: None - robustness of nested payload deserialization
pjfanning
force-pushed
the
nested-payload-depth
branch
from
September 1, 2026 10:15
c1a97a9 to
86372a9
Compare
pjfanning
requested review from
He-Pin,
Philippus,
nvollmar,
raboof and
samueleresca
September 1, 2026 11:04
He-Pin
approved these changes
Sep 1, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Motivation
Several of the remoting wire formats carry a payload whose enclosed message is itself a
serialized payload —
Some,Optional,Status.Failure,StatusReply, aThrowablecause, or an
ActorSelectionMessage. Each level is parsed on its own, so neither theprotobuf parser's own nesting limit nor the size of the message bounds how deep the chain
can go: the recursion tracks the nesting rather than the number of bytes. A sufficiently
deep chain fails with a
StackOverflowErrorrather than a serialization error, which isnot how the rest of the deserialization path reports a message it cannot read.
Modification
Add a per-thread nesting-depth counter (
NestedDeserialization,@InternalApi). A level iscounted where a serializer is actually invoked:
Serialization.deserializeByteArraycountsone, and in
WrappedPayloadSupport.deserializePayloadthe two branches that call a serializerdirectly count one each, while the branch that delegates back to
Serializationleaves thecounting to it. Nesting deeper than
pekko.actor.serialization-max-nesting-depth(default 32)is rejected with a
NotSerializableException. Ordinary nesting is unaffected; the default sitswell above anything the wrapper types produce in practice.
Result
An over-nested payload is reported as an ordinary serialization failure instead of a
StackOverflowError. No behaviour change for messages within the depth limit.Tests
sbt "remote/testOnly org.apache.pekko.remote.serialization.NestedPayloadDepthSpec"- 5 passedsbt "actor-tests/testOnly org.apache.pekko.serialization.SerializeSpec org.apache.pekko.serialization.WireManifestClassLoadingSpec"- passed unchangedsbt "actor/mimaReportBinaryIssues" "remote/mimaReportBinaryIssues"- no issuesscalafmton the changed Scala sourcesReferences
None - robustness of nested payload deserialization