Skip to content

[Windows] ntpath.realpath() of bytes root directory may raise TypeError in some cases #88013

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

Closed
9001 mannequin opened this issue Apr 14, 2021 · 4 comments
Closed

[Windows] ntpath.realpath() of bytes root directory may raise TypeError in some cases #88013

9001 mannequin opened this issue Apr 14, 2021 · 4 comments
Labels
3.8 (EOL) end of life 3.9 only security fixes easy OS-windows stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error

Comments

@9001
Copy link
Mannequin

9001 mannequin commented Apr 14, 2021

BPO 43847
Nosy @pfmoore, @tjguk, @zware, @eryksun, @zooba

Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

Show more details

GitHub fields:

assignee = None
closed_at = None
created_at = <Date 2021-04-14.12:51:02.772>
labels = ['easy', 'type-bug', '3.8', '3.9', '3.10', 'library', 'OS-windows']
title = '[Windows] ntpath.realpath() of bytes root directory may raise TypeError in some cases'
updated_at = <Date 2021-04-28.15:53:40.393>
user = 'https://github.com/9001'

bugs.python.org fields:

activity = <Date 2021-04-28.15:53:40.393>
actor = 'steve.dower'
assignee = 'none'
closed = False
closed_date = None
closer = None
components = ['Library (Lib)', 'Windows']
creation = <Date 2021-04-14.12:51:02.772>
creator = '9001'
dependencies = []
files = []
hgrepos = []
issue_num = 43847
keywords = ['easy']
message_count = 2.0
messages = ['391074', '391119']
nosy_count = 6.0
nosy_names = ['paul.moore', 'tim.golden', "Antoine d'Otreppe", 'zach.ware', 'eryksun', 'steve.dower']
pr_nums = []
priority = 'normal'
resolution = None
stage = 'needs patch'
status = 'open'
superseder = None
type = 'behavior'
url = 'https://bugs.python.org/issue43847'
versions = ['Python 3.8', 'Python 3.9', 'Python 3.10']

Linked PRs

@9001
Copy link
Mannequin Author

9001 mannequin commented Apr 14, 2021

some win7sp1 and win10:20H2 boxes cannot realpath a networked drive letter such as b"n:" (also affects b"n:\\")

  • observed with 3.8.7 and 3.9.1
  • 3.7.9 is fine

requirements to trigger:

  • bytestring (not unicode str)
  • just the drive letter (subfolders are ok)
  • networked drive (regular disks and vmhgfs are ok)
  • enterprise/AD network? (doesn't seem to happen with samba)

hits the following exceptions in succession:

  • access denied at L601: "path = _getfinalpathname(path)"
  • "cant concat str to bytes" at L621: "return path + tail"

@9001 9001 mannequin added 3.8 (EOL) end of life 3.9 only security fixes OS-windows type-bug An unexpected behavior, bug, or error labels Apr 14, 2021
@eryksun
Copy link
Contributor

eryksun commented Apr 15, 2021

In ntpath._getfinalpathname_nonstrict(), tail should be initialized to path[:0].

Currently tail is initialized to the empty string value ''. If an error occurs that's allowed and path is a root directory that's passed as bytes, then joining path + tail will fail if tail still has its initial value.

To reproduce this issue, create a substitute drive for a directory that grants no access. For example:

    import os
    os.mkdir('spam')
    os.system('icacls spam /inheritance:r')
    os.system('subst N: spam')
    >>> os.path.realpath(b'N:/')
    Traceback (most recent call last):
      File "C:\Program Files\Python39\lib\ntpath.py", line 647, in realpath
        path = _getfinalpathname(path)
    PermissionError: [WinError 5] Access is denied: b'N:\\'

    During handling of the above exception, another exception occurred:

    Traceback (most recent call last):
      File "C:\Program Files\Python39\lib\ntpath.py", line 601, in _getfinalpathname_nonstrict
        path = _getfinalpathname(path)
    PermissionError: [WinError 5] Access is denied: b'N:\\'

    During handling of the above exception, another exception occurred:

    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File "C:\Program Files\Python39\lib\ntpath.py", line 651, in realpath
        path = _getfinalpathname_nonstrict(path)
      File "C:\Program Files\Python39\lib\ntpath.py", line 621, in _getfinalpathname_nonstrict
        return path + tail
    TypeError: can't concat str to bytes

@eryksun eryksun added 3.10 only security fixes stdlib Python modules in the Lib dir labels Apr 15, 2021
@eryksun eryksun changed the title realpath of bytestr smb drive letters fail [Windows] ntpath.realpath() of bytes root directory may raise TypeError in some cases Apr 15, 2021
@eryksun eryksun added 3.10 only security fixes stdlib Python modules in the Lib dir labels Apr 15, 2021
@eryksun eryksun changed the title realpath of bytestr smb drive letters fail [Windows] ntpath.realpath() of bytes root directory may raise TypeError in some cases Apr 15, 2021
@ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
@aisk
Copy link
Contributor

aisk commented Mar 18, 2023

There is another simple way to reproduce the bug, just call ntpath.realpath() on a non exist drive.

@barneygale
Copy link
Contributor

barneygale commented Apr 7, 2023

Fixed in:

Thanks for the fix, @aisk!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
3.8 (EOL) end of life 3.9 only security fixes easy OS-windows stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

4 participants