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

[Validator][RecursiveContextualValidator] Prevent validated hash collisions #36415

Closed
wants to merge 1 commit into from

Conversation

fancyweb
Copy link
Contributor

Q A
Branch? 3.4
Bug fix? yes
New feature? -
Deprecations? -
Tickets -
License MIT
Doc PR -

RecursiveContextualValidator uses spl_object_hash at different places to generate cache keys. The problem with this method is that there can be collisions (same hash for two different objects) since once an object is destroyed, spl_object_hash can return a previously used hash.

I encountered the problem on a project with constraints that are created on the fly. The first constraint is applied and is "cached". Then it is destroyed. Then another constraint happen to have the exact same spl_object_hash. It is skipped while it would cause violations. I was able to reproduce the problem consistently in the unit test.

The easy way to deal with this is to keep references to all objects (proposed solution). But I guess it could leak too much.

We could also try to "clean" the ExecutionContext stores at the end of the three validate methods. However, we would still need to keep references to the initialized objects since they must really be initialized one time, even on multiple calls

if (\is_object($group)) {
$groupHash = spl_object_hash($group);

if (!isset($this->validatedObjectsReferences[$groupHash])) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Why is this needed for group sequences?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

GroupSequences can suffer from the same problem since they can be passed directly to the validate method as objects.

@nicolas-grekas nicolas-grekas modified the milestones: 4.4, 3.4 Apr 10, 2020
@nicolas-grekas
Copy link
Member

We should find a place where these arrays can be emptied. The memory leak should be avoided.

@fancyweb
Copy link
Contributor Author

We should find a place where these arrays can be emptied. The memory leak should be avoided.

I think we can clear constraints and group sequences arrays at the end of each validate methods. However, we can never clean the initialized objects. Would that work for you?

@fabpot
Copy link
Member

fabpot commented Aug 12, 2020

@fancyweb What's the status of this PR?

@fancyweb
Copy link
Contributor Author

I need to check Nicolas's suggestion, please keep it open.

@fancyweb
Copy link
Contributor Author

We should find a place where these arrays can be emptied. The memory leak should be avoided.

I think we can clear constraints and group sequences arrays at the end of each validate methods. However, we can never clean the initialized objects. Would that work for you?

Unfortunately that does not work. I had to remove this part.

@xabbuh
Copy link
Member

xabbuh commented Aug 23, 2020

Can you update the FormValidator class to remove the $fieldFormConstraint = new Form() statements (can be replaced with $formConstraint). These statements were only added in #36865 to precisely prevent the collisions being fixed here.

@fancyweb
Copy link
Contributor Author

@xabbuh I looked at this a few days ago but it needs more work. My patch does not fix all cases because it does not take into account the current defaultPropertyPath. One of FormValidator tests fails because of this. You can have a look if you want to.

@xabbuh
Copy link
Member

xabbuh commented Oct 2, 2020

continued in #38387, thanks for starting the work on this @fancyweb

@xabbuh xabbuh closed this Oct 2, 2020
@fancyweb fancyweb deleted the validator-spl-object-hash branch October 2, 2020 13:37
xabbuh added a commit that referenced this pull request Nov 13, 2020
…t hashes (fancyweb, xabbuh)

This PR was merged into the 4.4 branch.

Discussion
----------

[Validator] prevent hash collisions caused by reused object hashes

| Q             | A
| ------------- | ---
| Branch?       | 3.4
| Bug fix?      | yes
| New feature?  | no
| Deprecations? | no
| Tickets       | Fix #36415
| License       | MIT
| Doc PR        |

Commits
-------

8dd1a6e prevent hash collisions caused by reused object hashes
9645fa3 [Validator][RecursiveContextualValidator] Prevent validated hash collisions
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants