1
mirror of https://github.com/home-assistant/core synced 2024-08-02 23:40:32 +02:00

Check whitelisted paths #13107 (#13154)

This commit is contained in:
Johann Kellerman 2018-03-30 04:57:19 +02:00 committed by Paulus Schoutsen
parent 3e5462ebff
commit 507c658fe9
2 changed files with 9 additions and 4 deletions

View File

@ -1060,15 +1060,19 @@ class Config(object):
"""Check if the path is valid for access from outside.""" """Check if the path is valid for access from outside."""
assert path is not None assert path is not None
parent = pathlib.Path(path) thepath = pathlib.Path(path)
try: try:
parent = parent.resolve() # pylint: disable=no-member # The file path does not have to exist (it's parent should)
if thepath.exists():
thepath = thepath.resolve()
else:
thepath = thepath.parent.resolve()
except (FileNotFoundError, RuntimeError, PermissionError): except (FileNotFoundError, RuntimeError, PermissionError):
return False return False
for whitelisted_path in self.whitelist_external_dirs: for whitelisted_path in self.whitelist_external_dirs:
try: try:
parent.relative_to(whitelisted_path) thepath.relative_to(whitelisted_path)
return True return True
except ValueError: except ValueError:
pass pass

View File

@ -809,7 +809,8 @@ class TestConfig(unittest.TestCase):
valid = [ valid = [
test_file, test_file,
tmp_dir tmp_dir,
os.path.join(tmp_dir, 'notfound321')
] ]
for path in valid: for path in valid:
assert self.config.is_allowed_path(path) assert self.config.is_allowed_path(path)