Add security layer to send file output things (#8189)
* Add security layer to send file output things * Make telegram secure * fix lint * fix handling * invert check * resolve relative paths * add test for relative paths * fix lint * fix tests * Address paulus comments * fix style * fix tests * Add more tests * fix tests * fix tests * fix test p2 * fix lint * fix tests * Make it available for windows * Change name / address comments * fix set * fix test * fix tests * fix test * fix lint
This commit is contained in:
1 parent
2f2952e0ec
commit
2dd7f0616e
8 files changed
+86
-6
No files matched your search
@@ -210,6 +210,9 @@ def test_api_get_config(hass, mock_api_client):
|
||||
result = yield from resp.json()
|
||||
if 'components' in result:
|
||||
result['components'] = set(result['components'])
|
||||
if 'whitelist_external_dirs' in result:
|
||||
result['whitelist_external_dirs'] = \
|
||||
set(result['whitelist_external_dirs'])
|
||||
|
||||
assert hass.config.as_dict() == result
|
||||
|
||||
|
||||
@@ -278,6 +278,9 @@ def test_get_config(hass, websocket_client):
|
||||
|
||||
if 'components' in msg['result']:
|
||||
msg['result']['components'] = set(msg['result']['components'])
|
||||
if 'whitelist_external_dirs' in msg['result']:
|
||||
msg['result']['whitelist_external_dirs'] = \
|
||||
set(msg['result']['whitelist_external_dirs'])
|
||||
|
||||
assert msg['result'] == hass.config.as_dict()
|
||||
|
||||
|
||||
@@ -363,6 +363,7 @@ class TestConfig(unittest.TestCase):
|
||||
'name': 'Huis',
|
||||
CONF_UNIT_SYSTEM: CONF_UNIT_SYSTEM_IMPERIAL,
|
||||
'time_zone': 'America/New_York',
|
||||
'whitelist_external_dirs': '/tmp',
|
||||
}), self.hass.loop).result()
|
||||
|
||||
assert self.hass.config.latitude == 60
|
||||
@@ -371,6 +372,8 @@ class TestConfig(unittest.TestCase):
|
||||
assert self.hass.config.location_name == 'Huis'
|
||||
assert self.hass.config.units.name == CONF_UNIT_SYSTEM_IMPERIAL
|
||||
assert self.hass.config.time_zone.zone == 'America/New_York'
|
||||
assert len(self.hass.config.whitelist_external_dirs) == 2
|
||||
assert '/tmp' in self.hass.config.whitelist_external_dirs
|
||||
|
||||
def test_loading_configuration_temperature_unit(self):
|
||||
"""Test backward compatibility when loading core config."""
|
||||
@@ -428,6 +431,7 @@ class TestConfig(unittest.TestCase):
|
||||
mock_elevation):
|
||||
"""Test config remains unchanged if discovery fails."""
|
||||
self.hass.config = Config()
|
||||
self.hass.config.config_dir = "/test/config"
|
||||
|
||||
run_coroutine_threadsafe(
|
||||
config_util.async_process_ha_core_config(
|
||||
@@ -441,6 +445,8 @@ class TestConfig(unittest.TestCase):
|
||||
assert self.hass.config.location_name == blankConfig.location_name
|
||||
assert self.hass.config.units == blankConfig.units
|
||||
assert self.hass.config.time_zone == blankConfig.time_zone
|
||||
assert len(self.hass.config.whitelist_external_dirs) == 1
|
||||
assert "/test/config/www" in self.hass.config.whitelist_external_dirs
|
||||
|
||||
@mock.patch('asyncio.create_subprocess_exec')
|
||||
def test_check_ha_config_file_correct(self, mock_create):
|
||||
|
||||
+33
-1
@@ -1,11 +1,13 @@
|
||||
"""Test to verify that Home Assistant core works."""
|
||||
# pylint: disable=protected-access
|
||||
import asyncio
|
||||
import logging
|
||||
import os
|
||||
import unittest
|
||||
from unittest.mock import patch, MagicMock, sentinel
|
||||
from datetime import datetime, timedelta
|
||||
from tempfile import TemporaryDirectory
|
||||
|
||||
import logging
|
||||
import pytz
|
||||
import pytest
|
||||
|
||||
@@ -796,11 +798,41 @@ class TestConfig(unittest.TestCase):
|
||||
'time_zone': 'UTC',
|
||||
'components': set(),
|
||||
'config_dir': '/tmp/ha-config',
|
||||
'whitelist_external_dirs': set(),
|
||||
'version': __version__,
|
||||
}
|
||||
|
||||
self.assertEqual(expected, self.config.as_dict())
|
||||
|
||||
def test_is_allowed_path(self):
|
||||
"""Test is_allowed_path method."""
|
||||
with TemporaryDirectory() as tmp_dir:
|
||||
self.config.whitelist_external_dirs = set((
|
||||
tmp_dir,
|
||||
))
|
||||
|
||||
test_file = os.path.join(tmp_dir, "test.jpg")
|
||||
with open(test_file, "w") as tmp_file:
|
||||
tmp_file.write("test")
|
||||
|
||||
valid = [
|
||||
test_file,
|
||||
]
|
||||
for path in valid:
|
||||
assert self.config.is_allowed_path(path)
|
||||
|
||||
self.config.whitelist_external_dirs = set(('/home',))
|
||||
|
||||
unvalid = [
|
||||
"/hass/config/secure",
|
||||
"/etc/passwd",
|
||||
"/root/secure_file",
|
||||
"/hass/config/test/../../../etc/passwd",
|
||||
test_file,
|
||||
]
|
||||
for path in unvalid:
|
||||
assert not self.config.is_allowed_path(path)
|
||||
|
||||
|
||||
@patch('homeassistant.core.monotonic')
|
||||
def test_create_timer(mock_monotonic, loop):
|
||||
|
||||
Reference in new issue
Block a user