Skip to content

util: avoid Windows reserved device names in sanitize_path - #7203

Closed
Taiwudao wants to merge 1 commit into
beetbox:masterfrom
Taiwudao:fix/windows-reserved-names
Closed

Taiwudao wants to merge 1 commit into
beetbox:masterfrom
Taiwudao:fix/windows-reserved-names

Conversation

@Taiwudao

Copy link
Copy Markdown

On Windows, a file or directory named CON, PRN, AUX, NUL, COM1-COM9 or LPT1-LPT9 cannot be created at all. An extension does not help: CON.txt is reserved too. sanitize_path() leaves such components untouched, so importing an album or artist with one of those names fails when beets creates the destination directory.

Reproduction (Windows 10, CPython 3.12)

from beets.util import sanitize_path
import os, tempfile

base = tempfile.mkdtemp()
for name in ["CON", "COM1", "normal"]:
    target = os.path.join(base, sanitize_path(name))
    try:
        os.makedirs(target)
        print(f"{name!r}: ok")
    except OSError as exc:
        print(f"{name!r}: {type(exc).__name__}: {exc.strerror}")

Before this patch:

'CON'  -> sanitize_path returns 'CON'  -> NotADirectoryError: The directory name is invalid
'COM1' -> sanitize_path returns 'COM1' -> NotADirectoryError: The directory name is invalid
'normal' -> ok

Change

sanitize_path() now prefixes such a component with an underscore (CON -> _CON, CON.txt -> _CON.txt). The check looks at the name before the first dot, because that is what Windows reserves.

The rules apply on every platform, following the rationale already written above CHAR_REPLACE: these names are legal on Unix, but Samba shares hit exactly the same wall, which is why the reserved characters are already stripped everywhere.

Tests

Added to TestSanitizePath:

  • CON, con, CON.txt, NUL, aux.mp3, COM1, LPT9 get the prefix
  • normal, console, con_, COM10, LPT0 are left alone
  • a reserved name nested inside a longer path is fixed

Results:

pytest test/test_util.py 95 passed, 1 skipped
pytest test/test_library.py 215 passed
reverse check reverting util/__init__.py fails exactly the 8 new cases

A changelog entry is included under Unreleased -> Bug fixes.

@Taiwudao
Taiwudao requested a review from a team as a code owner November 21, 2026 05:40
Windows refuses to create a file or directory named CON, PRN, AUX, NUL,
COM1-COM9 or LPT1-LPT9, and an extension does not help: CON.txt is reserved
too. sanitize_path left those components untouched, so importing an album or
an artist with such a name failed with NotADirectoryError when beets tried to
create the directory.

Prefix an underscore instead. Following the CHAR_REPLACE rationale directly
above, this applies on every platform -- Samba shares hit the same limit.

Reproduced on Windows 10: sanitize_path("CON") returned "CON" and
os.makedirs failed with NotADirectoryError; same for "COM1".
@github-actions github-actions Bot added the windows Relates specifically to Windows OS label Nov 21, 2026
@snejus

snejus commented Nov 21, 2026

Copy link
Copy Markdown
Member

See our AI Policy.

@snejus snejus closed this Nov 21, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

windows Relates specifically to Windows OS

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants