-
-
Notifications
You must be signed in to change notification settings - Fork 6.9k
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
yii\web\UrlManager::createAbsoluteUrl() creates wrong url in version >= 2.0.50 #20199
Comments
Regression was introduced by commit b46e267, claiming to have Improved BaseUrl::isRelative($url) performance. The ProblemIn the commit, regex The original version 2.0.49 The solution introduced in v 2.0.50 As you can see and test, these two solutions outcome will be different, hence the discrepancy between 2.0.49 and 2.0.50. I've also performed a few speed tests with a 1core 3GB VM on a very old host machine: 9999999 BaseUrl::isRelative() checks on random urls, chosen among 5 different url examples. Every test is run 5 times and averaged. Results in seconds:
The SolutionFrom the tests performed above, if you were to perform the correct regex match you can see that in 10 millions checks, on a crappy hardware (in fact, not even hardware, a virtualisation of it!) you save ... drum roll... about a fifth of a second if you use PHP7 (officially deprecated). If you use PHP8 it actually takes more time than using native PHP functions. But in exchange, using regexes, we can now have a more vulnerable solution, prone to all sort of regex-driven attacks! If you ask me, I would rather avoid to use regexes where the input can be potentially user-made like in this case. Also, regexes performances are not a given, they can result in unpredictable performances depending what you feed them; plus, PCRE world is definitely not new to various injections and bo attacks. My propose is just to revert this line of the commit and bring it back on how it was, that's it. This until even yii2 will bump required php version to 8+, then I would use something similar to the fastest line in the tests above. For the ones curious, a regex example implementing exactly what the original function does can be found here: Hope to have been useful, I'll push a PR with the commit reverted. Live long and prosper, \\//_ |
This looks like an invalid call - I also tested this in Yii 2.0.47 and I get the same results, so I don't think this is regression in |
https://www.yiiframework.com/doc/api/2.0/yii-web-urlmanager#createAbsoluteUrl()-detail -
Because you're using a wrong tool -
Can you share this project? |
Ok, I definitely don't want to step on anyone's toes here. I am just pointing out it might be a documentation miss. If you read at the In version 2.0.50 the only problem is that the isRelative() regex does not do what the php used to in the previous version 2.0.49. The change claimed to be just a performance improvement, but it also changed the behaviour of the function as abundantly documented here. Also, I still think validate end-user strings with regex needs to be taken with a grain of salt and avoided. Sure I can share the project, it's literally a:
with this in basic/commands/HelloController.php:
and this added to basic/config/console.php
I'm not sure I also need to copy-paste commands to switch between versions but here you go: |
…0.49 version due to yii\web\UrlManager::createAbsoluteUrl() malfunction depending on this.
Can you point to specific place? I see I also tested your example and indeed it worked by accident. If you use |
Yes, thank you @rob006 , everything's much clearer now. My bad I drove you down this rabbit hole. All of the issues described in my first comment still persist though and I would add that using regexes to solve performance problems or, even worse, for validating data that may come from end-users, it's just commonly considered a bad idea. If you suggest a different title for this issue, I'm more than happy to change it for you, but this is pretty much it; yesterday I've created a PR to solve this problem in the interest of the community and mine. In case the PR is rejected, I'll post here a guide on how to solve this issue otherwise, still in a proper yii2 compliant way. Hope to have been useful, I'll keep you posted. Live long and prosper, 🖖 |
Do you have an example input for which this regex would be really slow? Not all regular expressions are slow and we already use it to parse user input (for example in some validators or for parsing URLs in UrlManager). This solution was also discussed at #20077 - you can find some benchmarks there. |
…compliant uris. Added couple of cases to also test the more relaxed rfc3986 section 3.4 definitions.
@edegaudenzi I assume you have resolved the issue and that you no longer have any issue with the implementation. |
@mtangoo thanks for your message. Yes I did solve the problem, I've also created and submitted a pull request; but I do still have issues with the regression introduced between 2.0.49 and 2.0.50, still there. I will also create a feature request to allow "createAbsoluteUrl()" to be able to Create Absolute Urls, not just the yii2 ones but any absolute url, as it was per 2.0.49 version. Personally, regression introduced in 2.0.50 meant I needed to patch every project I worked on in the last few years because they suddenly stopped to work, hence time, hence money. Sadly, this also means that soon I'll probably need to look to replace it with another framework. For those having the same problem described by this issue, I'll submit here a little patch you can apply to keep receiving yii2 updates but having absolute URLs still working. |
Unfortunately this issue is still NOT RESOLVED, despite its 'Closed' status, but I'm writing this for the records and to help someone else in the same situation I was. I'll also update this issue as soon as the yii2 maintainers will do their part to officially fix this. Here's how to patch your yii2 < 2.0.50 if you were affected by the yii2 >= 2.0.50 regression described in this issue, it's fairly simple. The whole concept is to say yii2 to use your own Url helper instead of its own; your Url helper will extend the core one and override the incriminated method. More details on customizing-helper-classes
job done, your Yii2 apps can build absolute urls again! Hope to have been helpful to someone else. Live long and prosper 🖖 |
@edegaudenzi have you tested on 2.0.51? |
@samdark Yes, I've just run a couple of tries:
|
@samdark Are you sure that UrlManager should be responsible for processing external URLs? Some kind of wrapper around |
@rob006 the issue is that it was processing it like that before and @edegaudenzi relied on that. |
@samdark He used this method in a wrong way, I explained it in #20199 (comment) and #20199 (comment). This issue really feels like https://xkcd.com/1172/ |
Re-read comments. You're right, @rob006, it was never supposed to accept non-routes. |
What steps will reproduce the problem?
In Yii 2.0.49 following code generates a correct Authorization URL (in this case for Hubspot OAuth2 authentication process, but it doesn't really matter).
On the other hand, in Yii 2.0.50 following code generates a wrong Authorization URL, prepending the host relative path.
It seems to be a regression introduced with 2.0.50. I'll need to investigate deeper but wanted to give an immediate heads up for everyone there that might be affected.
What is the expected result?
What do you get instead?
Additional info
The text was updated successfully, but these errors were encountered: