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

Fix PHP deprecation errors - Issue #13270 #13615

Merged
merged 5 commits into from May 2, 2024
Merged

Conversation

markusVJH
Copy link
Contributor

@markusVJH markusVJH commented Apr 9, 2024

Q A
Bug fix? (use the a.b branch) [ x ]
New feature/enhancement? (use the a.x branch) [ ]
Deprecations? [ ]
BC breaks? (use the c.x branch) [ ]
Automated tests included? [ ]
Related user documentation PR URL mautic/mautic-documentation#...
Related developer documentation PR URL mautic/developer-documentation#...
Issue(s) addressed Fixes #13270

Description:

When visiting the login page, the logs could have these deprecation notices:

PHP Deprecation - fnmatch(): Passing null to parameter #2 ($filename) of type string is deprecated - in file /app/middlewares/CORSMiddleware.php - at line 107
PHP Deprecated:  fnmatch(): Passing null to parameter #2 ($filename) of type string is deprecated in /app/middlewares/CORSMiddleware.php on line 107

This PR implements a simple fix proposed by @J-Wick4, where the variable $origin (which is passed as parameter 2 mentioned in the error message) is checked whether it is null before calling fnmatch(). If $origin is null, false is returned, and the function continues as before. This fixes the error messages.

Steps to test this PR:

  1. Open this PR on Gitpod or pull down for testing locally (see docs on testing PRs here)
  2. Go to the login page.
  3. Refresh the page, and check that the logs do not have the afore mentioned errors.

If testing locally with docker, you can keep eyes on the logs through Docker.

If testing on GitPod through browser, you can check the 'Problems' tab to see if the deprecated notice is there (expected string, found null).

Screenshot 2024-04-09 at 16 05 06

If testing with GitPod but not through browser, you can run ddev logs in the ide terminal to see the logs.

If you want to first see the error messages, and then see the effect of the solution:

  1. Open this PR on Gitpod or pull down for testing locally (see docs on testing PRs here)

  2. Replace function getAllowOriginHeaderValue(Request $request) with the following version. It makes sure the $origin is null and that the fnmatch is called in order to replicate the situation people were having with the deprecation notices in logs. in CORSMiddleware.php:

    private function getAllowOriginHeaderValue(Request $request)
    {
        // Temporary conditions for testing issue #13270
        $origin = null;
        $this->restrictCORSDomains = true; // Ensure domain restriction is enforced
        $this->validCORSDomains = ['*']; // Assuming a wildcard for simplicity

        // If we're not restricting domains, set the header to the request origin
        if (!$this->restrictCORSDomains || in_array($origin, $this->validCORSDomains)) {
            $this->requestOriginIsValid = true;

            return $origin;
        }

        // Check the domains using shell wildcard patterns
        $validCorsDomainFilter = function ($validCorsDomain) use ($origin) {
            return fnmatch($validCorsDomain, $origin, FNM_CASEFOLD);
        };
        if (array_filter($this->validCORSDomains, $validCorsDomainFilter)) {
            $this->requestOriginIsValid = true;
            $this->corsHeaders['Vary']  = 'Origin';

            return $origin;
        }

        $this->requestOriginIsValid = false;

        return null;
    }
  1. Add the previous test code, go to the log in page, and refresh the page. Now the error messages should show in the logs (Either the 'Problems' tab in the GitPod browser, ddev logs result in ide terminal or logs through Docker).

  2. Then to see the error messages fixed with the new solution, add the following code, refresh the log in page and check that the deprecation notice no longer appears:

    private function getAllowOriginHeaderValue(Request $request)
    {
        // Temporary conditions for testing issue #13270
        $origin = null;
        $this->restrictCORSDomains = true; // Ensure domain restriction is enforced
        $this->validCORSDomains = ['*']; // Assuming a wildcard for simplicity

        // If we're not restricting domains, set the header to the request origin
        if (!$this->restrictCORSDomains || in_array($origin, $this->validCORSDomains)) {
            $this->requestOriginIsValid = true;

            return $origin;
        }

        // Check the domains using shell wildcard patterns
        $validCorsDomainFilter = function ($validCorsDomain) use ($origin) {
            if ($origin === null) {
                return false;
            }

            return fnmatch($validCorsDomain, $origin, FNM_CASEFOLD);
        };

        if (array_filter($this->validCORSDomains, $validCorsDomainFilter)) {
            $this->requestOriginIsValid = true;
            $this->corsHeaders['Vary']  = 'Origin';

            return $origin;
        }

        $this->requestOriginIsValid = false;

        return null;
    }

@markusVJH markusVJH marked this pull request as ready for review April 9, 2024 13:18
@RCheesley
Copy link
Sponsor Member

Thanks for the PR @markusVJH and welcome to the community! I've just set the tests running 🚀

@RCheesley RCheesley added T1 Low difficulty to fix (issue) or test (PR) bug Issues or PR's relating to bugs ready-to-test PR's that are ready to test code-review-needed PR's that require a code review before merging tracking Anything related to tracking labels Apr 9, 2024
@RCheesley
Copy link
Sponsor Member

@markusVJH could you check out the failing tests please, let us know if you need any help!

