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

Simplify DropPathRoot and fix out of bounds issue #593

Merged
merged 4 commits into from Apr 27, 2021
Merged

Conversation

piksel
Copy link
Member

@piksel piksel commented Mar 7, 2021

Fixes #56

This did grow a bit, since a lot of stuff was discovered while working on this.
Prior to this, all tests (and indeed the behaviour) for handling paths just presumed that the platform was Windows.
The core problem method (DropPathRoot) has now been changed to use the framework/platforms path handling methods, but with some special exceptions for .NET < 4.6.2.
This does feel a bit hackish, but I think it's better than the previous method, which was to implement custom code for determining what a root path is. If that code fails to notice something that the OS would consider a absolute path it could lead to vulnerabilities. If this implementation fails to notice it, it would instead throw, which could then be fixed.
And also, since this only happens in older frameworks, in most cases it would just work as intended.

If creating from a windows OS, it will strip what the OS considers to be a root path from the file name, which ensures "correct" entries in our archive, and on reading entries from an archive, it will strip them as appropriate for the OS.

This PR also includes updates to tests which now have separated versions for windows and non-windows platforms, and also pipes the output of 7z to the test context, so that it will only be displayed if a test fails.

I certify that I own, and have sufficient rights to contribute, all source code and related material intended to be compiled or integrated with the source code for the SharpZipLib open source product (the "Contribution"). My Contribution is licensed under the MIT License.

@@ -15,48 +15,9 @@ public static class PathUtils
/// <remarks>Unlike the <see cref="System.IO.Path"/> class the path isn't otherwise checked for validity.</remarks>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Referring to the comment about not checking for validity - I can't recall if this is ever called on reading a file or just creating? - are there any situations where GetPathRoot might throw on .NET 4.x if it thinks something is invalid (e.g. same sort of situation as #341)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, you are probably right. I'll add a test for this and fix it if it triggers anything.
If it does, it is probably better to use another code path for cleaning "input" file names, rather than sharing it with the "output" ones.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The results are in!
image
The usual reserved characters for file names in windows are \ / : * ? " < > |, but out of those, only < > | " are listed in Path.GetInvalidPathChars() and throws on net46.
I guess the easiest way to solve it is to just replace those chars with _?

Copy link
Member Author

@piksel piksel Mar 14, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added an optional replaceInvalidChars to the arguments as well, since the work is already done, but per default it returns the stripped path, including any invalid path characters.

Scratch that since it won't be consistent across all platforms. This will only strip the root from the path, nothing else.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've forgotten all the details offhand about how DropPathRoot interacts with the other name transform/cleaning stuff, but looking at the comment about long paths reminded me of the \\?\ long path syntax on Windows, the handling of which might be another reason to leave it up to the built in functions instead of doing things manually

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's used in WindowsNameTransform (for zip -> windows file name), PathNameTransform and ZipFileTransform for ZipOutputStream and FastZip/ZipEntryFactory respectively.

@piksel piksel merged commit 8ab21b0 into master Apr 27, 2021
@piksel piksel deleted the winpath-droproot branch April 27, 2021 19:13
HowToDoThis added a commit to HowToDoThis/SharpZipLib that referenced this pull request Aug 11, 2021
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.

Bug in DropPathRoot - IndexOutOfBoundsException
2 participants