Skip to content

CleanPath: Allow slashes in path pattern for Windows to be able to handle partly normalized paths#40

Merged
nibra merged 13 commits intojoomla-framework:masterfrom
richard67:patch-1
Jan 30, 2021
Merged

CleanPath: Allow slashes in path pattern for Windows to be able to handle partly normalized paths#40
nibra merged 13 commits intojoomla-framework:masterfrom
richard67:patch-1

Conversation

@richard67
Copy link
Contributor

@richard67 richard67 commented Jan 29, 2021

Pull Request for Issue #

Summary of Changes

Fix the regex for Windows so it can handle partly normalized paths like e.g. C:\\xampp\\htdocs\\joomla-cms/tmp, which is the $tmp_path in configuration.php after a new installation of a current staging branch of the CMS on an XAMPP on Windows.

Partly normalized paths like e.g. C:\\xampp\\htdocs\\joomla-cms/tmp are absolutely ok in environments like PHP or like I have it in my job with Java, where paths on any OS are later normalized using "/" as separator (what out Path::clean does).

So we have to stick here to the rules of PHP how it handles Windows paths, and not to the rules of Windows, where a slash in a file or folder name is not allowed.

@zero-24 @nibra If having partly normalized paths like described above is not desired, we can replace the slashes / by backslashes \ at the end of the routine here

return preg_replace('~\\\\+~', '\\', $source);

by changing it to
return preg_replace('~(\\\\|/)+~', '\\', $source);
Then we would not have 'C:\Documents\Newsletters/tmp' or 'C:\Documents/Newsletters/tmp' but 'C:\Documents\Newsletters\tmp' as result of the 2 new unit tests.

I think we don't need that, i.e. the PR is good as it is, because when appending the update package's file name to the path, the Joomla Update component will anyway use a slash /.

What do you think?

Testing Instructions

Test joomla/joomla-cms#32076 with XAMPP on Windows.

Documentation Changes Required

None.

zero-24 and others added 3 commits January 29, 2021 13:53
Co-authored-by: Richard Fath <richard67@users.noreply.github.com>
@richard67
Copy link
Contributor Author

Merged the tests from #39 into here.

@richard67 richard67 mentioned this pull request Jan 29, 2021
2 tasks
@richard67
Copy link
Contributor Author

Due to the way how the regex works I have added with my last commit one more test for handling slashes also in on a folder level before the last one.

@richard67
Copy link
Contributor Author

richard67 commented Jan 30, 2021

@nibra Your change on the regex in this PR makes tests fail e.g. for 'C:\Documents\Newsletters\Summer2018.pdf' (test case "windows path"

'windows path' => array(
. You will see that if you debug the regex with a tool like e.g. https://regex101.com/ .

@richard67
Copy link
Contributor Author

@nibra The regex we have now (without this PR) is wrong anyway because it requires double backslash and so not allows single backslash at certain places, but as you can see the test cases assume that to be valid. The only reason why it worked was that for lucky circumstance we got a partial match for the expression. The right regex in my opinion would be:
^([A-Za-z]:(\\{1,2}|\/))?[A-Za-z0-9_-]+[A-Za-z0-9_\.-]*((\\{1,2}|\/)[A-Za-z0-9_-]+[A-Za-z0-9_\.-]*)*$

@richard67
Copy link
Contributor Author

@nibra Another mistake in your commit is that it doesn't change the expected result in the unit tests to reflect the replacement of the slashes which you have added.

@richard67
Copy link
Contributor Author

Will add more test cases to the unit tests soon. Stay tuned.

@richard67
Copy link
Contributor Author

Now the tests fail but for other reasons than before. It seems the Windows path Documents\Newsletters matches also the Linux pattern and so is handled there without replacement of slashes by backslashes, and so the unit test result is wrong.

@richard67
Copy link
Contributor Author

Now tests are passing, but for one kind of Windows path Documents\Newsletters the Linux pattern matches. It might be good to move the Windows check before the Linux check.

I will add more test cases, then we might see if something remains to be done.

@richard67 richard67 requested a review from nibra January 30, 2021 10:10
@richard67
Copy link
Contributor Author

@nibra I guess I have it for now. Please review again. It might be useful to add more tests for bad paths, but this could be done with some other PR if desired.

@nibra nibra merged commit bb3058c into joomla-framework:master Jan 30, 2021
@richard67 richard67 deleted the patch-1 branch January 30, 2021 13:12
@richard67
Copy link
Contributor Author

@nibra Will you make again a PR for the CMS to update the staging branch? Or shall I do that?

@richard67
Copy link
Contributor Author

@nibra Ah, and before one of us can make the PR for the CMS to update the staging branch, it needs a new tag or release 1.4.2.

@nibra
Copy link
Contributor

nibra commented Jan 30, 2021

Tagged 1.4.2, upmerged to 2.0-dev, tagged 2.0.0-beta4.
Feel free to update the CMS ;)
Thank you!

@richard67
Copy link
Contributor Author

Thanks. I'll do the CMS.

@richard67
Copy link
Contributor Author

PR for the CMS staging branch is joomla/joomla-cms#32206 .

@richard67
Copy link
Contributor Author

PR for the CMS 4.0-dev branch is joomla/joomla-cms#32207 .

@richard67
Copy link
Contributor Author

@nibra @zero-24 The PR to update the filter package in J4 has a problem, see joomla/joomla-cms#32207 (comment) . Any idea what could be wrong? Right now I have no clue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants