[New-bugs-announce] [issue27002] Support different modes in posixpath.realpath()
report at bugs.python.org
Wed May 11 14:54:44 EDT 2016
New submission from Serhiy Storchaka:
For now posixpath.realpath() don't raise an exception if encounter broken link. Instead it just lefts broken link name and following path components unresolved. This is dangerous since broken link name can be collapsed with following ".." and resulting valid path can point at wrong location. May be this is even security issue.
On other hand, Path.resolve() raises an exception when encounters broken link. This is not always desirable, there is a wish to make it more lenient. See issue19717 for more information.
The readlink utility from GNU coreutils has three mode for resolving file path:
canonicalize by following every symlink in every component of the given name recursively; all but the last component must exist
canonicalize by following every symlink in every component of the given name recursively, all components must exist
canonicalize by following every symlink in every component of the given name recursively, without requirements on components existence
Current behavior of posixpath.realpath() is matches (besides one minor detail) to `readlink -m`. The behavior of Path.resolve() matches `readlink -e`.
Proposed preliminary patch implements the support of all three modes in posixpath.realpath(): CAN_MISSING, CAN_ALL_BUT_LAST and CAN_EXISTING. It exactly matches the behavior of readlink. The default mode is CAN_MISSING.
There is minor behavior difference in the default mode. If there is a file "file", a link "link" that points to "file" and a broken link "broken", then "broken/../link" was resolved to "link" and now it is resolved to "file".
The patch lacks the documentation. Ternary flag looks as not the best API. Binary flag would be better. But I don't know what can be dropped. CAN_MISSING is needed for compatibility, but it looks less useful and may be insecure (not more than normpath()). CAN_EXISTING and CAN_ALL_BUT_LAST is needed in different cases. I think that in many cases CAN_ALL_BUT_LAST is actually needed instead of CAN_MISSING.
After resolving this issue the solution will be adopted for Path.resolve().
components: Library (Lib)
title: Support different modes in posixpath.realpath()
versions: Python 3.6
Added file: http://bugs.python.org/file42821/realpath_mode.patch
Python tracker <report at bugs.python.org>
More information about the New-bugs-announce