Page MenuHomePhabricator

PHP Warning: substr() expects parameter 1 to be string, array given
Closed, ResolvedPublicPRODUCTION ERROR

Description

Error
normalized_message
[{reqId}] {exception_url}   PHP Warning: substr() expects parameter 1 to be string, array given
exception.trace
from /srv/mediawiki/php-1.40.0-wmf.18/includes/search/SearchEngine.php(430)
#0 [internal function]: MWExceptionHandler::handleError(integer, string, string, integer, array)
#1 /srv/mediawiki/php-1.40.0-wmf.18/includes/search/SearchEngine.php(430): substr(array, integer, NULL)
#2 /srv/mediawiki/php-1.40.0-wmf.18/includes/search/SearchEngine.php(537): SearchEngine::parseNamespacePrefixes(array, boolean, boolean)
#3 /srv/mediawiki/php-1.40.0-wmf.18/includes/search/SearchEngine.php(600): SearchEngine->normalizeNamespaces(array)
#4 /srv/mediawiki/php-1.40.0-wmf.18/extensions/CirrusSearch/includes/CirrusSearch.php(466): SearchEngine->completionSearch(array)
#5 /srv/mediawiki/php-1.40.0-wmf.18/includes/Rest/Handler/SearchHandler.php(193): CirrusSearch\CirrusSearch->completionSearchWithVariants(array)
#6 /srv/mediawiki/php-1.40.0-wmf.18/includes/Rest/Handler/SearchHandler.php(387): MediaWiki\Rest\Handler\SearchHandler->doSearch(CirrusSearch\CirrusSearch)
#7 /srv/mediawiki/php-1.40.0-wmf.18/includes/Rest/Router.php(487): MediaWiki\Rest\Handler\SearchHandler->execute()
#8 /srv/mediawiki/php-1.40.0-wmf.18/includes/Rest/Router.php(406): MediaWiki\Rest\Router->executeHandler(MediaWiki\Rest\Handler\SearchHandler)
#9 /srv/mediawiki/php-1.40.0-wmf.18/includes/Rest/EntryPoint.php(191): MediaWiki\Rest\Router->execute(MediaWiki\Rest\RequestFromGlobals)
#10 /srv/mediawiki/php-1.40.0-wmf.18/includes/Rest/EntryPoint.php(131): MediaWiki\Rest\EntryPoint->execute()
#11 /srv/mediawiki/php-1.40.0-wmf.18/rest.php(31): MediaWiki\Rest\EntryPoint::main()
#12 /srv/mediawiki/w/rest.php(3): require(string)
#13 {main}
Impact
Notes

Probably related to T326763: PHP Warning: strpos() expects parameter 1 to be string, array given

Details

Request URL
https://www.mediawiki.org/w/rest.php/v1/search/title?limit=*&q%5B0%5D=*&q%5B1%5D=*

Event Timeline

dcausse added a subscriber: dcausse.

I think that the REST handler accepts array parameters 'param[0]=value1&param[1]=value2' and then is passing an array all over the place while a string is expected. The action API does remove all these array params before doing anything else:

if ( $ret === null ) {
        if ( $this->getRequest()->getArray( $name ) !== null ) {
                // See T12262 for why we don't just implode( '|', ... ) the
                // array.
                $this->addWarning( [ 'apiwarn-unsupportedarray', $name ] );
        }
        $ret = $default;
}

We could hack \Wikimedia\ParamValidator\ParamValidator to detect such parameters but I feel that there're some missing sanity checks in the MW REST subsystem.

Untagging search as I don't think this is something specific to search.

Yea: https://en.wikipedia.org/w/rest.php/v1/search/page?q[]=ouch. We could fix this in place by adding something line if ( is_array( $query ) ) { $query = reset( $query ); }. But there are some more places that might all have the same problem: https://codesearch.wmcloud.org/search/?q=getValidatedParams%5C(%5C)%5C%5B&files=%5C.php%24.

