Repository navigation
Conversation
) FileNameSanitizer.sanitize() truncated the filename to max_len first and then let the reserved-name handler append an escape marker (e.g. "CON" -> "CON_"), which grew the name back beyond max_len with no subsequent length check. The returned name therefore failed is_valid_filename(..., max_len=...): sanitize_filename("CON", platform="windows", max_len=3) # before: "CON_" (4 bytes, invalid for max_len=3) Re-fit the escaped name to max_len and shorten it until it is a valid, non-reserved filename, trimming trailing spaces/periods on Windows when an extension was cut off by truncation. If no non-empty name can fit within the limit (e.g. every single-byte name is reserved), raise ValidationError(INVALID_LENGTH) instead of returning an over-limit name. FilePathSanitizer benefits from the same fix because it sanitizes each path component through FileNameSanitizer. Fixes thombashi#83 Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This branch has not been deployed
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.
Summary
Fixes #83.
sanitize_filename()can return a name that is longer than the requestedmax_lenand therefore failsis_valid_filename(..., max_len=...)when the input is a Windows reserved name.Reproduction (before this change)
The same happens for
PRN/AUX/NULwithmax_len=3(e.g.'PRN_'), forCOM1/LPT1withmax_len=4(e.g.'COM1_'), etc. With an extension it can also leave a dangling fragment, e.g.sanitize_filename("CON.txt", platform="windows", max_len=5)returned'CON_.t'(6 bytes, invalid).Root cause
FileNameSanitizer.sanitize()processes the name in this order:max_len(pathvalidate/_filename.py:80);ReservedNameHandler.add_trailing_underscoreappends"_"(pathvalidate/_filename.py:85-90).Step 3 grows the name after it was truncated, and there is no subsequent length check, so the escaped name can exceed
max_len. Simply truncating the escaped name again does not fully solve it:"CON_"[:3] == "CON"is still the reserved name, and cutting in the middle of an extension can leave a trailing"."/ space, which is invalid on Windows.Fix
After applying the reserved-name replacement, re-fit the result to
max_lenwith a new_fit_reserved_name()helper:max_lenbytes using the existing byte-awaretruncate_str;ValidationError(INVALID_LENGTH)instead of silently returning an over-limit name.The "is this still reserved / valid?" decision is reused from the existing validator via
self._validator.is_valid(...), so the reserved-name rules stay in a single place.FilePathSanitizerbenefits from the same fix automatically, because it sanitizes each path component through aFileNameSanitizer.Concrete changes
pathvalidate/_filename.pyself._fit_reserved_name(...)after the reserved-namere.sub(...)insanitize();FileNameSanitizer._fit_reserved_name()helper.test/test_filename.py— regression tests covering:CON/PRN/AUX/NULwithmax_len=3,COM1/LPT1withmax_len=4);max_len;WIN_RESERVED_FILE_NAMESonwindows/universalacross severalmax_lenvalues and handlers (default, leading-underscore, trailing-underscore), asserting the result is always within the byte limit and valid;validate_after_sanitize=True(this used to raiseINVALID_AFTER_SANITIZE);ValidationError(INVALID_LENGTH).Potential impact / behavior
max_lenalready accommodates the escape marker (the normal case, including the default255), the output is unchanged:"CON"->"CON_","CON.txt"->"CON_.txt".max_len— changes, from returning an invalid over-limit name to returning a valid name within the limit (e.g."CON"withmax_len=3->"CO").as_isreserved-name handler (which intentionally keeps reserved names) is untouched.ValidationError(INVALID_LENGTH)is raised only in the pathological situation where no valid non-empty name can fit in the requested limit.Verification
pytest test/-> 4788 passed, 14 skipped (the skips are pre-existing, platform-specific).mypy pathvalidate->Success: no issues found in 14 source files.ruff format --check-> clean;ruff checkreports no findings in the changed lines (identical pre-existing warning count before/after).len(name.encode(fs_encoding)) <= max_lenfor whichis_valid_filename(...)isTrue.