Page MenuHomePhabricator

maintenance/shell.php is broken if there is a key in globals which is not a valid variable name (e.g. $GLOBALS['foo.bar'])
Closed, ResolvedPublic

Description

XXX (master)$ php maintenance/shell.php 
Psy Shell v0.9.6 (PHP 7.1.23-2+ubuntu16.04.1+deb.sury.org+1 — cli) by Justin Hileman
>>> echo "foo";
PHP Error:  syntax error, unexpected '.', expecting ',' or ';' in /XXXeval()'d code on line 1
>>>

eval.php works as normal.

Event Timeline

Today, I get this on Psy Shell v0.9.9 and PHP 7.3.1 cli;

:>mw-core (development-core)$ php maintenance/shell.php
Psy Shell v0.9.9 (PHP 7.3.1 — cli) by Justin Hileman
>>> echo "foo";
foo⏎
>>>

So it seems this only happens for Psy Shell v0.9.6? Maybe some other person can confirm!

Wikimedia production had 9.6 until a month ago, it worked fine.

You can get more info on the error with the wtf command.

Wikimedia production had 9.6 until a month ago, it worked fine.

I can't access production!

twn:/srv/mediawiki/workdir (master)$ php maintenance/shell.php 
Psy Shell v0.10.4 (PHP 7.3.19-1~deb10u1 — cli) by Justin Hileman
>>> echo "foo";
PHP Error:  syntax error, unexpected '.', expecting ',' or ';' in Psy Shell code on line 1
>>>

I have the same PHP (7.3.19 from Debian Buster), with the current master, no extension and it works for me:

$ php maintenance/shell.php 
Psy Shell v0.10.4 (PHP 7.3.19-1~deb10u1 — cli) by Justin Hileman
>>> echo "foo";
foo⏎

If you want to rule out installed PHP extensions, you can try ignoring php.ini and only load the required extensions:

php -n -d extension=mbstring.so -d extension=xml.so -d extension=ctype.so -d extension=json.so -d extension=iconv.so -d extension=fileinfo.so -d extension=tokenizer.so maintenance/shell.php
twn:/srv/mediawiki/workdir (master)$ php -n -d extension=mbstring.so -d extension=xml.so -d extension=ctype.so -d extension=json.so -d extension=iconv.so -d extension=fileinfo.so -d extension=tokenizer.so maintenance/shell.php
twn:/srv/mediawiki/workdir (master)$ less /www/translatewiki.net/logs/error_php
twn:/srv/mediawiki/workdir (master)$ php -n -d extension=mbstring.so -d extension=xml.so -d extension=ctype.so -d extension=json.so -d extension=iconv.so -d extension=fileinfo.so -d extension=tokenizer.so -d extension=curl.so maintenance/shell.php
[29a4693a81a26634d0227b40] [no req]   Error from line 106 of /srv/mediawiki/workdir/maintenance/doMaintenance.php: Call to undefined function posix_getpwuid()
Backtrace:
#0 /srv/mediawiki/workdir/maintenance/shell.php(104): require_once()
#1 {main}
twn:/srv/mediawiki/workdir (master)$ php -n -d extension=mbstring.so -d extension=xml.so -d extension=ctype.so -d extension=json.so -d extension=iconv.so -d extension=fileinfo.so -d extension=tokenizer.so -d extension=curl.so -d extension=posix.so maintenance/shell.php
Psy Shell v0.10.4 (PHP 7.3.19-1~deb10u1 — cli) by Justin Hileman
>>> echo "foo";
PHP Error:  syntax error, unexpected '.', expecting ',' or ';' in Psy Shell code on line 1
>>>

Is that specific to special syntax like echo/print (which is mostly pointless in a REPL shell, FWIW)? What is the value of $_e?

Is that specific to special syntax like echo/print (which is mostly pointless in a REPL shell, FWIW)? What is the value of $_e?

>>> echo $_e;
PHP Error:  syntax error, unexpected '.', expecting ',' or ';' in Psy Shell code on line 1
>>> var_dump( 'a' );
PHP Error:  syntax error, unexpected '.', expecting ',' or ';' in Psy Shell code on line 1
>>> $_e 
PHP Error:  syntax error, unexpected '.', expecting ',' or ';' in Psy Shell code on line 1
>>> $_e;
PHP Error:  syntax error, unexpected '.', expecting ',' or ';' in Psy Shell code on line 1
>>>

<s>Interesting behavior for the first one...</s> Had previous input there...ignore.

This is reproducible with https://gerrit.wikimedia.org/r/plugins/gitiles/translatewiki/+/refs/heads/master/puppet/README

Commenting out $config->setCodeCleaner( $codeCleaner ); makes the problem go away, it seems.

This is because CodeCleanerGlobalPass inserts global statements without much filtering. SMW creates a global with a period in its name, so the PHP parser fails.

This is a list of non-configuration variables that get inserted into the current scope:

CONF
DOMAIN
EXT
GROUPS
IP
LOGS
_REQUEST
__composer_autoload_files
argc
argv
lqtParams
maintClass
maintenance
privESService
self
smw.json
Nikerabbit renamed this task from maintenance/shell.php is broken to maintenance/shell.php is broken if there is a variable in globals which is not a valid variable name (e.g. $GLOBALS['foo.bar']).Dec 4 2020, 1:04 PM
Nikerabbit renamed this task from maintenance/shell.php is broken if there is a variable in globals which is not a valid variable name (e.g. $GLOBALS['foo.bar']) to maintenance/shell.php is broken if there is a key in globals which is not a valid variable name (e.g. $GLOBALS['foo.bar']).Dec 4 2020, 1:08 PM

Thanks! Setting a global with a dot in the name indeed reproduces this:

$ mwscript shell.php
Psy Shell v0.10.4 (PHP 7.2.31-1+0~20200514.41+debian9~1.gbpe2a56b+wmf1 — cli) by Justin Hileman
>>> $GLOBALS['a.b'] = 1
=> 1
>>> 1
PHP Error:  syntax error, unexpected '.', expecting ',' or ';' in Psy Shell code on line 1

CodeCleanerGlobalPass is a somewhat awkward attempt to emulate global scope. Given MediaWiki's heavy reliance on configuration globals, that's valuable IMO.

Change 645418 had a related patch set uploaded (by Gergő Tisza; owner: Gergő Tisza):
[mediawiki/core@master] Filter out invalid globals in shell.php global hack

https://gerrit.wikimedia.org/r/645418

Change 645418 merged by jenkins-bot:
[mediawiki/core@master] Filter out invalid globals in shell.php global hack

https://gerrit.wikimedia.org/r/645418

Tgr claimed this task.

QA: Confirmed on translatewiki.net