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

Why phar stream is being unregistered? #21973

Closed
kassner opened this issue Mar 27, 2019 · 10 comments
Closed

Why phar stream is being unregistered? #21973

kassner opened this issue Mar 27, 2019 · 10 comments
Assignees
Labels
Fixed in 2.3.x The issue has been fixed in 2.3 release line Issue: Format is valid Gate 1 Passed. Automatic verification of issue format passed

Comments

@kassner
Copy link
Contributor

kassner commented Mar 27, 2019

Summary

The 2.3.1 release introduced the line stream_wrapper_unregister('phar'); in the app/boostrap.php. It's unclear why it was added, but it breaks a few external tools, like PHPStan.

Examples

/var/www/magento $ phpstan analyse --autoload-file=app/bootstrap.php -c dev/tools/phpstan/config.neon --level=max app/code/
PHP Warning:  include(): Unable to find the wrapper "phar" - did you forget to enable it when you configured PHP? in /var/www/magento/vendor/composer/ClassLoader.php on line 444

Warning: include(): Unable to find the wrapper "phar" - did you forget to enable it when you configured PHP? in /var/www/magento/vendor/composer/ClassLoader.php on line 444
PHP Warning:  include(): Unable to find the wrapper "phar" - did you forget to enable it when you configured PHP? in /var/www/magento/vendor/composer/ClassLoader.php on line 444

Warning: include(): Unable to find the wrapper "phar" - did you forget to enable it when you configured PHP? in /var/www/magento/vendor/composer/ClassLoader.php on line 444
PHP Warning:  include(phar:///usr/local/bin/phpstan/vendor/composer/../../src/DependencyInjection/ContainerFactory.php): failed to open stream: No such file or directory in /var/www/magento/vendor/composer/ClassLoader.php on line 444

Warning: include(phar:///usr/local/bin/phpstan/vendor/composer/../../src/DependencyInjection/ContainerFactory.php): failed to open stream: No such file or directory in /var/www/magento/vendor/composer/ClassLoader.php on line 444
PHP Warning:  include(): Failed opening 'phar:///usr/local/bin/phpstan/vendor/composer/../../src/DependencyInjection/ContainerFactory.php' for inclusion (include_path='/var/www/magento/generated/code:/var/www/magento/generated/code:/var/www/magento/vendor/magento/zendframework1/library:.:') in /var/www/magento/vendor/composer/ClassLoader.php on line 444

Warning: include(): Failed opening 'phar:///usr/local/bin/phpstan/vendor/composer/../../src/DependencyInjection/ContainerFactory.php' for inclusion (include_path='/var/www/magento/generated/code:/var/www/magento/generated/code:/var/www/magento/vendor/magento/zendframework1/library:.:') in /var/www/magento/vendor/composer/ClassLoader.php on line 444
string(44) "PHPStan\DependencyInjection\ContainerFactory"
PHP Fatal error:  Uncaught Error: Class 'PHPStan\DependencyInjection\ContainerFactory' not found in phar:///usr/local/bin/phpstan/src/Command/CommandHelper.php:73
Stack trace:
#0 phar:///usr/local/bin/phpstan/src/Command/AnalyseCommand.php(46): PHPStan\Command\CommandHelper::begin(Object(_HumbugBox3fccee4f38a4\Symfony\Component\Console\Input\ArgvInput), Object(_HumbugBox3fccee4f38a4\Symfony\Component\Console\Output\ConsoleOutput), Array, NULL, NULL, '/var/www/magent...', 'dev/tools/phpst...', 'max')
#1 phar:///usr/local/bin/phpstan/vendor/symfony/console/Command/Command.php(228): PHPStan\Command\AnalyseCommand->execute(Object(_HumbugBox3fccee4f38a4\Symfony\Component\Console\Input\ArgvInput), Object(_HumbugBox3fccee4f38a4\Symfony\Component\Console\Output\ConsoleOutput))
#2 phar:///usr/local/bin/phpstan/vendor/symfony/console/Application.php(831): _HumbugBox3fccee4f38a4\Symfony\Component\Console\Command\Command->run(Object(_HumbugBox3fccee4f38a4\Symfony\Component\Console\Input\ArgvInput), Object(_HumbugBox3fccee4f38a4\Symfony\Co in phar:///usr/local/bin/phpstan/src/Command/CommandHelper.php on line 73

Fatal error: Uncaught Error: Class 'PHPStan\DependencyInjection\ContainerFactory' not found in phar:///usr/local/bin/phpstan/src/Command/CommandHelper.php:73
Stack trace:
#0 phar:///usr/local/bin/phpstan/src/Command/AnalyseCommand.php(46): PHPStan\Command\CommandHelper::begin(Object(_HumbugBox3fccee4f38a4\Symfony\Component\Console\Input\ArgvInput), Object(_HumbugBox3fccee4f38a4\Symfony\Component\Console\Output\ConsoleOutput), Array, NULL, NULL, '/var/www/magent...', 'dev/tools/phpst...', 'max')
#1 phar:///usr/local/bin/phpstan/vendor/symfony/console/Command/Command.php(228): PHPStan\Command\AnalyseCommand->execute(Object(_HumbugBox3fccee4f38a4\Symfony\Component\Console\Input\ArgvInput), Object(_HumbugBox3fccee4f38a4\Symfony\Component\Console\Output\ConsoleOutput))
#2 phar:///usr/local/bin/phpstan/vendor/symfony/console/Application.php(831): _HumbugBox3fccee4f38a4\Symfony\Component\Console\Command\Command->run(Object(_HumbugBox3fccee4f38a4\Symfony\Component\Console\Input\ArgvInput), Object(_HumbugBox3fccee4f38a4\Symfony\Co in phar:///usr/local/bin/phpstan/src/Command/CommandHelper.php on line 73

And removing the aforementioned line:

/var/www/magento $ phpstan analyse --autoload-file=app/bootstrap.php -c dev/tools/phpstan/config.neon --level=max app/code/
 203/203 [▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓] 100%

                                                                                                                        
 [OK] No errors                                                                                                         
                                                                                                                        

/var/www/magento $ 

Proposed solution

Ideally, we wouldn't have this line at all in the code, but someone added it there for some reason. I couldn't find any context about it why it's there, it's not mentioned in the Release Notes for the 2.3.1 version, neither the commit that introduced it gives any context about it.

Can we either have it removed or have an explanation about why we can't?

Thanks!

@magento-engcom-team magento-engcom-team added the Issue: Format is valid Gate 1 Passed. Automatic verification of issue format passed label Mar 27, 2019
@magento-engcom-team
Copy link
Contributor

Hi @kassner. Thank you for your report.
To help us process this issue please make sure that you provided the following information:

  • Summary of the issue
  • Information on your environment
  • Steps to reproduce
  • Expected and actual results

Please make sure that the issue is reproducible on the vanilla Magento instance following Steps to reproduce. To deploy vanilla Magento instance on our environment, please, add a comment to the issue:

@magento-engcom-team give me 2.3-develop instance - upcoming 2.3.x release

For more details, please, review the Magento Contributor Assistant documentation.

@kassner do you confirm that you was able to reproduce the issue on vanilla Magento instance following steps to reproduce?

  • yes
  • no

@kierenevans
Copy link

This was likely added due to PRODSECBUG-2261: Arbitrary code execution due to unsafe deserialization of a PHP archive - check https://magento.com/security/patches/magento-2.3.1-2.2.8-and-2.1.17-security-update

@ghost ghost self-assigned this Apr 1, 2019
@m2-assistant
Copy link

m2-assistant bot commented Apr 1, 2019

Hi @engcom-backlog-nazar. Thank you for working on this issue.
In order to make sure that issue has enough information and ready for development, please read and check the following instruction: 👇

  • 1. Verify that issue has all the required information. (Preconditions, Steps to reproduce, Expected result, Actual result).

    DetailsIf the issue has a valid description, the label Issue: Format is valid will be added to the issue automatically. Please, edit issue description if needed, until label Issue: Format is valid appears.

  • 2. Verify that issue has a meaningful description and provides enough information to reproduce the issue. If the report is valid, add Issue: Clear Description label to the issue by yourself.

  • 3. Add Component: XXXXX label(s) to the ticket, indicating the components it may be related to.

  • 4. Verify that the issue is reproducible on 2.3-develop branch

    Details- Add the comment @magento-engcom-team give me 2.3-develop instance to deploy test instance on Magento infrastructure.
    - If the issue is reproducible on 2.3-develop branch, please, add the label Reproduced on 2.3.x.
    - If the issue is not reproducible, add your comment that issue is not reproducible and close the issue and stop verification process here!

  • 5. Verify that the issue is reproducible on 2.2-develop branch.

    Details- Add the comment @magento-engcom-team give me 2.2-develop instance to deploy test instance on Magento infrastructure.
    - If the issue is reproducible on 2.2-develop branch, please add the label Reproduced on 2.2.x

  • 6. Add label Issue: Confirmed once verification is complete.

  • 7. Make sure that automatic system confirms that report has been added to the backlog.

@ghost
Copy link

ghost commented Apr 1, 2019

HI @kassner thank you for you report, I'm closing this one as this is resolved, if not, feel free to reopen ticket, thanks for collaboration.

@ghost ghost closed this as completed Apr 1, 2019
@lhall-amphibee
Copy link

Hi there,

We just ran into this issue and had to comment out the added line of code, which is ... not ideal. We are running a 2.3.1 Community Magento.

@engcom-backlog-nazar I don't see where it's been fixed, did I miss something ? I looked into 2.3-develop but it's untouched from what I can tell.

Thanks for your support.

@orlangur orlangur reopened this Apr 5, 2019
@ghost ghost assigned adaudenthun and unassigned ghost Apr 5, 2019
@magento-engcom-team magento-engcom-team added the Fixed in 2.3.x The issue has been fixed in 2.3 release line label Apr 15, 2019
@magento-engcom-team
Copy link
Contributor

Hi @kassner. Thank you for your report.
The issue has been fixed in #22171 by @adaudenthun in 2.3-develop branch
Related commit(s):

The fix will be available with the upcoming 2.3.2 release.

@orlangur
Copy link
Contributor

Warning is eliminated by mentioned PR but I doubt that

Warning: include(): Failed opening 'phar:///usr/local/bin/phpstan/vendor/composer/../../src/DependencyInjection/ContainerFactory.php' for inclusion (include_path='/var/www/magento/generated/code:/var/www/magento/generated/code:/var/www/magento/vendor/magento/zendframework1/library:.:') in /var/www/magento/vendor/composer/ClassLoader.php on line 444
string(44) "PHPStan\DependencyInjection\ContainerFactory"

is now solved.

@kassner why did you include bootstrap instead of app/autoload.php?

@kassner
Copy link
Contributor Author

kassner commented Apr 16, 2019

@orlangur I use it because I'm loading the ObjectManager, mainly to validate my code against the generated factories/interceptors/etc, which gives me more safety and avoids ignoring tons of errors on phpstan, at the expense of a slower run.

But, I fixed that with custom code based on this. Now I rely on app/autoload.php instead, and this is my autoloader for phpstan:

<?php

/* Find autoload path */
$rootPath = realpath(dirname(__FILE__));
while (!file_exists($rootPath . '/app/autoload.php') || $rootPath == '/') {
    $rootPath = realpath(dirname($rootPath));
}

/* Include Magento autoload file */
require_once $rootPath . '/app/autoload.php';

$loadedClasses = [];
$bootstrap = \Magento\Framework\App\Bootstrap::create(BP, $_SERVER);
$objectManager = $bootstrap->getObjectManager();

spl_autoload_register(function ($class) use ($objectManager, $loadedClasses) {
    if (isset($loadedClasses[$class])) {
        return $loadedClasses[$class];
    }

    try {
        $objectManager->get($class);
        $loadedClasses[$class] = true;
    } catch (\Throwable $e) {
        $loadedClasses[$class] = false;
    }

    return $loadedClasses[$class];
});

And my config.neon is quite generic:

parameters:
    reportUnmatchedIgnoredErrors: false
    ignoreErrors:
        - message: '#^Call to an undefined method [a-zA-Z0-9\\_]+::(get|set|uns|has)([A-Za-z0-9_]+)\(\)\.$#'
          path: '*.php'
        - message: '#Using \$this outside a class\.#'
          path: '*.phtml'
        - message: '#^Undefined variable: \$(block|this)$#'
          path: '*.phtml'
    fileExtensions:
        - php
        - phtml
    excludes_analyse:
        - */*/Test/*

@kassner
Copy link
Contributor Author

kassner commented Jan 25, 2021

I bumped into my own ticket trying to run phpunit via a phar this time, and although I can understand the rationale of not having PHAR enabled in production, I believe this should be a responsability of the environment, not Magento's own code. It's feasible (and even safer) to disable PHAR via php.ini instead.

Alternatively, I managed to run the integration tests without much fuss once I applied this patch:

--- dev/tests/integration/framework/bootstrap.php
+++ dev/tests/integration/framework/bootstrap.php
@@ -13,6 +13,11 @@
 require_once __DIR__ . '/../../../../app/bootstrap.php';
 require_once __DIR__ . '/autoload.php';
 
+// app/bootstrap.php disables phar, but we're load phpunit via phive
+if (!in_array('phar', \stream_get_wrappers())) {
+    \stream_wrapper_restore('phar');
+}
+
 // phpcs:ignore Magento2.Functions.DiscouragedFunction
 $testsBaseDir = dirname(__DIR__);
 $fixtureBaseDir = $testsBaseDir. '/testsuite';

@kolaente
Copy link
Contributor

I've managed to fix this by adding a file phpstan_bootstrap.php in my root directory with this content:

<?php

// See https://github.com/magento/magento2/issues/21973

// Magento 2.3.1 removes phar stream wrapper.
if (!in_array('phar', \stream_get_wrappers())) {
    \stream_wrapper_restore('phar');
}

then I've added it to the bootstrapFiles section of the phpstan.neon configuration so it is loaded:

    bootstrapFiles:
        - %rootDir%/../../../app/bootstrap.php
        - %rootDir%/../../../phpstan_bootstrap.php

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Fixed in 2.3.x The issue has been fixed in 2.3 release line Issue: Format is valid Gate 1 Passed. Automatic verification of issue format passed
Projects
None yet
Development

No branches or pull requests

7 participants