Closed YellowPepper closed 4 years ago
+1
it's returning null because of some regexp error can we use preg_last_error to check and throw exception in filterQueryOrFragment ?
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!
@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
@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...
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
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?
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
Version zend-diactoros 2.1.1 php 7.2.5
Expected results
TypeError does not occur