isislovecruft / python-gnupg

A modified version of python-gnupg, including security patches, extensive documentation, and extra features.
Other
424 stars 172 forks source link

_homedir_setter unable to handle windows paths #199

Open Paradoxis opened 7 years ago

Paradoxis commented 7 years ago

When setting the home directory, it's not possible to pass a windows style home directory. The path gets escaped before getting padded to _meta._create_if_necessary. The lines that cause this:

# Fix: change 'hd' to 'directory', since they don't handle shell commands at all
_util._create_if_necessary(hd) -> _util._create_if_necessary(directory)
 OSError: [WinError 123] The filename, directory name, or volume label syntax is incorrect: "C:\\Users\\Luke\\Documents\\Projects\\myproject\\'C:"
# in _homedir_setter
# directory = 'C:\\Users\\Paradoxis\\/.gnupg/'
hd = _parsers._fix_unsafe(directory)
# hd = '\\'C:\\Users\\Luke\\/.gnupg/\\''
# Path gets wrapped in quotes
log.debug("GPGBase._homedir_setter(): got directory '%s'" % hd)

if hd:
    log.debug("GPGBase._homedir_setter(): Check existence of '%s'" % hd)
    _util._create_if_necessary(hd)   # Quoted path passed to standard library

# in _create_if_necessary
# directory = '\\'C:\\Users\\Luke\\/.gnupg/\\''

if not os.path.isabs(directory):
    log.debug("Got non-absolute path: %s" % directory)
    directory = os.path.abspath(directory)
    # Standard directory doesn't understand the quotes, so appends the current working directory to it
    # directory = 'C:\\Users\\Luke\\Documents\\Projects\\myproject\\\\'C:\\Users\\Luke\\.gnupg\\\\''

if not os.path.isdir(directory):
    log.info("Creating directory: %s" % directory)  
    # Invalid path triggers exception
    # OSError: [WinError 123] The filename, directory name, or volume label syntax is incorrect: "C:\\Users\\Luke\\Documents\\Projects\\myproject\\'C:"
    try:
        os.makedirs(directory, 0x1C0)
Paradoxis commented 7 years ago

Working monkey-patch, but the entire library has issues handling paths in Windows which means its still useless.

from os.path import abspath, expanduser
import gnupg._meta
gnupg._meta.GPGBase._homedir_setter = lambda self, directory: setattr(self, "_homedir", abspath(expanduser(directory)))