thombashi / pathvalidate

A Python library to sanitize/validate a string such as filenames/file-paths/etc.
https://pathvalidate.rtfd.io/
MIT License
210 stars 12 forks source link

Many name validations that include '\' (backslash) fail on Linux #18

Closed Traktormaster closed 3 years ago

Traktormaster commented 3 years ago

The root of the problem seems to be that even on a non-windows platform the backslash is treated as a path separator character, leading to false absolute path detections. The following script demonstrates the problem with a few examples:

>>> from pathvalidate import validate_filename, ValidationError
>>> for name in ["\\", "\\\\", "\\ ", "C:\\", "c:\\", "\\xyz", "\\xyz "]:
...     try:
...         validate_filename(name, platform="linux")
...     except ValidationError as e:
...         print(e.reason, "for", repr(name))
...     else:
...         raise Exception("did not raise for %r" % (name,))
...     
ErrorReason.FOUND_ABS_PATH for '\\'
ErrorReason.FOUND_ABS_PATH for '\\\\'
ErrorReason.FOUND_ABS_PATH for '\\ '
ErrorReason.FOUND_ABS_PATH for 'C:\\'
ErrorReason.FOUND_ABS_PATH for 'c:\\'
ErrorReason.FOUND_ABS_PATH for '\\xyz'
ErrorReason.FOUND_ABS_PATH for '\\xyz '

I'm on Linux 5.11.10, I don't know if such file names are also valid for MacOS or not.

thombashi commented 3 years ago

Thank you for your report. The problem has fixed at pathvalidate 2.4.1.