GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — `posixpath.realpath('secretlink')` raises · Issue #118447 · python/cpython

posixpath.realpath('secretlink') raises #118447

New issue
Copy link
New issue
Copy link
Closed
Closed
posixpath.realpath('secretlink') raises#118447
Copy link
Labels
type-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error

Description

@nineteendo
nineteendo
opened on Apr 30, 2024
Issue body actions

Bug report

Bug description:

GNU coreutils realpath -m doesn't raise an error for secret symlinks (no read permission):

wannes@Stefans-iMac dirs % sudo ls -l secret-symlink
l---------  1 wannes  staff  44 Jun 30  2023 secret-symlink -> /Users/wannes/path-picker/link-test/dirs/dir
wannes@Stefans-iMac dirs % grealpath -m secret-symlink
/Users/wannes/path-picker/link-test/dirs/secret-symlink

But posixpath.realpath() does:

>>> import posixpath
>>> posixpath.realpath("secret-symlink")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<frozen posixpath>", line 435, in realpath
  File "<frozen posixpath>", line 495, in _joinrealpath
PermissionError: [Errno 13] Permission denied: 'secret-symlink'

CPython versions tested on:

3.12

Operating systems tested on:

macOS

Linked PRs

  • GH-118447: Fix handling of unreadable symlinks in os.path.realpath() #118489
  • [3.13] GH-118447: Fix handling of unreadable symlinks in os.path.realpath() (GH-118489) #119163
  • GH-118447: Fix FreeBSD test failures. #119170
  • [3.13] GH-118447: Fix FreeBSD test failures. (GH-119170) #119181

