Skip to content

Allow specifying a maximum recursion for the deserializer#1072

Open
aaubry wants to merge 1 commit intomasterfrom
deserializer-max-recursion
Open

Allow specifying a maximum recursion for the deserializer#1072
aaubry wants to merge 1 commit intomasterfrom
deserializer-max-recursion

Conversation

@aaubry
Copy link
Owner

@aaubry aaubry commented Jan 5, 2026

This adds a WithMaximumRecursion method to DeserializerBuilder. It allows to limit the maximum allowed depth when deserializing a document. This is particularly useful when parsing untrusted YAML as allowing unbounded depth may lead to a stack overflow which might crash the process.

The signature of the method is the same as the one on SerializerBuilder, but in this case there is no default limit as adding one would be a breaking change.

I did reuse the existing RecursionLevel class to control the recursion but had to make a few adjustments as I felt it was useful to have the start and end markers in the exception. It was also necessary to add an overload to the SerializerState class to enable constructors with parameters.

I have added a few tests for both methods since the one on SerializerBuilder didn't have any.

@EdwardCooke
Copy link
Collaborator

Well that build failure is unfortunate. I suspect a breaking change or something in gitversion. Looks like an environment variable can be set to fix that. Not sure appveyor works, but I'll see if I can set something.

@EdwardCooke
Copy link
Collaborator

Also, there's a static deserializer builder now that will need to be updated.

/// Sets the maximum recursion that is allowed while building the object graph.
/// </summary>
/// <remarks>
/// Setting this limit is stringly recommended when parsing untrusted input since
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Type-o on stringly. Should probably be strongly.

private bool enforceNullability;
private bool caseInsensitivePropertyMatching;
private bool enforceRequiredProperties;
private int? maximumRecursion;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We also need this on the staticdeserializerbuilder. Its used by AoT compiled applications.

@EdwardCooke
Copy link
Collaborator

PR build should be fixed again.

@aaubry
Copy link
Owner Author

aaubry commented Jan 14, 2026

Thanks for the review, I'll work on the comments in the next days.

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.

2 participants