Skip to content

[HttpFoundation] Add phpstan-pure to UriSigner to avoid unhandled checks #54297

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
wants to merge 1 commit into from

Conversation

alexander-schranz
Copy link
Contributor

@alexander-schranz alexander-schranz commented Mar 15, 2024

Q A
Branch? 5.4 7.1
Bug fix? yes // kind of help fixes bugs/security issues in projects
New feature? no
Deprecations? no
Issues Fix #...
License MIT

In a project I did refactor some controllers I did stumble over the following code:

    public function acceptAction(Request $request): Response
    {
        $this->uriSigner->checkRequest($request);
        
        // ...

Why it may first looks correctly this does nothing and even opens a security issue for the project. Via phpstan-pure static code analyzers can find such kind of issue and show a error that this method call was not handled.

https://phpstan.org/r/12844f00-e8d8-4f01-8893-8d43ca4efdfe

Adding phpstan-pure was suggested by Ondrej here: phpstan/phpstan-symfony#387.

Technical phpstan-pure means that method does not have any sideeffects (editing any internal state), so most service methods could be pure.

@derrabus
Copy link
Member

We usually don't accept this kind of change on LTS branches. Please target 7.1 instead.

@@ -37,6 +37,8 @@ public function __construct(#[\SensitiveParameter] string $secret, string $param
*
* The given URI is signed by adding the query string parameter
* which value depends on the URI and the secret.
*
* @phpstan-pure
Copy link
Member

Choose a reason for hiding this comment

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

As we use psalm in our CI (even though we use it in an incremental mode), we generally use the psalm- prefix rather than the phpstan- one.

@stof
Copy link
Member

stof commented Mar 15, 2024

so most service methods could be pure.

this is not true. Most services don't guarantee being pure in Symfony, which allow different implementations (for instance, an implementation storing the result in a Cache would not be pure). Declaring an API as being pure is a very strict requirement for the implementation.

@alexander-schranz
Copy link
Contributor Author

@stof aggree, depends mostly on which service you look at. Cache is sure a sideeffect which means not pure. Most factories, generators, matcher, .. services are pure as they don't have any internal states or write to something / output something. Things like cache, orm, ... are sure no pure methods, also twig Environment::display which does echo is not pure. Sure we can not add blindly pure to everything, mostly I would go the way were we see advantages of having it pure like the UriSigner we can with it avoid bugs.

@stof
Copy link
Member

stof commented Mar 15, 2024

@alexander-schranz but most of our interface cannot be marked as pure, as this would force all implementations to be pure (which would be a BC break by changing the contract of the interface btw)

@carsonbot carsonbot changed the title Add phpstan-pure to UriSigner to avoid unhandled checks [HttpFoundation] Add phpstan-pure to UriSigner to avoid unhandled checks Mar 15, 2024
@nicolas-grekas
Copy link
Member

Not a fan of adding these annotations in core. Can't this be moved to the symfony plugin?

@fabpot
Copy link
Member

fabpot commented Apr 5, 2024

I agree with @nicolas-grekas, also because I don't see why we would add these to this specific class and not elsewhere.

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.

7 participants