Activity

  1. nineteendo
    added
    type-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
    on Apr 30, 2024
  2. eryksun commented on Apr 30, 2024

    @eryksun
    eryksun
    on Apr 30, 2024
    Contributor
    More actions

    Setting permissions on a symlink is a BSD (including macOS) feature. Linux doesn't allow it. Testing this should be limited to platforms with os.chmod in os.supports_follow_symlinks.

    Reacted by Nice Zombies
  3. barneygale
    added a commit that references this issue on May 1, 2024

    pythonGH-118447: Handle unreadable symlinks in `os.path.realpath()`

    1619428
  4. nineteendo commented on May 2, 2024

    @nineteendo
    nineteendo
    on May 2, 2024
    ContributorAuthor
    More actions

    Upon closer inspection, this is a bug in coreutils:

    wannes@Stefans-iMac dirs % sudo ls -l secret-recursive-symlink
    l---------  1 wannes  staff  40 Jun 30  2023 secret-recursive-symlink -> /Users/wannes/path-picker/link-test/dirs
    wannes@Stefans-iMac dirs % sudo realpath secret-recursive-symlink/..
    /Users/wannes/path-picker/link-test
    wannes@Stefans-iMac dirs % grealpath -m secret-recursive-symlink/..
    /Users/wannes/path-picker/link-test/dirs
  5. nineteendo
    closed this as not plannedon May 2, 2024
  6. barneygale commented on May 2, 2024

    @barneygale
    barneygale
    on May 2, 2024
    Contributor
    More actions

    I wouldn't expect os.path.realpath(..., strict=False) to raise OSError, no matter what coreutils does!

  7. nineteendo commented on May 2, 2024

    @nineteendo
    nineteendo
    on May 2, 2024
    ContributorAuthor
    More actions

    That's probably a reason which this is not part of POSIX... I expected you needed permission to follow it, not to determine the real location.

    Return the canonical path of the specified filename, eliminating any symbolic links encountered in the path.

    We can't eliminate this symlink, because we don't know where it points to, which we need to know for determining the parent directory. Also, coreutils doesn't even raise an error in strict mode:

    wannes@Stefans-iMac ~ % ln -s . src        
    wannes@Stefans-iMac ~ % grealpath -e src/..
    /Users
    wannes@Stefans-iMac ~ % chmod -h 000 src
    wannes@Stefans-iMac ~ % grealpath -e src/..
    /Users/wannes

    Raising an error here will lead to the fewest bugs.

  8. nineteendo
    reopened this on May 2, 2024
  9. eryksun commented on May 2, 2024

    @eryksun
    eryksun
    on May 2, 2024
    Contributor
    More actions

    On macOS and NetBSD, fcntl() supports F_GETPATH. In that case, you could try to open "secret-symlink" and query the resolved path, e.g. target = os.fsdecode(fcntl.fcntl(fd, fcntl.F_GETPATH, bytes(1024))). You may have to revisit the design of fcntl.fcntl() to make it use an internal buffer bigger than 1024 bytes, though I think that's currently the maximum path length supported by macOS.

    Reacted by Nice Zombies
  10. nineteendo commented on May 2, 2024

    @nineteendo
    nineteendo
    on May 2, 2024
    ContributorAuthor
    More actions

    That works:

    >>> import fcntl
    >>> import os
    >>> os.symlink('.', 'src')
    >>> os.chmod('src', 0o000, follow_symlinks=False)
    >>> os.readlink('src')
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    PermissionError: [Errno 13] Permission denied: 'src'
    >>> fd = os.open('src', os.O_RDONLY)
    >>> os.fsdecode(fcntl.fcntl(fd, fcntl.F_GETPATH, bytes(1024))).rstrip('\x00')
    '/Users/wannes'

    But I think it might be cleaner to fix this in readlink...

  11. nineteendo commented on May 3, 2024

    @nineteendo
    nineteendo
    on May 3, 2024
    ContributorAuthor
    More actions

    But it doesn't work for broken symlinks:

    >>> import fcntl
    >>> import os
    >>> open('tmp', 'w', encoding='utf-8')
    <_io.TextIOWrapper name='tmp' mode='w' encoding='utf-8'>
    >>> os.symlink('tmp', 'dst')
    >>> os.unlink('tmp')
    >>> os.chmod('dst', 0o000, follow_symlinks=False)
    >>> os.readlink('dst')
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    PermissionError: [Errno 13] Permission denied: 'dst'
    >>> fd = os.open('dst', os.O_RDONLY)
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    FileNotFoundError: [Errno 2] No such file or directory: 'dst'
  12. barneygale
    added a commit that references this issue on May 18, 2024

    GH-118447: Fix handling of unreadable symlinks in `os.path.realpath()` (

    caf6064
  13. miss-islington
    added a commit that references this issue on May 18, 2024

    pythonGH-118447: Fix handling of unreadable symlinks in `os.path.real…

    57a36a8
  14. barneygale
    added 5 commits that reference this issue on May 18, 2024

    pythonGH-118447: Fix handling of unreadable symlinks in `os.path.real…

    b2cc8c2

    [3.12] pythonGH-118447: Fix handling of unreadable symlinks in `os.pa…

    bbabcfd

    [3.13] GH-118447: Fix handling of unreadable symlinks in `os.path.rea…

    7407267

    pythonGH-118447: Fix FreeBSD test failures.

    f7bf985

    GH-118447: Fix FreeBSD test failures. (#119170)

    4b76671
  15. miss-islington
    added a commit that references this issue on May 19, 2024

    pythonGH-118447: Fix FreeBSD test failures. (pythonGH-119170)

    eddddec
  16. barneygale
    added a commit that references this issue on May 19, 2024

    [3.13] GH-118447: Fix FreeBSD test failures. (GH-119170) (#119181)

    fdc50ba
  17. nineteendo commented on May 22, 2024

    @nineteendo
    nineteendo
    on May 22, 2024
    ContributorAuthor
    More actions

    With the FreeBSD test failures fixed, we can close this.

  18. nineteendo
    closed this as completedon May 22, 2024
  19. estyxx
    added 2 commits that reference this issue on Jul 17, 2024

    pythonGH-118447: Fix handling of unreadable symlinks in `os.path.real…

    2a55dab

    pythonGH-118447: Fix FreeBSD test failures. (python#119170)

    cb12128
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    type-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      You are visitor 01402503 — GeezerVison does not store the pages you look up.
      Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.