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

validate_filename() and sanitize_filename() pass '.' and '..' #19

Closed
mkbloke opened this issue Aug 8, 2021 · 4 comments
Closed

validate_filename() and sanitize_filename() pass '.' and '..' #19

mkbloke opened this issue Aug 8, 2021 · 4 comments

Comments

@mkbloke
Copy link

mkbloke commented Aug 8, 2021

In pathvalidate 2.4.1:

>>> pathvalidate.validate_filename(".")
>>> pathvalidate.validate_filename("..")
>>> pathvalidate.sanitize_filename(".")
'.'
>>> pathvalidate.sanitize_filename("..")
'..'

I know that . and .. can be valid in a path, but surely not as filenames on any of the supported platforms?

Thanks.

Edited to add:

My expectation would be that validate_filename() would perhaps raise a ValidationError with a reason of RESERVED_NAME in the two cases above?

Also, if these are considered reserved filenames, shouldn't _ be appended to each, as with, for example: sanitize_filename("con") returns con_?

@thombashi thombashi added bug and removed bug labels Sep 20, 2021
@blerrgh
Copy link

blerrgh commented May 11, 2023

Was this ever resolved? I have the same issue in 2.5.2

It would be great if sanitize_filename("..") (or maybe sanitize_filename("..", check_reserved=True)) returned "_"

@thombashi
Copy link
Owner

thombashi commented May 13, 2023

I have considered this previously and concluded that it is difficult to apply replacements unconditionally.
Unlike other reserved words, . and .. can be assumed to be recognized as reserved keywords and used as reserved names in most cases, so I think it is inappropriate to treat them in the same way as other reserved words.
In many cases, users would expect to preserve . and .. as they are, and there is a possibility that the valid file paths are broken if they are replaced.

In the future, another solution may be to add an interface that specifies a callback function to process reserved names.
By customizing this callback function, changing the behavior as you wish will be possible.

@blerrgh
Copy link

blerrgh commented May 15, 2023

In many cases, users would expect to preserve . and .. as they are, and there is a possibility that the valid file paths are broken if they are replaced.

I agree that they probably shouldn't be replaced in sanitize_filepath since both . and .. are valid in relative paths. But personally I'm not sure I would expect them to ever be valid via sanitize_filename since you cannot name a file . or ...

I suppose if people are using sanitize_filename to check individual path components then they might want to preserve periods.

@thombashi
Copy link
Owner

pathvalidate v3.2.0 can do the following:

from pathvalidate import sanitize_filename, ValidationError

def add_trailing_underscore(e: ValidationError) -> str:
    if e.reusable_name:
        return e.reserved_name

    return f"{e.reserved_name}_"

sanitize_filename(".", reserved_name_handler=add_trailing_underscore, additional_reserved_names=["."])
'._'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants