classification
Title: [Windows] ntpath.realpath() of bytes root directory may raise TypeError in some cases
Type: behavior Stage: needs patch
Components: Library (Lib), Windows Versions: Python 3.10, Python 3.9, Python 3.8
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: Antoine d'Otreppe, eryksun, paul.moore, steve.dower, tim.golden, zach.ware
Priority: normal Keywords: easy

Created on 2021-04-14 12:51 by 9001, last changed 2021-04-28 15:53 by steve.dower.

Messages (2)
msg391074 - (view) Author: ed (9001) Date: 2021-04-14 12:51
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"
msg391119 - (view) Author: Eryk Sun (eryksun) * (Python triager) Date: 2021-04-15 07:15
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
History
Date User Action Args
2021-04-28 15:53:40steve.dowersetkeywords: + easy
2021-04-15 07:15:50eryksunsettitle: realpath of bytestr smb drive letters fail -> [Windows] ntpath.realpath() of bytes root directory may raise TypeError in some cases
components: + Library (Lib)

nosy: + Antoine d'Otreppe, eryksun, - 9001
versions: + Python 3.10
messages: + msg391119
stage: needs patch
2021-04-14 12:51:029001create