path: Disallow unknown substitutions in expand_path
This commit is contained in:
parent
ac7edad86d
commit
a7b6ff7b18
@ -22,6 +22,9 @@ XDG_DIRS = {
|
|||||||
'XDG_MUSIC_DIR': glib.get_user_special_dir(glib.USER_DIRECTORY_MUSIC),
|
'XDG_MUSIC_DIR': glib.get_user_special_dir(glib.USER_DIRECTORY_MUSIC),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
# XDG_MUSIC_DIR can be none, so filter out any bad data.
|
||||||
|
XDG_DIRS = dict((k, v) for k, v in XDG_DIRS.items() if v is not None)
|
||||||
|
|
||||||
|
|
||||||
def get_or_create_dir(dir_path):
|
def get_or_create_dir(dir_path):
|
||||||
if not isinstance(dir_path, bytes):
|
if not isinstance(dir_path, bytes):
|
||||||
@ -97,10 +100,13 @@ def split_path(path):
|
|||||||
|
|
||||||
|
|
||||||
def expand_path(path):
|
def expand_path(path):
|
||||||
|
# TODO: document as we want people to use this.
|
||||||
if not isinstance(path, bytes):
|
if not isinstance(path, bytes):
|
||||||
raise ValueError('Path is not a bytestring.')
|
raise ValueError('Path is not a bytestring.')
|
||||||
# TODO: expandvars as well?
|
try:
|
||||||
path = string.Template(path).safe_substitute(XDG_DIRS)
|
path = string.Template(path).substitute(XDG_DIRS)
|
||||||
|
except KeyError:
|
||||||
|
return None
|
||||||
path = os.path.expanduser(path)
|
path = os.path.expanduser(path)
|
||||||
path = os.path.abspath(path)
|
path = os.path.abspath(path)
|
||||||
return path
|
return path
|
||||||
|
|||||||
@ -237,8 +237,7 @@ class ExpandPathTest(unittest.TestCase):
|
|||||||
path.expand_path(b'$XDG_DATA_DIR/foo'))
|
path.expand_path(b'$XDG_DATA_DIR/foo'))
|
||||||
|
|
||||||
def test_xdg_subsititution_unknown(self):
|
def test_xdg_subsititution_unknown(self):
|
||||||
self.assertEqual(
|
self.assertIsNone(
|
||||||
b'/tmp/$XDG_INVALID_DIR/foo',
|
|
||||||
path.expand_path(b'/tmp/$XDG_INVALID_DIR/foo'))
|
path.expand_path(b'/tmp/$XDG_INVALID_DIR/foo'))
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user