This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: os.path.abspath returns invalid path (resolves symbolic link)
Type: behavior Stage: resolved
Components: IO Versions: Python 3.8, Python 3.7
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: eryksun, rvisser
Priority: normal Keywords:

Created on 2021-04-12 05:24 by rvisser, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Messages (4)
msg390818 - (view) Author: Rene Visser (rvisser) Date: 2021-04-12 05:24
According to the python documentation os.path.abspath() does *not* resolve symbolic links. This however does not always seem to be true causing an invalid path return by abspath. This could potentially be exploited to crash python applications.

Example for bug reproduction on a linux terminal:
1. create a sub-directory "bug_abspath"
2. enter the sub-dir "bug_abspath"
3. create a symbolic link "local_link" onto the current dir using: "ln -s . local_link"
4. open python session and import os and enter the following:
5. path_correct = os.path.abspath('./../bug_abspath')  # returns correct path
6. path_invalid = os.path.abspath('local_link/../bug_abspath')  # returns invalid path with wrongly resolved "local_link"

From step 5 the correct/valid path is returned, from step 6 abspath returns an invalid path that is non-existing (contains non-existing "bug_abspath/bug_abspath" string. 
I consider this behavior incorrect and interpret it as a bug in the abspath routine which is not allowed to resolve the symbolic link "local_link".
(Note os.path.realpath works as expected but is unwanted by me).

Tested on
OS: linux ubuntu 20.04, CentOS 7.8
PY: python 3.7 and 3.8

Thanks for any help, best wishes, Rene
msg390820 - (view) Author: Eryk Sun (eryksun) * (Python triager) Date: 2021-04-12 05:40
In POSIX, os.path.abspath(p) is normpath(join(os.getcwd(), p)). normpath() doesn't touch the filesystem, and it's documented that its "string manipulation may change the meaning of a path that contains symbolic links". You can use os.path.realpath() to resolve symbolic links in a path.
msg390982 - (view) Author: Rene Visser (rvisser) Date: 2021-04-13 18:29
Thanks Eryk for your fast response. Apparently I somehow skipped the remark
about symbolic links in combination with abspath. Thank you for pointing
this out and apologies for wasting some of your time!!
Best wishes, Rene Visser

On Mon, Apr 12, 2021 at 7:40 AM Eryk Sun <report@bugs.python.org> wrote:

>
> Eryk Sun <eryksun@gmail.com> added the comment:
>
> In POSIX, os.path.abspath(p) is normpath(join(os.getcwd(), p)). normpath()
> doesn't touch the filesystem, and it's documented that its "string
> manipulation may change the meaning of a path that contains symbolic
> links". You can use os.path.realpath() to resolve symbolic links in a path.
>
> ----------
> nosy: +eryksun
> resolution:  -> not a bug
> stage:  -> resolved
> status: open -> closed
>
> _______________________________________
> Python tracker <report@bugs.python.org>
> <https://bugs.python.org/issue43810>
> _______________________________________
>

-- 

-------------------------------------------------------

René Visser
Stuttgart, Germany
email: renevisser@gmail.com

-------------------------------------------------------
msg391012 - (view) Author: Eryk Sun (eryksun) * (Python triager) Date: 2021-04-13 23:21
If realpath() resolves too much, you'll have to resolve manually via os.path.islink() and os.readlink(). If you need strict resolution, use pathlib.Path.resolve(). In strict mode it raises FileNotFoundError if a path component doesn't exist. Also, even in non-strict mode, it raises RuntimeError for a symlink loop.
History
Date User Action Args
2022-04-11 14:59:44adminsetgithub: 87976
2021-04-13 23:21:20eryksunsetmessages: + msg391012
2021-04-13 18:29:42rvissersetmessages: + msg390982
2021-04-12 05:40:06eryksunsetstatus: open -> closed

nosy: + eryksun
messages: + msg390820

resolution: not a bug
stage: resolved
2021-04-12 05:24:45rvissercreate