Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Validator incompatibility with ProxyManager #35544

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

Closed
alekitto opened this issue Jan 31, 2020 · 2 comments
Closed

Validator incompatibility with ProxyManager #35544

alekitto opened this issue Jan 31, 2020 · 2 comments

Comments

@alekitto
Copy link
Contributor

Symfony version(s) affected: 5.0.4

Description
When trying to validate a value holder proxy generated with ocramius/proxy-manager, all the public properties will be unset at construction.
This results in ReflectionProperty::isInitialized() returning false as clearly documented here.
PR #35532 introduced check on isInitialized returning null instead of firing an __get call.

Possible Solution
Check if __get method exists before blindly returning null, as per rfc "reads from uninitialized properties will generate a TypeError unless __get() is defined".

@nicolas-grekas
Copy link
Member

Would you mind sending a PR please?

@greedyivan
Copy link
Contributor

I want to clarify how this bug differs from #35454

Originally, there was an object

class Foo
{
     /**
     * @Assert\NotBlank()
     */
    public int $bar;
}

If a property is accessed in this initial uninitialized state, PHP throws an error:

Typed property Foo::$bar must not be accessed before initialization

Thus, when the validator tried to check the value and called ReflectionProperty::getValue() an error was triggered. #35532 fixed it.

But that fix broke one special case. If there is an object

class Foo
{
     /**
     * @Assert\NotBlank()
     */
    public int $bar;
    
    public function __construct()
    {
        unset($this->bar);
    }
    
    public function __get($name)
    {
        return 42;
    }
}

And a property is accessed in that unsetted but with __get method state, no error is triggered. Magic method will be called and 42 will be returned.

This issue is about that.

But there is one special case related with PHP 7.4.0 and 7.4.1+.

class Foo
{
     /**
     * @Assert\NotBlank()
     */
    public int $bar;
        
    public function __get($name)
    {
        return 42;
    }
}

This code will trigger error in 7.4.1+, but not in 7.4.0 (42 will be returned).

Therefore, a full implementation should only consider the latest version of PHP. There is no reason to implement a behavior that was found to be erroneous and fixed in future versions.

Recap
An unfixed validator implementation will fail with the second - unsetted with __get method - properties, considering them null.

@fabpot fabpot closed this as completed Feb 3, 2020
fabpot added a commit that referenced this issue Feb 3, 2020
…s uninitialized (alekitto)

This PR was merged into the 3.4 branch.

Discussion
----------

[Validator] check for __get method existence if property is uninitialized

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

Resolve bug #35544.

On PHP 7.4, check if object implements `__get` magic method if property is reported as uninitialized before returning null.

Commits
-------

427bc3a [Validator] try to call __get method if property is uninitialized
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants