-
Notifications
You must be signed in to change notification settings - Fork 7.8k
Fix #78939 - Windows relative path with drive letter #5001
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
Fix #78939 - Windows relative path with drive letter #5001
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Well, that doesn't look quite right; C:
is an absolute path, even though C:foo
is not. Since this is rather critical code, it needs to be investigated thoroughly. Also, please have a look at the failing test.
@cmb69 Why do you think
output:
|
5840af3
to
4ba5474
Compare
@cmb69 code updated, now only this test is failing:
It seems that the bug #71625 was solved with this PR, how to update this test? |
Because I misinterpreted a quick test, and didn't read the docs thoroughly, which state that @echo off
:: create test folder layout, unless already there
for %%i in (C:\test5001 C:\test5001\c D:\test5001 D:\test5001\d) do (
if not exist %%i mkdir %%i
)
:: set cwd for each drive
cd C:\test5001
cd D:\test5001
:: verify cwd is set independently for each drive
C:
echo %CD%
D:
echo %CD%
:: check PHP's behavior
php -r "var_dump(ZEND_THREAD_SAFE)";
php -r "var_dump(file_exists('C:c'));"
php -r "var_dump(realpath('C:c'));" I get
and
So |
According to https://devblogs.microsoft.com/oldnewthing/20101011-00/?p=12563 there should be only one current working directory and the behaviour in cmd should be considered cmd only. And the linked article (link in the previous article does not work) https://devblogs.microsoft.com/oldnewthing/20100506-00/?p=14133 So if there is only one cwd and this cwd is very well set/reset by php (otherwise standard relative paths will not work) and if relative links are properly detected (by this PR), all relative links should work if the resolution is done by the OS itself. The different results of Additionally, this PR fixes:
How should I fix this test? The bug is about
This was my first thought too and I support it. Even Microsoft is writing in every 2nd line that this is common source of errors. But for now I would propose to fix the original issue, i.e. wrong |
4ba5474
to
9c30e95
Compare
5223feb
to
a43805e
Compare
@cmb69 Can you please review the changes and merge? |
3f20997
to
a43805e
Compare
If there are no any other questions, the PR is ready to be merged. |
<?php
var_dump(chdir('C:/'));
var_dump(file_exists('C:/test5001'));
var_dump(file_exists('C:test5001')); prints now for NTS:
and for ZTS:
That doesn't look like the desired output. What am I missing? |
@cmb69 Please add a test case and I will try to make the code to pass. |
Pretty strange, but I can no longer reproduce the different behavior of NTS vs. ZTS with or without this patch. Maybe that is related to the latest Windows updates I got yesterday? Anyhow, I think it's best to start with a simple test, just to show the desired change:
This passes for me with the patch, while versions without this patch would have printed And please rebase onto any of the actively supported PHP versions or master. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Okay, original feedback has been addressed, but this definitely needs more test (and likely also manual testing).
c382be7
to
5c384d0
Compare
@cmb69 I have added a test case. I have also found that |
@cmb69 Can you review and merge to close it? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the ping – sorry, for the delay.
The patch doesn't look bad, but should have also tests to actually check whether drive relative paths work as expected for different file system functions (e.g. fopen(), file_get_contents()), but also for other functions which could be affected (cf. 3d3f11e). Also there should be tests regarding the behavior of open_basedir, but that may not be possible. It also would be good to have which show the behavior of changing to a drive relative path on another drive, but that may also be a bit of an issue (test must be skipped if other drive is not available; how to get info of available drives?) If automated tests are not possible, this would have to be tested manually.
Anyhow, neither I nor the PHP 7.4 RM @derickr would like to see this patch going into the release branches for stability reason. Therefore, please rebase onto master.
Thanks.
5c384d0
to
7224c94
Compare
Standard system functions use the same underlaying path resolution. Feel free to add more tests if needed, same for the manual multidrive test if needed.
Rebased and changed the merge target. All tests passed. |
Sorry, I don't have familiarity with Windows. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the ping, @cmb69. I would recommend against implementing this feature. We might not be able to guarantee the drive relative path is safe. There are similar issues with resolving symlinks, thanks for reminding on #1243 which looks quite similar . What comes in mind that needs to be tested
I would at least recommend to be hesitant merging at once, until these and possibly more tests have succeeded. The discrepancy between TS/NTS and sensitivity to internal chdir() calls might be hard to solve. Thanks. |
@weltling Appreciate your feedback. I am also more again supporting the drive relative paths as almost noone knows about this feature and when someone uses it, in a lot of cases it is by a mistake. I have implemented this patch as there is a partial support currently (in a few places in the PHP code) and I decided to improve the support. What do you think @weltling, @cmb69, @nikic about completely removing the support of drive relative paths and to return false from all file functions with warning? More specifically the same result as if path is outside open_basedir. |
I think that would be reasonable, but probably we should first deprecate using these drive relative paths. |
@cmb69 I think given that a) this is very niche, b) it's already broken / support is hit and miss, and c) we're at a major version, it makes sense to remove support directly (with a nice error message). Otherwise we have to carry around this potentially dangerous functionality for the whole major version. |
@nikic well, that actually makes sense. |
I agree as well, Windows relative path with drive letter should be not supported, almost noone knows about it and thus very confusing. Can someone update this PR to reject all Windows relative pathed with drive letter in all functions/extensions? |
closing as I have no time to remove the support as discussed in #5001 (comment) |
Fix # 78939, paths like "C:x.txt" are relative, see https://docs.microsoft.com/en-us/dotnet/standard/io/file-path-formats
Please note that path like "C:" is not absolute too (tested in Windows command line by
cd C:
- cwd did not changed).