Skip to content

Cannot use object of type Symfony\Component\VarDumper\Cloner\Stub as array #39679

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
JoyceBabu opened this issue Jan 1, 2021 · 3 comments
Closed

Comments

@JoyceBabu
Copy link

Symfony version(s) affected: 5.1.0
Description

VarCloner::cloneVar fails when cloning $GLOBALS, if $GLOBALS is used in spl_autoload_register callback.

This happens because VarCloner::cloneVar replaces $GLOBALS with an instance of Symfony\Component\VarDumper\Cloner\Stub. $GLOBALS is returned to the original array, once the method execution ends.

Normally, this is not an issue, because third-party code does not execute between this period. But if $GLOBALS is used in an autoload handler, the the handler may be invoked before $GLOBALS can be reset to its original value.

How to reproduce

Example 1: Minimal Reproducible Example

<?php

use Symfony\Component\VarDumper\Caster\Caster;
use Symfony\Component\VarDumper\Cloner\VarCloner;

include __DIR__ . '/../vendor/autoload.php';

spl_autoload_register(function ($class) {
    if (isset($GLOBALS['SOME_GLOBAL_VARIABLE'])) {}

    return false;
}, false, true);

$cloner = new VarCloner();
$cloneVar = $cloner->cloneVar($GLOBALS, Caster::EXCLUDE_VERBOSE);

Example 2

I am getting the error with Whoops error handler, which uses symfony var dumper.

<?php

use Whoops\Handler\PrettyPageHandler;
use Whoops\Run;

include __DIR__ . '/../vendor/autoload.php';


spl_autoload_register(function ($class) {
    if (empty($GLOBALS['__composer_autoload_files'])) {

    }
}, false, true);


$whoops = new Run();
$whoops->pushHandler(new PrettyPageHandler());
$whoops->register();

try {
    $db = new \PDO('mysql:dbname=testdb;host=127.0.0.1');
} catch (\Exception $e) {

}

// Trigger Whoops Handler
echo $undefined_variable;

Possible Solution

Additional context

@derrabus
Copy link
Member

derrabus commented Jan 2, 2021

Symfony 5.1.0 is fairly old. Does this still happen with 5.1.10 or 5.2.1?

@JoyceBabu
Copy link
Author

@derrabus I was able to replicate the issue with both 5.1.10 and 5.2.1.

@nicolas-grekas
Copy link
Member

Fixed in #39738

nicolas-grekas added a commit that referenced this issue Jan 7, 2021
…s-grekas)

This PR was merged into the 4.4 branch.

Discussion
----------

[VarDumper] fix mutating $GLOBALS while cloning it

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

Also preparing for https://wiki.php.net/rfc/restrict_globals_usage

Commits
-------

384b0ad [VarDumper] fix mutating $GLOBALS while cloning it
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

5 participants