Skip to content

[Process] ContextErrorException | Array to string conversion #25511

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
nanofelis opened this issue Dec 15, 2017 · 9 comments
Closed

[Process] ContextErrorException | Array to string conversion #25511

nanofelis opened this issue Dec 15, 2017 · 9 comments

Comments

@nanofelis
Copy link

Q A
Bug report? yes
Feature request? no
BC Break report? yes
RFC? no
Symfony version 3.4.2

Environnement variables passed to proc_open() in https://github.com/symfony/symfony/blob/v3.4.2/src/Symfony/Component/Process/Process.php#L334 throw a ContextErrorException "Array to string conversion"

The reason seems to be that env vars are now always inherited v3.4.1...v3.4.2#diff-f9f2411040cda7b73402481facf3e4dd
but if a query string is present in the url then $env contains argv as an array

[
 "CONTENT_LENGTH" => "8610"
 "CONTENT_TYPE" => "application/x-www-form-urlencoded"
 "REQUEST_METHOD" => "POST"
 "QUERY_STRING" => "uniqid=s5a339da72130a"
 "argv" => [
          0 => "uniqid=s5a339da72130a" 
   ]
]

and apparently proc_open() does not expect a multi dimensionnal array for this parameter.

@nicolas-grekas
Copy link
Member

How can that happen?
non-strings are filtered in https://github.com/symfony/symfony/blob/v3.4.2/src/Symfony/Component/Process/Process.php#L1723
Can you provide a reproducer?

@nanofelis
Copy link
Author

Apparently the filter only applies to values fetched from $_ENV but the argv array is already present in $env from the line $env = getenv(); line. Yes, I'll provide a reproducer today.

@nanofelis
Copy link
Author

Ok after some enquiry it seems we have a weird server behavior on our side that makes getenv() always include argc and argv. It has nothing to do with Symfony, sorry about that !

@Toyinster
Copy link

This issue just cropped up over here, same as OP, getenv() is including the argv as an array. and it's causing a Array to string exception.

@nicolas-grekas There isn't any validation on what getenv() returns. If you are on a high enough PHP version.

Reverting back to v3.4.1 fixes the issue for me.

@mamoot64
Copy link

Hi,

I have the same problem than @nanofelis, the getDefaultEnv() method preserves the "argc" & "argv" parameters from $env = getenv();

For me, the trick is as follow (radical) :

private function getDefaultEnv()
{
    $env = getenv();
    foreach ($_ENV as $k => $v) {
        if (is_string($v)) {
            $env[$k] = $v;
        } else {
            unset($env[$k]);
        }
    }
    return $env;
}

What is the "good way" to avoid this problem ?

Nicolas

@LavaToaster
Copy link

Alrighty then. This is a bit weird.

If a script accesses argv from the $_SERVER variable at any point in the script REGARDLESS of if it is called, PHP will make it available in the getenv output. HOWEVER, If you have xdebug installed, it won't affect you.

In my case this is actually caused because of a composer file include in psy/psysh , https://github.com/bobthecow/psysh/blob/master/src/Psy/functions.php#L295

@nicolas-grekas

Here's smallest (well, not codegolf small :P) piece of code to reproduce it:

<?php

var_dump(getenv());

function notcalled()
{
    $_SERVER['argv'];
}

@nicolas-grekas
Copy link
Member

Thanks for the hints. Fixed by #25559

@stof
Copy link
Member

stof commented Dec 20, 2017

this should probably be reported as a bug to PHP though

nicolas-grekas added a commit that referenced this issue Dec 20, 2017
…roduce subtle breaks accros PHP versions (nicolas-grekas)

This PR was merged into the 3.3 branch.

Discussion
----------

[Process] Dont use getenv(), it returns arrays and can introduce subtle breaks accros PHP versions

| Q             | A
| ------------- | ---
| Branch?       | 3.3
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #25511
| License       | MIT
| Doc PR        | -

Commits
-------

0d4bce6 [Process] Dont use getenv(), it returns arrays and can introduce subtle breaks accros PHP versions
@LavaToaster
Copy link

Following @stof's comment I filed this https://bugs.php.net/bug.php?id=75712

Thanks for the fix @nicolas-grekas, do you know when it will be tagged?

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

8 participants