@markusVJH
Copy link
Contributor Author

Thanks for the PR @markusVJH and welcome to the community! I've just set the tests running 🚀

Thank you! I will check the failing tests today.

@markusVJH
Copy link
Contributor Author

@markusVJH could you check out the failing tests please, let us know if you need any help!

Added the change for the CS Fixer job 👍 For the codecov test I am not sure what to do. Based on Slack messages I found it looks like re-running the job might fix it?

@RCheesley
Copy link
Sponsor Member

Thanks @markusVJH - we have had some issues with Codecov as they deprecated an older version which we were running in favour of the GitHub app - should be all fixed now I've merged in the latest changes.

Would you be able to rebase this onto the 5.x branch if possible? We're about to release 5.1 so we should be able to get this into that, or the next patch release.

@markusVJH
Copy link
Contributor Author

Thank you for the reply, I did later find that a lot of PRs were having the same issue 🙏

I will try to do the rebase 👍

@markusVJH markusVJH changed the base branch from 5.0 to 5.x April 22, 2024 10:51
@markusVJH
Copy link
Contributor Author

Thanks @markusVJH - we have had some issues with Codecov as they deprecated an older version which we were running in favour of the GitHub app - should be all fixed now I've merged in the latest changes.

Would you be able to rebase this onto the 5.x branch if possible? We're about to release 5.1 so we should be able to get this into that, or the next patch release.

Rebased to 5.x! Thank you @escopecz for the advice

Hopefully the tests will pass

@escopecz
Copy link
Sponsor Member

The tests don't want to kick off. I'll close and reopen the PR. It usually fixes it.

@escopecz escopecz closed this Apr 22, 2024
@escopecz escopecz reopened this Apr 22, 2024
Co-authored-by: John Linhart <jan@linhart.email>
Copy link
Sponsor Member

@escopecz escopecz left a comment

Choose a reason for hiding this comment

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

Thanks! The code change looks good to me 👍

@escopecz escopecz added code-review-passed PRs which have passed code review pending-test-confirmation PR's that require one test before they can be merged and removed code-review-needed PR's that require a code review before merging ready-to-test PR's that are ready to test labels Apr 23, 2024
Copy link

codecov bot commented Apr 23, 2024

Codecov Report

Attention: Patch coverage is 0% with 2 lines in your changes are missing coverage. Please review.

Project coverage is 61.50%. Comparing base (b2345c1) to head (99b7f41).

Additional details and impacted files

Impacted file tree graph

@@             Coverage Diff              @@
##                5.x   #13615      +/-   ##
============================================
- Coverage     61.50%   61.50%   -0.01%     
- Complexity    34067    34068       +1     
============================================
  Files          2241     2241              
  Lines        101850   101852       +2     
============================================
  Hits          62646    62646              
- Misses        39204    39206       +2     
Files Coverage Δ
app/middlewares/CORSMiddleware.php 0.00% <0.00%> (ø)

@RCheesley
Copy link
Sponsor Member

Hi @markusVJH I am trying to reproduce this before testing the PR but I am struggling - neither locally with DDEV or on Gitpod can I see those errors.

screenshot-mautic-mautic-zire6je2qpg ws-eu110 gitpod io-2024 04 25-17_19_34

I see plenty of other deprecation warnings in my DDEV logs, but not this one.

Any chance it was fixed somewhere else?

@RCheesley RCheesley added the pending-feedback PR's and issues that are awaiting feedback from the author label Apr 25, 2024
@markusVJH
Copy link
Contributor Author

Hi @RCheesley ! Sorry the late reply, I was away :s

We had this error showing up in the logs after a new Mautic installation for production. I was asked if I wanted to try to make a pr that would fix it from happening.

I struggled to repeat the error locally, but I added that testing guidance where $origin can be forced to be null in order test the errors coming up like we had in the production, and then test how the fix would remove them even if $origin were to be null. Is this ok?

@RCheesley
Copy link
Sponsor Member

If @escopecz thinks it's a good addition then it's fine with me - I just wasn't able to do the user testing as I couldn't reproduce the bug in the first place ;)

Copy link
Sponsor Member

@escopecz escopecz left a comment

Choose a reason for hiding this comment

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

This cannot hurt anything. It happens only if the Origin header in the request is missing.

My previous suggestion to change return false to return null was wrong as it's in a callback function, not the return of the method itself. But null is evaluated by PHP as false so it has no functional effect. So let's keep it as is.

@escopecz escopecz removed pending-feedback PR's and issues that are awaiting feedback from the author pending-test-confirmation PR's that require one test before they can be merged labels May 2, 2024
@escopecz escopecz added this to the 5.1.0 milestone May 2, 2024
@escopecz escopecz merged commit 771c9a7 into mautic:5.x May 2, 2024
15 of 16 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Issues or PR's relating to bugs code-review-passed PRs which have passed code review T1 Low difficulty to fix (issue) or test (PR) tracking Anything related to tracking
Projects
Status: 🥳 Done
Development

Successfully merging this pull request may close these issues.

PHP Deprecation - fnmatch(): Passing null to parameter #2 ($filename) of type string is deprecated
3 participants