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

Uncaught TypeError: Return value of Zend\\Diactoros\\Uri::filterQueryOrFragment() must be of the type string, null returned #357

Closed
YellowPepper opened this issue Apr 3, 2019 · 7 comments · Fixed by #372

Comments

@YellowPepper
Copy link

commented Apr 3, 2019

PHP Fatal error:  Uncaught TypeError: Return value of Zend\\Diactoros\\Uri::filterQueryOrFragment() must be of the type string, null returned in /vendor/zendframework/zend-diactoros/src/Uri.php:660\n
Stack trace:\n
#0 /vendor/zendframework/zend-diactoros/src/Uri.php(618): Zend\\Diactoros\\Uri->filterQueryOrFragment('\\xCF')\n
#1 /vendor/zendframework/zend-diactoros/src/Uri.php(410): Zend\\Diactoros\\Uri->filterQuery('Keyword=\\xCF')\n
#2 /vendor/zendframework/zend-diactoros/src/functions/marshal_uri_from_sapi.php(218): Zend\\Diactoros\\Uri->withQuery('Keyword=\\xCF3')\n

Version
zend-diactoros 2.1.1
php 7.2.5

Expected results

TypeError does not occur

@krowinski

This comment has been minimized.

Copy link

commented Sep 27, 2019

+1

it's returning null because of some regexp error can we use preg_last_error to check and throw exception in filterQueryOrFragment ?

@weierophinney

This comment has been minimized.

Copy link
Member

commented Oct 8, 2019

Can you provide example $server and/or $header arrays that demonstrate the issue, please? From there we can write a test, which will help us create a fix and prevent a future regression.

Thanks!

@krowinski

This comment has been minimized.

Copy link

commented Oct 8, 2019

@weierophinney Hi, here you go

<?php
var_dump(
    preg_replace_callback(
        '/(?:[^a-zA-Z0-9_\-\.~\pL!\$&\'\(\)\*\+,;=%:@\/\?]+|%(?![A-Fa-f0-9]{2}))/u',
        static function (array $matches) {return rawurlencode($matches[0]);} ,
        ("\x21\x92")
    ), 
    preg_last_error()
);

http://sandbox.onlinephpfunctions.com/code/9d90fde0fac05f1d26c0f3310fee3e6cf2405b91

@weierophinney

This comment has been minimized.

Copy link
Member

commented Oct 8, 2019

@krowinski I'm not asking for a patch, I'm asking for a reproduce case. I need that so I can write a unit test...

@krowinski

This comment has been minimized.

Copy link

commented Oct 9, 2019

what? this is not a patch its a test how to break preg match to return null, I will fork repo and write test myself

@freyr

This comment has been minimized.

Copy link

commented Oct 9, 2019

This is not even a problem for recreating this particular case. Uri::filterPath() has type checks to return string. And preg_replace_callback that is inside might return null if some errors occurs. There is no checks for that situation and it might happen that Uri::filterPath() will return null - thus breaking contract.

In my local environment diactoros unit tests fails due to this exact problem:
this url http://example.com/тестовый_путь/ fails:

TypeError : Return value of Zend\Diactoros\Uri::filterPath() must be of the type string, null returned

And preg_last_error() return 4 which is PREG_BAD_UTF8_ERROR code.

I'm trying to find which configuration option is doing this, but i suspect that some locale configuration might do that.

However this is not even an issue as there are environments that are running with this configuration and they are affected by this TypeError issue. AFAIK throwing exception or emmiting error will be BC way of fixing that (changing return type to nullable is probably bad idea)

I could attach my php.ini configuration to maybe help recreating this issue?

@webimpress webimpress added this to the 2.1.5 milestone Oct 9, 2019
@krowinski

This comment has been minimized.

Copy link

commented Oct 9, 2019

I added to tests example that will fail preg match here

https://github.com/krowinski/zend-diactoros/commit/81085ad4528bcb25ca505b4f614a824356e02ca7

and travis will fail
https://travis-ci.org/krowinski/zend-diactoros/jobs/595632127

There was 1 error:
1) ZendTest\Diactoros\UriTest::testUtf8Path with data set #2 ('http://example.com/!�', '')
TypeError: Return value of Zend\Diactoros\Uri::filterPath() must be of the type string, null returned
/home/travis/build/krowinski/zend-diactoros/src/Uri.php:590
/home/travis/build/krowinski/zend-diactoros/src/Uri.php:466
/home/travis/build/krowinski/zend-diactoros/src/Uri.php:115
/home/travis/build/krowinski/zend-diactoros/test/UriTest.php:609
@webimpress webimpress self-assigned this Oct 9, 2019
webimpress added a commit to webimpress/zend-diactoros that referenced this issue Oct 9, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
5 participants
You can’t perform that action at this time.