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

Allows some reserved Windows filenames with trailing "0", superscript numerals, or multipart extension (COM¹, LPT0, etc.) #57

Open
jplarocque opened this issue Sep 29, 2024 · 0 comments

Comments

@jplarocque
Copy link

Hi,

Quoting the Microsoft article Naming Files, Paths, and Namespaces, section "Naming Conventions":

The following fundamental rules enable applications to create and process valid names for files and directories, regardless of the file system:

  • ...

  • Do not use the following reserved names for the name of a file:

    CON, PRN, AUX, NUL, COM0, COM1, COM2, COM3, COM4, COM5, COM6, COM7, COM8, COM9, COM¹, COM², COM³, LPT0, LPT1, LPT2, LPT3, LPT4, LPT5, LPT6, LPT7, LPT8, LPT9, LPT¹, LPT², and LPT³. Also avoid these names followed immediately by an extension; for example, NUL.txt and NUL.tar.gz are both equivalent to NUL. For more information, see Namespaces.

    Note

    Windows recognizes the 8-bit ISO/IEC 8859-1 superscript digits ¹, ², and ³ as digits and treats them as valid parts of COM# and LPT# device names, making them reserved in every directory. For example, echo test > COM¹ fails to create a file.

pathvalidate doesn't catch these cases:

  • COM or LPT followed by 0 or the superscript digits 1, 2, or 3.

  • The use of any of these reserved names followed by a multi-part file name extension.

import pathvalidate, itertools
for parts in itertools.product(
    ["COM", "LPT"],
    ["0",
     "\N{SUPERSCRIPT ONE}", "\N{SUPERSCRIPT TWO}", "\N{SUPERSCRIPT THREE}",
     "1.foo.bin"],
):
    name = "".join(parts)
    pathvalidate.validate_filename(name)
    print("Valid:", name)

Yields:

Valid: COM0
Valid: COM¹
Valid: COM²
Valid: COM³
Valid: COM1.foo.bin
Valid: LPT0
Valid: LPT¹
Valid: LPT²
Valid: LPT³
Valid: LPT1.foo.bin

Inside a Windows 11 VM with an NTFS filesystem, I was able to confirm that some of these filenames were illegal, but I was also able to create files com0, lpt0, and com1.foo.bin.

names

At first glance, those last three names seem like they should be illegal, but the Microsoft article states that it lists general rules "regardless of the file system", so to ensure strict conformance, I would recommend also catching COM0, LPT0, and otherwise-reserved names with multi-part extensions.

Thanks!

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

No branches or pull requests

1 participant