Add detection of Windows reserved filenames #891
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Description
This PR adds functionality to detect Windows reserved filenames (like COM1, LPT1, etc.) in the WindowsNameTransform class. The implementation adds a new
IsReservedName
method that detects reserved names despite differences between different Windows versions.Background
I encountered an issue on a Windows 10 based system today where an InvalidNameException was thrown:
The only problem was that this was completely unrelated to parent traversal but was actually because the filename was
C:\example\folder\con.xhtml
, which in Windows 10 is considered a reserved word. The incorrect exception message about the parent directory traversal led us down the wrong path when diagnosing the file's issue, so I wanted to update SharpZipLib to handle this scenario better.Additional Information
To make this slightly more complicated, Windows has changed how it handles reserved names across different OS versions:
More information on this can be found on the dotnet/runtime issue and Microsoft's own Naming Conventions Guide.
Solution
The new
IsReservedName
method usesPath.GetFullPath()
and checks if the result starts with\\.\
or\\?\
, which identifies Windows reserved names. When detected, the code throws anInvalidNameException
with an appropriate message pointing to Microsoft's documentation.Changes
IsReservedName
method with detailed documentationMakeValidName
to check for reserved namesWindowsNameTransformHandling.cs
to check theInvalidNameException
is being thrown as expectedI 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.