thcipriani added a subscriber: thcipriani.
Error
labels.normalized_message
[{reqId}] {exception_url}   PHP Warning: strpos() expects parameter 1 to be string, array given
error.stack_trace
from /srv/mediawiki/php-1.41.0-wmf.3/includes/search/SearchEngine.php(406)
#0 [internal function]: MWExceptionHandler::handleError(integer, string, string, integer, array)
#1 /srv/mediawiki/php-1.41.0-wmf.3/includes/search/SearchEngine.php(406): strpos(array, string)
#2 /srv/mediawiki/php-1.41.0-wmf.3/includes/search/SearchEngine.php(538): SearchEngine::parseNamespacePrefixes(array, boolean, boolean)
#3 /srv/mediawiki/php-1.41.0-wmf.3/includes/search/SearchEngine.php(601): SearchEngine->normalizeNamespaces(array)
#4 /srv/mediawiki/php-1.41.0-wmf.3/extensions/CirrusSearch/includes/CirrusSearch.php(466): SearchEngine->completionSearch(array)
#5 /srv/mediawiki/php-1.41.0-wmf.3/includes/Rest/Handler/SearchHandler.php(193): CirrusSearch\CirrusSearch->completionSearchWithVariants(array)
#6 /srv/mediawiki/php-1.41.0-wmf.3/includes/Rest/Handler/SearchHandler.php(387): MediaWiki\Rest\Handler\SearchHandler->doSearch(CirrusSearch\CirrusSearch)
#7 /srv/mediawiki/php-1.41.0-wmf.3/includes/Rest/Router.php(517): MediaWiki\Rest\Handler\SearchHandler->execute()
#8 /srv/mediawiki/php-1.41.0-wmf.3/includes/Rest/Router.php(422): MediaWiki\Rest\Router->executeHandler(MediaWiki\Rest\Handler\SearchHandler)
#9 /srv/mediawiki/php-1.41.0-wmf.3/includes/Rest/EntryPoint.php(195): MediaWiki\Rest\Router->execute(MediaWiki\Rest\RequestFromGlobals)
#10 /srv/mediawiki/php-1.41.0-wmf.3/includes/Rest/EntryPoint.php(135): MediaWiki\Rest\EntryPoint->execute()
#11 /srv/mediawiki/php-1.41.0-wmf.3/rest.php(31): MediaWiki\Rest\EntryPoint::main()
#12 /srv/mediawiki/w/rest.php(3): require(string)
#13 {main}
Notes

Noticed this this morning during log triage. Causes a cascade of errors for a small number of requests.

Pretty easy to recreate for testing: https://en.wikipedia.org/w/rest.php/v1/search/title?limit=1&q[0]=hi

One request triggers warnings about 2×strpos, strlen, mb_strlen, and 2×substr—six log messages per request.

Change 921762 had a related patch set uploaded (by D3r1ck01; author: Derick Alangi):

[mediawiki/core@master] ParamValidator: Validate and normalize `q` param to string

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

Test wiki created on Patch demo by X-Savitar using patch(es) linked to this task:
https://patchdemo.wmflabs.org/wikis/17bb8aaba1/wiki/Main Page

Tested on PD and it seems to work as expected.

Change 922119 had a related patch set uploaded (by D3r1ck01; author: Derick Alangi):

[mediawiki/core@master] ParamValidator: Throw exception if there is a type to value mismatch

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

Change 921762 abandoned by D3r1ck01:

[mediawiki/core@master] ParamValidator: Validate and normalize `q` param to string

Reason:

Going with the exception route: I507e5c4725b63d652771517ecd127da81082db31

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

Change 922119 merged by jenkins-bot:

[mediawiki/core@master] ParamValidator: Throw exception if there is a type to value mismatch

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

xSavitar claimed this task.

This should be fine now. Please reopen if error still occurs.