Skip to content

Add detection of Windows reserved filenames #891

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

mpdunlop
Copy link

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:

ICSharpCode.SharpZipLib.Core.InvalidNameException: Parent traversal in paths is not allowed.

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:

  • In older Windows versions:
    • Path.GetFullPath("COM3") gives \.\COM3
    • Path.GetFullPath("C:\COM3") gives \.\COM3
    • Path.GetFullPath("COM3.txt") gives \.\COM3
  • In newer Windows versions, this behavior can be inconsistent depending on context
    • Path.GetFullPath("COM3") gives \.\COM3
    • Path.GetFullPath("C:\COM3") gives C:\COM3
    • Path.GetFullPath("COM3.txt") gives "COM3.txt

More information on this can be found on the dotnet/runtime issue and Microsoft's own Naming Conventions Guide.

Solution

The new IsReservedName method uses Path.GetFullPath() and checks if the result starts with \\.\ or \\?\, which identifies Windows reserved names. When detected, the code throws an InvalidNameException with an appropriate message pointing to Microsoft's documentation.

Changes

  • Added new IsReservedName method with detailed documentation
  • Updated MakeValidName to check for reserved names
  • Added xml documentation and comments
  • Added a test to WindowsNameTransformHandling.cs to check the InvalidNameException is being thrown as expected

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.

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"
@icsharpcode icsharpcode deleted a comment from SourceproStudio Mar 13, 2025
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