Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add maximum depth to serializer to prevent infinite recursion (#167) #171

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

mhvis
Copy link

@mhvis mhvis commented Jul 19, 2024

Fixes #167

I'm not sure if this is a good approach. Other suggestions are welcome.

Thanks for reacting to the issue @ilario-pierbattista and thanks for creating this great library.

The issue doesn't really have a high priority, as one shouldn't put circular objects into a Mongo database anyway. So if you don't want to merge this or don't want to fix the issue I can totally understand.

@ilario-pierbattista
Copy link
Member

thank @mhvis for you contribution.

Thanks for reacting to the issue @ilario-pierbattista and thanks for creating this great library.

Just to make things clear, @Algatux is the original author of the bundle and a lot of @facile-it folks and external contributors maintain it. Kudos should go to @Algatux and all the other contributors as well, you included 🎉

I'll give it a quick look right now, even if it's not highest priority.

Comment on lines +46 to 59
public static function prepareItemData($item, int $depth = 0)
{
// Prevent infinite recursion
if ($depth > 1_000) {
return null;
}

if (\is_scalar($item)) {
return $item;
}

if (\is_array($item)) {
return self::prepareUnserializableData($item);
return self::prepareUnserializableData($item, $depth);
}

Choose a reason for hiding this comment

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

Why don't we use a maxDepth approach?

Default it to 256 or so, and then decrease it at each iteration int $maxDepth = 256

To stop infinite recursion you should add

if ($depth < 0) {
     return null;
}

so that also negative $maxDepth are correctly managed.

Also, having maxDepth as a parameter cloud make it configurable in the future.

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.

Debug toolbar data collector has infinite recursion when serializing a cyclic object
2 participants