Add detection of Windows reserved filenames#891
Open
mpdunlop wants to merge 1 commit intoicsharpcode:masterfrom
Open
Add detection of Windows reserved filenames#891mpdunlop wants to merge 1 commit intoicsharpcode:masterfrom
mpdunlop wants to merge 1 commit intoicsharpcode:masterfrom
Conversation
Add `IsReservedName` method to detect Windows reserved filenames by checking if Path.GetFullPath returns a path that starts with "\\.\\" or "\\?\\".
This handles the differences between older and newer Windows versions where:
- Older Windows: Path.GetFullPath("COM3.txt") gives "\\.\COM3.txt"
- Newer Windows: Path.GetFullPath("COM3.txt") gives "COM3.txt"
pviano-ria
approved these changes
Apr 2, 2025
airceluearth
approved these changes
Apr 2, 2025
rizucoorg97
approved these changes
Jun 14, 2025
rizucoorg97
approved these changes
Jun 14, 2025
rizucoorg97
approved these changes
Jun 15, 2025
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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
IsReservedNamemethod 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
IsReservedNamemethod usesPath.GetFullPath()and checks if the result starts with\\.\or\\?\, which identifies Windows reserved names. When detected, the code throws anInvalidNameExceptionwith an appropriate message pointing to Microsoft's documentation.Changes
IsReservedNamemethod with detailed documentationMakeValidNameto check for reserved namesWindowsNameTransformHandling.csto check theInvalidNameExceptionis 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.